Example usage for java.lang.reflect AnnotatedElement isAnnotationPresent

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

Introduction

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

Prototype

default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Document

Returns true if an annotation for the specified type is present on this element, else false.

Usage

From source file:com.haulmont.cuba.gui.ControllerDependencyInjector.java

protected void doInjection(AnnotatedElement element, Class annotationClass) {
    Class<?> type;//from  www  .j  a va2  s .  c  om
    String name = null;
    if (annotationClass == Named.class)
        name = element.getAnnotation(Named.class).value();
    else if (annotationClass == Resource.class)
        name = element.getAnnotation(Resource.class).name();
    else if (annotationClass == WindowParam.class)
        name = element.getAnnotation(WindowParam.class).name();

    boolean required = true;
    if (element.isAnnotationPresent(WindowParam.class))
        required = element.getAnnotation(WindowParam.class).required();

    if (element instanceof Field) {
        type = ((Field) element).getType();
        if (StringUtils.isEmpty(name))
            name = ((Field) element).getName();
    } else if (element instanceof Method) {
        Class<?>[] types = ((Method) element).getParameterTypes();
        if (types.length != 1)
            throw new IllegalStateException("Can inject to methods with one parameter only");
        type = types[0];
        if (StringUtils.isEmpty(name)) {
            if (((Method) element).getName().startsWith("set"))
                name = StringUtils.uncapitalize(((Method) element).getName().substring(3));
            else
                name = ((Method) element).getName();
        }
    } else {
        throw new IllegalStateException("Can inject to fields and setter methods only");
    }

    Object instance = getInjectedInstance(type, name, annotationClass, element);

    if (instance != null) {
        assignValue(element, instance);
    } else if (required) {
        Class<?> declaringClass = ((Member) element).getDeclaringClass();
        Class<? extends Frame> frameClass = frame.getClass();

        String msg;
        if (frameClass == declaringClass) {
            msg = String.format("CDI - Unable to find an instance of type '%s' named '%s' for instance of '%s'",
                    type, name, frameClass.getCanonicalName());
        } else {
            msg = String.format(
                    "CDI - Unable to find an instance of type '%s' named '%s' declared in '%s' for instance of '%s'",
                    type, name, declaringClass.getCanonicalName(), frameClass.getCanonicalName());
        }

        Logger log = LoggerFactory.getLogger(ControllerDependencyInjector.class);
        log.warn(msg);
    }
}

From source file:com.haulmont.cuba.core.global.MetadataTools.java

/**
 * Determine whether the given property is on the owning side of an association.
 *///w w w.  j  av a  2s .  c om
public boolean isOwningSide(MetaProperty metaProperty) {
    checkNotNullArgument(metaProperty, "metaProperty is null");
    if (!metaProperty.getRange().isClass())
        return false;

    AnnotatedElement el = metaProperty.getAnnotatedElement();
    for (Annotation annotation : el.getAnnotations()) {
        if (annotation instanceof ManyToOne)
            return true;
        if (annotation instanceof OneToMany || annotation instanceof OneToOne)
            return el.isAnnotationPresent(JoinColumn.class) || el.isAnnotationPresent(JoinTable.class);
        if (annotation instanceof ManyToMany)
            return el.isAnnotationPresent(JoinTable.class);
    }

    return false;
}

From source file:org.apache.camel.util.ObjectHelper.java

/**
 * Checks if a Class or Method are annotated with the given annotation
 *
 * @param elem the Class or Method to reflect on
 * @param annotationType the annotation type
 * @param checkMetaAnnotations check for meta annotations
 * @return true if annotations is present
 *//* w  w w . j av a2 s. c  o m*/
