How to declare a warning in a field with AspectJ

I want to declare a warning about all fields annotated with @org.jboss.weld.context.ejb.Ejb in AspectJ.

But I can not find a way to select this field.

I assume this aspect should be something like this:

 public aspect WrongEjbAnnotationWarningAspect { declare warning : within(com.queomedia..*) && ??? (@org.jboss.weld.context.ejb.Ejb) : "WrongEjbAnnotationErrorAspect: use javax.ejb.EJB instead of weld Ejb!"; } 

Or is it impossible to declare field warnings at all?

+4
source share
2 answers

The only field points I see are for get and set. This makes sense, as aspects relate mostly to code execution. Declaring compiler warnings is a great advantage. If we just talk about the field, regardless of the use of this field, when will the pointcut be deleted? I think you should do what you want with the Annotation Tool instead of AspectJ. Here is the first hit on it, mostly copied from an example on the tool’s web page above.

 public class EmitWarningsForEjbAnnotations implements AnnotationProcessorFactory { // Process any set of annotations private static final Collection<String> supportedAnnotations = unmodifiableCollection(Arrays.asList("*")); // No supported options private static final Collection<String> supportedOptions = emptySet(); public Collection<String> supportedAnnotationTypes() { return supportedAnnotations; } public Collection<String> supportedOptions() { return supportedOptions; } public AnnotationProcessor getProcessorFor( Set<AnnotationTypeDeclaration> atds, AnnotationProcessorEnvironment env) { return new EjbAnnotationProcessor(env); } private static class EjbAnnotationProcessor implements AnnotationProcessor { private final AnnotationProcessorEnvironment env; EjbAnnotationProcessor(AnnotationProcessorEnvironment env) { this.env = env; } public void process() { for (TypeDeclaration typeDecl : env.getSpecifiedTypeDeclarations()) typeDecl.accept(new ListClassVisitor()); } private static class ListClassVisitor extends SimpleDeclarationVisitor { public void visitClassDeclaration(ClassDeclaration d) { for (FieldDeclaration fd : d.getFields()) { fd.getAnnotation(org.jboss.weld.context.ejb.Ejb.class); } } } } } 
+2
source

Agree with @JohnWatts, but also feel that get () will work for you:

 declare warning : within(com.queomedia..*) && get(@org.jboss.weld.context.ejb.Ejb * *.*) : "WrongEjbAnnotationErrorAspect: use javax.ejb.EJB instead of weld Ejb!"; 

This will show a warning for any code that tries to use fields annotated with @org.jboss.weld.context.ejb.Ejb rather than the field itself, but should this be a compile-time warning?

+1
source

Source: https://habr.com/ru/post/1402864/


All Articles