/** * Resolves component type for given property descriptor. */ public Class resolveComponentType(boolean declared) { Class componentType = null; Getter getter = getGetter(declared); if (getter != null) { componentType = getter.getGetterRawComponentType(); } if (componentType == null) { FieldDescriptor fieldDescriptor = getFieldDescriptor(); if (fieldDescriptor != null) { componentType = fieldDescriptor.getRawComponentType(); } } return componentType; }
/** * Resolves key type for given property descriptor. */ public Class resolveKeyType(boolean declared) { Class keyType = null; Getter getter = getGetter(declared); if (getter != null) { keyType = getter.getGetterRawKeyComponentType(); } if (keyType == null) { FieldDescriptor fieldDescriptor = getFieldDescriptor(); if (fieldDescriptor != null) { keyType = fieldDescriptor.getRawKeyComponentType(); } } return keyType; }
/** * Returns all bean property names. */ protected String[] getAllBeanPropertyNames(Class type, boolean declared) { ClassDescriptor classDescriptor = ClassIntrospector.lookup(type); PropertyDescriptor[] propertyDescriptors = classDescriptor.getAllPropertyDescriptors(); ArrayList<String> names = new ArrayList<>(propertyDescriptors.length); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { MethodDescriptor getter = propertyDescriptor.getReadMethodDescriptor(); if (getter != null) { if (getter.matchDeclared(declared)) { names.add(propertyDescriptor.getName()); } } else if (includeFields) { FieldDescriptor field = propertyDescriptor.getFieldDescriptor(); if (field != null) { if (field.matchDeclared(declared)) { names.add(field.getName()); } } } } return names.toArray(new String[names.size()]); }
/** * Resolves component type for given property descriptor. */ public Class resolveComponentType(final boolean declared) { Class componentType = null; Getter getter = getGetter(declared); if (getter != null) { componentType = getter.getGetterRawComponentType(); } if (componentType == null) { FieldDescriptor fieldDescriptor = getFieldDescriptor(); if (fieldDescriptor != null) { componentType = fieldDescriptor.getRawComponentType(); } } return componentType; }
/** * Resolves key type for given property descriptor. */ public Class resolveKeyType(final boolean declared) { Class keyType = null; Getter getter = getGetter(declared); if (getter != null) { keyType = getter.getGetterRawKeyComponentType(); } if (keyType == null) { FieldDescriptor fieldDescriptor = getFieldDescriptor(); if (fieldDescriptor != null) { keyType = fieldDescriptor.getRawKeyComponentType(); } } return keyType; }
/** * Extracts references for given property. Returns {@code null} if property is not marked with an * annotation. */ public BeanReferences readReferenceFromAnnotation(final PropertyDescriptor propertyDescriptor) { final MethodDescriptor writeMethodDescriptor = propertyDescriptor.getWriteMethodDescriptor(); final FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor(); PetiteInject ref = null; if (writeMethodDescriptor != null) { ref = writeMethodDescriptor.getMethod().getAnnotation(PetiteInject.class); } if (ref == null && fieldDescriptor != null) { ref = fieldDescriptor.getField().getAnnotation(PetiteInject.class); } if (ref == null) { return null; } BeanReferences reference = null; String name = ref.value().trim(); if (name.length() != 0) { reference = BeanReferences.of(name); } reference = updateReferencesWithDefaultsIfNeeded(propertyDescriptor, reference); reference = reference.removeDuplicateNames(); return reference; }
/** * Returns all bean property names. */ protected String[] getAllBeanPropertyNames(final Class type, final boolean declared) { ClassDescriptor classDescriptor = ClassIntrospector.get().lookup(type); PropertyDescriptor[] propertyDescriptors = classDescriptor.getAllPropertyDescriptors(); ArrayList<String> names = new ArrayList<>(propertyDescriptors.length); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { MethodDescriptor getter = propertyDescriptor.getReadMethodDescriptor(); if (getter != null) { if (getter.matchDeclared(declared)) { names.add(propertyDescriptor.getName()); } } else if (includeFields) { FieldDescriptor field = propertyDescriptor.getFieldDescriptor(); if (field != null) { if (field.matchDeclared(declared)) { names.add(field.getName()); } } } } return names.toArray(new String[0]); }
public SetInjectionPoint(final PropertyDescriptor propertyDescriptor) { Objects.requireNonNull(propertyDescriptor); this.propertyDescriptor = propertyDescriptor; this.type = resolveSetType(propertyDescriptor); // resolve component type Class targetClass = null; MethodDescriptor writeMethodDescriptor = propertyDescriptor.getWriteMethodDescriptor(); FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor(); if (writeMethodDescriptor != null) { targetClass = writeMethodDescriptor.getSetterRawComponentType(); } if (targetClass == null && fieldDescriptor != null) { targetClass = fieldDescriptor.getRawComponentType(); } this.targetClass = targetClass; if (targetClass == null) { throw new PetiteException("Unknown Petite set component type " + type.getSimpleName() + '.' + propertyDescriptor.getName()); } }
if (propertyDescriptor.getFieldDescriptor() != null) { ann = propertyDescriptor.getFieldDescriptor().getField().getAnnotation(annotations);
public ValueInjectionPoint[] resolveParamInjectionPoints(final Class type) { final ClassDescriptor cd = ClassIntrospector.get().lookup(type); final List<ValueInjectionPoint> valueInjectionPointList = new ArrayList<>(); for (final PropertyDescriptor pd : cd.getAllPropertyDescriptors()) { final FieldDescriptor fd = pd.getFieldDescriptor(); if (fd != null) { final PetiteValue petiteValue = fd.getField().getAnnotation(PetiteValue.class); if (petiteValue != null) { valueInjectionPointList.add(new ValueInjectionPoint(pd.getName(), petiteValue.value())); continue; } } MethodDescriptor md = pd.getWriteMethodDescriptor(); if (md != null) { final PetiteValue petiteValue = md.getMethod().getAnnotation(PetiteValue.class); if (petiteValue != null) { valueInjectionPointList.add(new ValueInjectionPoint(pd.getName(), petiteValue.value())); continue; } } } return valueInjectionPointList.toArray(new ValueInjectionPoint[0]); }
FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor();
/** * Visits a type. */ public void visit() { ClassDescriptor classDescriptor = ClassIntrospector.get().lookup(type); if (classMetadataName != null) { // process first 'meta' fields 'class' onProperty(classMetadataName, null, false); } PropertyDescriptor[] propertyDescriptors = classDescriptor.getAllPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { Getter getter = propertyDescriptor.getGetter(declared); if (getter != null) { String propertyName = propertyDescriptor.getName(); boolean isTransient = false; // check for transient flag FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor(); if (fieldDescriptor != null) { isTransient = Modifier.isTransient(fieldDescriptor.getField().getModifiers()); } onProperty(propertyName, propertyDescriptor, isTransient); } } }
/** * Process all annotations of provided properties. */ protected void collectPropertyAnnotationChecks(final List<Check> annChecks, final PropertyDescriptor propertyDescriptor) { FieldDescriptor fd = propertyDescriptor.getFieldDescriptor(); if (fd != null) { Annotation[] annotations = fd.getField().getAnnotations(); collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations); } MethodDescriptor md = propertyDescriptor.getReadMethodDescriptor(); if (md != null) { Annotation[] annotations = md.getMethod().getAnnotations(); collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations); } md = propertyDescriptor.getWriteMethodDescriptor(); if (md != null) { Annotation[] annotations = md.getMethod().getAnnotations(); collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations); } }
@Test void testFieldPrefix1() { LifeBean lifeBean = new LifeBean(); String foo = BeanUtil.pojo.getProperty(lifeBean, "foo").toString(); assertEquals("foo", foo); ClassIntrospector.Implementation.set(new CachingIntrospector(true, true, true, new String[] {"_"})); foo = BeanUtil.pojo.getProperty(lifeBean, "foo").toString(); assertEquals("foo", foo); ClassDescriptor cd = ClassIntrospector.get().lookup(LifeBean.class); PropertyDescriptor[] pds = cd.getAllPropertyDescriptors(); assertEquals(3, pds.length); assertEquals("bar", pds[0].getName()); assertEquals("_bar", pds[0].getFieldDescriptor().getName()); assertEquals("www", pds[2].getName()); assertEquals(null, pds[2].getFieldDescriptor()); ClassIntrospector.Implementation.set(new CachingIntrospector()); }
@Test void testFieldPrefix1withEmpty() { LifeBean lifeBean = new LifeBean(); String foo = BeanUtil.pojo.getProperty(lifeBean, "foo").toString(); assertEquals("foo", foo); ClassIntrospector.Implementation.set(new CachingIntrospector(true, true, true, new String[] {"_", ""})); foo = BeanUtil.pojo.getProperty(lifeBean, "foo").toString(); assertEquals("foo", foo); ClassDescriptor cd = ClassIntrospector.get().lookup(LifeBean.class); PropertyDescriptor[] pds = cd.getAllPropertyDescriptors(); assertEquals(3, pds.length); assertEquals("bar", pds[0].getName()); assertEquals("_bar", pds[0].getFieldDescriptor().getName()); assertEquals("www", pds[2].getName()); assertEquals("www", pds[2].getFieldDescriptor().getName()); ClassIntrospector.Implementation.set(new CachingIntrospector()); }
@Test void testOverload() { ClassDescriptor cd = ClassIntrospector.get().lookup(Overload.class); PropertyDescriptor[] pds = cd.getAllPropertyDescriptors(); assertEquals(1, pds.length); PropertyDescriptor pd = pds[0]; assertNotNull(pd.getFieldDescriptor()); assertNotNull(pd.getReadMethodDescriptor()); assertNull(pd.getWriteMethodDescriptor()); }
assertNotNull(pd.getReadMethodDescriptor()); assertNotNull(pd.getWriteMethodDescriptor()); assertNotNull(pd.getFieldDescriptor()); assertNull(pd.getReadMethodDescriptor()); assertNull(pd.getWriteMethodDescriptor()); assertNotNull(pd.getFieldDescriptor()); assertNotNull(pd.getReadMethodDescriptor()); assertNull(pd.getWriteMethodDescriptor()); assertNull(pd.getFieldDescriptor());
assertNull(propertyDescriptors[i].getReadMethodDescriptor()); assertNotNull(propertyDescriptors[i].getFieldDescriptor());
@Test void testPropertiesTwoSubClass() throws InvocationTargetException, IllegalAccessException { ClassDescriptor cd = ClassIntrospector.get().lookup(TwoSub.class); PropertyDescriptor[] propertyDescriptors = cd.getAllPropertyDescriptors(); assertEquals(2, propertyDescriptors.length); assertEquals("fone", propertyDescriptors[0].getName()); assertEquals("ftwo", propertyDescriptors[1].getName()); for (int i = 0; i < 2; i++) { assertNull(propertyDescriptors[i].getWriteMethodDescriptor()); assertNotNull(propertyDescriptors[i].getReadMethodDescriptor()); assertNotNull(propertyDescriptors[i].getFieldDescriptor()); } // change value TwoSub one = new TwoSub(); Setter setter = propertyDescriptors[0].getSetter(true); setter.invokeSetter(one, "one!"); assertEquals("one!", one.getFone()); // fields FieldDescriptor[] fieldDescriptors = cd.getAllFieldDescriptors(); assertEquals(1, fieldDescriptors.length); assertEquals("ftwo", fieldDescriptors[0].getName()); // beanutil BeanUtil.declared.setProperty(one, "fone", "!!!"); assertEquals("!!!", one.getFone()); }
@Test void testPropertiesOneSubClass() throws InvocationTargetException, IllegalAccessException { ClassDescriptor cd = ClassIntrospector.get().lookup(OneSub.class); PropertyDescriptor[] propertyDescriptors = cd.getAllPropertyDescriptors(); assertEquals(2, propertyDescriptors.length); assertEquals("fone", propertyDescriptors[0].getName()); assertEquals("ftwo", propertyDescriptors[1].getName()); for (int i = 0; i < 2; i++) { assertNull(propertyDescriptors[i].getWriteMethodDescriptor()); assertNotNull(propertyDescriptors[i].getReadMethodDescriptor()); assertNotNull(propertyDescriptors[i].getFieldDescriptor()); } // change value OneSub one = new OneSub(); Setter setter = propertyDescriptors[0].getSetter(true); setter.invokeSetter(one, "one!"); assertEquals("one!", one.getFone()); // fields FieldDescriptor[] fieldDescriptors = cd.getAllFieldDescriptors(); assertEquals(1, fieldDescriptors.length); assertEquals("ftwo", fieldDescriptors[0].getName()); // beanutil BeanUtil.declared.setProperty(one, "fone", "!!!"); assertEquals("!!!", one.getFone()); }