Here is a test class:
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class TestAnnotations {
#interface Annotate{}
#Annotate public void myMethod(){}
public static void main(String[] args) {
try{
Method[] methods = TestAnnotations.class.getDeclaredMethods();
Method m = methods[1];
assert m.getName().equals("myMethod");
System.out.println("method inspected ? " + m.getName());
Annotation a = m.getAnnotation(Annotate.class);
System.out.println("annotation ? " + a);
System.out.println("annotations length ? "
+ m.getDeclaredAnnotations().length);
}
catch(Exception e){
e.printStackTrace();
}
}
}
Here is my output :
method inspected ? myMethod
annotation : null
annotations length : 0
What I am missing to make annotations visible through reflection ?
Do I need an annotation processor even for just checking their presence ?
In order to access an annotation at runtime, it needs to have a Retention policy of Runtime.
#Retention(RetentionPolicy.RUNTIME) #interface Annotate {}
Otherwise, the annotations are dropped and the JVM is not aware of them.
For more information, see here.
Related
I have read the following valuable links:
Spring AOP pointcut for annotated argument
How to write an Aspect pointcut based on an annotated parameter
AspectJ pointcut expression match parameter annotations at any position
Consider this request for a setter method
public void setSomething(#ParameterLevel(name="abc") String something){
this.something = something;
}
I have the following and works fine:
#Pointcut("execution(* *.*(#somepackage.ParameterLevel (*)))")
void parameterLevel01() {}
Now I want retrieve the #ParameterLevel annotation through a method's parameter such as the following:
#Pointcut("execution(* *.*(#somepackage.ParameterLevel (*)))")
void parameterLevel01(ParameterLevel parameterLevel) {} <--To be used directly in the advice method
The purpose is use the Annotation directly how a parameter in the advice method
Something similar such as:
#within(classLevel) for #ClassLevel in:
#ClassLevel
public class SomeClass {
...
}
#annotation(methodLevel) for #MethodLevel in:
#MethodLevel
public void somethingToDo(){
...
}
How accomplish this goal. Is possible? I am working with AspectJ 1.9.6
No matter if you use .., #MyAnnotation (*), .. or just #MyAnnotation (*), which only removes the ambiguity of possibly multiple matches, there is no direct way to bind a method argument annotation to an advice argument, only the method argument itself. This has not changed in AspectJ. You would have seen it mentioned in the release notes otherwise, because it would be a new feature.
So you will have to use the method from my other two answers which you have already linked to in your question, i.e. iterating over parameter types and annotations manually.
Somewhat off-topic, there is a very old Bugzilla ticket #233718 which is about binding multiple matched (annotated) parameters, but not about binding their annotations. It came up in a recent discussion I had with AspectJ maintainer Andy Clement. But even if this was implemented one day, it would not solve your problem.
I think you can take it from here and adapt my solution from the linked questions to your needs. Feel free to let me know if you have any follow-up questions about that, but it should be pretty straightforward. You might be able to optimise because you know the exact parameter position (think array index), if you feel so inclined, i.e. you don't need to iterate over all parameters.
Update: Here is a little MCVE for you. It is based on this answer and has been simplified to assume the annotation is always on the first parameter and the first parameter only.
Please learn what an MCVE is and provide one by yourself next time because it is your job, not mine. This was your free shot.
Marker annotation + driver application:
package de.scrum_master.app;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
#Retention(RUNTIME)
public #interface ParameterLevel {
String name();
}
package de.scrum_master.app;
public class Application {
public static void main(String[] args) {
new Application().doSomething("foo");
}
public void doSomething(#ParameterLevel(name="abc") String string) {}
}
Aspect:
package de.scrum_master.aspect;
import java.lang.annotation.Annotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.SoftException;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import de.scrum_master.app.ParameterLevel;
#Aspect
public class ParameterLevelAspect {
#Before("execution(public * *(#de.scrum_master.app.ParameterLevel (*))) && args(string)")
public void beforeAdvice(JoinPoint thisJoinPoint, String string) {
System.out.println(thisJoinPoint + " -> " + string);
MethodSignature signature = (MethodSignature) thisJoinPoint.getSignature();
String methodName = signature.getMethod().getName();
Class<?>[] parameterTypes = signature.getMethod().getParameterTypes();
Annotation[] annotations;
try {
annotations = thisJoinPoint.getTarget().getClass()
.getMethod(methodName, parameterTypes)
.getParameterAnnotations()[0];
} catch (NoSuchMethodException | SecurityException e) {
throw new SoftException(e);
}
ParameterLevel parameterLevel = null;
for (Annotation annotation : annotations) {
if (annotation.annotationType() == ParameterLevel.class) {
parameterLevel = (ParameterLevel) annotation;
break;
}
}
assert parameterLevel != null;
System.out.println(" " + parameterLevel + " -> " + parameterLevel.name());
}
}
Console log:
execution(void de.scrum_master.app.Application.doSomething(String)) -> foo
#de.scrum_master.app.ParameterLevel(name="abc") -> abc
I am getting null pointer exception while executing following method. I have debugged and found that cons.getAnnotation(MyAssessment.class); is returning null. Plese help
import java.lang.annotation.*;
import java.lang.reflect.*;
enum GradeLevel { POOR, AVERAGE, GOOD, VERYGOOD, EXTRAORDINARY }
#interface MyAssessment {
GradeLevel score();
}
public class Problem {
#MyAssessment(score=GradeLevel.GOOD)
public Problem(){
System.out.println("This is a constuructor");
}
public static void main(String... args){
try {
Class c = new Problem().getClass();
Constructor cons = c.getDeclaredConstructor();
MyAssessment an = (MyAssessment) cons.getAnnotation(MyAssessment.class);
System.out.println("YOUR SCORE IS : " + an.score());
}
catch(NoSuchMethodException nsme) {
System.out.println("Constructor thrown exception");
}
}
}
Annotations are not by default available at runtime. You need to annotate your annotation :P
#Retention(RetentionPolicy.RUNTIME)
#interface MyAssessment {
You can also add a #Target of say CONSTRUCTOR to say it should only appear on constructors.
You need to annotate your annotation with:
#Retention(RetentionPolicy.RUNTIME)
You can change the Retention Policy of your annotation. Using #Retention.
This is the first time I am trying to write a custom annotations in java.
I am not sure whether it is possible or not but wanted to give it a try before approaching another solution.
So here is the scenario, I have a lots of method that sends the data out from the application to a device. I have a requirement to log all these data in database.
I would like to create an annotation for this so that I can write the code in the annotation to log the data in database and then annotation all the methods with this annotation.
I can modify the code to log into the database but in that case I have to go in each method and place my code at correct place inorder to log them into database.
This is the reason I am looking for annotation based approach.
Is it possible what I am looking for or am I asking more.
Any pointers will be appreciated or If someone has different approach for my solution that will be really help full.
Instead of writing your own Annotations and processing them, have a look at what Spring provides, e.g. Interceptors:
Interceptors vs Aspects in Spring?
You can try below approach
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
#interface Todo {
public enum Priority {LOW, MEDIUM, HIGH}
String logInfo() default "Logging...";
Priority priority() default Priority.LOW;
}
package annotation;
public class BusinessLogic {
public BusinessLogic() {
super();
}
public void compltedMethod() {
System.out.println("This method is complete");
}
#Todo(priority = Todo.Priority.HIGH)
public void notYetStartedMethod() {
// No Code Written yet
}
#Todo(priority = Todo.Priority.MEDIUM, logInfo = "Inside DAO")
public void incompleteMethod1() {
//Some business logic is written
//But its not complete yet
}
#Todo(priority = Todo.Priority.LOW)
public void incompleteMethod2() {
//Some business logic is written
//But its not complete yet
}
}
package annotation;
import java.lang.reflect.Method;
public class TodoReport {
public TodoReport() {
super();
}
public static void main(String[] args) {
Class businessLogicClass = BusinessLogic.class;
for(Method method : businessLogicClass.getMethods()) {
Todo todoAnnotation = (Todo)method.getAnnotation(Todo.class);
if(todoAnnotation != null) {
System.out.println(" Method Name : " + method.getName());
System.out.println(" Author : " + todoAnnotation.logInfo());
System.out.println(" Priority : " + todoAnnotation.priority());
System.out.println(" --------------------------- ");
}
}
}
}
I want to create custom annotation in java for DirtyChecking. Like I want to compare two string values using this annotation and after comparing it will return a boolean value.
For instance: I will put #DirtyCheck("newValue","oldValue") over properties.
Suppose I made an interface:
public #interface DirtyCheck {
String newValue();
String oldValue();
}
My Questions are:
Where I make a class to create a method for comparison for two string values? I mean, how this annotation notifies that this method I have to call?
How to retreive returning values of this method ?
First you need to mark if annotation is for class, field or method. Let's say it is for method: so you write this in your annotation definition:
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
public #interface DirtyCheck {
String newValue();
String oldValue();
}
Next you have to write let's say DirtyChecker class which will use reflection to check if method has annotation and do some job for example say if oldValue and newValue are equal:
final class DirtyChecker {
public boolean process(Object instance) {
Class<?> clazz = instance.getClass();
for (Method m : clazz.getDeclaredMethods()) {
if (m.isAnnotationPresent(DirtyCheck.class)) {
DirtyCheck annotation = m.getAnnotation(DirtyCheck.class);
String newVal = annotation.newValue();
String oldVal = annotation.oldValue();
return newVal.equals(oldVal);
}
}
return false;
}
}
Cheers,
Michal
To answer your second question: your annotation can't return a value. The class which processes your annotation can do something with your object. This is commonly used for logging for example.
I'm not sure if using an annotation for checking if an object is dirty makes sense except you want to throw an exception in this case or inform some kind of DirtyHandler.
For your first question: you could really spent some effort in finding this yourself. There are enough information here on stackoverflow and the web.
CustomAnnotation.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
public #interface CustomAnnotation {
int studentAge() default 21;
String studentName();
String stuAddress();
String stuStream() default "CS";
}
How to use the field of Annotation in Java?
TestCustomAnnotation.java
package annotations;
import java.lang.reflect.Method;
public class TestCustomAnnotation {
public static void main(String[] args) {
new TestCustomAnnotation().testAnnotation();
}
#CustomAnnotation(
studentName="Rajesh",
stuAddress="Mathura, India"
)
public void testAnnotation() {
try {
Class<? extends TestCustomAnnotation> cls = this.getClass();
Method method = cls.getMethod("testAnnotation");
CustomAnnotation myAnno = method.getAnnotation(CustomAnnotation.class);
System.out.println("Name: "+myAnno.studentName());
System.out.println("Address: "+myAnno.stuAddress());
System.out.println("Age: "+myAnno.studentAge());
System.out.println("Stream: "+myAnno.stuStream());
} catch (NoSuchMethodException e) {
}
}
}
Output:
Name: Rajesh
Address: Mathura, India
Age: 21
Stream: CS
Reference
Java Gurus,
I am pretty new for annotations and haven't searched for this a lot, so please bear with me...
I would like to implement a Custom Annotation which will intercept a method call; to start with something very basic it can just print the methods name and parameters so that I could avoid the logger statement.
A sample call like this:
public MyAppObject findMyAppObjectById(Long id) throws MyCustomException {
log.debug("in findMyAppObjectById(" + id + ")");
//....
}
can be converted into:
#LogMethodCall(Logger.DEBUG)
public MyAppObject findMyAppObjectById(Long id) throws MyCustomException {
//....
}
Could I get some hints about this?
Based in your answers of my comments, you will not be able to do this with just annotations. You can, of course, create your annotations and create some reflective code that will detected then and execute some code, but this will not change your code too much, because you will need to call the parser method before you call your methods and I think that will not help you too much, since you will need to call the parser method before each call.
If you need the behavior that you mentioned (automatic call), you will need to combine your annotations with some AOP framework like Spring (plain Java) or AspectJ (AspectJ code). With then, you can set pointcuts and everytime this point is reached, some code may be executed. You can configure then to execute some code before and/or after method execution.
If the first scenario is sufficient, you can do something like:
Logger: enum
public enum Logger {
INFO,
DEBUG;
}
LogMethodCall: annotation
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Retention( RetentionPolicy.RUNTIME ) // the annotation will be available during runtime
#Target( ElementType.METHOD ) // this can just used in methods
public #interface LogMethodCall {
Logger logLevel() default Logger.INFO;
}
Person: annotated class
public class Person {
// will use the default log level (INFO)
#LogMethodCall
public void foo( int a ) {
System.out.println( "foo! " + a );
}
#LogMethodCall( logLevel = Logger.DEBUG )
public void bar( int b ) {
System.out.println( "bar! " + b );
}
}
Utils: class with the log static method (this will perform the "parsing")
public class Utils {
public static void log( Object o, String methodName ) {
// gets the object class
Class klass = o.getClass();
// iterate over its methods
for ( Method m : klass.getMethods() ) {
// verify if the method is the wanted one
if ( m.getName().equals( methodName ) ) {
// yes, it is
// so, iterate over its annotations
for ( Annotation a : m.getAnnotations() ) {
// verify if it is a LogMethodCall annotation
if ( a instanceof LogMethodCall ) {
// yes, it is
// so, cast it
LogMethodCall lmc = ( LogMethodCall ) a;
// verify the log level
switch ( lmc.logLevel() ) {
case INFO:
System.out.println( "performing info log for \"" + m.getName() + "\" method" );
break;
case DEBUG:
System.out.println( "performing debug log for \"" + m.getName() + "\" method" );
break;
}
}
}
// method encountered, so the loop can be break
break;
}
}
}
}
AnnotationProcessing: class with code to test the annotation processing
public class AnnotationProcessing {
public static void main(String[] args) {
Person p = new Person();
Utils.log( p, "foo" );
p.foo( 2 );
Utils.log( p, "bar" );
p.bar( 3 );
}
}
Of course, you will need to improve my code to fit your needs. It is just a start point.
More about annotations:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html
http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html
http://tutorials.jenkov.com/java-reflection/annotations.html
More about AOP:
http://en.wikipedia.org/wiki/Aspect-oriented_programming
http://static.springsource.org/spring/docs/3.0.x/reference/aop.html
http://www.eclipse.org/aspectj/
Use Spring AOP along with Java Annotation. Spring AOP negates the requirement for writing a util class for parsing of Java classes using Java Reflection.
Example -
Custom Annotation -
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface A {
boolean startA() default false;
boolean endA() default false;
}
Aspect-
#Aspect
public class AAspect {
#Pointcut(value = "execution(* *.*(..))")
public void allMethods() {
LOGGER.debug("Inside all methods");
}
#Before("allMethods() && #annotation(A)")
public void startAProcess(JoinPoint pjp, A a) throws Throwable {
if (a.startA()) {
//Do something
}
}
Enable AspectJ -
#Configuration
#EnableAspectJAutoProxy
public class AConfig {
}
Use in code -
#A(startA = true, endA = true)
public void setUp(){
//Do something- logic
}
As already suggested, AOP and annotations is the best option. I would recommend to use a ready-made mechanism from jcabi-aspects (I'm a developer):
#Loggable(Loggable.DEBUG)
public String load(URL url) {
return url.openConnection().getContent();
}
All method calls will be logged to SLF4J.