Example usage for java.lang.reflect Field isAnnotationPresent

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

Introduction

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

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:com.conversantmedia.mapreduce.tool.AnnotatedToolContext.java

@Override
protected void initExtraOptions(Options options) {
    // Find all the fields with @Option annotations...
    fieldsMap = new HashMap<>();
    addedOptionsMap = new HashMap<>();
    defaultValuesMap = new HashMap<>();

    Class<?> clazz = this.bean.getClass();
    while (clazz != Object.class) {
        for (Field field : clazz.getDeclaredFields()) {
            if (field.isAnnotationPresent(Option.class)) {
                Option option = field.getAnnotation(Option.class);
                // Ensure we don't add the same option more than 1x
                // For example, if our annotated bean includes an 'input',
                // we don't want to add a second version
                String optName = getValue(option.name(), field.getName());
                org.apache.commons.cli.Option opt;
                if (options.hasOption(optName)) {
                    opt = options.getOption(optName);
                    updateOption(option, opt);
                } else {
                    opt = initOption(options, option, optName);
                    options.addOption(opt);
                    addedOptionsMap.put(opt.getLongOpt(), field);
                }/*from  w  ww.j a  v  a 2  s. com*/
                fieldsMap.put(opt.getLongOpt(), field);
                defaultValuesMap.put(opt.getLongOpt(), option.defaultValue());
            }
        }
        clazz = clazz.getSuperclass();
    }
}

From source file:de.taimos.dvalin.interconnect.core.spring.InterconnectBeanPostProcessor.java

private InjectionMetadata buildResourceMetadata(Class<?> clazz) {
    LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
    Class<?> targetClass = clazz;

    do {//  w w  w  . jav a 2  s .c o  m
        LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>();
        for (Field field : targetClass.getDeclaredFields()) {
            if (field.isAnnotationPresent(Interconnect.class)) {
                if (Modifier.isStatic(field.getModifiers())) {
                    throw new IllegalStateException(
                            "@Interconnect annotation is not supported on static fields");
                }
                currElements.add(new InterconnectElement(field, null));
            }
        }
        for (Method method : targetClass.getDeclaredMethods()) {
            Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
            if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
                continue;
            }
            if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                if (bridgedMethod.isAnnotationPresent(Interconnect.class)) {
                    if (Modifier.isStatic(method.getModifiers())) {
                        throw new IllegalStateException(
                                "@Interconnect annotation is not supported on static methods");
                    }
                    if (method.getParameterTypes().length != 1) {
                        throw new IllegalStateException(
                                "@Interconnect annotation requires a single-arg method: " + method);
                    }
                    PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                    currElements.add(new InterconnectElement(method, pd));
                }
            }
        }
        elements.addAll(0, currElements);
        targetClass = targetClass.getSuperclass();
    } while ((targetClass != null) && (targetClass != Object.class));

    return new InjectionMetadata(clazz, elements);
}

From source file:com.haulmont.chile.jpa.loader.JPAAnnotationsLoader.java

protected boolean isPrimaryKey(Field field) {
    return field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class);
}

From source file:com.haulmont.chile.jpa.loader.JPAAnnotationsLoader.java

protected boolean isEmbedded(Field field) {
    return field.isAnnotationPresent(Embedded.class) || field.isAnnotationPresent(EmbeddedId.class);
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test default configuration.//from w  w  w  . ja  va  2s  .c  o m
 */
@Test
public void checkDefaultConfiguration() {
    Class<XMenFactory.DefaultConfig> oC = XMenFactory.DefaultConfig.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            assertEquals(xlsElement.comment(), "");
            assertEquals(xlsElement.commentRules(), "");
            assertEquals(xlsElement.decorator(), "");
            assertEquals(xlsElement.formatMask(), "");
            assertEquals(xlsElement.transformMask(), "");
            assertEquals(xlsElement.isFormula(), false);
            assertEquals(xlsElement.formula(), "");
            assertEquals(xlsElement.customizedRules(), "");
            assertEquals(xlsElement.columnWidthInUnits(), 0);
            assertEquals(xlsElement.parentSheet(), false);
        }
    }
}

From source file:org.neovera.jdiablo.environment.SpringEnvironment.java

