/** * 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); } }
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); } } }
/** * 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; }
/** * 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()]); }
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()); } }
int c = 0; for (PropertyDescriptor property : properties) { if (property.isFieldOnly()) continue; if (property.isPublic()) c++; assertEquals("fooProp", pd.getName()); assertNotNull(pd.getReadMethodDescriptor()); assertNotNull(pd.getWriteMethodDescriptor()); assertNotNull(pd.getFieldDescriptor()); assertEquals("shared", pd.getName()); assertNull(pd.getReadMethodDescriptor()); assertNull(pd.getWriteMethodDescriptor()); assertNotNull(pd.getFieldDescriptor()); assertEquals("something", pd.getName()); assertNotNull(pd.getReadMethodDescriptor()); assertNull(pd.getWriteMethodDescriptor()); assertNull(pd.getFieldDescriptor());
if (propertyDescriptor.isGetterOnly()) { continue; Class propertyType = propertyDescriptor.getType(); if (!ClassUtil.isTypeOf(propertyType, Collection.class)) { continue; MethodDescriptor writeMethodDescriptor = propertyDescriptor.getWriteMethodDescriptor(); FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor();
if (propertyDescriptor.getFieldDescriptor() != null) { ann = propertyDescriptor.getFieldDescriptor().getField().getAnnotation(annotations); if (ann == null && propertyDescriptor.getWriteMethodDescriptor() != null) { ann = propertyDescriptor.getWriteMethodDescriptor().getMethod().getAnnotation(annotations); if (ann == null && propertyDescriptor.getReadMethodDescriptor() != null) { ann = propertyDescriptor.getReadMethodDescriptor().getMethod().getAnnotation(annotations);
/** * 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); } } }
setterMethod = existing.getWriteMethodDescriptor(); MethodDescriptor existingMethodDescriptor = existing.getReadMethodDescriptor(); if (existingMethodDescriptor != null) { getterMethod = existing.getReadMethodDescriptor();
@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()); }
/** * 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; }
if (propertyDescriptor.isGetterOnly()) { continue; if (usedPropertyNames.contains(propertyDescriptor.getName())) { continue; Class propertyType = propertyDescriptor.getType(); if (ClassUtil.isTypeOf(propertyType, Collection.class)) { continue; usedPropertyNames.add(propertyDescriptor.getName());
/** * 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); }
@Test void testPropertyMatches() { ClassDescriptor cd = ClassIntrospector.get().lookup(Cbean.class); PropertyDescriptor pd; pd = cd.getPropertyDescriptor("s1", false); assertNull(pd); pd = cd.getPropertyDescriptor("s1", true); assertFalse(pd.isPublic()); assertTrue(pd.getReadMethodDescriptor().isPublic()); assertFalse(pd.getWriteMethodDescriptor().isPublic()); assertNotNull(getPropertyGetterDescriptor(cd, "s1", false)); assertNull(getPropertySetterDescriptor(cd, "s1", false)); pd = cd.getPropertyDescriptor("s2", false); assertNull(pd); pd = cd.getPropertyDescriptor("s2", true); assertFalse(pd.isPublic()); assertFalse(pd.getReadMethodDescriptor().isPublic()); assertTrue(pd.getWriteMethodDescriptor().isPublic()); assertNull(getPropertyGetterDescriptor(cd, "s2", false)); assertNotNull(getPropertySetterDescriptor(cd, "s2", false)); pd = cd.getPropertyDescriptor("s3", false); assertNotNull(pd); pd = cd.getPropertyDescriptor("s3", true); assertTrue(pd.isPublic()); assertTrue(pd.getReadMethodDescriptor().isPublic()); assertTrue(pd.getWriteMethodDescriptor().isPublic()); assertNotNull(getPropertyGetterDescriptor(cd, "s3", false)); assertNotNull(getPropertySetterDescriptor(cd, "s3", false)); }
assertEquals("flag", propertyDescriptors[0].getName()); assertEquals("isFlag", propertyDescriptors[0].getReadMethodDescriptor().getMethod().getName()); MethodDescriptor[] mds = cd.getAllMethodDescriptors(); int c = 0; assertEquals("flag", propertyDescriptors[0].getName()); assertEquals("isFlag", propertyDescriptors[0].getReadMethodDescriptor().getMethod().getName()); mds = cd.getAllMethodDescriptors(); c = 0;
/** * Injects value into the targets property. */ protected void injectValueIntoObject(final Object target, final PropertyDescriptor pd, final Object value) { Object convertedValue = value; if (value != null) { Class targetClass = pd.getType(); convertedValue = convertType(value, targetClass); } try { Setter setter = pd.getSetter(true); if (setter != null) { setter.invokeSetter(target, convertedValue); } } catch (Exception ex) { throw new JsonException(ex); } }
@SuppressWarnings({"unchecked"}) protected Class<T> resolveSetType(final PropertyDescriptor propertyDescriptor) { Class<T> type = (Class<T>) propertyDescriptor.getType(); if (ClassUtil.isTypeOf(type, Collection.class)) { return type; } throw new PetiteException("Unsupported Petite set type: " + type.getName()); }
/** * Returns setter. */ public Setter getSetter(boolean declared) { loadPropertyDescriptor(); return propertyDescriptor != null ? propertyDescriptor.getSetter(declared) : null; }