All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graphwalker.java.annotation.AnnotationUtils Maven / Gradle / Ivy

The newest version!
package org.graphwalker.java.annotation;

/*
 * #%L
 * GraphWalker Java
 * %%
 * Copyright (C) 2005 - 2014 GraphWalker
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */

import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;
import io.github.classgraph.ScanResult;
import org.graphwalker.core.machine.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Nils Olsson
 */
public abstract class AnnotationUtils {

  private static final Logger logger = LoggerFactory.getLogger(AnnotationUtils.class);

  public static Set> findTests() {
    return find(Context.class, GraphWalker.class);
  }

  public static Set> find( Class type, Class annotation) {
    Set> classes = new HashSet<>();

    try (ScanResult scanResult = new ClassGraph().enableClassInfo().enableAnnotationInfo().scan()) {
      for (ClassInfo classInfo : scanResult.getClassesWithAnnotation(annotation)) {
        if (type.isAssignableFrom(classInfo.loadClass())) {
          classes.add(classInfo.loadClass());
        }
      }
    }
    return classes;
  }

  private static boolean isAnnotationPresent(Class type, Class annotation) {
    return isAnnotationPresent(type, annotation, Thread.currentThread().getContextClassLoader());
  }

  @SuppressWarnings("unchecked")
  private static boolean isAnnotationPresent(Class type, Class annotation, ClassLoader classLoader) {
    try {
      Class a = (Class) classLoader.loadClass(annotation.getName());
      return type.isAnnotationPresent(a);
    } catch (ClassNotFoundException e) {
      logger.error(e.getMessage());
      throw new RuntimeException();
    }
  }

  public static  Set getAnnotations(final Class clazz, final Class annotation) {
    Set annotations = new HashSet<>();
    Class queryClass = clazz;
    while (null != queryClass) {
      addAnnotation(queryClass, annotations, annotation);
      for (Class interfaceClass : queryClass.getInterfaces()) {
        addAnnotation(interfaceClass, annotations, annotation);
      }
      queryClass = queryClass.getSuperclass();
    }
    return annotations;
  }

  private static  void addAnnotation(final Class clazz, final Set annotations, final Class annotation) {
    if (clazz.isAnnotationPresent(annotation)) {
      annotations.add(clazz.getAnnotation(annotation));
    }
  }

  public static void execute(Class annotation, Object implementation) {
    for (Method method : implementation.getClass().getMethods()) {
      if (method.isAnnotationPresent(annotation)) {
        try {
          method.invoke(implementation);
        } catch (IllegalAccessException | InvocationTargetException e) {
          e.printStackTrace();
        }
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy