Example usage for java.lang.reflect Field getAnnotations

List of usage examples for java.lang.reflect Field getAnnotations

Introduction

In this page you can find the example usage for java.lang.reflect Field getAnnotations.

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java

public static boolean isByRef(Field field) {

    Annotation[] fieldAnnotations = field.getAnnotations();
    Class<?> type = field.getType();
    return isByRef(type, fieldAnnotations);
}

From source file:org.mule.module.extension.internal.introspection.MuleExtensionAnnotationParser.java

private static void parseGroupParameters(DataType parameterType,
        List<ParameterDescriptor> parameterDescriptors) {
    for (Field field : IntrospectionUtils.getParameterFields(parameterType.getRawType())) {
        if (field.getAnnotation(org.mule.extension.annotations.ParameterGroup.class) != null) {
            parseGroupParameters(getFieldDataType(field), parameterDescriptors);
        } else {/*from  w  ww.j a v a 2 s.com*/
            ParameterDescriptor parameterDescriptor = doParseParameter(field.getName(), getFieldDataType(field),
                    toMap(field.getAnnotations()));
            if (parameterDescriptor != null) {
                parameterDescriptors.add(parameterDescriptor);
            }
        }
    }
}

From source file:ml.shifu.shifu.container.meta.MetaFactory.java

private static boolean isJsonIngoreField(Field field) {
    Annotation[] annotations = field.getAnnotations();
    for (Annotation annotation : annotations) {
        if (annotation.annotationType().getName().equals(JsonIgnore.class.getName())) {
            return true;
        }/* www  . j a  v a 2  s.c  om*/
    }
    return false;
}

From source file:org.openlegacy.support.DefaultRegistryLoader.java

private static void handleEntityAnnotationFields(
        final Collection<FieldAnnotationsLoader> fieldAnnotationLoadersCollection,
        final EntitiesRegistry<?, ?, ?> entitiesRegistry, final Class<?> beanClass) {
    final List<FieldAnnotationsLoader> fieldAnnotationLoaders = sortFieldAnnoationLoaders(
            fieldAnnotationLoadersCollection);

    final CounterContainer counterContainer = new CounterContainer();

    ReflectionUtils.doWithFields(beanClass, new FieldCallback() {

        @Override/*w w  w . ja  va  2s  .  c om*/
        public void doWith(Field field) {
            loadDefinitionFromAnnotations(entitiesRegistry, beanClass, fieldAnnotationLoaders, field);
            counterContainer.counter++;
        }

        private void loadDefinitionFromAnnotations(final EntitiesRegistry<?, ?, ?> entitiesRegistry,
                final Class<?> beanClass, final Collection<FieldAnnotationsLoader> fieldAnnotationLoaders,
                Field field) {
            Annotation[] annotations = field.getAnnotations();
            for (FieldAnnotationsLoader fieldAnnotationsLoader : fieldAnnotationLoaders) {
                for (Annotation annotation : annotations) {
                    if (fieldAnnotationsLoader.match(annotation)) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(MessageFormat.format(
                                    "Loading annotation {0} for field {1} in entity {2} into registry",
                                    annotation.annotationType().getSimpleName(), field.getName(), beanClass));
                        }
                        fieldAnnotationsLoader.load(entitiesRegistry, field, annotation, beanClass,
                                counterContainer.counter);
                    }
                }
            }
        }

    });

}

From source file:net.zcarioca.zcommons.config.data.BeanPropertySetterFactory.java

private static Collection<Annotation> getPropertyAnnotations(Field field, PropertyDescriptor descriptor) {
    Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();

    if (descriptor != null) {
        if (descriptor.getWriteMethod() != null) {
            addAnnotationsToMap(annotations, descriptor.getWriteMethod().getAnnotations());
        }//from   www. j  a v a 2  s  .com
        if (descriptor.getReadMethod() != null) {
            addAnnotationsToMap(annotations, descriptor.getReadMethod().getAnnotations());
        }
    }
    if (field != null) {
        addAnnotationsToMap(annotations, field.getAnnotations());
    }
    return annotations.values();
}

