summaryrefslogtreecommitdiff
path: root/src/main/java/bscv/asm/api/AnnotationChecker.java
blob: a0abbda445246cc511cb8fbe886cc1970f392350 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package bscv.asm.api;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

import java.lang.annotation.Annotation;

/**
 * ClassVisitor which checks whether a Class is annotated with
 * a specific Annotation.
 * @author Fraaz
 */
public class AnnotationChecker extends ClassVisitor {
   private boolean annotationPresent;
   private final String annotationDesc;

   public AnnotationChecker(Class<? extends Annotation> a) {
      super(Opcodes.ASM8); //hopefully lol
      this.annotationPresent = false;
      this.annotationDesc = Type.getDescriptor(a);
   }

   @Override
   public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
      if (visible && desc.equals(this.annotationDesc))
         this.annotationPresent = true;
      return super.visitAnnotation(desc, visible);
      //returning null would delete our annotation, but we don't want that
      //so we jut delegate to superclass
   }

   public boolean isAnnotationPresent() {
      return this.annotationPresent;
   }
}