Last active
May 23, 2017 12:53
-
-
Save vegaasen/c8b61f7b648b520e4aba9078d6339ee3 to your computer and use it in GitHub Desktop.
Simple example-structure in order to illustrate how to locate all classes where a specific annotation are used
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Target({ElementType.TYPE}) | |
@Retention(RetentionPolicy.RUNTIME) | |
@Documented | |
public @interface Job { | |
String value() default ""; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.reflections.Reflections; | |
import org.reflections.scanners.ResourcesScanner; | |
import org.reflections.scanners.SubTypesScanner; | |
import org.reflections.scanners.TypeAnnotationsScanner; | |
import org.reflections.util.ClasspathHelper; | |
import org.reflections.util.ConfigurationBuilder; | |
import org.reflections.util.FilterBuilder; | |
import java.util.stream.Stream; | |
/** | |
* Simple dummy start task related to the startup of this jar-file. Simply prints the various possibilities found within the jar-thing | |
* | |
* @author <a href="mailto:[email protected]">vegard aasen</a> | |
*/ | |
public class Operation { | |
private static final String PACKAGE = "my.package.structure"; | |
public static void main(String... args) { | |
System.out.println("####\nWelcome to the Batch scripts\n#####\n\nThis project does not have any default methods, however you may try any of the following jobs registered in the library:\n"); | |
locateMainMethodClasses(); | |
} | |
private static void locateMainMethodClasses() { | |
new Reflections(new ConfigurationBuilder() | |
.setScanners(new SubTypesScanner(false), new ResourcesScanner()) | |
.addScanners(new TypeAnnotationsScanner()) | |
.setUrls(ClasspathHelper.forClassLoader(new ClassLoader[]{ClasspathHelper.contextClassLoader(), ClasspathHelper.staticClassLoader()})) | |
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(PACKAGE)))) | |
.getTypesAnnotatedWith(Job.class) | |
.stream() | |
.forEach( | |
c -> System.out.println(String.format("[COMMAND] java -cp <this-file>.jar %s --- (%s)", c.getName(), Stream.of(c.getAnnotations()).findFirst().get())) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment