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

com.michaelhradek.aurkitu.core.AnnotationParser Maven / Gradle / Ivy

There is a newer version: 0.0.7.6
Show newest version
package com.michaelhradek.aurkitu.core;

import java.lang.annotation.Annotation;
import java.net.MalformedURLException;
import java.util.*;

import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.MojoExecutionException;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.reflections.Reflections;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;

import com.michaelhradek.aurkitu.Application;

/**
 * @author m.hradek
 *
 */
public class AnnotationParser {

    /**
     *
     * @param artifactReference The ArtifactReference
     * @param input A list of Aurkitu annotations.
     * @return A list of classes which are annotated with the above annotations.
     * @throws MojoExecutionException when there is a MalformedURLException in the classpathElements
     */
    public static Set> findAnnotatedClasses(ArtifactReference artifactReference, Class input) throws MojoExecutionException {
        try {
            return findAnnotatedClasses(Utilities.buildReflections(artifactReference), input);
        } catch (DependencyResolutionRequiredException e) {
            throw new MojoExecutionException("Dependency resolution failed", e);
        } catch (ArtifactResolutionException e) {
            throw new MojoExecutionException("Artifact resolution failed", e);
        } catch (MalformedURLException e) {
            throw new MojoExecutionException("Malformed URL", e);
        }
    }

    /**
     * @param input A list of Aurkitu annotations.
     * @return A list of classes which are annotated with the above annotations.
     */
    public static Set> findAnnotatedClasses(Class input) {
        Reflections reflections =
                new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forJavaClassPath()));

        return findAnnotatedClasses(reflections, input);
    }

    /**
     * @param path The path to traverse.
     * @param input A list of Aurkitu annotations.
     * @return A list of classes which are annotated with the above annotations.
     */
    public static Set> findAnnotatedClasses(String path, Class input) {
        return findAnnotatedClasses(new Reflections(path), input);
    }

    /**
     * @param reflections Reflections to traverse.
     * @param input A list of Aurkitu annotations.
     * @return A list of classes which are annotated with the above annotations.
     */
    private static Set> findAnnotatedClasses(Reflections reflections, Class input) {

        Set> classes = reflections.getTypesAnnotatedWith(input);
        for (Class clazz : classes) {
            String prefix = "Find: " + input.getName();
            Application.getLogger().debug(prefix + " -> Found annotated class: " + clazz.getName());
        }

        return classes;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy