Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - loading annotated classes

Tags:

I know there are incredible set of tools for loading plugin classes in java, but today an idea came to my mind.

What if I have a bunch of annotated and un-annotated classes in package "org.home.junk" (annotated with annotation "@AnnotatedClass") and those classes have annotated methods with say annotation "@AnnotatedMethod".

First question: can I at run-time get an array/collection of all the classes in that specific package, so that I could check which one's are annotated and create an instance of them. (I am aware however how to check if Some.class has annotations courtesy of this guide: http://isagoksu.com/2009/development/java/creating-custom-annotations-and-making-use-of-them/)

Second question: - If I can do what I'd like in first question - what would be the most political way to do this?

I believe it is possible, as I understand JUnit loads test-case classes in some similar manner.

Also it would be cool if this could be done with minimal third party libraries and such, again - if it's possible :)

like image 343
Xeperis Avatar asked Aug 18 '12 19:08

Xeperis


People also ask

Can classes be annotated in Java?

Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements. When used on a declaration, each annotation often appears, by convention, on its own line. As of the Java SE 8 release, annotations can also be applied to the use of types.

Can classes be annotated?

Yes you have to annotate both classes.

How does spring find annotated classes?

Solution: If you use Spring annotations like @Component, @Repository or @Service, then Spring will find such classes, but will make them Spring beans. Good news is that Spring classpath scanning mechanism is configurable and available in any Spring application.

How are annotations executed in Java?

Annotations don't execute; they're notes or markers that are read by various tools. Some are read by your compiler, like @Override ; others are embedded in the class files and read by tools like Hibernate at runtime.

What are the annotations applied to Java code?

Some annotations are applied to Java code and some to other annotations. Let's understand the built-in annotations first. @Override annotation assures that the subclass method is overriding the parent class method. If it is not so, compile time error occurs. Sometimes, we does the silly mistake such as spelling mistakes etc.

How to know if a class is annotated or not?

To know if a class is annotated or not is a reflection issue. Getting the annotations used in a class is done this way: Be sure to define your custom annotation with a retention policy type visible at run time.

What is the difficulty level of annotation in Java?

Annotations in Java. Difficulty Level : Hard. Last Updated : 08 Apr, 2020. Annotations are used to provide supplement information about a program. Annotations start with ‘ @ ’. Annotations do not change action of a compiled program.

How to create an index file of all annotated classes in Java?

This approach is implemented by libraries like Scannotationand Reflections. Another approach is to use Java Pluggable Annotation Processing APIto write annotation processor which will collect all annotated classes at compile time and build the index file for runtime use. The above mechanism is implemented in ClassIndexlibrary.


1 Answers

First answer: Take a look at this project.

Reflections reflections = new Reflections("org.home.junk"); Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(javax.persistence.Entity.class); 

It returns all the classes from org.home.junk annotated with javax.persistence.Entity annotation.

Second Answer: To create new instance of above classes you can do this

for (Class<?> clazz : annotated) {     final Object newInstance = clazz.newInstance(); } 

Hope this answers everything.

like image 92
Kowser Avatar answered Oct 28 '22 23:10

Kowser