private void injectProperties(Object object) {
    Map<String, PropertyPlaceholderProvider> map = _context.getBeansOfType(PropertyPlaceholderProvider.class);
    PropertyPlaceholderProvider ppp = null;
    if (map.size() != 0) {
        ppp = map.values().iterator().next();
    }//from  w ww. j a v a  2 s .com

    // Analyze members to see if they are annotated.
    Map<String, String> propertyNamesByField = new HashMap<String, String>();
    Class<?> clz = object.getClass();
    while (!clz.equals(Object.class)) {
        for (Field field : clz.getDeclaredFields()) {
            if (field.isAnnotationPresent(PropertyPlaceholder.class)) {
                propertyNamesByField.put(
                        field.getName().startsWith("_") ? field.getName().substring(1) : field.getName(),
                        field.getAnnotation(PropertyPlaceholder.class).value());
            }
        }
        clz = clz.getSuperclass();
    }

    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(object.getClass());
    for (PropertyDescriptor pd : descriptors) {
        if (propertyNamesByField.keySet().contains(pd.getName())) {
            if (ppp == null) {
                _logger.error(
                        "Field {} is annotated with PropertyPlaceholder but no bean of type "
                                + "PropertyPlaceholderProvider is defined in the Spring application context.",
                        pd.getName());
                break;
            } else {
                setValue(pd, object, ppp.getProperty(propertyNamesByField.get(pd.getName())));
            }
        } else if (pd.getReadMethod() != null
                && pd.getReadMethod().isAnnotationPresent(PropertyPlaceholder.class)) {
            if (ppp == null) {
                _logger.error(
                        "Field {} is annotated with PropertyPlaceholder but no bean of type "
                                + "PropertyPlaceholderProvider is defined in the Spring application context.",
                        pd.getName());
                break;
            } else {
                setValue(pd, object,
                        ppp.getProperty(pd.getReadMethod().getAnnotation(PropertyPlaceholder.class).value()));
            }
        }
    }
}

From source file:com.haulmont.chile.jpa.loader.JPAAnnotationsLoader.java

protected boolean isPersistent(Field field) {
    return field.isAnnotationPresent(Column.class) || field.isAnnotationPresent(ManyToOne.class)
            || field.isAnnotationPresent(OneToMany.class) || field.isAnnotationPresent(ManyToMany.class)
            || field.isAnnotationPresent(OneToOne.class) || field.isAnnotationPresent(Embedded.class)
            || field.isAnnotationPresent(EmbeddedId.class);
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the title attribute over multiples types with
 * specific value./*w w  w  .  ja  v  a 2  s. co m*/
 */
@Test
public void checkTitleAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (f.getName().equals("dateAttribute")) {
                assertEquals(xlsElement.title(), "Date value");
            } else if (f.getName().equals("stringAttribute")) {
                assertEquals(xlsElement.title(), "String value");
            } else if (f.getName().equals("integerAttribute")) {
                assertEquals(xlsElement.title(), "Integer value");
            } else if (f.getName().equals("doubleAttribute1")) {
                assertEquals(xlsElement.title(), "Double value 1");
            } else if (f.getName().equals("doubleAttribute2")) {
                assertEquals(xlsElement.title(), "Double value 2");
            } else if (f.getName().equals("sum")) {
                assertEquals(xlsElement.title(), "Sum double 1 & double 2");
            }
        }
    }
}

From source file:com.haulmont.chile.jpa.loader.JPAAnnotationsLoader.java

@Override
protected boolean isMetaPropertyField(Field field) {
    return field.isAnnotationPresent(Column.class) || field.isAnnotationPresent(ManyToOne.class)
            || field.isAnnotationPresent(OneToMany.class) || field.isAnnotationPresent(ManyToMany.class)
            || field.isAnnotationPresent(OneToOne.class) || field.isAnnotationPresent(Embedded.class)
            || field.isAnnotationPresent(EmbeddedId.class) || super.isMetaPropertyField(field);
}

From source file:net.ceos.project.poi.annotated.annotation.XlsFreeElementTest.java

/**
 * Test initialization of the comment attribute with specific value.
 *///from  ww  w. j a v a2s.  c o  m
@Test
public void checkCommentAttribute() {
    Class<XMenFactory.ProfessorX> oC = XMenFactory.ProfessorX.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsFreeElement
        if (f.isAnnotationPresent(XlsFreeElement.class)) {

            XlsFreeElement xlsFreeElement = (XlsFreeElement) f.getAnnotation(XlsFreeElement.class);

            if (f.getName().equals("stringFreeAttribute3")
                    && StringUtils.isNotBlank(xlsFreeElement.comment())) {
                assertEquals(xlsFreeElement.comment(), "Free element with sample comment");
            }
        }
    }
}