Iterate on all class having a specific annotation [duplicate] - java

This question already has answers here:
Scanning Java annotations at runtime
(13 answers)
Can you find all classes in a package using reflection?
(30 answers)
Closed 9 years ago.
I want to write a simple annotation like "#interface MyClassAnnotation" and "#interface MyMethodAnnotation" that targets Classes and Methods! but the main problem is that on main function i need to list all Class and Method instances on all JVM having that annotations! and I do not want to use any extra libraries, just pure built-in java functions.
Can anyone give me a good snippet for it?

You can determine whether your annotation is present on a particular class or method using the getAnnotation element, which is implemented by both Class and java.lang.reflect.Method.
The real question is, which classes and methods do you want to test this on? "All of them" is a bit hard to define. There doesn't seem to be a way to enumerate all of the classes that have been loaded by the JVM.

It's not feasible to go through all classes, you have to somehow narrow the scope. Either provide a configurable list of packages in which the classes with your particular annotation may be in or use the ServiceLoader concept in Java to declare the set of classes you need to find (this is useful if the code loading the service may be know about or link against all the implementations).

Related

How to understand the internal code of annotations [duplicate]

This question already has answers here:
How do annotations like #Override work internally in Java?
(6 answers)
Closed 4 years ago.
I am working on annotations from last few years without understanding how it works internally. But now I want to understand what is the internal logic for a particular annotation.
If I take an example of #autowire or #controller, I understand where to use this annotations but I am facing difficult in finding the logic behind this annotations.
Could someone help me - how to find the code for a particular annotation.
Initially I felt difficult to understand the functionality behind the annotation feature.
To be specific to the answer for every annotation class there will be processor, when we use the annotation the corresponding processor class will be called and will be executed. This corresponding class has to extend AbstractProcessor.

Call method inside annotation in Java [duplicate]

This question already has answers here:
Which types can be used for Java annotation members?
(4 answers)
Closed 4 years ago.
Can I able to call a method which returns string inside an annotation.
If so please guide me how to achieve this?
I tried like this but this doesn't work for me.
#Description(value = Resource.getWord("key"))
An annotation only takes compile time constants (as they might be used during compile time), therefore you cannot make any calculation within the definition, as they are unknown during the compile time.
Allowed constant types are (taken from java-annotation-members):
Primitive
String
Class
Enum
Another Annotation
An array of any of the above
Possible solution for your situation:
As I understand you would like to localize the #Description content.
As this is only meant to be exposed to other developers anyway, you are safe to simply use English, in my opinion. Localization is for the end user, not the developer.
I can imagine an aspect being wired up to process methods annotated like this, where the "key" is in the annotation, and the aspect processing then uses the key at run time... but I'm not sure this is what you're looking for.

Necessity of Annotation in Java [duplicate]

This question already has answers here:
How and where are Annotations used in Java?
(14 answers)
Closed 8 years ago.
Could anyone please explain what is the necessity of annotations in java? OR Let me put it more specifically as 'What would have happened if there were annotations in java?'.
I have often seen #Override annotation used extensively. But code can compile even without this annotation then what is it's need? I do understand this makes code less prone to spell mistakes but are annotations optional in general?
To quote the Java Tutorials:
Annotations, a form of metadata, provide data about a program that is
not part of the program itself. Annotations have no direct effect on
the operation of the code they annotate.
That does not mean that they are not very useful. The value of the #Override is demonstrated when you apply the annotation to a method that does not actually override any superclass method. The Java compiler will generate an error that alerts the developer that they are not overriding the method they think they are. This effectively turns a run-time error into a compile-time error.
Annotations are also widely used in a number of Java libraries and external libraries. For example, JAXB utilizes annotations to customize the XML text output from its bindings, and Hiberrnate uses annotations to map Java classes to relational database tables.

Reflection api in Java [duplicate]

This question already has answers here:
What is reflection and why is it useful?
(23 answers)
Closed 9 years ago.
I need explanation for the below questions:
What is reflection in java?
In which situation i need to use reflection?
Real time scenarios and examples for the need of using reflection?
I am very confused in reflection. I had read a lot of documents but I am still confused. Please give me a explanation where I can understand it completely.
what is reflection in java?
Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.
In which situation i need to use reflection?
mapping objects to tables in a database at runtime.
Real time scenarios and examples for the need of using reflection?
Good Real time example is MyEclipse IDE. if you put your mouse pointer over any class name it reflect the class name and with some information. This is also one type of reflection using reflection api.
Go through this Reflction API link you can get some more info.

interfaces and their use [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Use of Java [Interfaces / Abstract classes]
What's the benefit of interfaces over normal Java classes. Someone explained to me that an interface is like a contract, but I just don't get it. Why not just stick with classes?
Interfaces are useful for a couple reasons:
1) A class can extend only one other class, but it can implement any number of interfaces. This allows a method of multiple-inheritance while limiting the difficulties caused by multiple-inheritance.
2) They allow you to hide your implementation when you provide an API to your code, thus allowing you the freedom to change your implementation details in any way you wish as long as you don't violate the previously-defined interface.
For very small projects, interfaces may not be useful. For any medium-sized or large project, interfaces definitely help define the boundaries between the components so that the individual components can be tested in isolation from each other. Appropriate use of interfaces can also help you avoid circular dependencies between your JAR files.
When you are coding against a concrete class, it is easy to make use of implementation details that may not remain in future versions of the class. When you code against an interface, you cannot do this.
Read What Is an Interface? from the Java tutorials, it's well explained.

Categories