Example usage for org.springframework.core.annotation AnnotationUtils getAnnotation

List of usage examples for org.springframework.core.annotation AnnotationUtils getAnnotation

Introduction

In this page you can find the example usage for org.springframework.core.annotation AnnotationUtils getAnnotation.

Prototype

@Nullable
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) 

Source Link

Document

Get a single Annotation of annotationType from the supplied Method , where the annotation is either present or meta-present on the method.

Usage

From source file:org.dataconservancy.packaging.tool.ser.SerializationAnnotationUtil.java

/**
 * Answers a {@code Map} of {@link PropertyDescriptor} instances, which are used to reflectively access the
 * {@link Serialize serializable} streams on {@code annotatedClass} instances.
 * <p>//from  w w w. j a  v  a 2  s  .  c om
 * Use of {@code PropertyDescriptor} is simply a convenience in lieu of the use of underlying Java reflection.
 * </p>
 * <p>
 * This method looks for fields annotated by the {@code Serialize} annotation on the {@code annotatedClass}.
 * A {@code PropertyDescriptor} is created for each field, and is keyed by the {@code StreamId} in the returned
 * {@code Map}.
 * </p>
 *
 * @param annotatedClass the class to scan for the presense of {@code @Serialize}
 * @return a Map of PropertyDescriptors keyed by their StreamId.
 */
public static Map<StreamId, PropertyDescriptor> getStreamDescriptors(Class annotatedClass) {
    HashMap<StreamId, PropertyDescriptor> results = new HashMap<>();

    Arrays.stream(annotatedClass.getDeclaredFields())
            .filter(candidateField -> AnnotationUtils.getAnnotation(candidateField, Serialize.class) != null)
            .forEach(annotatedField -> {
                AnnotationAttributes attributes = AnnotationUtils.getAnnotationAttributes(annotatedField,
                        AnnotationUtils.getAnnotation(annotatedField, Serialize.class));
                StreamId streamId = (StreamId) attributes.get("streamId");
                PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(PackageState.class,
                        annotatedField.getName());
                results.put(streamId, descriptor);
            });

    return results;
}

From source file:com.stehno.sjdbcx.reflection.extractor.RowMapperExtractor.java

public RowMapper extract(final Method method) {
    final com.stehno.sjdbcx.annotation.RowMapper mapper = AnnotationUtils.getAnnotation(method,
            com.stehno.sjdbcx.annotation.RowMapper.class);
    if (mapper == null) {
        Class mappedType = method.getReturnType();

        if (Collection.class.isAssignableFrom(mappedType)) {
            mappedType = (Class) ((ParameterizedType) method.getGenericReturnType())
                    .getActualTypeArguments()[0];

        } else if (mappedType.isArray()) {
            throw new UnsupportedOperationException("Auto-mapping for array return types is not yet supported");

        } else if (mappedType.isPrimitive()) {
            if (mappedType == int.class || mappedType == long.class) {
                return new SingleColumnRowMapper();

            } else if (mappedType == boolean.class) {
                return new RowMapper<Boolean>() {
                    @Override/*from w  ww  .  ja  v a 2  s  . com*/
                    public Boolean mapRow(final ResultSet resultSet, final int i) throws SQLException {
                        return resultSet.getBoolean(1);
                    }
                };
            }
        }

        return new BeanPropertyRowMapper(mappedType);

    } else {
        final String extractKey = mapper.value();
        if (StringUtils.isEmpty(extractKey)) {
            return resolve((Class<RowMapper>) mapper.type());
        } else {
            return resolve(extractKey);
        }
    }
}

From source file:com.developmentsprint.spring.breaker.annotations.SpringCircuitBreakerAnnotationParser.java

public CircuitBreakerAttribute parseCircuitBreakerAnnotation(AnnotatedElement ae) {
    CircuitBreaker ann = AnnotationUtils.getAnnotation(ae, CircuitBreaker.class);
    if (ann != null) {
        CircuitBreakerAttribute cbAttribute = parseCircuitBreakerAnnotation(ann, ae);
        return cbAttribute;
    } else {/*  w  w  w .  j  a  v  a 2 s.com*/
        return null;
    }
}

From source file:com.yqboots.security.core.audit.annotation.AuditAnnotationParserImpl.java

/**
 * {@inheritDoc}/*from   w  w w. ja va2s  .  c o m*/
 */
@Override
public AuditAttribute parseAuditAnnotation(AnnotatedElement ae) {
    AuditAttribute result = null;

    final Auditable ann = AnnotationUtils.getAnnotation(ae, Auditable.class);
    if (ann != null) {
        result = parseAuditAnnotation(ann);
    }

    return result;
}

From source file:org.activiti.spring.components.aop.util.MetaAnnotationMethodMatcher.java

@Override
@SuppressWarnings("rawtypes")
public boolean matches(Method method, Class targetClass) {
    if (AnnotationUtils.getAnnotation(method, this.annotationType) != null) {
        return true;
    }// w w  w . j  a  v a  2 s  .c  o m
    // The method may be on an interface, so let's check on the target class as well.
    Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
    return (specificMethod != method
            && (AnnotationUtils.getAnnotation(specificMethod, this.annotationType) != null));
}

