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.MethodVisitor;
28  import org.objectweb.asm.Opcodes;
29  import org.objectweb.asm.Type;
30  
31  /**
32   * Method visitor.
33   *
34   * @author Slawomir Jaranowski
35   */
36  public class MojoMethodVisitor extends MethodVisitor implements MojoParameterVisitor {
37      private final String className;
38      private final String fieldName;
39      private final List<String> typeParameters;
40      private Map<String, MojoAnnotationVisitor> annotationVisitorMap = new HashMap<>();
41  
42      public MojoMethodVisitor(String fieldName, String className, List<String> typeParameters) {
43          super(Opcodes.ASM9);
44          this.fieldName = fieldName;
45          this.className = className;
46          this.typeParameters = typeParameters;
47      }
48  
49      @Override
50      public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
51          String annotationClassName = Type.getType(desc).getClassName();
52          if (!MojoAnnotationsScanner.METHOD_LEVEL_ANNOTATIONS.contains(annotationClassName)) {
53              return null;
54          }
55  
56          MojoAnnotationVisitor mojoAnnotationVisitor = new MojoAnnotationVisitor(annotationClassName);
57          annotationVisitorMap.put(annotationClassName, mojoAnnotationVisitor);
58          return mojoAnnotationVisitor;
59      }
60  
61      @Override
62      public String getFieldName() {
63          return fieldName;
64      }
65  
66      @Override
67      public String getClassName() {
68          return className;
69      }
70  
71      @Override
72      public List<String> getTypeParameters() {
73          return typeParameters;
74      }
75  
76      @Override
77      public Map<String, MojoAnnotationVisitor> getAnnotationVisitorMap() {
78          return annotationVisitorMap;
79      }
80  
81      @Override
82      public boolean isAnnotationOnMethod() {
83          return true;
84      }
85  }