/** * Checks if class is eligible as resource type (must have {@link Type} annotation and field annotated with * {@link Id}. * @param type type to test * @return <code>true</code> if type is eligible */ public static boolean isEligibleType(Class<?> type) { return type.isAnnotationPresent(Type.class) && !ReflectionUtils.getAnnotatedFields(type, Id.class, true).isEmpty(); }
/** * Registers relationship resolver for given type. Resolver will be used if relationship resolution is enabled * trough relationship annotation. * @param resolver resolver instance * @param type type */ public void setTypeResolver(RelationshipResolver resolver, Class<?> type) { if (resolver != null) { String typeName = ReflectionUtils.getTypeName(type); if (typeName != null) { typedResolvers.put(type, resolver); } } } /**
List<Field> relationshipFields = ReflectionUtils.getAnnotatedFields(clazz, Relationship.class, true); Class<?> targetType = ReflectionUtils.getFieldType(relationshipField); relationshipTypeMap.get(clazz).put(relationship.value(), targetType); relationshipFieldMap.get(clazz).put(relationship.value(), relationshipField); List<Field> relMetaFields = ReflectionUtils.getAnnotatedFields(clazz, RelationshipMeta.class, true); Class<?> targetType = ReflectionUtils.getFieldType(relMetaField); relationshipMetaTypeMap.get(clazz).put(relationshipMeta.value(), targetType); fieldRelationshipMetaMap.put(relMetaField, relationshipMeta); List<Field> relLinkFields = ReflectionUtils.getAnnotatedFields(clazz, RelationshipLinks.class, true); List<Field> idAnnotatedFields = ReflectionUtils.getAnnotatedFields(clazz, Id.class, true); List<Field> metaFields = ReflectionUtils.getAnnotatedFields(clazz, Meta.class, true); if (metaFields.size() == 1) { Field metaField = metaFields.get(0); metaField.setAccessible(true); Class<?> metaType = ReflectionUtils.getFieldType(metaField); metaTypeMap.put(clazz, metaType); metaFieldMap.put(clazz, metaField); List<Field> linkFields = ReflectionUtils.getAnnotatedFields(clazz, com.github.jasminb.jsonapi.annotations.Links.class, true); Class<?> metaType = ReflectionUtils.getFieldType(linkField);
List<Field> relationshipFields = ReflectionUtils.getAnnotatedFields(clazz, Relationship.class, true); Class<?> targetType = ReflectionUtils.getFieldType(relationshipField); relationshipTypeMap.get(clazz).put(relationship.value(), targetType); relationshipFieldMap.get(clazz).put(relationship.value(), relationshipField); List<Field> relMetaFields = ReflectionUtils.getAnnotatedFields(clazz, RelationshipMeta.class, true); Class<?> targetType = ReflectionUtils.getFieldType(relMetaField); relationshipMetaTypeMap.get(clazz).put(relationshipMeta.value(), targetType); fieldRelationshipMetaMap.put(relMetaField, relationshipMeta); List<Field> relLinkFields = ReflectionUtils.getAnnotatedFields(clazz, RelationshipLinks.class, true); List<Field> idAnnotatedFields = ReflectionUtils.getAnnotatedFields(clazz, Id.class, true); List<Field> metaFields = ReflectionUtils.getAnnotatedFields(clazz, Meta.class, true); if (metaFields.size() == 1) { Field metaField = metaFields.get(0); metaField.setAccessible(true); Class<?> metaType = ReflectionUtils.getFieldType(metaField); metaTypeMap.put(clazz, metaType); metaFieldMap.put(clazz, metaField); List<Field> linkFields = ReflectionUtils.getAnnotatedFields(clazz, com.github.jasminb.jsonapi.annotations.Links.class, true); Class<?> metaType = ReflectionUtils.getFieldType(linkField);
/** * Checks if class is eligible as resource type (must have {@link Type} annotation and field annotated with * {@link Id}. * @param type type to test * @return <code>true</code> if type is eligible */ public static boolean isEligibleType(Class<?> type) { return type.isAnnotationPresent(Type.class) && !ReflectionUtils.getAnnotatedFields(type, Id.class, true).isEmpty(); }
/** * Registers relationship resolver for given type. Resolver will be used if relationship resolution is enabled * trough relationship annotation. * @param resolver resolver instance * @param type type */ public void setTypeResolver(RelationshipResolver resolver, Class<?> type) { if (resolver != null) { String typeName = ReflectionUtils.getTypeName(type); if (typeName != null) { typedResolvers.put(type, resolver); } } } /**
/** * Returns all field from a given class that are annotated with provided annotation type. * @param clazz source class * @param annotation target annotation * @param checkSuperclass if true, method will follow class hierarchy to look for fields with target annotation * @return list of fields or empty collection in case no fields were found */ public static List<Field> getAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotation, boolean checkSuperclass) { Field [] fields = clazz.getDeclaredFields(); List<Field> result = new ArrayList<>(); for (Field field : fields) { if (field.isAnnotationPresent(annotation)) { result.add(field); } } if (checkSuperclass && clazz.getSuperclass() != null && !clazz.getSuperclass().equals(Object.class)) { result.addAll(getAnnotatedFields(clazz.getSuperclass(), annotation, true)); } return result; }
/** * Returns all field from a given class that are annotated with provided annotation type. * @param clazz source class * @param annotation target annotation * @param checkSuperclass if true, method will follow class hierarchy to look for fields with target annotation * @return list of fields or empty collection in case no fields were found */ public static List<Field> getAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotation, boolean checkSuperclass) { Field [] fields = clazz.getDeclaredFields(); List<Field> result = new ArrayList<>(); for (Field field : fields) { if (field.isAnnotationPresent(annotation)) { result.add(field); } } if (checkSuperclass && clazz.getSuperclass() != null && !clazz.getSuperclass().equals(Object.class)) { result.addAll(getAnnotatedFields(clazz.getSuperclass(), annotation, true)); } return result; }