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);
}
}
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));
}
}
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;
}
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());
}
}
}
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());
}
}
}
Aggregations