List of usage examples for java.lang.reflect Field getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:nl.surfnet.coin.teams.control.AbstractControllerTest.java
private void doAutoWireRemainingResources(Object target, Field[] fields) throws IllegalAccessException { for (Field field : fields) { ReflectionUtils.makeAccessible(field); if (field.getAnnotation(Autowired.class) != null && field.get(target) == null) { field.set(target, mock(field.getType(), new DoesNothing())); }/*w w w. java 2s . c o m*/ } }
From source file:de.tarent.maven.plugins.pkg.Utils.java
/** * Sets all unset properties, either to the values of the parent or to a * (hard-coded) default value if the property is not set in the parent. * // w ww. ja v a 2 s . com * <p> * Using this method the packaging plugin can generate a merge of the * default and a distro-specific configuration. * </p> * * @param child * @param parent * @return * @throws MojoExecutionException */ public static TargetConfiguration mergeConfigurations(TargetConfiguration child, TargetConfiguration parent) throws MojoExecutionException { if (child.isReady()) { throw new MojoExecutionException( String.format("target configuration '%s' is already merged.", child.getTarget())); } Field[] allFields = TargetConfiguration.class.getDeclaredFields(); for (Field field : allFields) { field.setAccessible(true); if (field.getAnnotation(MergeMe.class) != null) { try { Object defaultValue = new Object(); IMerge merger; if (field.getAnnotation(MergeMe.class).defaultValueIsNull()) { defaultValue = null; } if (field.getType() == Properties.class) { if (defaultValue != null) { defaultValue = new Properties(); } merger = new PropertiesMerger(); } else if (field.getType() == List.class) { if (defaultValue != null) { defaultValue = new ArrayList<Object>(); } merger = new CollectionMerger(); } else if (field.getType() == Set.class) { if (defaultValue != null) { defaultValue = new HashSet<Object>(); } merger = new CollectionMerger(); } else if (field.getType() == String.class) { if (defaultValue != null) { defaultValue = field.getAnnotation(MergeMe.class).defaultString(); } merger = new ObjectMerger(); } else if (field.getType() == Boolean.class) { defaultValue = field.getAnnotation(MergeMe.class).defaultBoolean(); merger = new ObjectMerger(); } else { merger = new ObjectMerger(); } Object childValue = field.get(child); Object parentValue = field.get(parent); try { field.set(child, merger.merge(childValue, parentValue, defaultValue)); } catch (InstantiationException e) { throw new MojoExecutionException("Error merging configurations", e); } } catch (SecurityException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (IllegalArgumentException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (IllegalAccessException e) { throw new MojoExecutionException(e.getMessage(), e); } } } child.setReady(true); return child; }
From source file:jofc2.OFC.java
/** * ??/*from w ww . j a v a 2s .c o m*/ * @param c */ private void doAlias(Class<?> c) { /** * */ if (c.isAnnotationPresent(Alias.class)) { converter.alias(c.getAnnotation(Alias.class).value(), c); } /** * ?? */ for (Field f : c.getDeclaredFields()) { if (f.isAnnotationPresent(Alias.class)) { if (f.getAnnotation(Alias.class).value().equals("")) { System.out.println(f.getName()); } converter.aliasField(f.getAnnotation(Alias.class).value(), c, f.getName()); } } }
From source file:com.github.rvesse.airline.Accessor.java
public <T extends Annotation> T getAnnotation(Class<T> annotationCls) { Field lastField = path.get(path.size() - 1); return lastField.getAnnotation(annotationCls); }
From source file:be.fedict.trust.service.snmp.SNMPInterceptor.java
private void updateSnmpFields(Object target) { for (Field field : target.getClass().getDeclaredFields()) { SNMP fieldSnmp = field.getAnnotation(SNMP.class); /*//from ww w . j a v a 2 s. c o m * Derived SNMP fields are updated on SNMPCounter method invocations */ if (null == fieldSnmp || fieldSnmp.derived()) { continue; } // retrieve the possibly changed field value field.setAccessible(true); Long value; try { value = (Long) field.get(target); } catch (Exception e) { LOG.error("Failed to get field=" + field.getName() + " value", e); return; } // increment the SNMP counter's value with the delta of the current // field value and the initial value Long increment = value - this.values.get(field.getName()); if (increment != 0L) increment(fieldSnmp.oid(), fieldSnmp.service(), value - this.values.get(field.getName())); } }
From source file:com.khubla.cbean.CBeanType.java
/** * find id field/* w w w . jav a2 s . c o m*/ */ private Id findId() { final Field[] fields = clazz.getDeclaredFields(); if (null != fields) { for (int i = 0; i < fields.length; i++) { final Field field = fields[i]; final Id id = field.getAnnotation(Id.class); if (null != id) { return id; } } } return null; }
From source file:com.khubla.cbean.CBeanType.java
/** * get the name of the Id field//from w w w .ja va2s .c om */ private Field findIdField() { final Field[] fields = clazz.getDeclaredFields(); if (null != fields) { for (int i = 0; i < fields.length; i++) { final Field field = fields[i]; final Id id = field.getAnnotation(Id.class); if (null != id) { return field; } } } return null; }
From source file:es.logongas.ix3.dao.impl.ExceptionTranslator.java
private ClassAndLabel getFieldLabel(Class clazz, String fieldName) { Field field = ReflectionUtils.findField(clazz, fieldName); if (field == null) { return null; }/*ww w . j a va 2s.c o m*/ Label label = field.getAnnotation(Label.class); if (label != null) { return new ClassAndLabel(field.getType(), label.value()); } else { return new ClassAndLabel(field.getType(), null); } }
From source file:com.flowpowered.cerealization.config.annotated.AnnotatedObjectConfiguration.java
private void save(ConfigurationNodeSource source, Object object, String[] path, Set<Member> members) throws ConfigurationException { final Set<Method> methods = new HashSet<Method>(); for (Member member : members) { if (member instanceof Method) { final Method method = (Method) member; if (method.isAnnotationPresent(Save.class)) { methods.add(method);/* w w w . j ava 2s. com*/ } continue; } final Field field = (Field) member; field.setAccessible(true); String[] fieldPath = field.getAnnotation(Setting.class).value(); if (fieldPath.length == 0) { fieldPath = new String[] { field.getName() }; } final ConfigurationNode fieldNode = source.getNode(ArrayUtils.addAll(path, fieldPath)); try { fieldNode.setValue(field.getGenericType(), field.get(object)); } catch (IllegalAccessException ex) { throw new ConfigurationException(ex); } } invokeMethods(methods, object, source.getNode(path)); }
From source file:info.archinnov.achilles.internal.metadata.parsing.EmbeddedIdParser.java
private Integer extractReversedClusteredKey(Class<?> embeddedIdClass) { log.debug("Extract reversed clustering component from embedded id class {} ", embeddedIdClass.getCanonicalName()); @SuppressWarnings("unchecked") Set<Field> candidateFields = getFields(embeddedIdClass, ReflectionUtils.<Field>withAnnotation(Order.class)); List<Integer> reversedFields = new LinkedList<Integer>(); for (Field candidateField : candidateFields) { Order orderAnnotation = candidateField.getAnnotation(Order.class); if (orderAnnotation.reversed()) { reversedFields.add(orderAnnotation.value()); }/*from w w w.j a v a2s. co m*/ } Validator.validateBeanMappingTrue(reversedFields.size() <= 1, "There should be at most 1 field annotated with @Order(reversed=true) for the @EmbeddedId class '%s'", embeddedIdClass.getCanonicalName()); return reversedFields.size() > 0 ? reversedFields.get(0) : null; }