Search in sources :

Example 31 with AnnotatedElement

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

the class ReturnTypeTest method projectionTypeInternal.

/**
 * Get compile-time version of projection. P parameter for {@code Projection<P>}
 */
private static Type projectionTypeInternal(Projection<?> projection) throws ClassNotFoundException, NoSuchFieldException {
    Objects.requireNonNull(projection, "projection");
    final Path path = (Path) Matchers.toExpression(projection);
    AnnotatedElement element = path.element();
    final Class<?> declaringClass;
    if (element instanceof Field) {
        declaringClass = ((Field) element).getDeclaringClass();
    } else if (element instanceof Method) {
        declaringClass = ((Method) element).getDeclaringClass();
    } else {
        throw new IllegalArgumentException(String.format("Expected %s to be either field or method but was %s", path, element.getClass().getName()));
    }
    // manually get criteria class
    Class<?> criteriaClass = Class.forName(declaringClass.getPackage().getName() + "." + declaringClass.getSimpleName() + "Criteria");
    Field field = criteriaClass.getField(path.toStringPath());
    for (Type type : field.getType().getGenericInterfaces()) {
        if (type instanceof ParameterizedType && ((ParameterizedType) type).getRawType() == Projection.class) {
            // resolve Projection<P>
            Type resolved = TypeToken.of(field.getGenericType()).resolveType(type).getType();
            return ((ParameterizedType) resolved).getActualTypeArguments()[0];
        }
    }
    throw new IllegalArgumentException(String.format("Couldn't resolve type variable (%s) for %s %s", Projection.class.getSimpleName(), path, element));
}
Also used : Path(org.immutables.criteria.expression.Path) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method)

Example 32 with AnnotatedElement

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

the class IdAnnotationModuleTest method mappers.

static List<ObjectMapper> mappers() {
    ObjectMapper template = new ObjectMapper().registerModule(new Jdk8Module()).registerModule(new GuavaModule()).registerModule(new JavaTimeModule());
    ObjectMapper mapper1 = template.copy().registerModule(new IdAnnotationModule());
    ObjectMapper mapper2 = template.copy().registerModule(IdAnnotationModule.fromPredicate(m -> ((AnnotatedElement) m).isAnnotationPresent(Criteria.Id.class)));
    ObjectMapper mapper3 = template.copy().registerModule(IdAnnotationModule.fromAnnotation(Criteria.Id.class));
    return Arrays.asList(mapper1, mapper2, mapper3);
}
Also used : Person(org.immutables.criteria.personmodel.Person) Arrays(java.util.Arrays) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) Criteria(org.immutables.criteria.Criteria) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Checkers.check(org.immutables.check.Checkers.check) PersonGenerator(org.immutables.criteria.personmodel.PersonGenerator) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) JavaBean1(org.immutables.criteria.javabean.JavaBean1) ImmutablePerson(org.immutables.criteria.personmodel.ImmutablePerson) MethodSource(org.junit.jupiter.params.provider.MethodSource) AnnotatedElement(java.lang.reflect.AnnotatedElement) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) AnnotatedElement(java.lang.reflect.AnnotatedElement) Criteria(org.immutables.criteria.Criteria) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule)

Example 33 with AnnotatedElement

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

the class WSRefAnnotationProcessor method processRef.

