Search in sources :

Example 1 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project jersey by jersey.

the class AutowiredInjectResolver method resolve.

@Override
public Object resolve(Injectee injectee) {
    AnnotatedElement parent = injectee.getParent();
    String beanName = null;
    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);
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement) Qualifier(org.springframework.beans.factory.annotation.Qualifier)

Example 2 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project jersey by jersey.

the class ParamInjectionResolver method resolve.

@Override
@SuppressWarnings("unchecked")
public Object resolve(Injectee injectee) {
    AnnotatedElement annotated = injectee.getParent();
    Annotation[] annotations;
    if (annotated.getClass().equals(Constructor.class)) {
        annotations = ((Constructor) annotated).getParameterAnnotations()[injectee.getPosition()];
    } else {
        annotations = annotated.getDeclaredAnnotations();
    }
    Class componentClass = injectee.getInjecteeClass();
    Type genericType = injectee.getRequiredType();
    final Type targetGenericType;
    if (injectee.isFactory()) {
        targetGenericType = ReflectionHelper.getTypeArgument(genericType, 0);
    } else {
        targetGenericType = genericType;
    }
    final Class<?> targetType = ReflectionHelper.erasure(targetGenericType);
    final Parameter parameter = Parameter.create(componentClass, componentClass, hasEncodedAnnotation(injectee), targetType, targetGenericType, annotations);
    final Supplier<?> paramValueSupplier = valueSupplierProvider.getValueSupplier(parameter);
    if (paramValueSupplier != null) {
        if (injectee.isFactory()) {
            return paramValueSupplier;
        } else {
            return paramValueSupplier.get();
        }
    }
    return null;
}
Also used : Type(java.lang.reflect.Type) Constructor(java.lang.reflect.Constructor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Parameter(org.glassfish.jersey.server.model.Parameter) Annotation(java.lang.annotation.Annotation)

Example 3 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project camel by apache.

the class FallbackTypeConverter method createContext.

protected synchronized <T> JAXBContext createContext(Class<T> type) throws JAXBException {
    AnnotatedElement ae = hasXmlRootElement(type) ? type : type.getPackage();
    JAXBContext context = contexts.get(ae);
    if (context == null) {
        if (hasXmlRootElement(type)) {
            context = JAXBContext.newInstance(type);
            contexts.put(type, context);
        } else {
            context = JAXBContext.newInstance(type.getPackage().getName());
            contexts.put(type.getPackage(), context);
        }
    }
    return context;
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement) JAXBContext(javax.xml.bind.JAXBContext)

Example 4 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project querydsl by querydsl.

the class HibernateDomainExporter method collectTypes.

@Override
protected void collectTypes() throws IOException, XMLStreamException, ClassNotFoundException, NoSuchMethodException {
    // super classes
    Iterator<?> superClassMappings = configuration.getMappedSuperclassMappings();
    while (superClassMappings.hasNext()) {
        MappedSuperclass msc = (MappedSuperclass) superClassMappings.next();
        EntityType entityType = createSuperType(msc.getMappedClass());
        if (msc.getDeclaredIdentifierProperty() != null) {
            handleProperty(entityType, msc.getMappedClass(), msc.getDeclaredIdentifierProperty());
        }
        Iterator<?> properties = msc.getDeclaredPropertyIterator();
        while (properties.hasNext()) {
            handleProperty(entityType, msc.getMappedClass(), (org.hibernate.mapping.Property) properties.next());
        }
    }
    // entity classes
    Iterator<?> classMappings = configuration.getClassMappings();
    while (classMappings.hasNext()) {
        PersistentClass pc = (PersistentClass) classMappings.next();
        EntityType entityType = createEntityType(pc.getMappedClass());
        if (pc.getDeclaredIdentifierProperty() != null) {
            handleProperty(entityType, pc.getMappedClass(), pc.getDeclaredIdentifierProperty());
        } else if (!pc.isInherited() && pc.hasIdentifierProperty()) {
            logger.info(entityType.toString() + pc.getIdentifierProperty());
            handleProperty(entityType, pc.getMappedClass(), pc.getIdentifierProperty());
        } else if (pc.getIdentifier() != null) {
            KeyValue identifier = pc.getIdentifier();
            if (identifier instanceof Component) {
                Component component = (Component) identifier;
                Iterator<?> properties = component.getPropertyIterator();
                if (component.isEmbedded()) {
                    while (properties.hasNext()) {
                        handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next());
                    }
                } else {
                    String name = component.getNodeName();
                    Class<?> clazz = component.getType().getReturnedClass();
                    Type propertyType = getType(pc.getMappedClass(), clazz, name);
                    AnnotatedElement annotated = getAnnotatedElement(pc.getMappedClass(), name);
                    Property property = createProperty(entityType, name, propertyType, annotated);
                    entityType.addProperty(property);
                    // handle component properties
                    EntityType embeddedType = createEmbeddableType(propertyType);
                    while (properties.hasNext()) {
                        handleProperty(embeddedType, clazz, (org.hibernate.mapping.Property) properties.next());
                    }
                }
            }
        // TODO handle other KeyValue subclasses such as Any, DependentValue and ToOne
        }
        Iterator<?> properties = pc.getDeclaredPropertyIterator();
        while (properties.hasNext()) {
            handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next());
        }
    }
}
Also used : org.hibernate.mapping(org.hibernate.mapping) AnnotatedElement(java.lang.reflect.AnnotatedElement) EntityType(com.querydsl.codegen.EntityType) Type(com.mysema.codegen.model.Type) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.mysema.codegen.model.SimpleType) Property(com.querydsl.codegen.Property)