public static boolean hasAnnotation(AnnotatedElement elem, Class<? extends Annotation> annotationType,
        boolean checkMetaAnnotations) {
    if (elem.isAnnotationPresent(annotationType)) {
        return true;
    }
    if (checkMetaAnnotations) {
        for (Annotation a : elem.getAnnotations()) {
            for (Annotation meta : a.annotationType().getAnnotations()) {
                if (meta.annotationType().getName().equals(annotationType.getName())) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:org.apache.sling.models.impl.model.AbstractInjectableElement.java

private static boolean getHasDefaultValue(AnnotatedElement element,
        InjectAnnotationProcessor2 annotationProcessor) {
    if (annotationProcessor != null) {
        return annotationProcessor.hasDefault();
    }/*from ww  w .  j av a 2  s  .  c  o m*/
    return element.isAnnotationPresent(Default.class);
}

From source file:org.apache.sling.models.impl.model.AbstractInjectableElement.java

private static boolean getOptional(AnnotatedElement element, InjectAnnotationProcessor annotationProcessor) {
    if (annotationProcessor != null) {
        Boolean optional = annotationProcessor.isOptional();
        if (optional != null) {
            return optional.booleanValue();
        }/*from  ww w  . j av a 2  s .  c  o m*/
    }
    return element.isAnnotationPresent(Optional.class);
}

From source file:org.apache.sling.models.impl.model.AbstractInjectableElement.java

private static boolean getRequired(AnnotatedElement element, InjectAnnotationProcessor annotationProcessor) {
    // do not evaluate the injector-specific annotation (those are only considered for optional)
    // even setting optional=false will not make an attribute mandatory
    return element.isAnnotationPresent(Required.class);
}

From source file:org.castor.cpa.jpa.processors.classprocessors.JPACacheProcessor.java

/**
 * {@inheritDoc}/*from   w  w  w .  j a  v  a 2 s  .co  m*/
 */
public final <I extends BaseNature, A extends Annotation> boolean processAnnotation(final I info,
        final A annotation, final AnnotatedElement target) throws AnnotationTargetException {

    if ((info instanceof JPAClassNature) && (annotation instanceof Cache) && (target instanceof Class<?>)
            && (target.isAnnotationPresent(Cache.class))) {
        _log.debug("processing class annotation " + annotation.toString());

        JPAClassNature jpaClassNature = (JPAClassNature) info;

        Cache cache = (Cache) annotation;
        CacheProperty[] annotatedProperties = cache.value();
        Properties plainProperties = new Properties();
        for (CacheProperty property : annotatedProperties) {
            plainProperties.setProperty(property.key(), property.value());
        }
        jpaClassNature.setCacheProperties(plainProperties);
        return true;
    }

    return false;
}

From source file:org.castor.cpa.jpa.processors.classprocessors.JPAInheritanceProcessor.java

/**
 * {@inheritDoc}/*  w w  w . j  a  v a  2 s .com*/
 * 
 * @see org.castor.core.annotationprocessing.TargetAwareAnnotationProcessor#
 *      processAnnotation(BaseNature, Annotation, AnnotatedElement)
 */
public <I extends BaseNature, A extends Annotation> boolean processAnnotation(final I info, final A annotation,
        final AnnotatedElement target) throws AnnotationTargetException {

    if ((info instanceof JPAClassNature) && (annotation instanceof Inheritance) && (target instanceof Class<?>)
            && (target.isAnnotationPresent(Inheritance.class))) {
        LOG.debug("Processing class annotation " + annotation.toString());

        JPAClassNature jpaClassNature = (JPAClassNature) info;
        Inheritance inheritance = (Inheritance) annotation;
        InheritanceType strategy = inheritance.strategy();

        if (strategy != InheritanceType.JOINED) {
            throw new AnnotationTargetException("InheritanceType not supported: " + strategy.toString());
        }

        jpaClassNature.setInheritanceStrategy(strategy);
        return true;
    }

    return false;
}

From source file:org.castor.cpa.jpa.processors.classprocessors.JPAMappedSuperclassProcessor.java

/**
 * {@inheritDoc}//from  www .  j av a  2s . c  o  m
 */
public final <I extends BaseNature, A extends Annotation> boolean processAnnotation(final I info,
        final A annotation, final AnnotatedElement target) throws AnnotationTargetException {
    if ((info instanceof JPAClassNature) && (annotation instanceof MappedSuperclass)
            && (target instanceof Class<?>) && (target.isAnnotationPresent(MappedSuperclass.class))) {
        LOG.debug("Processing class annotation " + annotation.toString());

        final JPAClassNature jpaClassNature = (JPAClassNature) info;
        jpaClassNature.setMappedSuperclass(true);
        return true;
    }
    return false;
}

From source file:org.castor.cpa.jpa.processors.classprocessors.JPANamedNativeQueriesProcessor.java

/**
 * {@inheritDoc}/* www  .  j  a v  a2s . c o  m*/
 */
public final <I extends BaseNature, A extends Annotation> boolean processAnnotation(final I info,
        final A annotation, final AnnotatedElement target) throws AnnotationTargetException {
    if ((info instanceof JPAClassNature) && (annotation instanceof NamedNativeQueries)
            && (target instanceof Class<?>) && (target.isAnnotationPresent(NamedNativeQueries.class))) {
        LOG.debug("Processing class annotation " + annotation.toString());

        final JPAClassNature jpaClassNature = (JPAClassNature) info;
        final NamedNativeQueries namedNativeQueries = (NamedNativeQueries) annotation;
        final NamedNativeQuery[] namedNativeQueryValues = namedNativeQueries.value();
        final Map<String, String> namedNativeQueryMap = new HashMap<String, String>();

        if (namedNativeQueryValues != null && namedNativeQueryValues.length > 0) {
            for (NamedNativeQuery query : namedNativeQueryValues) {
                namedNativeQueryMap.put(query.name(), query.query());
            }
        }
        jpaClassNature.setNamedNativeQuery(namedNativeQueryMap);
        return true;
    }
    return false;
}