0

I'm using reflection for find a method in a class and the get the annotation "PermessiNecessari" that describe the type of the operation (CREATE, DELETE,...) that a method implements.

This is a piece of authorization check implementing a picketlink class PathAuthorizer. I get a called url, i split it and from the url i find the class that implements the web service. Then I search for the method that it will be called and read what type of operation it use.

This is a piece of the searching-method :

Class facadeinterface = Class.forName(pm); // get the interface
Method metodo = getMetodoClasse(facadeinterface, metodoRest); // find method with @Path annotation
if(metodo != null){
   PermessiNecessari rp = metodo.getAnnotation(PermessiNecessari.class);

   if(rp != null){ // caso metodo con permessi
       return checkPermessiModulo(m, rp);
   }

   if(metodo.isAnnotationPresent(NonProtetto.class)){ 
       return true;
   }
   LOG.log(Level.WARNING, "Metodo trovato : {0}/{1}, ma non annotato!", new Object[]{metodoRest,metodo});

For istance, this is the checked class:

public interface VlpgmoduliManagerFacadeRemote 
   extends InterfacciaFacadeRemote<Vlpgmoduli>{

  @POST 
  @javax.ws.rs.Path("getpermessimoduligruppo")
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
  @PermessiNecessari(operation = STANDARD_OP.READ)
  public GridResponse<Vlpgmoduli> getPermessiModuliGruppo(MultivaluedMap<String, String> formParams, 
        String callback) 
        throws BadRequestException;
...

The method is found via @javax.ws.rs.Path annotation, but when i want to get "PermessiNecessari" annotation, this annotation is not found!

PS : in other classes this system works fine. The method in the parent interface are not found too! Tryed with another interface that extends the same interface and all the methods (the inherited too) are found.

This is my annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PermessiNecessari {    
    Class resourceClass() default void.class;
    String operation() default "";
    String modulo() default "";
}

This is the method for searching the method that implement the web service:

private Method getMetodoClasse(Class facade, String metodo){
    for(Method method : facade.getMethods()){
        Path p = method.getAnnotation(Path.class);
        if(p != null && ( p.value().equals(metodo) || p.value().equals("/"+metodo) )){
            return method;
        }
    }
    for (Class c : facade.getInterfaces()) {
        for (Method method : c.getDeclaredMethods()) {
            Path p = method.getAnnotation(Path.class);
            if(p != null && ( p.value().equals(metodo) || p.value().equals("/"+metodo) )){
                return method;
            }
        }
    }
    return null;
}

EDIT : It's not a problem of annotation. I tryed with this check:

public boolean haAnnotazione(Method method, Class annotationType){
    Annotation annotazioni[] = method.getAnnotations();
    for(Annotation a : annotazioni){
      if(a.annotationType().getName().equals(annotationType.getName())){
            return true;
        }
    }
    return false;
}

if I use a.annotationType().equals(annotationType) it returns false even if they are the same; if i use the name of the class, it works!

Maybe a classloader problem? The software run in Wildfly.

Daniele Licitra
  • 1,520
  • 21
  • 45
  • Possible duplicate of [How do different retention policies affect my annotations?](http://stackoverflow.com/questions/3107970/how-do-different-retention-policies-affect-my-annotations) – Dariusz Nov 09 '16 at 12:38
  • @Daniele could you please explain your edit, as I have no idea what you had in mind? – Antoniossss Nov 09 '16 at 14:28
  • @Antoniossss i'have changed the question, i hope it's more clear now – Daniele Licitra Nov 09 '16 at 16:30

2 Answers2

1

Solved!

It was a dependency error. There were 2 different dependencies version in the project, so the loaded annotations were really two different classes.

For the next time: check in your pom.xml for different dependencies if you have some cast exception or if two equal Class are not equal.

...really not solved, there is a classloader problem between class in EAR/lib and in WAR/lib, but this is another question

Daniele Licitra
  • 1,520
  • 21
  • 45
0

@PermessiNecessari must be of Runtime retention because compiler will remove not incluide information about that annotation into the bytecode if retention will be different.

That is why your annotation is not found upon runtime - it is not there.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99