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:
What is the "default" implementation of method defined in an Interface?
(3 answers)
Closed 6 years ago.
I was studying lambada and there was a point which states that in java 8 we can declare a method with definition in interfaces like
interface Test {
default String method(){
return "string";
}
}
and as per specification we can use two methods with same signature but depends on programmer how he wants to use it?
Now the question is same task can be if achieved by using definition not declaration then what's the point of using default method?
like they behave same as regular method definition and programmer need to declare body and rest part?
what is the actual point as it seems a bit hard to grasp
thanks #ElliottFrisch and #kagemusha for hint after searching i got the answer
Why default methods?
List<?> list = …
list.forEach(…); // lambda code goes here
The forEach isn’t declared by java.util.List nor the java.util.Collection interface yet. One obvious solution would be to just add the new method to the existing interface and provide the implementation where required in the JDK. However, once published, it is impossible to add methods to an interface without breaking the existing implementation.
So it’d be really frustrating if we have lambdas in Java 8 but couldn’t use those with the standard collections library since backwards compatibility can’t be sacrificed.
Due to the problem described above a new concept was introduced. Virtual extension methods, or, as they are often called, defender methods, can now be added to interfaces providing a default implementation of the declared behavior.
Simply speaking, interfaces in Java can now implement methods. The benefit that default methods bring is that now it’s possible to add a new default method to the interface and it doesn’t break the implementations.
It doesn’t seem to be the language feature that would be appropriate to use every day, but it seems to be essential for Java Collections API update to be able to use lambdas naturally.
This question already has an answer here:
Passing parameterized Class instance to the constructor
(1 answer)
Closed 8 years ago.
I wanted to obtain class object of Iterator and discovered that
Iterator<String>.class
is not valid. This highlighted a question if its possible at all to use
".class"
syntax for
Object<T>
kind of objects in Java.
If yes, then how it can be done?
If not, what are the alternatives ?
I am doing Mocking for Iterator using Mockito's
Mockito.mock(Class<T>)
syntax.
You can't do this because generics in Java are implemented using erasure i.e. you don't get specialized version of your classes for each type parameter used at runtime. Take a look at this answer in case it didn't turn up in your search.
It is not possible. The way that generics are implemented in Java, they only exist at compile time, for the purpose of type checking.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert String to code
Run piece of code contained in a String
is there any way to turn a variable(string) into to a piece of code?
My string has to be changeable (non-static)
If you can explain it simply that would be nice :D
I assume, that you want to create a String object with some Java statements and execute the content as if it was a method.
No, it is not possible, because that would imply, that java was an interpreted programming language. Which is not the case.
You could merge the line with some sort of class template (on another string), store it to a file, compile that using the jdk (call javac with `Runtime.exec) and load the class with a custom classloader, then reflect the method and hope for the best.
There is a rather complicated way using the fairly new Java Compiler API, but I guess, that is far beyond your needs. (And there have to be some more complicated tricks, because the eclipse IDE provides views that allow executing Java statements.)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What’s “#Override” there for in java?
Since Java 1.5 this annotation was incorporated to the language to be used on methods that overwrite a superclass methods.
Now, what changes in a method that uses this annotation to one that doesn't use it? Is this just convention?
Assuming, obviously, that both be methods that overwrite a method from its superclass...
#Override creates a compile-time check that a method is being overridden.
This is very useful to make sure you do not have a silly signature issue when trying to override
It not only makes the compiler check but also documents the
developer's intention.
if you override a method but don't use it anywhere from the type itself, someone coming to the code later may know the purpose. The annotation explains its purpose.
A good IDE will helpfully flag any method that overrides a method without #Override, so the combination of the two will help ensure that you're doing what you're trying to.
it also improves readability
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you find all subclasses of a given class in Java?
I have a super class, MyClass, and it is abstract. Anyone can implement it.
At runtime, I need to determine which classes have inherited from this class, using java reflection. How can one do this?
You can use the Reflections library.
Using Reflections you can query your metadata such as:
get all subtypes of some type
get all types/methods/fields annotated with some annotation, w/o
annotation parameters matching
get all resources matching matching a regular expression
A tutorial about that is Java Tip 113: Identify subclasses at runtime.
The only way to do it (without resorting to external libraries) is to loop through all your classes and check them individually - there's no efficient method built in (nor can there really be, how else would you do it than scanning all the classes since they can be loaded dynamically?)
See Stack Overflow question How do you find all subclasses of a given class in Java? for much more detail.