Example usage for java.lang.reflect Method isAnnotationPresent

List of usage examples for java.lang.reflect Method isAnnotationPresent

Introduction

In this page you can find the example usage for java.lang.reflect Method isAnnotationPresent.

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:org.jetbrains.webdemo.executors.JunitExecutor.java

public static void main(String[] args) {
    try {/*from  w  w  w .  j a v  a  2 s  . c  om*/
        JUnitCore jUnitCore = new JUnitCore();
        jUnitCore.addListener(new MyRunListener());
        List<Class> classes = getAllClassesFromTheDir(new File(args[0]));
        for (Class cl : classes) {
            boolean hasTestMethods = false;
            for (Method method : cl.getMethods()) {
                if (method.isAnnotationPresent(Test.class)) {
                    hasTestMethods = true;
                    break;
                }
            }
            if (!hasTestMethods)
                continue;

            Request request = Request.aClass(cl);
            jUnitCore.run(request);
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            SimpleModule module = new SimpleModule();
            module.addSerializer(Throwable.class, new ThrowableSerializer());
            module.addSerializer(junit.framework.ComparisonFailure.class,
                    new JunitFrameworkComparisonFailureSerializer());
            module.addSerializer(org.junit.ComparisonFailure.class, new OrgJunitComparisonFailureSerializer());
            objectMapper.registerModule(module);
            System.setOut(standardOutput);

            Map<String, List<TestRunInfo>> groupedTestResults = new HashMap<>();
            for (TestRunInfo testRunInfo : output) {
                if (!groupedTestResults.containsKey(testRunInfo.className)) {
                    groupedTestResults.put(testRunInfo.className, new ArrayList<TestRunInfo>());
                }
                groupedTestResults.get(testRunInfo.className).add(testRunInfo);
            }

            System.out.print(objectMapper.writeValueAsString(groupedTestResults));
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (Throwable e) {
        System.setOut(standardOutput);
        System.out.print("[\"");
        e.printStackTrace();
        System.out.print("\"]");
    }
}

From source file:MyMarker.java

@MyMarker
public static void myMethod() throws Exception {
    Main ob = new Main();
    Method m = ob.getClass().getMethod("myMethod");
    if (m.isAnnotationPresent(MyMarker.class)) {
        System.out.println("MyMarker is present.");
    }/*ww  w . j a  va  2s.c o  m*/
}

From source file:MyMarker.java

@MyMarker
public static void myMeth() {
    Marker ob = new Marker();

    try {// ww  w .  j av  a  2  s. co m
        Method m = ob.getClass().getMethod("myMeth");

        if (m.isAnnotationPresent(MyMarker.class))
            System.out.println("MyMarker is present.");

    } catch (NoSuchMethodException exc) {
        System.out.println("Method Not Found.");
    }
}

From source file:Util.java

public static List<Method> findAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationClass) {
    Method[] methods = clazz.getMethods();
    List<Method> annotatedMethods = new ArrayList<Method>(methods.length);
    for (Method method : methods) {
        if (method.isAnnotationPresent(annotationClass)) {
            annotatedMethods.add(method);
        }/*from w w  w.  j a  va 2  s. c  o  m*/
    }
    return annotatedMethods;
}

From source file:Util.java

public static Method findAnnotatedMethod(Class<?> clazz, Class<? extends Annotation> annotationClass) {
    for (Method method : clazz.getMethods())
        if (method.isAnnotationPresent(annotationClass))
            return (method);
    return (null);
}

From source file:org.seedstack.netflix.hystrix.internal.utils.MethodUtils.java

/**
 * Reads the annotation on the command method to retrieve the fallback method, if it exists.
 * <p>/*from w  ww.j  a  v  a  2 s.  c  o m*/
 * The fallback method must have the same signature as the command method.
 *
 * @param type          the object declaring the method
 * @param commandMethod the method
 * @return the fallback method
 */
public static Method getFallbackMethod(Class<?> type, Method commandMethod) {
    if (commandMethod.isAnnotationPresent(HystrixCommand.class)) {
        HystrixCommand hystrixCommandAnnotation = commandMethod.getAnnotation(HystrixCommand.class);
        if (StringUtils.isNotBlank(hystrixCommandAnnotation.fallbackMethod())) {
            Class<?>[] parameterTypes = commandMethod.getParameterTypes();
            Optional<Method> fallbackMethodOptional = getMethod(type, hystrixCommandAnnotation.fallbackMethod(),
                    parameterTypes);
            if (fallbackMethodOptional.isPresent()) {
                return fallbackMethodOptional.get();
            } else
                throw new RuntimeException(
                        "Fallback method not found: " + hystrixCommandAnnotation.fallbackMethod() + "("
                                + Arrays.toString(parameterTypes) + ")");
        }
    }
    return null;
}

From source file:Main.java

public static boolean hasAnnotation(Class<? extends Annotation> annotation, Object object, String methodName) {
    try {//from   ww w .j a  v  a2 s  .  c om
        Class<? extends Object> c = object.getClass();

        for (Method m : c.getMethods()) {
            if (m.getName().equals(methodName)) {
                if (m.isAnnotationPresent(annotation)) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
    }
    return false;
}

From source file:org.jboss.arquillian.qunit.utils.ReflectionUtilities.java

public static Method findFirstMethodWithAnnotation(Method[] m, Class<? extends Annotation> c) {
    if (!ArrayUtils.isEmpty(m) && c != null) {
        for (Method method : m) {
            if (method != null && method.isAnnotationPresent(c)) {
                return method;
            }// w ww  .ja va2  s .c  o m
        }
    }
    return null;
}

From source file:com.netflix.hystrix.contrib.javanica.cache.CacheInvocationContextFactory.java

/**
 * Create {@link CacheInvocationContext} parametrized with {@link CacheResult} annotation.
 *
 * @param metaHolder the meta holder, see {@link com.netflix.hystrix.contrib.javanica.command.MetaHolder}
 * @return initialized and configured {@link CacheInvocationContext}
 *//*from  www .  j a  v a2 s.co  m*/
public static CacheInvocationContext<CacheResult> createCacheResultInvocationContext(MetaHolder metaHolder) {
    Method method = metaHolder.getMethod();
    if (method.isAnnotationPresent(CacheResult.class)) {
        CacheResult cacheResult = method.getAnnotation(CacheResult.class);
        MethodExecutionAction cacheKeyMethod = createCacheKeyAction(cacheResult.cacheKeyMethod(), metaHolder);
        return new CacheInvocationContext<CacheResult>(cacheResult, cacheKeyMethod, metaHolder.getObj(), method,
                metaHolder.getArgs());
    }
    return null;
}

From source file:com.netflix.hystrix.contrib.javanica.cache.CacheInvocationContextFactory.java

/**
 * Create {@link CacheInvocationContext} parametrized with {@link CacheRemove} annotation.
 *
 * @param metaHolder the meta holder, see {@link com.netflix.hystrix.contrib.javanica.command.MetaHolder}
 * @return initialized and configured {@link CacheInvocationContext}
 *//*from  ww  w .jav  a2  s  . c  o m*/
public static CacheInvocationContext<CacheRemove> createCacheRemoveInvocationContext(MetaHolder metaHolder) {
    Method method = metaHolder.getMethod();
    if (method.isAnnotationPresent(CacheRemove.class)) {
        CacheRemove cacheRemove = method.getAnnotation(CacheRemove.class);
        MethodExecutionAction cacheKeyMethod = createCacheKeyAction(cacheRemove.cacheKeyMethod(), metaHolder);
        return new CacheInvocationContext<CacheRemove>(cacheRemove, cacheKeyMethod, metaHolder.getObj(), method,
                metaHolder.getArgs());
    }
    return null;
}