Search in sources :

Example 6 with IndexedPropertyDescriptor

use of java.beans.IndexedPropertyDescriptor in project jdbi by jdbi.

the class JpaClass method inspectProperties.

private static void inspectProperties(Class<?> clazz, Map<String, JpaMember> members, boolean hasColumnAnnotation) {
    try {
        Stream.of(Introspector.getBeanInfo(clazz).getPropertyDescriptors()).filter(property -> !members.containsKey(property.getName())).filter(property -> !(property instanceof IndexedPropertyDescriptor)).filter(property -> !"class".equals(property.getName())).forEach(property -> {
            Method getter = property.getReadMethod();
            Method setter = property.getWriteMethod();
            Column column = Stream.of(getter, setter).filter(Objects::nonNull).map(method -> method.getAnnotation(Column.class)).filter(Objects::nonNull).findFirst().orElse(null);
            if ((column != null) == hasColumnAnnotation) {
                members.put(property.getName(), new JpaMember(clazz, column, property));
            }
        });
    } catch (IntrospectionException e) {
        logger.warn("Unable to introspect " + clazz, e);
    }
}
Also used : Logger(org.slf4j.Logger) Collections.unmodifiableList(java.util.Collections.unmodifiableList) MappedSuperclass(javax.persistence.MappedSuperclass) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) IntrospectionException(java.beans.IntrospectionException) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Column(javax.persistence.Column) Introspector(java.beans.Introspector) List(java.util.List) Stream(java.util.stream.Stream) Locale(java.util.Locale) Collections.synchronizedMap(java.util.Collections.synchronizedMap) Map(java.util.Map) Method(java.lang.reflect.Method) WeakHashMap(java.util.WeakHashMap) Column(javax.persistence.Column) Objects(java.util.Objects) IntrospectionException(java.beans.IntrospectionException) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) Method(java.lang.reflect.Method)

Example 7 with IndexedPropertyDescriptor

use of java.beans.IndexedPropertyDescriptor in project freemarker by apache.

the class ClassIntrospector method addPropertyDescriptorToClassIntrospectionData.

private void addPropertyDescriptorToClassIntrospectionData(Map<Object, Object> introspData, PropertyDescriptor pd, Class<?> clazz, Map<MethodSignature, List<Method>> accessibleMethods) {
    Method readMethod = getMatchingAccessibleMethod(pd.getReadMethod(), accessibleMethods);
    if (readMethod != null && !isAllowedToExpose(readMethod)) {
        readMethod = null;
    }
    Method indexedReadMethod;
    if (pd instanceof IndexedPropertyDescriptor) {
        indexedReadMethod = getMatchingAccessibleMethod(((IndexedPropertyDescriptor) pd).getIndexedReadMethod(), accessibleMethods);
        if (indexedReadMethod != null && !isAllowedToExpose(indexedReadMethod)) {
            indexedReadMethod = null;
        }
        if (indexedReadMethod != null) {
            getArgTypesByMethod(introspData).put(indexedReadMethod, indexedReadMethod.getParameterTypes());
        }
    } else {
        indexedReadMethod = null;
    }
    if (readMethod != null || indexedReadMethod != null) {
        introspData.put(pd.getName(), new FastPropertyDescriptor(readMethod, indexedReadMethod));
    }
}
Also used : IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) Method(java.lang.reflect.Method)

Example 8 with IndexedPropertyDescriptor

use of java.beans.IndexedPropertyDescriptor in project freemarker by apache.

the class ClassIntrospector method getPropertyDescriptors.

/**
 * Very similar to {@link BeanInfo#getPropertyDescriptors()}, but can deal with Java 8 default methods too.
 */
