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