Example usage for java.lang Class getAnnotationsByType

List of usage examples for java.lang Class getAnnotationsByType

Introduction

In this page you can find the example usage for java.lang Class getAnnotationsByType.

Prototype

@Override
public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) 

Source Link

Usage

From source file:com.github.yongchristophertang.config.PropertyHandler.java

private static <A extends Annotation> A[] getAnnotations(A[] annos, Class<?> testClass, Class<A> annoClass) {
    return testClass == Object.class ? annos
            : getAnnotations(ArrayUtils.addAll(annos, testClass.getAnnotationsByType(annoClass)),
                    testClass.getSuperclass(), annoClass);
}

From source file:com.sonicle.webtop.core.app.util.ClassHelper.java

public static Set<Annotation> getClassAnnotations(Class clazz, Class annotationClass) {
    if (clazz == null)
        return null;
    Annotation[] annotations = clazz.getAnnotationsByType(annotationClass);
    return Arrays.stream(annotations).unordered().collect(Collectors.toSet());
}

From source file:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Collect class annotations./*from   w w w . ja  v  a 2s . c o m*/
 *
 * @param <T> the generic type
 * @param clazz the clazz
 * @param annotClass the annot class
 * @param annots the annots
 * @param visitedClasses the visited classes
 */
public static <T extends Annotation> void collectClassAnnotations(Class<?> clazz, Class<T> annotClass,
        List<T> annots, Set<Class<?>> visitedClasses) {
    if (visitedClasses.contains(clazz) == true) {
        return;
    }
    visitedClasses.add(clazz);
    T[] ana = clazz.getAnnotationsByType(annotClass);
    if (ana != null) {
        for (T an : ana) {
            annots.add(an);
        }
    }
    Class<?> superclz = clazz.getSuperclass();
    if (superclz == null || superclz == Object.class) {
        return;
    }
    collectClassAnnotations(superclz, annotClass, annots, visitedClasses);
    Class<?>[] ifaces = clazz.getInterfaces();
    if (ifaces == null || ifaces.length == 0) {
        return;
    }
    for (Class<?> iclazz : ifaces) {
        collectClassAnnotations(iclazz, annotClass, annots, visitedClasses);
    }
}

From source file:nu.famroos.spring.hateoas.polymorphism.JsonRootRelProvider.java

private <T extends Annotation> T getAnnotationByType(Class<?> type, Class<T> annotationType) {
    T[] annotations = type.getAnnotationsByType(annotationType);
    return (annotations.length == 0) ? null : annotations[0];
}

From source file:com.github.yongchristophertang.database.guice.DBAnnotationModuleBuilder.java

/**
 * Return all marked {@link java.lang.annotation.Annotation}s from the {@code Class\<\?\> testClass} and its
 * superclasses.//  w w w . ja va 2 s  .  c  o  m
 */
protected <A extends Annotation> A[] getAnnotations(A[] annos, Class<?> testClass, Class<A> annoClass) {
    return testClass == Object.class ? annos
            : getAnnotations(ArrayUtils.addAll(annos, testClass.getAnnotationsByType(annoClass)),
                    testClass.getSuperclass(), annoClass);
}

From source file:io.seventyone.mongoutils.MongoServiceImplementation.java

private void internalSetupIndexes(Class<?> entityClass, MongoCollection<Document> collection) {

    if (entityClass == null || collection == null) {
        return;// www .j av a 2  s .  com
    }

    MongoIndex[] indexes = entityClass.getAnnotationsByType(MongoIndex.class);
    if (indexes != null && indexes.length > 0) {
        for (MongoIndex index : indexes) {
            Document indexDocument = new Document();
            indexDocument.put(index.key(), index.direction());

            IndexOptions options = new IndexOptions();
            options.unique(index.unique());
            options.background(index.background());
            collection.createIndex(indexDocument, options);
        }
    }
}

From source file:de.codesourcery.spring.contextrewrite.AnnotationParser.java

/**
 * Parse rewrite annotations.// ww w . j  a v a 2  s . c  om
 * 
 * @param clazz class whose annotations should be parsed. This method will also parse parent classes and merge any rewriting annotations it encounters 
 *             according to the rules described in {@link RewriteConfig#getRules()}.
 *               
 * @return rewriting configuration
 * @throws NoSuchElementException if neither the input class nor any of its parents had a {@link ContextConfiguration} annotation.
 */
