/** * 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()]); }
/** * 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); } }
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]); }
/** * Builds default field references. */ public BeanReferences buildDefaultReference(final PropertyDescriptor propertyDescriptor) { final PetiteReferenceType[] lookupReferences = petiteConfig.getLookupReferences(); final String[] references = new String[lookupReferences.length]; for (int i = 0; i < references.length; i++) { switch (lookupReferences[i]) { case NAME: references[i] = propertyDescriptor.getName(); break; case TYPE_SHORT_NAME: references[i] = StringUtil.uncapitalize(propertyDescriptor.getType().getSimpleName()); break; case TYPE_FULL_NAME: references[i] = propertyDescriptor.getType().getName(); break; } } return BeanReferences.of(references); }
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()); } }
private PropertyDescriptor field(Class type, String name) { PropertyDescriptor[] propertyDescriptors = ClassIntrospector.get().lookup(type).getAllPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getName().equals(name)) { return propertyDescriptor; } } throw new IllegalArgumentException(); }
/** * 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]); }
protected void wireProperties() { for (PropertyInjectionPoint pip : beanDefinition.properties) { final BeanReferences refNames = pip.references; final Object value = pc.lookupMixingScopedBean(this.definition(), refNames); if (value == null) { if ((beanDefinition.wiringMode == WiringMode.STRICT)) { throw new PetiteException("Wiring failed. Beans references: '" + refNames + "' not found for property: " + beanDefinition.type.getName() + '#' + pip.propertyDescriptor.getName()); } continue; } // BeanUtil.setDeclaredProperty(bean, pip.propertyDescriptor.getName(), value); final Setter setter = pip.propertyDescriptor.getSetter(true); try { setter.invokeSetter(bean, value); } catch (Exception ex) { throw new PetiteException("Wiring failed", ex); } } }
/** * 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); } } }
@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()); }
assertEquals("fooProp", pd.getName()); assertNotNull(pd.getReadMethodDescriptor()); assertNotNull(pd.getWriteMethodDescriptor()); assertEquals("shared", pd.getName()); assertNull(pd.getReadMethodDescriptor()); assertNull(pd.getWriteMethodDescriptor()); assertEquals("something", pd.getName()); assertNotNull(pd.getReadMethodDescriptor()); assertNull(pd.getWriteMethodDescriptor());
assertEquals("flag", propertyDescriptors[0].getName()); assertEquals("isFlag", propertyDescriptors[0].getReadMethodDescriptor().getMethod().getName()); MethodDescriptor[] mds = cd.getAllMethodDescriptors(); assertEquals("flag", propertyDescriptors[0].getName()); assertEquals("isFlag", propertyDescriptors[0].getReadMethodDescriptor().getMethod().getName()); mds = cd.getAllMethodDescriptors();
@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()); }
@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()); }