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));
}
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);
}
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);
}
}
}
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;
}
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());
}
Aggregations