Search in sources :

Example 96 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project antlr4 by tunnelvisionlabs.

the class RuleDependencyChecker method getDependencies.

public static List<Tuple2<RuleDependency, AnnotatedElement>> getDependencies(Class<?> clazz) {
    List<Tuple2<RuleDependency, AnnotatedElement>> result = new ArrayList<Tuple2<RuleDependency, AnnotatedElement>>();
    List<ElementType> supportedTarget = Arrays.asList(RuleDependency.class.getAnnotation(Target.class).value());
    for (ElementType target : supportedTarget) {
        switch(target) {
            case TYPE:
                if (!clazz.isAnnotation()) {
                    getElementDependencies(clazz, result);
                }
                break;
            case ANNOTATION_TYPE:
                if (!clazz.isAnnotation()) {
                    getElementDependencies(clazz, result);
                }
                break;
            case CONSTRUCTOR:
                for (Constructor<?> ctor : clazz.getDeclaredConstructors()) {
                    getElementDependencies(ctor, result);
                }
                break;
            case FIELD:
                for (Field field : clazz.getDeclaredFields()) {
                    getElementDependencies(field, result);
                }
                break;
            case LOCAL_VARIABLE:
                System.err.println("Runtime rule dependency checking is not supported for local variables.");
                break;
            case METHOD:
                for (Method method : clazz.getDeclaredMethods()) {
                    getElementDependencies(method, result);
                }
                break;
            case PACKAGE:
                // package is not a subset of class, so nothing to do here
                break;
            case PARAMETER:
                System.err.println("Runtime rule dependency checking is not supported for parameters.");
                break;
        }
    }
    return result;
}
Also used : Field(java.lang.reflect.Field) ElementType(java.lang.annotation.ElementType) ArrayList(java.util.ArrayList) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method) RuleDependency(org.antlr.v4.runtime.RuleDependency)

Example 97 with AnnotatedElement

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);
}
Also used : QueryEntity(com.querydsl.core.annotations.QueryEntity) AnnotatedElement(java.lang.reflect.AnnotatedElement) Test(org.junit.Test)

Example 98 with AnnotatedElement

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);
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList) AnnotatedElement(java.lang.reflect.AnnotatedElement) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) MultipleFailureException(org.junit.runners.model.MultipleFailureException) AssumptionViolatedException(org.junit.AssumptionViolatedException) UncaughtException(com.carrotsearch.randomizedtesting.RandomizedRunner.UncaughtException)

Example 99 with AnnotatedElement

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;
    }
}
Also used : IContext(com.carrotsearch.randomizedtesting.FilterExpressionParser.IContext) HashMap(java.util.HashMap) AnnotatedElement(java.lang.reflect.AnnotatedElement) Annotation(java.lang.annotation.Annotation)

Example 100 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project spring-framework by spring-projects.

the class ParameterResolutionDelegate method isAutowirable.

/**
 * Determine if the supplied {@link Parameter} can <em>potentially</em> be
 * autowired from an {@link AutowireCapableBeanFactory}.
 * <p>Returns {@code true} if the supplied parameter is annotated or
 * meta-annotated with {@link Autowired @Autowired},
 * {@link Qualifier @Qualifier}, or {@link Value @Value}.
 * <p>Note that {@link #resolveDependency} may still be able to resolve the
 * dependency for the supplied parameter even if this method returns {@code false}.
 * @param parameter the parameter whose dependency should be autowired
 * (must not be {@code null})
 * @param parameterIndex the index of the parameter in the constructor or method
 * that declares the parameter
 * @see #resolveDependency
 */
public static boolean isAutowirable(Parameter parameter, int parameterIndex) {
    Assert.notNull(parameter, "Parameter must not be null");
    AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
    return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) || AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) || AnnotatedElementUtils.hasAnnotation(annotatedParameter, Value.class));
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement)

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