View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.tools.plugin.extractor.annotations.scanner.visitors;
20  
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotationsScanner;
26  import org.objectweb.asm.AnnotationVisitor;
27  import org.objectweb.asm.FieldVisitor;
28  import org.objectweb.asm.Opcodes;
29  import org.objectweb.asm.Type;
30  
31  /**
32   * Visitors for fields.
33   *
34   * @author Olivier Lamy
35   * @since 3.0
36   */
37  public class MojoFieldVisitor extends FieldVisitor implements MojoParameterVisitor {
38      private String fieldName;
39  
40      private Map<String, MojoAnnotationVisitor> annotationVisitorMap = new HashMap<>();
41  
42      private String className;
43  
44      private final List<String> typeParameters;
45  
46      MojoFieldVisitor(String fieldName, String className, List<String> typeParameters) {
47          super(Opcodes.ASM9);
48          this.fieldName = fieldName;
49          this.className = className;
50          this.typeParameters = typeParameters;
51      }
52  
53      @Override
54      public Map<String, MojoAnnotationVisitor> getAnnotationVisitorMap() {
55          return annotationVisitorMap;
56      }
57  
58      @Override
59      public String getFieldName() {
60          return fieldName;
61      }
62  
63      @Override
64      public List<String> getTypeParameters() {
65          return typeParameters;
66      }
67  
68      @Override
69      public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
70          String annotationClassName = Type.getType(desc).getClassName();
71          if (!MojoAnnotationsScanner.FIELD_LEVEL_ANNOTATIONS.contains(annotationClassName)) {
72              return null;
73          }
74          MojoAnnotationVisitor mojoAnnotationVisitor = new MojoAnnotationVisitor(annotationClassName);
75          annotationVisitorMap.put(annotationClassName, mojoAnnotationVisitor);
76          return mojoAnnotationVisitor;
77      }
78  
79      @Override
80      public String getClassName() {
81          return className;
82      }
83  
84      @Override
85      public boolean isAnnotationOnMethod() {
86          return false;
87      }
88  }