use of java.lang.reflect.AnnotatedElement in project querydsl by querydsl.
the class HibernateDomainExporter method handleProperty.
private void handleProperty(EntityType entityType, Class<?> cl, org.hibernate.mapping.Property p) throws NoSuchMethodException, ClassNotFoundException {
if (p.isBackRef()) {
return;
}
Class<?> clazz = Object.class;
try {
clazz = p.getType().getReturnedClass();
} catch (MappingException e) {
// ignore
}
Type propertyType = getType(cl, clazz, p.getName());
try {
propertyType = getPropertyType(p, propertyType);
} catch (MappingException e) {
// ignore
}
AnnotatedElement annotated = getAnnotatedElement(cl, p.getName());
propertyType = getTypeOverride(propertyType, annotated);
if (propertyType == null) {
return;
}
if (p.isComposite()) {
EntityType embeddedType = createEmbeddableType(propertyType);
Iterator<?> properties = ((Component) p.getValue()).getPropertyIterator();
while (properties.hasNext()) {
handleProperty(embeddedType, embeddedType.getJavaClass(), (org.hibernate.mapping.Property) properties.next());
}
propertyType = embeddedType;
} else if (propertyType.getCategory() == TypeCategory.ENTITY || p.getValue() instanceof ManyToOne) {
propertyType = createEntityType(propertyType);
} else if (propertyType.getCategory() == TypeCategory.CUSTOM) {
propertyType = createEmbeddableType(propertyType);
} else if (p.getValue() instanceof org.hibernate.mapping.Collection) {
org.hibernate.mapping.Collection collection = (org.hibernate.mapping.Collection) p.getValue();
if (collection.getElement() instanceof OneToMany) {
String entityName = ((OneToMany) collection.getElement()).getReferencedEntityName();
if (entityName != null) {
if (collection.isMap()) {
Type keyType = typeFactory.get(Class.forName(propertyType.getParameters().get(0).getFullName()));
Type valueType = typeFactory.get(Class.forName(entityName));
propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), keyType), normalize(propertyType.getParameters().get(1), valueType));
} else {
Type componentType = typeFactory.get(Class.forName(entityName));
propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), componentType));
}
}
} else if (collection.getElement() instanceof Component) {
Component component = (Component) collection.getElement();
Class<?> embeddedClass = Class.forName(component.getComponentClassName());
EntityType embeddedType = createEmbeddableType(embeddedClass);
Iterator<?> properties = component.getPropertyIterator();
while (properties.hasNext()) {
handleProperty(embeddedType, embeddedClass, (org.hibernate.mapping.Property) properties.next());
}
}
}
Property property = createProperty(entityType, p.getName(), propertyType, annotated);
entityType.addProperty(property);
}
use of java.lang.reflect.AnnotatedElement in project querydsl by querydsl.
the class PathTest method getAnnotatedElement.
@Test
public void getAnnotatedElement() {
Entity entity = Alias.alias(Entity.class);
AnnotatedElement element = $(entity).getAnnotatedElement();
// type
assertEquals(Entity.class, element);
}
use of java.lang.reflect.AnnotatedElement in project randomizedtesting by randomizedtesting.
the class ThreadLeakControl method forTest.
/**
* A {@link Statement} for wrapping test-level execution.
*/
Statement forTest(final Statement s, final TestCandidate c) {
final int timeout = determineTimeout(c);
return new Statement() {
@Override
public void evaluate() throws Throwable {
checkZombies();
final StatementRunner sr = new StatementRunner(s);
final List<Throwable> errors = new ArrayList<Throwable>();
final Set<Thread> beforeTestState = getThreads(suiteFilters);
final boolean timedOut = forkTimeoutingTask(sr, timeout, errors);
if (suiteTimedOut.get()) {
return;
}
if (timedOut) {
LOGGER.warning("Test execution timed out: " + c.description + formatThreadStacksFull());
}
if (timedOut) {
errors.add(RandomizedRunner.augmentStackTrace(emptyStack(new Exception("Test timeout exceeded (>= " + timeout + " msec)."))));
}
final AnnotatedElement[] chain = { c.method, c.getTestClass(), DefaultAnnotationValues.class };
List<Throwable> threadLeakErrors = timedOut ? new ArrayList<Throwable>() : errors;
checkThreadLeaks(beforeTestState, threadLeakErrors, LifecycleScope.TEST, c.description, chain);
processUncaught(errors, runner.handler.getUncaughtAndClear());
MultipleFailureException.assertEmpty(errors);
}
};
}
use of java.lang.reflect.AnnotatedElement in project randomizedtesting by randomizedtesting.
the class GroupEvaluator method getIgnoreReason.
/**
* @return Returns a non-null string with the reason why the annotated element (class, test or test-class pair)
* should be ignored in the execution. This is an expert-level method, typically tests
* shouldn't be concerned with this.
*/
public String getIgnoreReason(AnnotatedElement... elements) {
final Map<String, Annotation> annotations = new HashMap<String, Annotation>();
for (AnnotatedElement element : elements) {
for (Annotation ann : element.getAnnotations()) {
Class<? extends Annotation> annType = ann.annotationType();
if (annType.isAnnotationPresent(TestGroup.class)) {
if (!testGroups.containsKey(annType)) {
testGroups.put(annType, new TestGroupInfo(annType));
}
annotations.put(testGroups.get(annType).name, ann);
}
}
}
String defaultState = null;
for (Annotation ann : annotations.values()) {
TestGroupInfo g = testGroups.get(ann.annotationType());
if (!g.enabled) {
defaultState = "'" + g.name + "' test group is disabled (" + toString(ann) + ")";
break;
}
}
if (hasFilteringExpression()) {
final String defaultStateCopy = defaultState;
boolean enabled = filter.evaluate(new IContext() {
@Override
public boolean defaultValue() {
return defaultStateCopy == null;
}
@Override
public boolean hasGroup(String value) {
if (value.startsWith("@"))
value = value.substring(1);
for (Annotation ann : annotations.values()) {
if (value.equalsIgnoreCase(testGroups.get(ann.annotationType()).name)) {
return true;
}
}
return false;
}
});
return enabled ? null : "Test filter condition: " + filterExpression;
} else {
return defaultState;
}
}
use of java.lang.reflect.AnnotatedElement in project spring-framework by spring-projects.
the class AnnotationUtils method findAnnotation.
/**
* Find a single {@link Annotation} of {@code annotationType} on the supplied
* {@link Method}, traversing its super methods (i.e., from superclasses and
* interfaces) if the annotation is not <em>directly present</em> on the given
* method itself.
* <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
* <p>Meta-annotations will be searched if the annotation is not
* <em>directly present</em> on the method.
* <p>Annotations on methods are not inherited by default, so we need to handle
* this explicitly.
* @param method the method to look for annotations on
* @param annotationType the annotation type to look for
* @return the first matching annotation, or {@code null} if not found
* @see #getAnnotation(Method, Class)
*/
@SuppressWarnings("unchecked")
public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) {
Assert.notNull(method, "Method must not be null");
if (annotationType == null) {
return null;
}
AnnotationCacheKey cacheKey = new AnnotationCacheKey(method, annotationType);
A result = (A) findAnnotationCache.get(cacheKey);
if (result == null) {
Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
result = findAnnotation((AnnotatedElement) resolvedMethod, annotationType);
if (result == null) {
result = searchOnInterfaces(method, annotationType, method.getDeclaringClass().getInterfaces());
}
Class<?> clazz = method.getDeclaringClass();
while (result == null) {
clazz = clazz.getSuperclass();
if (clazz == null || Object.class == clazz) {
break;
}
try {
Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
Method resolvedEquivalentMethod = BridgeMethodResolver.findBridgedMethod(equivalentMethod);
result = findAnnotation((AnnotatedElement) resolvedEquivalentMethod, annotationType);
} catch (NoSuchMethodException ex) {
// No equivalent method found
}
if (result == null) {
result = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
}
}
if (result != null) {
result = synthesizeAnnotation(result, method);
findAnnotationCache.put(cacheKey, result);
}
}
return result;
}
Aggregations