List of usage examples for java.lang.reflect Field getAnnotations
public Annotation[] getAnnotations()
From source file:Main.java
public static List<Field> listAnnotatedFields(Class<?> cls) { ArrayList<Class<?>> clsTree = new ArrayList<Class<?>>(); boolean enteredDroidParts = false; do {//from w w w . ja va 2 s . co m clsTree.add(0, cls); boolean inDroidParts = cls.getName().startsWith("org.droidparts"); if (enteredDroidParts && !inDroidParts) { break; } else { enteredDroidParts = inDroidParts; cls = cls.getSuperclass(); } } while (cls != null); ArrayList<Field> fields = new ArrayList<Field>(); for (Class<?> c : clsTree) { for (Field f : c.getDeclaredFields()) { if (f.getAnnotations().length > 0) { fields.add(f); } } } return fields; }
From source file:org.openmobster.core.synchronizer.server.engine.Tools.java
public static String getOid(MobileBean record) { try {/*from ww w . j a v a 2s.c o m*/ String id = ""; Class recordClazz = record.getClass(); Field[] declaredFields = recordClazz.getDeclaredFields(); for (Field field : declaredFields) { Annotation[] annotations = field.getAnnotations(); for (Annotation annotation : annotations) { if (annotation instanceof MobileBeanId) { return BeanUtils.getProperty(record, field.getName()); } } } return id; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.nortal.petit.beanmapper.BeanMappingReflectionUtils.java
private static void readAnnotations(List<Annotation> l, Class<?> type, String name) { Column ao = getAttributeOverride(type, name); if (ao != null) { l.add(ao);/*from w w w . java 2s . co m*/ } Field field = FieldUtils.getDeclaredField(type, name, true); if (field != null) { addAll(l, field.getAnnotations()); } PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(type, name); if (pd != null) { if (pd.getReadMethod() != null) { addAll(l, pd.getReadMethod().getAnnotations()); } } if (type.getSuperclass() != null) { readAnnotations(l, type.getSuperclass(), name); } }
From source file:org.nuunframework.cli.NuunCliTypeListener.java
static boolean annotationPresent(Field field, Class<? extends Annotation> annoClass, AnnotationPointer annotationPointer) { for (Annotation anno : field.getAnnotations()) { if (AssertUtils.hasAnnotationDeep(anno.annotationType(), annoClass)) { annotationPointer.annotation = anno; return true; }//from w ww .j a v a2 s. com } return false; }
From source file:com.erudika.para.validation.ValidationUtils.java
/** * Returns all validation constraints that are defined by Java annotation in the core classes. * * @return a map of all core types to all core annotated constraints. See JSR-303. *///from w w w.j av a 2 s.c o m public static Map<String, Map<String, Map<String, Map<String, ?>>>> getCoreValidationConstraints() { if (coreValidationConstraints.isEmpty()) { for (Map.Entry<String, Class<? extends ParaObject>> e : ParaObjectUtils.getCoreClassesMap() .entrySet()) { String type = e.getKey(); List<Field> fieldlist = Utils.getAllDeclaredFields(e.getValue()); for (Field field : fieldlist) { Annotation[] annos = field.getAnnotations(); if (annos.length > 1) { Map<String, Map<String, ?>> constrMap = new HashMap<String, Map<String, ?>>(); for (Annotation anno : annos) { if (isValidConstraintType(anno.annotationType())) { Constraint c = fromAnnotation(anno); if (c != null) { constrMap.put(c.getName(), c.getPayload()); } } } if (!constrMap.isEmpty()) { if (!coreValidationConstraints.containsKey(type)) { coreValidationConstraints.put(type, new HashMap<String, Map<String, Map<String, ?>>>()); } coreValidationConstraints.get(type).put(field.getName(), constrMap); } } } } } return Collections.unmodifiableMap(coreValidationConstraints); }
From source file:com.sugaronrest.restapicalls.ModuleInfo.java
/** * Gets json names from module fields annotations. * * @param type The Java module type.// w w w .j a v a 2 s .c o m * @return List of json property names. */ private static List<ModuleProperty> getFieldAnnotations(Class type) { List<ModuleProperty> modelProperties = new ArrayList<ModuleProperty>(); Iterable<Field> fields = getFieldsUpTo(type, Object.class); for (Field field : fields) { Annotation[] annotations = field.getAnnotations(); for (Annotation annotation : annotations) { if (annotation instanceof JsonProperty) { JsonProperty property = (JsonProperty) annotation; ModuleProperty moduleProperty = new ModuleProperty(); moduleProperty.name = field.getName(); moduleProperty.jsonName = property.value(); moduleProperty.type = field.getType(); moduleProperty.isNumeric = isTypeNumeric(field.getType()); modelProperties.add(moduleProperty); } } } return modelProperties; }
From source file:com.erudika.para.utils.ValidationUtils.java
/** * Returns the JSON Node representation of all validation constraints for a single type. * @param app the app//from ww w . ja v a 2 s. c o m * @param type a valid Para data type * @return a JSON Node object */ public static Map<String, Map<String, Map<String, Object>>> getValidationConstraints(App app, String type) { Map<String, Map<String, Map<String, Object>>> fieldsMap = new HashMap<String, Map<String, Map<String, Object>>>(); if (app != null && !StringUtils.isBlank(type)) { try { List<Field> fieldlist = Utils.getAllDeclaredFields(Utils.toClass(type)); for (Field field : fieldlist) { Annotation[] annos = field.getAnnotations(); if (annos.length > 1) { Map<String, Map<String, Object>> consMap = new HashMap<String, Map<String, Object>>(); for (Annotation anno : annos) { if (isValidConstraintType(anno.annotationType())) { Constraint c = fromAnnotation(anno); if (c != null) { consMap.put(c.getName(), c.getPayload()); } } } if (consMap.size() > 0) { fieldsMap.put(field.getName(), consMap); } } } Map<String, Map<String, Map<String, Object>>> appConstraints = app.getValidationConstraints() .get(type); if (appConstraints != null && !appConstraints.isEmpty()) { fieldsMap.putAll(appConstraints); } } catch (Exception ex) { logger.error(null, ex); } } return fieldsMap; }
From source file:com.test.edusys.common.utils.reflection.ReflectionUtils.java
/** * Comment?// w ww .ja va 2s . co m */ public static Map<String, String> getCommentName(final Class clazz) { Field[] fields = clazz.getDeclaredFields(); Map<String, String> map = new HashMap<String, String>(); for (Field f : fields) { Annotation[] aAnnotation = f.getAnnotations(); for (Annotation annotation : aAnnotation) { if (annotation.annotationType() == Comment.class) { Comment element = (Comment) annotation; map.put(f.getName(), element.value()); } } } return map; }
From source file:net.neevek.android.lib.paginize.util.AnnotationUtils.java
public static void initAnnotatedFields(Class clazz, Object object, ViewFinder viewFinder, boolean initForLazy) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException {/* w w w .j av a2 s. c om*/ Field fields[] = clazz.getDeclaredFields(); Map<Class, Object> targetListenerCache = new HashMap<Class, Object>(); for (int i = 0; i < fields.length; ++i) { Field field = fields[i]; Annotation[] annotations = field.getAnnotations(); if (annotations == null || annotations.length == 0) { continue; } for (int j = 0; j < annotations.length; ++j) { Annotation anno = annotations[j]; if (!InjectView.class.isAssignableFrom(anno.getClass())) { continue; } InjectView injectViewAnno = (InjectView) anno; if ((!initForLazy && injectViewAnno.lazy()) || (initForLazy && !injectViewAnno.lazy())) { continue; } View view = viewFinder.findViewById(injectViewAnno.value()); if (view == null) { if (initForLazy && injectViewAnno.lazy()) { // view == null is tolerable in this case, we assume that this field // be injected later with another call to ViewWrapper.lazyInitializeLayout(). continue; } throw new IllegalArgumentException("View 0x" + Integer.toHexString(injectViewAnno.value()) + " specified in @InjectView on this field is not found: " + field.getName() + ", if this field is meant to be injected lazily, remember to specify the 'lazy' attribute."); } try { field.setAccessible(true); field.set(object, view); } catch (IllegalAccessException e) { String errMsg = "@InjectView() on '" + field.getName() + "' failed. "; if (field.getType() != view.getClass()) { errMsg += (view.getClass().getSimpleName() + " cannot be cast to " + field.getType().getSimpleName()); } throw new IllegalAccessException(errMsg); } catch (IllegalArgumentException e) { String errMsg = "@InjectView() on '" + field.getName() + "' failed. "; if (field.getType() != view.getClass()) { errMsg += (view.getClass().getSimpleName() + " cannot be cast to " + field.getType().getSimpleName()); } throw new IllegalArgumentException(errMsg); } Class[] listenerTypes = injectViewAnno.listenerTypes(); if (listenerTypes == null || listenerTypes.length == 0) { continue; } Object targetListener = getTargetListener(clazz, object, targetListenerCache, injectViewAnno.listener(), "@InjectView"); if (targetListener == null) { targetListener = object; } AnnotationUtils.setListenersForView(view, injectViewAnno.listenerTypes(), targetListener); } } }
From source file:com.test.edusys.common.utils.reflection.ReflectionUtils.java
/** * //ww w. j a v a 2 s . c om */ public static List<String> getShowLogFieldName(final Class clazz) { List<String> lS = new ArrayList<String>(); Field[] fields = clazz.getDeclaredFields(); for (Field f : fields) { String filedName = f.getName(); Annotation[] annotation = f.getAnnotations(); for (Annotation annotation2 : annotation) { if (annotation2.annotationType() == ShowLog.class) lS.add(filedName); } } return lS; }