This question already has answers here:
Modify a class definition's annotation string parameter at runtime
(7 answers)
Closed 6 years ago.
Below is the annotation.
#Before(value="execution(* class.method(**)")
Can i change the value of single value annotation using java reflection?
Please suggest.
The description of what you are looking for can be found here. But be wary about using such approach, some of the caveats of it are also described.
Also, the retention policy of the annotation has to be runtime, for this.
The link in the comment to your question leads to a post that looks like something you are asking about, but the difference is that it is about a JPA/Hibernate annotation where in the run-time you get a proxy class to your class.
Related
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.
This question already has answers here:
How to get the MethodInfo of a Java 8 method reference?
(11 answers)
How to get string name of a method in java?
(7 answers)
Closed 5 years ago.
So if I were logging and wanted to reference a class, it might not be a bad idea to use something like this:
log.warn("I did not find an instance of class "+SomeClass.class.getName());
This way a refactor would be guaranteed to update the string to use the correct name.
It would be really neat to be able to do this with Methods. You can't with Java 7, but perhaps something based on java 8 would work, like this:
log.warn("You forgot to call "+(SomeClass::aMethod).getName()+" before calling this method");
--it WOULD work great if SomeClass::aMethod was actually a method reference and not a Lambda.
Does anyone know of a way to make this work, perhaps extract the original method name from the lambda?
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:
How to declare or mark a Java method as deprecated?
(6 answers)
Closed 8 years ago.
I have a method in one of my classes that I don't want it to be used any more. Basically it breaks encapsulation, and I now have better methods I want to be used instead.
What's the best practice for signaling that a method is deprecated?
Add an #Deprecated annotation to that method and tell witch other classes to use in javadoc.
Mark using Deprecated annotation.
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.