Example usage for java.lang Class getAnnotation

List of usage examples for java.lang Class getAnnotation

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) 

Source Link

Usage

From source file:com.epam.ta.reportportal.database.search.CriteriaMapFactory.java

/**
 * Main criteria for finding classes in package
 * /*  ww  w.  j a  va  2 s .  com*/
 * @param metadataReader
 * @return
 * @throws ClassNotFoundException
 */
private boolean isCandidate(MetadataReader metadataReader) throws ClassNotFoundException {
    try {
        Class<?> c = Class.forName(metadataReader.getClassMetadata().getClassName());
        if (c.getAnnotation(Document.class) != null) {
            return true;
        }
    } catch (Exception e) {
        LOGGER.error("Class with name " + metadataReader.getClassMetadata().getClassName() + " not found", e);
    }
    return false;
}

From source file:org.ops4j.pax.carrot.spring.SpringTestContextFixtureLoader.java

private Object lookupBeanByName(String fixtureName) throws Exception {

    if (context.getConfiguration() instanceof Class<?>) {
        Class<?> configClass = (Class<?>) context.getConfiguration();
        ContextConfiguration cc = configClass.getAnnotation(ContextConfiguration.class);
        if (cc != null) {
            TestContextManager contextManager = new TestContextManager(configClass);
            ApplicationContextHolder applicationContextHolder = new ApplicationContextHolder();
            contextManager.registerTestExecutionListeners(applicationContextHolder);
            contextManager.prepareTestInstance(configClass.newInstance());
            ApplicationContext applicationContext = applicationContextHolder.getApplicationContext();
            if (applicationContext.containsBean(fixtureName)) {
                return applicationContext.getBean(fixtureName);
            }//from w  w  w.  jav a  2s. c om
        }
    }
    return null;
}

From source file:adalid.core.XS1.java

private static void checkAbstractClassReference(Field declaringField, Class<?> type) {
    if (type.isAnnotationPresent(AbstractClass.class)) {
        String message = "Abstract Class Reference: " + declaringField + "; " + type.getSimpleName()
                + " is annotated with AbstractClass";
        if (type.isAnnotationPresent(InheritanceMapping.class)) {
            InheritanceMapping annotation = type.getAnnotation(InheritanceMapping.class);
            message += " and its inheritance mapping strategy is " + annotation.strategy();
            if (InheritanceMappingStrategy.TABLE_PER_CLASS.equals(annotation.strategy())) {
                TLC.getProject().getParser().error(message);
            }//from   w  w  w  . j a  va  2  s .  co m
        }
    }
}

From source file:stormy.pythian.core.description.ComponentDescriptionFactory.java

public ComponentDescription createDescription(Class<? extends Component> componentClass) {
    checkArgument(componentClass != null, "Component class is mandatory");

    Documentation documentation = componentClass.getAnnotation(Documentation.class);
    checkArgument(documentation != null,
            "No documentation found for " + componentClass + " but documentation is mandatory!");

    ComponentDescription description = new ComponentDescription(componentClass, documentation.name(),
            documentation.description(), documentation.type());
    description.addProperties(propertyDescriptionFactory.createPropertyDescriptions(componentClass));
    description.addInputStreams(inputStreamDescriptionFactory.createInputStreamDescriptions(componentClass));
    description.addOutputStreams(outputStreamDescriptionFactory.createOutputStreamDescriptions(componentClass));
    description.addStates(stateDescriptionFactory.createDescriptions(componentClass));

    description.ensureNoDuplicatedInputStreams();
    description.ensureOutputStreamReference();
    description.ensureNoDuplicatedOutputStreams();
    description.ensureNoDuplicatedProperties();
    description.ensureNoDuplicatedStateName();

    return description;
}

From source file:com.fluidops.iwb.api.CommunicationServiceImpl.java

/**
 * translates a class into the ontology using annotations and a default if no annotation is present
 * @param clazz             the class that defines the ontology
 * @return                  the class's type URI
 *///  w ww.  j  a v  a2 s .co m
public static URI getClassURI(Class<?> clazz) {
    OWLClass prop = (OWLClass) clazz.getAnnotation(OWLClass.class);
    if (prop != null)
        return EndpointImpl.api().getNamespaceService().guessURI(prop.className());
    else
        return EndpointImpl.api().getNamespaceService()
                .guessURI(StringUtil.getFieldNameFromBean(clazz.getSimpleName()));
}

From source file:li.klass.fhem.testsuite.category.CategorySuite.java

private Class<?> getIncludedCategory(Class<?> cls) {
    Categories.IncludeCategory annotation = cls.getAnnotation(Categories.IncludeCategory.class);
    return annotation == null ? null : annotation.value();
}

From source file:li.klass.fhem.testsuite.category.CategorySuite.java

