+final class IdentityCodec extends AbstractIllegalArgumentCodec<QName, BaseIdentity> implements BindingIdentityCodec {
+ private final LoadingCache<@NonNull QName, @NonNull BaseIdentity> values = CacheBuilder.newBuilder()
+ .build(new CacheLoader<>() {
+ @Override
+ public BaseIdentity load(final QName key) {
+ final var clazz = context.getIdentityClass(key);
+ final Field field;
+ try {
+ field = clazz.getField(BindingMapping.VALUE_STATIC_FIELD_NAME);
+ } catch (NoSuchFieldException e) {
+ throw new LinkageError(clazz + " does not define required field "
+ + BindingMapping.VALUE_STATIC_FIELD_NAME, e);
+ }
+ if (!Modifier.isStatic(field.getModifiers())) {
+ throw new LinkageError(field + " is not static");
+ }
+
+ final Object value;
+ try {
+ value = clazz.cast(field.get(null));
+ } catch (IllegalAccessException e) {
+ throw new LinkageError(field + " is not accesssible", e);
+ }
+ if (value == null) {
+ throw new LinkageError(field + " is null");
+ }
+ try {
+ return clazz.cast(value);
+ } catch (ClassCastException e) {
+ throw new LinkageError(field + " value " + value + " has illegal type", e);
+ }
+ }
+ });
+