From source file:org.grails.datastore.mapping.keyvalue.mapping.config.AnnotationKeyValueMappingFactory.java

@Override
public KeyValue createMappedForm(PersistentProperty mpp) {
    final Class javaClass = mpp.getOwner().getJavaClass();
    final ClassPropertyFetcher cpf = ClassPropertyFetcher.forClass(javaClass);

    final PropertyDescriptor pd = cpf.getPropertyDescriptor(mpp.getName());
    final KeyValue kv = super.createMappedForm(mpp);
    Index index = AnnotationUtils.getAnnotation(pd.getReadMethod(), Index.class);

    if (index == null) {
        final Field field = ReflectionUtils.findField(javaClass, mpp.getName());
        if (field != null) {
            ReflectionUtils.makeAccessible(field);
            index = field.getAnnotation(Index.class);
        }//  ww w.java 2s  .  co m
    }
    if (index != null) {
        kv.setIndex(true);
    }

    return kv;
}

From source file:viewfx.view.support.fxml.FxmlViewLoader.java

@SuppressWarnings("unchecked")
public View load(Class<? extends View> viewClass) {
    FxmlView fxmlView = AnnotationUtils.getAnnotation(viewClass, FxmlView.class);

    final Class<? extends FxmlView.PathConvention> convention;
    final Class<? extends FxmlView.PathConvention> defaultConvention = (Class<? extends FxmlView.PathConvention>) getDefaultValue(
            FxmlView.class, "convention");

    final String specifiedLocation;
    final String defaultLocation = (String) getDefaultValue(FxmlView.class, "location");

    if (fxmlView == null) {
        convention = defaultConvention;/*from  www  .ja  v a 2 s. c om*/
        specifiedLocation = defaultLocation;
    } else {
        convention = fxmlView.convention();
        specifiedLocation = fxmlView.location();
    }

    if (convention == null || specifiedLocation == null)
        throw new IllegalStateException("Convention and location should never be null.");

    try {
        final String resolvedLocation;
        if (specifiedLocation.equals(defaultLocation))
            resolvedLocation = convention.newInstance().apply(viewClass);
        else
            resolvedLocation = specifiedLocation;

        URL fxmlUrl = viewClass.getClassLoader().getResource(resolvedLocation);
        if (fxmlUrl == null)
            throw new ViewfxException(
                    "Failed to load view class [%s] because FXML file at [%s] could not be loaded "
                            + "as a classpath resource. Does it exist?",
                    viewClass, specifiedLocation);

        return loadFromFxml(fxmlUrl);
    } catch (InstantiationException | IllegalAccessException ex) {
        throw new ViewfxException(ex, "Failed to load view from class %s", viewClass);
    }
}

From source file:com.stehno.sjdbcx.support.SqlResolver.java

/**
 * Uses the Sql and JdbcRepository annotations to resolve the SQL string to be used.
 *
 * @param prototype the repository prototype class
 * @param method the method to resolve SQL for
 * @return the String of SQL//w  ww .jav  a  2s.  co  m
 */
public String resolve(final Class prototype, final Method method) {
    final JdbcRepository jdbcAnno = AnnotationUtils.findAnnotation(prototype, JdbcRepository.class);
    final Sql sqlAnno = AnnotationUtils.getAnnotation(method, Sql.class);

    if (shouldLookupSql(jdbcAnno, sqlAnno)) {
        final String sqlKey = StringUtils.hasLength(sqlAnno.value()) ? sqlAnno.value()
                : method.getName().toLowerCase();
        final Resource resource = determineResolverResource(prototype, jdbcAnno);

        final String sql = resolve(resource).getProperty(sqlKey);

        log.debug("Resolved SQL ({}) for repository ({}) from resource ({}) property ({})", sql, prototype,
                resource, sqlKey);

        return sql;

    } else {
        return sqlAnno.value();
    }
}

From source file:com.payu.ratel.client.ContextAnnotationAutowireCandidateResolver.java

protected boolean isLazy(DependencyDescriptor descriptor) {
    for (Annotation ann : descriptor.getAnnotations()) {
        Lazy lazy = AnnotationUtils.getAnnotation(ann, Lazy.class);
        if (lazy != null && lazy.value()) {
            return true;
        }//from   w  ww .  ja v a 2  s . c  o m
    }
    MethodParameter methodParam = descriptor.getMethodParameter();
    if (methodParam == null) {
        return false;
    }
    Method method = methodParam.getMethod();
    if (method == null || void.class.equals(method.getReturnType())) {
        Lazy lazy = AnnotationUtils.getAnnotation(methodParam.getAnnotatedElement(), Lazy.class);
        if (lazy != null && lazy.value()) {
            return true;
        }
    }

    return false;
}

From source file:org.jdal.text.FormatUtils.java

public static <A extends Annotation> A getAnnotation(PropertyDescriptor pd, Class<A> annotationType) {
    A annotation = AnnotationUtils.getAnnotation(pd.getReadMethod(), annotationType);
    if (annotation != null)
        return annotation;

    Field field = getDeclaredField(pd);

    if (field != null)
        annotation = AnnotationUtils.getAnnotation(field, annotationType);

    return annotation;
}