Reputation: 3350
I am working with custom annotation building ,I am creating some custom annotation that will validating some data.I am not able to write the annotation processor for that annotation.
i.e. I created a custom annotation UserRole as :
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER })
public @interface UserRole
{
int[] value() default {0};
}
I will use this annotation as follow :
import java.util.List;
public class Demo
{
public void checkValidUser(@UserRole({1,2})List<Integer> roles){
// some code here
}
}
I want to write Annotation processor so that I can check if given list contain any role that is specified in annotation at runtime because I will provide List at runtime. I need help to write that annotation processor.
Thanks in Advance.
Upvotes: 1
Views: 2369
Reputation: 1159
After your last comment lets try to understand how does how does jpa works
when you use dao.find(id, class); the entity manager checks class metadata like( primary key and tablename... etc) which is parsed before .. (as i was trying tell you in the classInfo example) and adds it to entitymanager has map field
Map<Class,EntityInfo>
named as entities ( for my example it must be map must be like that Map ) when you run dao.find(id,User.class) .. it checks that map. gets the id field and table information and parsers from there ( parses sql something like this "select * from" + entity.getTableName() +" where "+ entity.getIdField().getName() +" = "+ id ) and runs query and returns the result ... i hope now you can understand what i was trying to tell you ..
How and where are Annotations used in Java? ... good answer first read what is annotation ..
Annotation describe way what the method , field or class will do .. so it is not passing parameter, it is defining a rule ... for your example if there is 3 type of user .. Admin Normal Guest and u have a method which can be call by only user types Admin .. like deleteProduct..
u have to use :
@UserRoles([Admin])
public void deleteProduct(){
///......
}
@UserRoles([Admin,Normal])
public void commentProduct() {}
for example you have an url with
www.site.com/comments/15/likes?offset=15
and you are writing an webroute handler .. which has annotations @WebMethod (Post , Get , Put, Delete etc) , @WebParameter (POST and GET PARAMETER , @URLParam( URL PARAM handler) ;
comment/:id/likes/
you have to do that like that ..
public class Comments {
// handles url path like /comments/12/likes?offset=15
@WebMethod(MethodType.GET)
@WebURL("comment/:id/likes/")
public void listLikes(@WebParameter(name="offset") int offset, @URLParam("id") long id) {
//operatio to Do so here offset will be 15 ,id 12
}
}
the logic of annotations something like that .. Here is an another example ..
First you have to create a holder Object ( which holds all information readed by reflection ) so you can avolute it with proxy of object ..
For example if you have annotation like @Id and @ColumnName for Fields , and @Async for method ...
you have to write to Class Object FieldInfo which has field Id ,name
For Class
class ClassInfo {
private final Set<MethodInfo> methods;
private final Set<FieldInfo> fields;
}
For Fields :
class FieldInfo {
private boolean id=false;
private Field fieldInfo ;
private String columnName;
/// More info for fields like field name
// constructors
//Getters and Setters
//methods to use
}
For Methods :
class MethodInfo {
private Method methodInfo;
private boolean async= false;
/// More info for methods like field name
// constructors
//Getters and Setters
//methods to use
}
So u can use reflection ( i do it only for Field you can do it for method and method parameters as like as you want )
final ClassInfo classInfo = new ClassInfo();
Field[] fields = Demo.class.getFields();
for(final field :fields) {
FieldInfo fieldInfo = new FieldInfo(field);
if(field.hasAnnotation(Id.class)) {
fieldInfo.setId(true);
}
if(field.hasAnnotation(ColumnName.class){
final ColumnName col = field.getAnnotation(ColumnName.class);
fieldInfo.setColumnName(col.value());
}
}
and methods , method parameters you have parse what u have to do with it ..
i hope this information helps you ... if you have any more question , feel free to ask
Upvotes: 2