public <T> Codec<T> create(AnnotatedElement metadata, Class<T> type,
ResolverContext context) {
if (metadata == null) {
return null;
}
BoundString settings = metadata.getAnnotation(BoundString.class);
if (String.class.equals(type) && settings != null) {
try {
Charset charset; // Encodings are now given as strings, and turned into Charsets
charset = Charset.availableCharsets().get(settings.encoding());
// This throws a NullPointerException if the Charset can't be found
if (settings.size().length() > 0) {
Expression<Integer, Resolver> expr;
expr = Expressions.createInteger(context, settings.size());
return (Codec<T>) new FixedLengthStringCodec(
charset, //Note that this is a Charset, not an Encoding
expr,
settings.match(),
settings.converter().newInstance());
} else {
return (Codec<T>) new NullTerminatedStringCodec(
charset, //Note that this is a Charset, not an Encoding
settings.match(),
settings.converter().newInstance());
}
} catch (InstantiationException e) {
throw new CodecConstructionException(e.getMessage());
} catch (IllegalAccessException e) {
throw new CodecConstructionException(e.getMessage());