View Javadoc
1   package org.apache.maven.shared.dependency.analyzer.asm;
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 org.objectweb.asm.AnnotationVisitor;
23  import org.objectweb.asm.ClassVisitor;
24  import org.objectweb.asm.FieldVisitor;
25  import org.objectweb.asm.MethodVisitor;
26  import org.objectweb.asm.Opcodes;
27  import org.objectweb.asm.Type;
28  import org.objectweb.asm.signature.SignatureReader;
29  import org.objectweb.asm.signature.SignatureVisitor;
30  
31  
32  /**
33   * Computes the set of classes referenced by visited code.
34   * Inspired by <code>org.objectweb.asm.depend.DependencyVisitor</code> in the ASM dependencies example.
35   *
36   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
37   * @version $Id$
38   */
39  public class DefaultClassVisitor
40      extends ClassVisitor
41  {
42      // fields -----------------------------------------------------------------
43  
44      private final ResultCollector resultCollector;
45  
46      private final SignatureVisitor signatureVisitor;
47  
48      private final AnnotationVisitor annotationVisitor;
49  
50      private final FieldVisitor fieldVisitor;
51  
52      private final MethodVisitor methodVisitor;
53  
54      // constructors -----------------------------------------------------------
55  
56      public DefaultClassVisitor( SignatureVisitor signatureVisitor, AnnotationVisitor annotationVisitor,
57                                  FieldVisitor fieldVisitor, MethodVisitor methodVisitor,
58                                  ResultCollector resultCollector )
59      {
60          super( Opcodes.ASM8 );
61          this.signatureVisitor = signatureVisitor;
62          this.annotationVisitor = annotationVisitor;
63          this.fieldVisitor = fieldVisitor;
64          this.methodVisitor = methodVisitor;
65          this.resultCollector = resultCollector;
66      }
67  
68      public void visit( final int version, final int access, final String name, final String signature,
69                         final String superName, final String[] interfaces )
70      {
71          if ( signature == null )
72          {
73              resultCollector.addName( superName );
74              resultCollector.addNames( interfaces );
75          }
76          else
77          {
78              addSignature( signature );
79          }
80      }
81  
82      public AnnotationVisitor visitAnnotation( final String desc, final boolean visible )
83      {
84          resultCollector.addDesc( desc );
85  
86          return annotationVisitor;
87      }
88  
89      public FieldVisitor visitField( final int access, final String name, final String desc, final String signature,
90                                      final Object value )
91      {
92          if ( signature == null )
93          {
94              resultCollector.addDesc( desc );
95          }
96          else
97          {
98              addTypeSignature( signature );
99          }
100 
101         if ( value instanceof Type )
102         {
103             resultCollector.addType( (Type) value );
104         }
105 
106         return fieldVisitor;
107     }
108 
109     public MethodVisitor visitMethod( final int access, final String name, final String desc, final String signature,
110                                       final String[] exceptions )
111     {
112         if ( signature == null )
113         {
114             resultCollector.addMethodDesc( desc );
115         }
116         else
117         {
118             addSignature( signature );
119         }
120 
121         resultCollector.addNames( exceptions );
122 
123         return methodVisitor;
124     }
125 
126     public void visitNestHost( final String nestHost )
127     {
128         resultCollector.addName( nestHost );
129     }
130 
131     public void visitNestMember( final String nestMember )
132     {
133         resultCollector.addName( nestMember );
134     }
135 
136     // private methods --------------------------------------------------------
137 
138     private void addSignature( final String signature )
139     {
140         if ( signature != null )
141         {
142             new SignatureReader( signature ).accept( signatureVisitor );
143         }
144     }
145 
146     private void addTypeSignature( final String signature )
147     {
148         if ( signature != null )
149         {
150             new SignatureReader( signature ).acceptType( signatureVisitor );
151         }
152     }
153 
154 
155 }