Search in sources :

Example 1 with Field

use of org.forgerock.openam.tokens.Field in project OpenAM by OpenRock.

the class JavaBeanAdapter method initialise.

/**
     * Process the annotations on the bean class, and throw exceptions for invalid configuration.
     */
private void initialise() {
    this.tokenType = beanClass.getAnnotation(Type.class).value();
    BeanInfo beanInfo;
    try {
        beanInfo = Introspector.getBeanInfo(beanClass);
    } catch (IntrospectionException e) {
        throw new IllegalStateException("Could not introspect type " + beanClass.getName(), e);
    }
    for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
        if (pd.getReadMethod() != null && pd.getWriteMethod() != null) {
            Method readMethod = pd.getReadMethod();
            Field f = readMethod.getAnnotation(Field.class);
            Method writeMethod = pd.getWriteMethod();
            if (f == null) {
                f = writeMethod.getAnnotation(Field.class);
            }
            if (f == null) {
                try {
                    java.lang.reflect.Field field = beanClass.getDeclaredField(pd.getName());
                    f = field.getAnnotation(Field.class);
                } catch (NoSuchFieldException e) {
                // fine - field isn't for storage in CTS.
                }
            }
            if (f != null) {
                CoreTokenField tokenField = f.field();
                Class<?> attributeType = tokenField.getAttributeType();
                Class<?> beanFieldType = readMethod.getReturnType();
                Class<? extends Converter> converterType = f.converter();
                if (converterType.equals(Converter.IdentityConverter.class) && !beanFieldType.equals(attributeType)) {
                    throw new IllegalStateException("Field " + pd.getDisplayName() + " does not have a compatible type" + "and does not declare a converter");
                }
                validateConverterType(attributeType, beanFieldType, converterType);
                Converter converter = InjectorHolder.getInstance(converterType);
                boolean generated = f.generated();
                FieldDetails field = new FieldDetails(tokenField, readMethod, writeMethod, converter, generated);
                if (tokenField == CoreTokenField.TOKEN_ID) {
                    idField = field;
                } else {
                    if (generated) {
                        throw new IllegalStateException("Non-id values cannot be generated: " + f.toString());
                    }
                    fields.add(field);
                }
                fieldsMap.put(pd.getName(), field);
            }
        }
    }
    if (idField == null) {
        throw new IllegalStateException("The bean class does not declare an ID field");
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) Method(java.lang.reflect.Method) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) Field(org.forgerock.openam.tokens.Field) Converter(org.forgerock.openam.tokens.Converter)

Aggregations

BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1 Converter (org.forgerock.openam.tokens.Converter)1 CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)1 Field (org.forgerock.openam.tokens.Field)1