Example 5 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project acceptance-test-harness by jenkinsci.

the class TSRJenkinsAcceptanceTestRule method apply.

@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
    final Description description = Description.createTestDescription(method.getMethod().getDeclaringClass(), method.getName(), method.getAnnotations());
    return new Statement() {

        @Inject
        JenkinsController controller;

        @Inject
        Injector injector;

        @Override
        public void evaluate() throws Throwable {
            World world = World.get();
            Injector injector = world.getInjector();
            world.startTestScope(description.getDisplayName());
            injector.injectMembers(target);
            injector.injectMembers(this);
            System.out.println("=== Starting " + description.getDisplayName());
            try {
                decorateWithRules(base).evaluate();
            } catch (AssumptionViolatedException e) {
                throw e;
            } catch (Exception | AssertionError e) {
                // Errors and failures
                controller.diagnose(e);
                throw e;
            } finally {
                world.endTestScope();
            }
        }

        /**
         * Look for annotations on a test and honor {@link RuleAnnotation}s in them.
         */
        private Statement decorateWithRules(Statement body) {
            Set<Class<? extends Annotation>> annotations = new HashSet<>();
            collectAnnotationTypes(method.getMethod(), annotations);
            collectAnnotationTypes(target.getClass(), annotations);
            Description testDescription = Description.createTestDescription(target.getClass(), method.getName(), method.getAnnotations());
            for (Class<? extends Annotation> a : annotations) {
                RuleAnnotation r = a.getAnnotation(RuleAnnotation.class);
                if (r != null) {
                    TestRule tr = injector.getInstance(r.value());
                    body = tr.apply(body, testDescription);
                }
            }
            return body;
        }

        private void collectAnnotationTypes(AnnotatedElement e, Collection<Class<? extends Annotation>> types) {
            for (Annotation a : e.getAnnotations()) {
                types.add(a.annotationType());
            }
        }
    };
}
Also used : Description(org.junit.runner.Description) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) AnnotatedElement(java.lang.reflect.AnnotatedElement) World(org.jenkinsci.test.acceptance.guice.World) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Annotation(java.lang.annotation.Annotation) TestRule(org.junit.rules.TestRule) JenkinsController(org.jenkinsci.test.acceptance.controller.JenkinsController) Injector(com.google.inject.Injector) Collection(java.util.Collection) HashSet(java.util.HashSet)

Aggregations

AnnotatedElement (java.lang.reflect.AnnotatedElement)104 Method (java.lang.reflect.Method)22 Annotation (java.lang.annotation.Annotation)17 Field (java.lang.reflect.Field)16 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)11 HashMap (java.util.HashMap)9 Test (org.junit.jupiter.api.Test)8 Member (java.lang.reflect.Member)7 LinkedHashSet (java.util.LinkedHashSet)7 List (java.util.List)7 Type (java.lang.reflect.Type)6 Map (java.util.Map)6 Constructor (java.lang.reflect.Constructor)5 HashSet (java.util.HashSet)5 By (org.openqa.selenium.By)5 Statement (org.junit.runners.model.Statement)4 FindBy (org.openqa.selenium.support.FindBy)4 EntityType (com.querydsl.codegen.EntityType)3 Property (com.querydsl.codegen.Property)3