Example usage for java.lang.reflect AnnotatedElement getAnnotation

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

Introduction

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

Prototype

<T extends Annotation> T getAnnotation(Class<T> annotationClass);

Source Link

Document

Returns this element's annotation for the specified type if such an annotation is present, else null.

Usage

From source file:org.zkybase.kite.throttle.interceptor.AnnotationThrottleSource.java

private ThrottleTemplate parseAnnotation(AnnotatedElement elem) {
    assert (elem != null);

    GuardedByThrottle ann = elem.getAnnotation(ANN_CLASS);

    // Meta-annotation search (Spring 3)
    // FIXME It looks like this is getting annotations off the element, not
    // off of other annotations...
    //      if (ann == null) {
    //         for (Annotation metaAnn : elem.getAnnotations()) {
    //            ann = metaAnn.annotationType().getAnnotation(ANN_CLASS);
    //            if (ann != null) { break; }
    //         }/*  w  ww . j  av  a2  s  .  co  m*/
    //      }

    // If ann is null, then this returns null
    return parseAnnotation(ann);
}

From source file:com.adobe.acs.commons.models.injectors.annotation.impl.SharedValueMapValueAnnotationProcessorFactory.java

@Override
public InjectAnnotationProcessor2 createAnnotationProcessor(final AnnotatedElement element) {
    // check if the element has the expected annotation
    SharedValueMapValue annotation = element.getAnnotation(SharedValueMapValue.class);
    if (annotation != null) {
        return new SharedValueMapValueAnnotationProcessor(annotation);
    }/*from w w w. j  av  a 2s .c o m*/
    return null;
}

From source file:com.sf.ddao.crud.ops.SelectWithCallbackThenUpdateSqlOperation.java

@Override
public void init(AnnotatedElement element, Annotation annotation) {
    SelectWithCallbackThenUpdate selectWithCallbackThenUpdate = element
            .getAnnotation(SelectWithCallbackThenUpdate.class);
    String sql[] = selectWithCallbackThenUpdate.value();
    if (sql.length != 2) {
        throw new InitializerException(SelectWithCallbackThenUpdate.class.getSimpleName()
                + " annotation has to have 2 sql statments, but got:" + Arrays.toString(sql) + ", for method "
                + element);//from   w ww.j ava  2s  .  c o  m
    }
    try {
        selectSqlOp.init(element, sql[0]);
        super.init(element, sql[1]);
    } catch (Exception e) {
        throw new InitializerException(
                "Failed to setup sql operations " + Arrays.toString(sql) + " for method " + element, e);
    }
}

From source file:com.gradecak.alfresco.mvc.aop.AuthenticationAdvice.java

private AlfrescoAuthentication parseAnnotation(AnnotatedElement ae) {
    AlfrescoAuthentication ann = ae.getAnnotation(AlfrescoAuthentication.class);
    if (ann == null) {
        for (Annotation metaAnn : ae.getAnnotations()) {
            ann = metaAnn.annotationType().getAnnotation(AlfrescoAuthentication.class);
            if (ann != null) {
                break;
            }//from www . ja va2s  .  co  m
        }
    }
    if (ann != null) {
        return parseAnnotation(ann);
    } else {
        return null;
    }
}

From source file:com.sf.ddao.ops.InsertAndGetGeneratedKeySqlOperation.java

public void init(AnnotatedElement element, Annotation annotation) {
    method = (Method) element;/* w w  w .  ja  v  a  2 s. c om*/
    InsertAndGetGeneratedKey insertAndGetGeneratedKey = element.getAnnotation(InsertAndGetGeneratedKey.class);
    String sql = insertAndGetGeneratedKey.value();
    try {
        statementFactory.init(element, sql);
    } catch (StatementFactoryException e) {
        throw new InitializerException("Failed to setup sql operation " + sql + " for method " + method, e);
    }
}

From source file:com.basho.riak.client.convert.RiakBeanSerializerModifier.java

/**
 * Checks if the property has any of the Riak annotations on it or the 
 * Jackson JsonProperty annotation. // w ww.j  ava 2 s  .  c  om
 * 
 * If a Riak annotation is present without the Jackson JsonProperty
 * annotation, this will return false.
 * 
 * If a property has been annotated with both the Jackson JsonProperty
 * annotation and a Riak annotation, the Jackson annotation takes precedent 
 * and this will return true.
 * 
 * @param beanPropertyWriter
 *            a {@link BeanPropertyWriter} to check for Riak* annotations
 * @return true if the property is not Riak annotated or is Jackson
* JsonProperty annotated, false otherwise
 */