public RewriteConfig parse(Class<?> clazz) throws NoSuchElementException {
    Validate.notNull(clazz, "clazz must not be NULL");

    RewriteConfig first = null;
    RewriteConfig previous = null;
    Class<?> currentClass = clazz;
    while (currentClass != Object.class) {
        final Optional<ContextConfiguration> ctxConfiguration = Optional
                .ofNullable(currentClass.getAnnotation(ContextConfiguration.class));

        final RewriteConfig config = new RewriteConfig(currentClass);

        if (ctxConfiguration.isPresent()) {
            config.setContextPath(ctxConfiguration.get().value());
            config.setDebug(ctxConfiguration.get().debug());
            config.setDumpXML(ctxConfiguration.get().dumpRewrittenXML());
        }

        config.addRules(XMLRewrite.wrap(currentClass.getAnnotationsByType(ReplaceRule.class)));
        config.addRules(XMLRewrite.wrap(currentClass.getAnnotationsByType(RemoveRule.class)));
        config.addRules(XMLRewrite.wrap(currentClass.getAnnotationsByType(InsertElementRule.class)));
        config.addRules(XMLRewrite.wrap(currentClass.getAnnotationsByType(InsertAttributeRule.class)));

        if (ctxConfiguration.isPresent() || config.hasRules()) {
            if (previous != null) {
                previous.setParent(config);
            }
            previous = config;

            if (first == null) {
                first = config;
            }
        }
        currentClass = currentClass.getSuperclass();
    }
    if (first == null) {
        throw new NoSuchElementException("Found no @" + ContextConfiguration.class.getName() + " annotation on "
                + clazz.getName() + " or any of its super classes");
    }
    return first;
}

From source file:net.udidb.engine.ops.impls.help.HelpMessageProvider.java

@Inject
public HelpMessageProvider(@Named("OP_PACKAGES") String[] opPackages) {

    Set<URL> packages = new HashSet<>();
    for (String opPackage : opPackages) {
        packages.addAll(ClasspathHelper.forPackage(opPackage));
    }/*www  .  java2s  .c  o m*/
    Reflections reflections = new Reflections(packages, new SubTypesScanner());
    for (Class<? extends Operation> opClass : reflections.getSubTypesOf(Operation.class)) {
        if (Modifier.isAbstract(opClass.getModifiers()))
            continue;

        HelpMessage[] helpMessages = opClass.getAnnotationsByType(HelpMessage.class);
        DisplayName displayName = opClass.getAnnotation(DisplayName.class);

        if (helpMessages.length == 0 || displayName == null) {
            throw new RuntimeException(opClass.getSimpleName() + " is an invalid Operation");
        }

        String name = displayName.value();

        HelpMessageDescriptor descriptor = new HelpMessageDescriptor();

        descriptor.global = opClass.isAnnotationPresent(GlobalOperation.class);
        descriptor.shortMessage = selectMessage(helpMessages);
        descriptor.longMessage = createLongMessage(name, descriptor.shortMessage, opClass);

        helpMessageDescriptors.put(name, descriptor);
    }
}

From source file:net.udidb.engine.ops.impls.help.HelpMessageProvider.java

private String getOperandDescription(Field field) {
    HelpMessage[] helpMessages = field.getAnnotationsByType(HelpMessage.class);
    String message;/*www .  ja  va  2  s .  co m*/
    if (helpMessages.length != 0) {
        message = selectMessage(helpMessages);
    } else {
        message = null;
    }

    if (message == null) {
        Class<?> mixInClass = operandTypeHelpMixins.get(field.getType().getCanonicalName());
        if (mixInClass != null) {
            HelpMessage[] mixInMessages = mixInClass.getAnnotationsByType(HelpMessage.class);
            message = selectMessage(mixInMessages);
        }
    }

    return message;
}

From source file:org.apache.tinkerpop.gremlin.AbstractGremlinSuite.java

private void validateOptInToSuite(final Class<? extends Graph> klass) throws InitializationError {
    final Graph.OptIn[] optIns = klass.getAnnotationsByType(Graph.OptIn.class);
    if (!gremlinFlavorSuite && !Arrays.stream(optIns)
            .anyMatch(optIn -> optIn.value().equals(this.getClass().getCanonicalName())))
        throw new InitializationError(
                "The suite will not run for this Graph until it is publicly acknowledged with the @OptIn annotation on the Graph instance itself");
}