我使用反射来查找类中的方法,并获取注释"PermessiNecessari",该注释描述了方法实现的操作(CREATE, DELETE,…)的类型。
这是一个实现picketlink类PathAuthorizer的授权检查。我得到一个被调用的url,我将其拆分,并从url中找到实现web服务的类。然后,我搜索将被调用的方法,并读取它使用的操作类型。
这是搜索方法的一部分:
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});
例如,这是选中的类:
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;
...
方法是通过@javax.ws.rs找到的。路径注释,但当我想获得"PermessiNecessari"注释时,此注释未找到!
PS:在其他类这个系统工作良好。父接口中的方法也没有找到!尝试使用扩展相同接口的另一个接口,并找到所有方法(包括继承的方法)。
这是我的注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PermessiNecessari {
Class resourceClass() default void.class;
String operation() default "";
String modulo() default "";
}
这是搜索实现web服务的方法的方法:
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;
}
编辑:这不是注释的问题。我尝试了这个检查:
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;
}
如果我使用a.a notationtype ().equals(annotationType)它返回false,即使它们是相同的;如果我使用类的名称,它就能工作!
可能是类加载器问题?
解决!
依赖错误。项目中有两个不同的依赖版本,所以加载的注释实际上是两个不同的类。
下次:检查你的pom.xml不同的依赖关系,如果你有一些强制转换异常或如果两个相等的类不相等。
…真的没有解决,在EAR/lib和WAR/lib中的类之间存在类加载器问题,但这是另一个问题
@PermessiNecessari
必须是运行时保留的,因为如果保留不同,编译器将删除不包含有关该注释的信息到字节码中。
这就是为什么你的注释在运行时找不到-它不在那里。