From source file:eu.crisis_economics.abm.model.ModelUtils.java

/**
  * Get a list of subconfiguration objects for a class instance (<code>X</code>). This 
  * method will do the following:/* w ww  . j  a v a  2s.  c o m*/
  * 
  * <ul>
  *   <li> Identify any fields in the input <code>X</code> which carry the
  *        <code>@Parameter</code> and <code>@Submodel</code> annotations.
  *   <li> If <code>X</code> is an instance of {@link ComponentConfiguration}, and
  *        the ID of the accompanying <code>@Parameter</code> annotation is nonempty,
  *        set the scope string of the field to the parameter ID.
  *   <li> Add a reference to the field to a list <code>L</code>.
  *   <li> Repeat the above steps depth-first recursively for each field in <code>X</code>.
  *   <li> Return <code>L</code>
  * </ul>
  * 
  * @param on <br>
  *        The class instance <code>X</code> to search.
  */
public static List<ComponentConfiguration> getSubconfigurators(final Object on) {
    final Class<?> objClass = Preconditions.checkNotNull(on).getClass();
    final List<ComponentConfiguration> result = new ArrayList<ComponentConfiguration>();
    for (Class<?> typeToSearch = objClass; typeToSearch != null; typeToSearch = typeToSearch.getSuperclass()) {
        for (final Field field : typeToSearch.getDeclaredFields()) {
            if (!ComponentConfiguration.class.isAssignableFrom(field.getType()))
                continue;
            field.setAccessible(true);
            try {
                Annotation drilldownAnnotation = null, modelParameterAnnotation = null;
                for (final Annotation element : field.getAnnotations()) {
                    if (element.annotationType().getName() == Submodel.class.getName()) { // Proxies
                        drilldownAnnotation = element;
                        continue;
                    } else if (element.annotationType().getName() == Parameter.class.getName()) { // Proxies
                        modelParameterAnnotation = element;
                        continue;
                    } else
                        continue;
                }
                if (modelParameterAnnotation != null && drilldownAnnotation != null) {
                    final Object value = field.get(on);
                    if (value == null)
                        throw new IllegalStateException(on.getClass().getSimpleName()
                                + ": the value of a configurator member field" + " in "
                                + (on.getClass().getSimpleName() + on.hashCode()) + " is null.");
                    result.add((ComponentConfiguration) value);
                    final boolean isScoped = (Boolean) modelParameterAnnotation.annotationType()
                            .getMethod("Scoped").invoke(modelParameterAnnotation);
                    if (!isScoped)
                        continue;
                    final String id = (String) modelParameterAnnotation.annotationType().getMethod("ID")
                            .invoke(modelParameterAnnotation);
                    if (id.isEmpty())
                        continue;
                    ((ComponentConfiguration) value).setScope(id);
                }
            } catch (final SecurityException e) {
                throw new IllegalStateException(on.getClass().getSimpleName()
                        + ": a security exception was raised when testing a field with name " + field.getName()
                        + " for model parameter annotations. Details follow: " + e.getMessage() + ".");
            } catch (final IllegalArgumentException e) {
                throw new IllegalStateException(on.getClass().getSimpleName()
                        + "search: an illegal argument exception was raised when testing a field with name "
                        + field.getName() + " for model parameter annotations. Details follow: "
                        + e.getMessage() + ".");
            } catch (final IllegalAccessException e) {
                throw new IllegalStateException(on.getClass().getSimpleName()
                        + "search: a security exception was raised when testing a field with name "
                        + field.getName() + " for model parameter annotations. Details follow: "
                        + e.getMessage() + ".");
            } catch (final InvocationTargetException e) {
                throw new IllegalStateException(on.getClass().getSimpleName()
                        + "search: an invokation target exception was raised when testing a field with"
                        + " name " + field.getName() + " for model parameter annotations. Details follow: "
                        + e.getMessage() + ".");
            } catch (final NoSuchMethodException e) {
                throw new IllegalStateException(on.getClass().getSimpleName()
                        + "search: a missing-method exception was raised when testing a field with name "
                        + field.getName() + " for model parameter annotations. Details follow: "
                        + e.getMessage() + ".");
            }
        }
    }
    return result;
}