private static void processRef(final DeploymentUnit unit, final String type, final WSRefAnnotationWrapper annotation, final ClassInfo classInfo, final InjectionTarget injectionTarget, final String bindingName) throws DeploymentUnitProcessingException {
    final EEModuleDescription moduleDescription = unit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
    final AnnotatedElement target = createAnnotatedElement(unit, classInfo, injectionTarget);
    final String componentClassName = classInfo.name().toString();
    final Map<String, String> bindingMap = new HashMap<String, String>();
    boolean isEJB = false;
    for (final ComponentDescription componentDescription : moduleDescription.getComponentsByClassName(componentClassName)) {
        if (componentDescription instanceof SessionBeanComponentDescription) {
            isEJB = true;
            bindingMap.put(componentDescription.getComponentName() + "/" + bindingName, bindingName);
        }
    }
    if (!isEJB) {
        bindingMap.put(bindingName, bindingName);
    }
    for (String refKey : bindingMap.keySet()) {
        String refName = bindingMap.get(refKey);
        ManagedReferenceFactory factory = WebServiceReferences.createWebServiceFactory(unit, type, annotation, target, refName, refKey);
        final EEModuleClassDescription classDescription = moduleDescription.addOrGetLocalClassDescription(classInfo.name().toString());
        // Create the binding from whence our injection comes.
        final InjectionSource serviceRefSource = new FixedInjectionSource(factory, factory);
        final BindingConfiguration bindingConfiguration = new BindingConfiguration(refName, serviceRefSource);
        classDescription.getBindingConfigurations().add(bindingConfiguration);
        // our injection comes from the local lookup, no matter what.
        final ResourceInjectionConfiguration injectionConfiguration = injectionTarget != null ? new ResourceInjectionConfiguration(injectionTarget, new LookupInjectionSource(refName)) : null;
        if (injectionConfiguration != null) {
            classDescription.addResourceInjection(injectionConfiguration);
        }
    }
}
Also used : SessionBeanComponentDescription(org.jboss.as.ejb3.component.session.SessionBeanComponentDescription) ComponentDescription(org.jboss.as.ee.component.ComponentDescription) HashMap(java.util.HashMap) AnnotatedElement(java.lang.reflect.AnnotatedElement) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource) InjectionSource(org.jboss.as.ee.component.InjectionSource) FixedInjectionSource(org.jboss.as.ee.component.FixedInjectionSource) FixedInjectionSource(org.jboss.as.ee.component.FixedInjectionSource) EEModuleClassDescription(org.jboss.as.ee.component.EEModuleClassDescription) SessionBeanComponentDescription(org.jboss.as.ejb3.component.session.SessionBeanComponentDescription) BindingConfiguration(org.jboss.as.ee.component.BindingConfiguration) LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource) ResourceInjectionConfiguration(org.jboss.as.ee.component.ResourceInjectionConfiguration)

Example 34 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project hudson-2.x by hudson.

the class TestExtensionLoader method find.

@Override
public <T> Collection<ExtensionComponent<T>> find(Class<T> type, Hudson hudson) {
    TestEnvironment env = TestEnvironment.get();
    List<ExtensionComponent<T>> result = new ArrayList<ExtensionComponent<T>>();
    ClassLoader cl = hudson.getPluginManager().uberClassLoader;
    for (IndexItem<TestExtension, Object> item : Index.load(TestExtension.class, Object.class, cl)) {
        try {
            AnnotatedElement e = item.element();
            Class<?> extType;
            if (e instanceof Class) {
                extType = (Class) e;
                if (!isActive(env, extType))
                    continue;
            } else if (e instanceof Field) {
                Field f = (Field) e;
                if (!f.getDeclaringClass().isInstance(env.testCase))
                    // not executing the enclosing test
                    continue;
                extType = f.getType();
            } else if (e instanceof Method) {
                Method m = (Method) e;
                if (!m.getDeclaringClass().isInstance(env.testCase))
                    // not executing the enclosing test
                    continue;
                extType = m.getReturnType();
            } else
                throw new AssertionError();
            String testName = item.annotation().value();
            if (testName.length() > 0 && !env.testCase.getName().equals(testName))
                // doesn't apply to this test
                continue;
            if (type.isAssignableFrom(extType)) {
                Object instance = item.instance();
                if (instance != null)
                    result.add(new ExtensionComponent<T>(type.cast(instance)));
            }
        } catch (InstantiationException e) {
            LOGGER.log(Level.WARNING, "Failed to load " + item.className(), e);
        }
    }
    return result;
}
Also used : ExtensionComponent(hudson.ExtensionComponent) ArrayList(java.util.ArrayList) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field)

Example 35 with AnnotatedElement

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

the class CSJacksonAnnotationIntrospector method findSerializer.

@Override
public Object findSerializer(Annotated a) {
    AnnotatedElement ae = a.getAnnotated();
    Url an = ae.getAnnotation(Url.class);
    if (an == null) {
        return null;
    }
    if (an.type() == String.class) {
        return new UriSerializer(an);
    } else if (an.type() == List.class) {
        return new UrisSerializer(an);
    }
    throw new UnsupportedOperationException("Unsupported type " + an.type());
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement) List(java.util.List)

Aggregations

AnnotatedElement (java.lang.reflect.AnnotatedElement)106 Method (java.lang.reflect.Method)23 Annotation (java.lang.annotation.Annotation)17 Field (java.lang.reflect.Field)17 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 Constructor (java.lang.reflect.Constructor)6 Type (java.lang.reflect.Type)6 Map (java.util.Map)6 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 Collection (java.util.Collection)3