List of usage examples for org.apache.commons.lang3.reflect FieldUtils getAllFieldsList
public static List<Field> getAllFieldsList(final Class<?> cls)
From source file:com.adobe.acs.commons.mcp.util.ValueMapSerializer.java
public static void serializeToMap(Map<String, Object> map, Object sourceObject) { if (sourceObject == null) { return;// w ww.j av a 2 s . c o m } FieldUtils.getAllFieldsList(sourceObject.getClass()).stream().filter(IntrospectionUtil::isSimple) .forEach(f -> { try { Object value = FieldUtils.readField(f, sourceObject, true); if (value != null) { map.put(f.getName(), value); } } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ValueMapSerializer.class.getName()).log(Level.SEVERE, null, ex); } }); }
From source file:com.birbit.jsonapi.JsonApiResourceDeserializer.java
@SuppressWarnings("WeakerAccess") public JsonApiResourceDeserializer(String apiType, Class<T> klass) { this.klass = klass; this.apiType = apiType; for (Field field : FieldUtils.getAllFieldsList(klass)) { ResourceId resourceId = field.getAnnotation(ResourceId.class); if (resourceId != null) { validateResourceId(field.getType()); idSetter = new FieldSetter(field); }//from w w w. j a v a 2 s . c om Relationship relationship = field.getAnnotation(Relationship.class); if (relationship != null) { String name = validateRelationship(field.getType(), relationship); relationshipSetters.put(name, new FieldSetter(field)); } ResourceLink resourceLink = field.getAnnotation(ResourceLink.class); if (resourceLink != null) { String name = validateResourceLink(field.getType(), resourceLink); linkSetters.put(name, new FieldSetter(field)); } } for (Method method : klass.getDeclaredMethods()) { ResourceId resourceId = method.getAnnotation(ResourceId.class); if (resourceId != null) { validateMethodParameters(ResourceId.class, method); idSetter = new MethodSetter(method); } Relationship relationship = method.getAnnotation(Relationship.class); if (relationship != null) { Class<?> parameter = validateMethodParameters(Relationship.class, method); String name = validateRelationship(parameter, relationship); relationshipSetters.put(name, new MethodSetter(method)); } ResourceLink resourceLink = method.getAnnotation(ResourceLink.class); if (resourceLink != null) { Class<?> parameter = validateMethodParameters(ResourceLink.class, method); String name = validateResourceLink(parameter, resourceLink); linkSetters.put(name, new MethodSetter(method)); } } if (idSetter == null) { throw new IllegalStateException("Must provide a ResourceId for " + klass); } }
From source file:cn.lambdalib.s11n.SerializationHelper.java
private List<Field> buildExposedFields(Class<?> type) { return FieldUtils.getAllFieldsList(type).stream().filter(f -> { Class<?> declaringClass = f.getDeclaringClass(); SerializeStrategy anno = declaringClass.getAnnotation(SerializeStrategy.class); ExposeStrategy strategy = anno == null ? ExposeStrategy.PUBLIC : anno.strategy(); boolean serializeAll = anno == null ? false : anno.all(); if (f.isAnnotationPresent(SerializeIncluded.class)) { return true; } else if (f.isAnnotationPresent(SerializeExcluded.class)) { return false; } else {/* ww w .j ava 2s.co m*/ if (!serializeAll && !isS11nType(f.getType())) { return false; } else { int mod = f.getModifiers(); switch (strategy) { case PUBLIC: return Modifier.isPublic(mod) && !Modifier.isStatic(mod) && !Modifier.isFinal(mod); case ALL: return !Modifier.isStatic(mod) && !Modifier.isFinal(mod); default: return false; } } } }).map(f -> { f.setAccessible(true); return f; }).collect(Collectors.toList()); }
From source file:com.robertsmieja.test.utils.junit.GettersAndSettersUtils.java
static List<Field> getFields(Class<?> aClass, Predicate<Field> fieldPredicate) { List<Field> allFields = FieldUtils.getAllFieldsList(aClass); List<Field> excludedFields = FieldUtils.getFieldsListWithAnnotation(aClass, IgnoreForTests.class); return allFields.stream().filter(field -> !field.isSynthetic()) .filter(field -> !excludedFields.contains(field)).filter(field -> !isFinal(field.getModifiers())) .filter(fieldPredicate).collect(Collectors.toList()); }
From source file:com.thinkbiganalytics.annotations.AnnotationFieldNameResolver.java
/** * Walk a class and obtain {@link AnnotatedFieldProperty} objects matching any fields with the {@link #annotation} supplied * * @param clazz the class to inspect and parse annotations * @return a list of objects describing the annotated fields *///w ww .ja va 2s . com public List<AnnotatedFieldProperty> getProperties(Class clazz) { processedClasses.add(clazz); classPropertyFields.put(clazz, new HashSet<AnnotatedFieldProperty>()); List<AnnotatedFieldProperty> names = new ArrayList<>(); List<Field> fields = FieldUtils.getFieldsListWithAnnotation(clazz, annotation); List<Field> allFields = FieldUtils.getAllFieldsList(clazz); for (Field field : fields) { AnnotatedFieldProperty p = addFieldProperty(clazz, names, field); classPropertyFields.get(clazz).add(p); Class fieldType = field.getType(); if (!processedClasses.contains(fieldType)) { names.addAll(getProperties(fieldType)); } } for (Field field : allFields) { Class fieldType = field.getType(); if (!processedClasses.contains(fieldType)) { stack.push(field.getName()); names.addAll(getProperties(fieldType)); //check to see if field is annotated with deserialize JsonDeserialize deserialize = field.getAnnotation(JsonDeserialize.class); if (deserialize != null) { Class<?> deserializeClass = deserialize.as(); if (!processedClasses.contains(deserializeClass)) { names.addAll(getProperties(deserializeClass)); } } stack.pop(); } else if (classPropertyFields.containsKey(fieldType)) { stack.push(field.getName()); for (AnnotatedFieldProperty prop : classPropertyFields.get(fieldType)) { addFieldProperty(clazz, names, prop.getField()); } //check to see if field is annotated with deserialize JsonDeserialize deserialize = field.getAnnotation(JsonDeserialize.class); if (deserialize != null) { Class<?> deserializeClass = deserialize.as(); if (classPropertyFields.containsKey(deserializeClass)) { for (AnnotatedFieldProperty prop : classPropertyFields.get(deserializeClass)) { addFieldProperty(clazz, names, prop.getField()); } } } stack.pop(); } } return names; }
From source file:com.feilong.core.lang.reflect.FieldUtil.java
/** * <code>klass</code> ? <code>excludeFieldNames</code> ?list. * * @param klass// w ww . jav a 2 s .c o m * the klass * @param excludeFieldNames * ?field names,?nullOrEmpty ? * @return <code>obj</code> null, {@link NullPointerException}<br> * {@link FieldUtils#getAllFieldsList(Class)} nullempty, {@link Collections#emptyList()}<br> * @see FieldUtils#getAllFieldsList(Class) * @since 1.7.1 */ //no static public static List<Field> getAllFieldList(final Class<?> klass, String... excludeFieldNames) { // {@link Field},parents, public/protect/private/inherited... List<Field> fieldList = FieldUtils.getAllFieldsList(klass); if (isNullOrEmpty(fieldList)) { return Collections.emptyList(); } //********************************************************************************************** Predicate<Field> excludeFieldPredicate = BeanPredicateUtil.containsPredicate("name", excludeFieldNames); Predicate<Field> staticPredicate = new Predicate<Field>() { @Override public boolean evaluate(Field field) { int modifiers = field.getModifiers(); // ??? log serialVersionUID boolean isStatic = Modifier.isStatic(modifiers); String pattern = "[{}.{}],modifiers:[{}]{}"; LOGGER.trace(pattern, klass.getSimpleName(), field.getName(), modifiers, isStatic ? " [isStatic]" : EMPTY); return isStatic; } }; return CollectionsUtil.selectRejected(fieldList, PredicateUtils.orPredicate(excludeFieldPredicate, staticPredicate)); }
From source file:nz.co.testamation.hibernate.HibernateEntityTemplate.java
public <T> T reload(Session session, T obj) { Field field = Iterables.getOnlyElement( Iterables.filter(FieldUtils.getAllFieldsList(obj.getClass()), new Predicate<Field>() { @Override/*from ww w . j a v a 2 s.c om*/ public boolean apply(Field field) { return field.getAnnotation(Id.class) != null; } })); return (T) session.load(obj.getClass(), ReflectionUtil.getFieldValue(obj, field.getName(), Serializable.class)); }
From source file:org.apache.servicecomb.it.junit.ITJUnitUtils.java
private static void initClasses(Class<?>[] classes) throws Throwable { for (Class<?> cls : classes) { for (Field field : FieldUtils.getAllFieldsList(cls)) { if (Consumers.class.isAssignableFrom(field.getType()) || GateRestTemplate.class.isAssignableFrom(field.getType()) || ITSCBRestTemplate.class.isAssignableFrom(field.getType())) { Object target = FieldUtils.readStaticField(field, true); MethodUtils.invokeMethod(target, "init"); }/* ww w . j ava 2 s. c om*/ } } }
From source file:org.force66.cxfutils.reflect.ReflectUtils.java
public static void reflectionAppendContextValues(ExceptionContext context, Object embeddedInfo, String label) { Validate.notNull(context, "Null context not allowed."); Validate.notEmpty(label, "Null or blank label not allowed."); if (embeddedInfo == null) { context.addContextValue(label, embeddedInfo); return;/*from w ww .java2s .co m*/ } List<Field> fieldList = FieldUtils.getAllFieldsList(embeddedInfo.getClass()); Object fieldValue; String fieldLabel; for (Field field : fieldList) { fieldLabel = label + "." + field.getName(); fieldValue = readField(field, embeddedInfo); if (fieldValue instanceof Collection) { reflectionAppendContextValues(context, (Collection) fieldValue, fieldLabel); } else if (field.getType().isPrimitive() || field.getType().getPackage().getName().startsWith("java")) { context.addContextValue(fieldLabel, fieldValue); } else { reflectionAppendContextValues(context, fieldValue, fieldLabel); } } }
From source file:org.objectpocket.references.ReferenceSupport.java
private List<Field> getFields(Object obj) { if (!fieldsForType.containsKey(obj.getClass().getName())) { List<Field> allFields = FieldUtils.getAllFieldsList(obj.getClass()); List<Field> filteredFields = filterTransientFields(allFields); filteredFields = filterReferencingFields(filteredFields); fieldsForType.put(obj.getClass().getName(), filteredFields); }//from w w w . ja va 2 s . c o m return fieldsForType.get(obj.getClass().getName()); }