/** * Collects field-level parameters from class. * * @param cls is a class for collecting * @param components * @return the collection of supported parameters */ public static List<Parameter> collectFieldParameters(Class<?> cls, Components components, javax.ws.rs.Consumes classConsumes, JsonView jsonViewAnnotation) { final List<Parameter> parameters = new ArrayList<Parameter>(); for (Field field : ReflectionUtils.getDeclaredFields(cls)) { final List<Annotation> annotations = Arrays.asList(field.getAnnotations()); final Type genericType = field.getGenericType(); parameters.addAll(collectParameters(genericType, annotations, components, classConsumes, jsonViewAnnotation)); } return parameters; }
/** * Returns the list of declared fields from the class <code>cls</code> and its superclasses * excluding <code>Object</code> class. If the field from child class hides the field from superclass, * the field from superclass won't be added to the result list. * * @param cls is the processing class * @return list of Fields */ public static List<Field> getDeclaredFields(Class<?> cls) { if (cls == null || Object.class.equals(cls)) { return Collections.emptyList(); } final List<Field> fields = new ArrayList<Field>(); final Set<String> fieldNames = new HashSet<String>(); for (Field field : cls.getDeclaredFields()) { fields.add(field); fieldNames.add(field.getName()); } for (Field field : getDeclaredFields(cls.getSuperclass())) { if (!fieldNames.contains(field.getName())) { fields.add(field); } } return fields; }
@Test public void getDeclaredFieldsFromInterfaceTest() throws NoSuchMethodException { final Class cls = IParent.class; Assert.assertEquals(Collections.emptyList(), ReflectionUtils.getDeclaredFields(cls)); }
/** * Collects field-level parameters from class. * * @param cls is a class for collecting * @param components * @return the collection of supported parameters */ public static List<Parameter> collectFieldParameters(Class<?> cls, Components components, javax.ws.rs.Consumes classConsumes, JsonView jsonViewAnnotation) { final List<Parameter> parameters = new ArrayList<Parameter>(); for (Field field : ReflectionUtils.getDeclaredFields(cls)) { final List<Annotation> annotations = Arrays.asList(field.getAnnotations()); final Type genericType = field.getGenericType(); parameters.addAll(collectParameters(genericType, annotations, components, classConsumes, jsonViewAnnotation)); } return parameters; }
/** * Returns the list of declared fields from the class <code>cls</code> and its superclasses * excluding <code>Object</code> class. If the field from child class hides the field from superclass, * the field from superclass won't be added to the result list. * * @param cls is the processing class * @return list of Fields */ public static List<Field> getDeclaredFields(Class<?> cls) { if (cls == null || Object.class.equals(cls)) { return Collections.emptyList(); } final List<Field> fields = new ArrayList<Field>(); final Set<String> fieldNames = new HashSet<String>(); for (Field field : cls.getDeclaredFields()) { fields.add(field); fieldNames.add(field.getName()); } for (Field field : getDeclaredFields(cls.getSuperclass())) { if (!fieldNames.contains(field.getName())) { fields.add(field); } } return fields; }