From source file:com.vmware.qe.framework.datadriven.impl.injector.DataInjectorUsingAnnotations.java

@Override
public void inject(Object test, HierarchicalConfiguration data, HierarchicalConfiguration context) {
    Class<? extends Object> testClass = test.getClass();
    for (Field field : testClass.getDeclaredFields()) {
        for (Annotation annotation : field.getAnnotations()) {
            if (annotation.annotationType().isAssignableFrom(Data.class)) {
            }//from  ww  w  . j  av a 2 s .  com
            Data dataAnnotation = (Data) annotation;
            field.setAccessible(true);
            try {
                if (dataAnnotation.name().equals("")) {
                    field.set(test, data);
                } else {
                    String value = data.getString(dataAnnotation.name());
                    field.set(test, value);
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                throw new DDException("Error in injecting data to field with name = " + field.getName(), e);
            }
        }
    }
}

From source file:eu.crisis_economics.abm.model.ModelUtils.java

/**
  * A depth-first recursive parameter search tool. This function accepts an object
  * ({@code X}) and a {@link String} ID ({@code P}) of a parameter to search for.<br><br>
  * /*w w  w.  j  ava  2 s  .c  o  m*/
  * Any object {@code O} in the configuration hierarchy of {@code X} which possesses a field with
  * a) the appropriate annotation and b) parameter ID is identified and returned.
  * This method will search for member fields {@code F} (with any modifer) which satisfy:
  * 
  * <ul>
  *   <li> {@code F} carries a {@link Parameter} {@link Annotation}.
  *   <li> The {@code ID} of this {@link Parameter} is equal to {@code P}.
  *   <li> {@code F} does not itself belongs to a {@link Submodel} field that satisfies the
  *        above two conditions.
  * </ul>
  * 
  * This search operates as follows:<br><br>
  * 
  * <ul>
  *   <li> If {@code X} contains a member field {@code F} satisfying the above conditions, 
  *        {@code F} is accepted and returned.
  *   <li> Otherwise, each supertype in the inheritance hierarchy of {@code X} is searched.
  *   <li> Apply the above steps recursively (depth-first) to every field {@code F}
  *        in {@code X} annotated with {@link Submodel}, unless {@code F} itself
  *        satisfies the above conditions, in which case {@code F} is accepted and returned.
  * </ul>
  * 
  * This method returns a {@link List} of {@link ConfigurationModifier} objects. Each element
  * in this list corresponds to a field {@code F} somewhere in the inheritance hierarchy of
  * {@code X} which satisfied the above search conditions. {@link ConfigurationModifier}
  * objects facilitate direct changes to the value of each such {@code F}.<br><br>
  * 
  * @param on (<code>X</code>) <br>
  *        The object to search.
  * @param parameterIdToFind on (<code>P</code>) <br>
  *        The ID of the {@link Parameter} to search for.
  */