private List<PropertyDescriptor> getPropertyDescriptors(BeanInfo beanInfo, Class<?> clazz) {
    PropertyDescriptor[] introspectorPDsArray = beanInfo.getPropertyDescriptors();
    List<PropertyDescriptor> introspectorPDs = introspectorPDsArray != null ? Arrays.asList(introspectorPDsArray) : Collections.<PropertyDescriptor>emptyList();
    if (!treatDefaultMethodsAsBeanMembers || _JavaVersions.JAVA_8 == null) {
        // java.beans.Introspector was good enough then.
        return introspectorPDs;
    }
    // introspectorPDs contains each property exactly once. But as now we will search them manually too, it can
    // happen that we find the same property for multiple times. Worse, because of indexed properties, it's possible
    // that we have to merge entries (like one has the normal reader method, the other has the indexed reader
    // method), instead of just replacing them in a Map. That's why we have introduced PropertyReaderMethodPair,
    // which holds the methods belonging to the same property name. IndexedPropertyDescriptor is not good for that,
    // as it can't store two methods whose types are incompatible, and we have to wait until all the merging was
    // done to see if the incompatibility goes away.
    // This could be Map<String, PropertyReaderMethodPair>, but since we rarely need to do merging, we try to avoid
    // creating those and use the source objects as much as possible. Also note that we initialize this lazily.
    LinkedHashMap<String, Object> /*PropertyReaderMethodPair|Method|PropertyDescriptor*/
    mergedPRMPs = null;
    // here, we don't utilize the accessibleMethods Map, which we might already have at this point.)
    for (Method method : clazz.getMethods()) {
        if (_JavaVersions.JAVA_8.isDefaultMethod(method) && method.getReturnType() != void.class && !method.isBridge()) {
            Class<?>[] paramTypes = method.getParameterTypes();
            if (paramTypes.length == 0 || paramTypes.length == 1 && paramTypes[0] == int.class) /* indexed property reader */
            {
                String propName = _MethodUtil.getBeanPropertyNameFromReaderMethodName(method.getName(), method.getReturnType());
                if (propName != null) {
                    if (mergedPRMPs == null) {
                        // Lazy initialization
                        mergedPRMPs = new LinkedHashMap<String, Object>();
                    }
                    if (paramTypes.length == 0) {
                        mergeInPropertyReaderMethod(mergedPRMPs, propName, method);
                    } else {
                        // It's an indexed property reader method
                        mergeInPropertyReaderMethodPair(mergedPRMPs, propName, new PropertyReaderMethodPair(null, method));
                    }
                }
            }
        }
    }
    if (mergedPRMPs == null) {
        // We had no interfering Java 8 default methods, so we can chose the fast route.
        return introspectorPDs;
    }
    for (PropertyDescriptor introspectorPD : introspectorPDs) {
        mergeInPropertyDescriptor(mergedPRMPs, introspectorPD);
    }
    // Now we convert the PRMPs to PDs, handling case where the normal and the indexed read methods contradict.
    List<PropertyDescriptor> mergedPDs = new ArrayList<PropertyDescriptor>(mergedPRMPs.size());
    for (Entry<String, Object> entry : mergedPRMPs.entrySet()) {
        String propName = entry.getKey();
        Object propDescObj = entry.getValue();
        if (propDescObj instanceof PropertyDescriptor) {
            mergedPDs.add((PropertyDescriptor) propDescObj);
        } else {
            Method readMethod;
            Method indexedReadMethod;
            if (propDescObj instanceof Method) {
                readMethod = (Method) propDescObj;
                indexedReadMethod = null;
            } else if (propDescObj instanceof PropertyReaderMethodPair) {
                PropertyReaderMethodPair prmp = (PropertyReaderMethodPair) propDescObj;
                readMethod = prmp.readMethod;
                indexedReadMethod = prmp.indexedReadMethod;
                if (readMethod != null && indexedReadMethod != null && indexedReadMethod.getReturnType() != readMethod.getReturnType().getComponentType()) {
                    // Here we copy the java.beans.Introspector behavior: If the array item class is not exactly the
                    // the same as the indexed read method return type, we say that the property is not indexed.
                    indexedReadMethod = null;
                }
            } else {
                throw new BugException();
            }
            try {
                mergedPDs.add(indexedReadMethod != null ? new IndexedPropertyDescriptor(propName, readMethod, null, indexedReadMethod, null) : new PropertyDescriptor(propName, readMethod, null));
            } catch (IntrospectionException e) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn("Failed creating property descriptor for " + clazz.getName() + " property " + propName, e);
                }
            }
        }
    }
    return mergedPDs;
}
Also used : IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) BugException(freemarker.core.BugException)

Example 9 with IndexedPropertyDescriptor

use of java.beans.IndexedPropertyDescriptor in project j2objc by google.

the class IntrospectorTest method test_MixedBooleanSimpleClass36.

public void test_MixedBooleanSimpleClass36() throws Exception {
    BeanInfo info = Introspector.getBeanInfo(MixedBooleanSimpleClass36.class);
    Method normalGetter = MixedBooleanSimpleClass36.class.getDeclaredMethod("getList");
    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        if (propertyName.equals(pd.getName())) {
            assertFalse(pd instanceof IndexedPropertyDescriptor);
            assertEquals(normalGetter, pd.getReadMethod());
            assertNull(pd.getWriteMethod());
        }
    }
}
Also used : IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) SimpleBeanInfo(java.beans.SimpleBeanInfo) FakeFox01BeanInfo(org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) Method(java.lang.reflect.Method)

Example 10 with IndexedPropertyDescriptor

use of java.beans.IndexedPropertyDescriptor in project j2objc by google.

the class IntrospectorTest method test_MixedBooleanSimpleClass1.

public void test_MixedBooleanSimpleClass1() throws Exception {
    BeanInfo info = Introspector.getBeanInfo(MixedBooleanSimpleClass1.class);
    Method getter = MixedBooleanSimpleClass1.class.getDeclaredMethod("isList");
    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        if (propertyName.equals(pd.getName())) {
            assertFalse(pd instanceof IndexedPropertyDescriptor);
            assertEquals(getter, pd.getReadMethod());
            assertNull(pd.getWriteMethod());
        }
    }
}
Also used : IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) SimpleBeanInfo(java.beans.SimpleBeanInfo) FakeFox01BeanInfo(org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) Method(java.lang.reflect.Method)

Aggregations

IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)201 Method (java.lang.reflect.Method)171 PropertyDescriptor (java.beans.PropertyDescriptor)144 BeanInfo (java.beans.BeanInfo)127 SimpleBeanInfo (java.beans.SimpleBeanInfo)126 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)126 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)49 IntrospectionException (java.beans.IntrospectionException)21 EventSetDescriptor (java.beans.EventSetDescriptor)3 MethodDescriptor (java.beans.MethodDescriptor)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 PropertyInfo (cern.gp.beans.PropertyInfo)1 CachingStrategy (cern.gp.nodes.cache.CachingStrategy)1 NoCachingStrategy (cern.gp.nodes.cache.NoCachingStrategy)1 StickyCachingStrategy (cern.gp.nodes.cache.StickyCachingStrategy)1 TimeLimitedCachingStrategy (cern.gp.nodes.cache.TimeLimitedCachingStrategy)1 BugException (freemarker.core.BugException)1 KeyEvent (java.awt.event.KeyEvent)1