private boolean keepProperty(BeanPropertyWriter beanPropertyWriter) {
    RiakKey key = null;
    RiakUsermeta usermeta = null;
    RiakLinks links = null;
    RiakIndex index = null;
    RiakVClock vclock = null;
    JsonProperty jacksonJsonProperty = null;
    RiakTombstone tombstone = null;

    AnnotatedMember member = beanPropertyWriter.getMember();
    if (member instanceof AnnotatedField) {
        AnnotatedElement element = member.getAnnotated();
        key = element.getAnnotation(RiakKey.class);
        usermeta = element.getAnnotation(RiakUsermeta.class);
        links = element.getAnnotation(RiakLinks.class);
        index = element.getAnnotation(RiakIndex.class);
        vclock = element.getAnnotation(RiakVClock.class);
        tombstone = element.getAnnotation(RiakTombstone.class);
        jacksonJsonProperty = element.getAnnotation(JsonProperty.class);
    } else {
        @SuppressWarnings("rawtypes")
        Class clazz = member.getDeclaringClass();
        Field field;
        try {
            field = clazz.getDeclaredField(beanPropertyWriter.getName());
            key = field.getAnnotation(RiakKey.class);
            usermeta = field.getAnnotation(RiakUsermeta.class);
            links = field.getAnnotation(RiakLinks.class);
            index = field.getAnnotation(RiakIndex.class);
            vclock = field.getAnnotation(RiakVClock.class);
            tombstone = field.getAnnotation(RiakTombstone.class);
            jacksonJsonProperty = field.getAnnotation(JsonProperty.class);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            // ignore, not a field means not a Riak annotated field.
        }
    }

    if (jacksonJsonProperty != null) {
        return true;
    } else {
        return key == null && usermeta == null && links == null && vclock == null && index == null
                && tombstone == null;
    }
}

From source file:org.glassfish.jersey.server.spring.AutowiredInjectResolver.java

@Override
public Object resolve(Injectee injectee, ServiceHandle<?> root) {
    AnnotatedElement parent = injectee.getParent();
    String beanName = null;//from w ww .ja  v a 2s. c om
    if (parent != null) {
        Qualifier an = parent.getAnnotation(Qualifier.class);
        if (an != null) {
            beanName = an.value();
        }
    }
    return getBeanFromSpringContext(beanName, injectee);
}

From source file:nl.jeslee.jersey.server.spring.AutowiredInjectResolver.java

@Override
public Object resolve(Injectee injectee, ServiceHandle<?> root) {
    AnnotatedElement parent = injectee.getParent();
    String beanName = null;//from   ww  w. j  ava 2  s .c o  m
    if (parent != null) {
        Qualifier an = parent.getAnnotation(Qualifier.class);
        if (an != null) {
            beanName = an.value();
        }
    }
    boolean required = parent != null ? parent.getAnnotation(Autowired.class).required() : false;
    return getBeanFromSpringContext(beanName, injectee, required);
}

From source file:com.iorga.iraj.json.ContextParamsContextCaller.java

public ContextParamsContextCaller(final String elementName, final AnnotatedElement annotatedElement,
        final Class<?> declaringClass, final ContextParams contextParamsAnnotation) {
    final ContextPath contextPathAnnotation = annotatedElement.getAnnotation(ContextPath.class);
    // First we have to know what is the first context path part of the target annotated member
    final String nextPath;
    if (contextPathAnnotation != null) {
        // the context path is not null, let's take the first element as a key for the context map
        final String contextPath = contextPathAnnotation.value();
        contextMapKey = StringUtils.substringBefore(contextPath, ".");
        nextPath = StringUtils.substringAfter(contextPath, ".");
    } else {/*from   w w  w  . j  a  v  a2s  .  co m*/
        // No @ContextPath, let's take the elementName
        contextMapKey = elementName;
        nextPath = null;
    }

    // Now search the @ContextParam corresponding to the targetAnnotatedMember
    ContextCaller nextContextCaller = null;
    Type returnType = null;
    for (final ContextParam contextParam : contextParamsAnnotation.value()) {
        String contextName = contextParam.name();
        final Class<?> contextClass = contextParam.value();
        if (StringUtils.isBlank(contextName)) {
            contextName = StringUtils.uncapitalize(contextClass.getSimpleName());
        }
        if (contextMapKey.equals(contextName)) {
            // we have our @ContextParam now let's try to know if there is a "nextContextCaller"
            if (StringUtils.isEmpty(nextPath)) {
                // finished to handle the path, there is no other caller
                nextContextCaller = null;
                returnType = TemplateUtils.getGenericType(contextParam);
            } else {
                nextContextCaller = new ObjectContextCaller(contextClass, nextPath);
                returnType = nextContextCaller.getReturnType();
            }
            break;
        }
    }
    if (returnType == null) {
        throw new IllegalArgumentException(
                "Couln't find the @ContextParam corresponding to " + contextMapKey + " in " + declaringClass);
    } else {
        this.nextContextCaller = nextContextCaller;
        this.returnType = returnType;
    }
}

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

@Override
protected Class getTypeOverride(AnnotatedElement element) {
    Temporal temporal = element.getAnnotation(Temporal.class);
    if (temporal != null && temporal.value().equals(TemporalType.DATE))
        return java.sql.Date.class;
    else if (temporal != null && temporal.value().equals(TemporalType.TIME))
        return java.sql.Time.class;
    else//from   w ww. ja  v  a2s  .  co m
        return null;
}