public static List<ConfigurationModifier> search(final Object on, final String parameterIdToFind) {
    if (on == null)
        throw new IllegalArgumentException("search: object to search is null.");
    if (parameterIdToFind == null || parameterIdToFind.isEmpty())
        throw new IllegalArgumentException("search: parameter name is empty or null.");
    if (VERBOSE_MODE)
        System.out.println("search: searching object " + on.getClass().getSimpleName() + on.hashCode()
                + " for parameters of type " + parameterIdToFind + ".");
    final Class<?> objClass = on.getClass();
    final List<ConfigurationModifier> methodsIdentified = new ArrayList<ConfigurationModifier>();
    for (Class<?> typeToSearch = objClass; typeToSearch != null; typeToSearch = typeToSearch.getSuperclass()) {
        for (final Field field : typeToSearch.getDeclaredFields()) {
            field.setAccessible(true);
            if (VERBOSE_MODE)
                System.out.println("inspecting field with name: " + field.getName() + ".");
            try {
                Annotation drilldownAnnotation = null, modelParameterAnnotation = null;
                for (final Annotation element : field.getAnnotations()) {
                    if (element.annotationType().getName() == Submodel.class.getName()) { // Proxies
                        drilldownAnnotation = element;
                        if (VERBOSE_MODE)
                            System.out.println("field " + field.getName() + " is a subconfiguration.");
                        continue;
                    } else if (element.annotationType().getName() == Parameter.class.getName()) { // Proxies
                        final Class<? extends Annotation> type = element.annotationType();
                        final String id = (String) type.getMethod("ID").invoke(element);
                        if (parameterIdToFind.equals(id)) {
                            modelParameterAnnotation = element;
                            if (VERBOSE_MODE)
                                System.out.println("* field is valid.");
                            continue;
                        } else if (VERBOSE_MODE)
                            System.out.println("field ID [" + id + "] does not match the required ID: "
                                    + parameterIdToFind + ".");
                        continue;
                    } else
                        continue;
                }
                if (modelParameterAnnotation != null) {
                    final ConfigurationModifier fieldWithMutators = findGetterSetterMethods(field, on,
                            parameterIdToFind);
                    methodsIdentified.add(fieldWithMutators);
                    continue;
                } else if (drilldownAnnotation != null) {
                    if (VERBOSE_MODE)
                        System.out.println("descending into subconfiguration: " + field.getName());
                    final Object fieldValue = field.get(on);
                    methodsIdentified.addAll(search(fieldValue, parameterIdToFind));
                    continue;
                }
                if (VERBOSE_MODE)
                    System.out.println("rejecting parameter: " + field.getName());
            } catch (final SecurityException e) {
                throw new IllegalStateException(
                        "search: a security exception was raised when testing a field with name "
                                + field.getName() + " for model parameter annotations. Details follow: "
                                + e.getMessage() + ".");
            } catch (final IllegalArgumentException e) {
                throw new IllegalStateException(
                        "search: an illegal argument exception was raised when testing a field with name "
                                + field.getName() + " for model parameter annotations. Details follow: "
                                + e.getMessage() + ".");
            } catch (final IllegalAccessException e) {
                throw new IllegalStateException(
                        "search: a security exception was raised when testing a field with name "
                                + field.getName() + " for model parameter annotations. Details follow: "
                                + e.getMessage() + ".");
            } catch (final InvocationTargetException e) {
                throw new IllegalStateException(
                        "search: an invokation target exception was raised when testing a field with" + " name "
                                + field.getName() + " for model parameter annotations. Details follow: "
                                + e.getMessage() + ".");
            } catch (final NoSuchMethodException e) {
                throw new IllegalStateException(
                        "search: a missing-method exception was raised when testing a field with name "
                                + field.getName() + " for model parameter annotations. Details follow: "
                                + e.getMessage() + ".");
            }
        }
    }
    if (VERBOSE_MODE)
        System.out.println("searched: " + on.getClass().getSimpleName() + on.hashCode()
                + " for parameters of type " + parameterIdToFind + ".");
    return methodsIdentified;
}

From source file:io.nuun.plugin.configuration.common.ConfigurationTypeListener.java

private boolean annotationPresent(Field field, Class<? extends Annotation> annoClass, Holder h) {

    for (Annotation anno : field.getAnnotations()) {
        if (AssertUtils.hasAnnotationDeep(anno.annotationType(), annoClass)) {
            h.annotation = anno;//from   w  w  w  .  j  av a  2  s. c  o m
            return true;
        }
    }

    return false;
}

From source file:com.dalaran.annotation.FieldMerge.java

public boolean merge(Fragment fragment, View view) {
    Field[] declaredFields = fragment.getClass().getDeclaredFields();
    boolean valid = false;
    for (Field field : declaredFields) {
        Annotation[] annotations = field.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().equals(ViewById.class)) {
                boolean b = checkToMerge(fragment, field, (ViewById) annotation, view);
                if (b) {
                    valid = b;/*from   w  w  w  . j  a  va2s. c  om*/
                }
            }
        }
    }
    return valid;
}