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.
Related
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.
This question already has answers here:
What does the # sign means in java? [duplicate]
(4 answers)
Closed 5 years ago.
just started learning android/java and i noticed many appearances of '#' in the code. #Override i can find using google, but the rest of it is a mystery.
Eg while using ButterKnife library we declare variables like this:
#BindView(R.id.note1) TextView importantNote;
Does '#' have any special functionality, or is it just a regular part of method BindView() name? Since I couldnt find anything specific about '#', I came up with an idea it could be some kind of convention. Am I right? If so, what is the convention?
# is declaring an Annotation. When compiling the code, an Annotation Processor is scanning the code for those annotations and can generate code as you need (for example, the BindView annotation generates the findViewById code needed to bind a view to a variable) or as additional compiler instruction (for example, Override makes sure that the variable actually overrides something or else you get a compiler error).
For more on how to create your own annotations (and how annotation processors work) read here: https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/Processor.html
Check out https://docs.oracle.com/javase/tutorial/java/annotations/
These are "annotations".
For example:
#Override
protected void overridenMethod() {
}
Annotations can have arguments, like this:
#SuppressWarnings("unused")
The example of #BindView is an inline annotation. Inline annotations are very uncommon, other examples of inline annotations are very specific #SuppressLint annotations.
You can make your own annotations, however, if you are making annotations for compiler warnings, error etc., in this case the IDE must detect the annotation. For example, SuppressLint will work only if IDE supports the annotation, otherwise, annotation won't have any effect.
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.
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.
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).