private Class<?> getExcludedCategory(Class<?> cls) {
    Categories.ExcludeCategory annotation = cls.getAnnotation(Categories.ExcludeCategory.class);
    return annotation == null ? null : annotation.value();
}

From source file:org.apache.activemq.apollo.dto.ApolloTypeIdResolver.java

public void init(JavaType baseType) {
    this.baseType = baseType;
    ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
    classes.add(baseType.getRawClass());
    classes.addAll(Arrays.asList(DtoModules.extensionClasses()));
    for (Class<?> c : classes) {
        if (baseType.getRawClass().isAssignableFrom(c)) {
            JsonTypeName jsonAnnoation = c.getAnnotation(JsonTypeName.class);
            if (jsonAnnoation != null && jsonAnnoation.value() != null) {
                typeToId.put(c, jsonAnnoation.value());
                idToType.put(jsonAnnoation.value(),
                        TypeFactory.defaultInstance().constructSpecializedType(baseType, c));
                idToType.put(c.getName(), TypeFactory.defaultInstance().constructSpecializedType(baseType, c));
            } else {
                XmlRootElement xmlAnnoation = c.getAnnotation(XmlRootElement.class);
                if (xmlAnnoation != null && xmlAnnoation.name() != null) {
                    typeToId.put(c, xmlAnnoation.name());
                    idToType.put(xmlAnnoation.name(),
                            TypeFactory.defaultInstance().constructSpecializedType(baseType, c));
                    idToType.put(c.getName(),
                            TypeFactory.defaultInstance().constructSpecializedType(baseType, c));
                }/*from   w  ww.  j  ava2 s .co m*/
            }
        }
    }
}

From source file:org.springmodules.validation.bean.conf.loader.annotation.handler.hibernate.HibernatePropertyValidationAnnotationHandler.java

/**
 * Checks whether the annnotation is one of the annotations defined by hibernate-annotations lib.
 *
 * @see PropertyValidationAnnotationHandler#supports(java.lang.annotation.Annotation, Class, java.beans.PropertyDescriptor)
 *//*from   w  w  w  . ja v a 2  s  . co m*/
public boolean supports(Annotation annotation, Class clazz, PropertyDescriptor descriptor) {
    Class annotationClass = annotation.annotationType();
    ValidatorClass validatorClassAnnotation = (ValidatorClass) annotationClass
            .getAnnotation(ValidatorClass.class);
    if (validatorClassAnnotation != null) {
        return true;
    }

    if (annotation instanceof Valid) {
        return true;
    }

    return false;
}

From source file:edu.usu.sdl.openstorefront.doc.JaxrsProcessor.java

private static void mapComplexTypes(List<APITypeModel> typeModels, Field fields[], boolean onlyConsumeField) {
    //Should strip duplicate types
    Set<String> typesInList = new HashSet<>();
    typeModels.forEach(type -> {/*from   w  ww. ja v a  2s  .c o  m*/
        typesInList.add(type.getName());
    });

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    for (Field field : fields) {
        boolean capture = true;
        if (onlyConsumeField) {
            ConsumeField consumeField = (ConsumeField) field.getAnnotation(ConsumeField.class);
            if (consumeField == null) {
                capture = false;
            }
        }

        if (capture) {

            Class fieldClass = field.getType();
            DataType dataType = (DataType) field.getAnnotation(DataType.class);
            if (dataType != null) {
                fieldClass = dataType.value();
            }

            if (ReflectionUtil.isComplexClass(fieldClass)) {

                APITypeModel typeModel = new APITypeModel();
                typeModel.setName(fieldClass.getSimpleName());

                APIDescription aPIDescription = (APIDescription) fieldClass.getAnnotation(APIDescription.class);
                if (aPIDescription != null) {
                    typeModel.setDescription(aPIDescription.value());
                }

                Set<String> fieldList = mapValueField(typeModel.getFields(), fieldClass.getDeclaredFields(),
                        onlyConsumeField);
                if (fieldClass.isEnum()) {
                    typeModel.setObject(Arrays.toString(fieldClass.getEnumConstants()));
                } else {
                    if (fieldClass.isInterface() == false) {
                        try {
                            typeModel.setObject(objectMapper.writeValueAsString(fieldClass.newInstance()));

                            String cleanUpJson = StringProcessor.stripeFieldJSON(typeModel.getObject(),
                                    fieldList);
                            typeModel.setObject(cleanUpJson);

                        } catch (InstantiationException | IllegalAccessException | JsonProcessingException ex) {
                            log.log(Level.WARNING,
                                    "Unable to process/map complex field: " + fieldClass.getSimpleName(), ex);
                            typeModel.setObject("{ Unable to view }");
                        }
                        mapComplexTypes(typeModels, fieldClass.getDeclaredFields(), onlyConsumeField);
                    }
                }
                typeModels.add(typeModel);
                typesInList.add(typeModel.getName());
            }

        }
    }
}