View Javadoc
1   /*
2    * Copyright (C) 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.plexus.metadata.ann;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.ArrayList;
22  
23  import org.objectweb.asm.AnnotationVisitor;
24  import org.objectweb.asm.ClassReader;
25  import org.objectweb.asm.ClassVisitor;
26  import org.objectweb.asm.FieldVisitor;
27  import org.objectweb.asm.MethodVisitor;
28  import org.objectweb.asm.Opcodes;
29  
30  /**
31   * @author Eugene Kuleshov
32   */
33  public class AnnReader extends ClassVisitor {
34  
35      private final AnnClass annClass;
36  
37      private AnnReader(AnnClass annClass) {
38          super(Opcodes.ASM9);
39          this.annClass = annClass;
40      }
41  
42      public static AnnClass read(InputStream is, ClassLoader cl) throws IOException {
43          AnnClass annClass = new AnnClass(cl);
44          AnnReader cv = new AnnReader(annClass);
45          ClassReader r = new ClassReader(is);
46          r.accept(cv, ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);
47          return annClass;
48      }
49  
50      public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
51          annClass.setName(name);
52          annClass.setAccess(access);
53          annClass.setSuperName(superName);
54          annClass.setInterfaces(interfaces);
55      }
56  
57      public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
58          Ann ann = new Ann(desc);
59          annClass.addAnn(ann);
60          return new AnnAnnReader(ann);
61      }
62  
63      public FieldVisitor visitField(int access, final String name, final String desc, String signature, Object value) {
64          final AnnField field = new AnnField(annClass, access, name, desc);
65          annClass.addField(field);
66          return new FieldVisitor(Opcodes.ASM9) {
67  
68              public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
69                  Ann ann = new Ann(desc);
70                  field.addAnn(ann);
71                  return new AnnAnnReader(ann);
72              }
73          };
74      }
75  
76      public MethodVisitor visitMethod(
77              int access, final String mname, final String mdesc, String signature, String[] exceptions) {
78          final AnnMethod method = new AnnMethod(annClass, access, mname, mdesc);
79          annClass.addMethod(method);
80  
81          return new MethodVisitor(Opcodes.ASM9) {
82  
83              public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
84                  Ann ann = new Ann(desc);
85                  method.addAnn(ann);
86                  return new AnnAnnReader(ann);
87              }
88  
89              public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
90                  Ann ann = new Ann(desc);
91                  method.addParamAnn(parameter, ann);
92                  return new AnnAnnReader(ann);
93              }
94          };
95      }
96  
97      static class AnnAnnReader extends AnnotationVisitor {
98          private Ann ann;
99  
100         public AnnAnnReader(Ann ann) {
101             super(Opcodes.ASM9);
102             this.ann = ann;
103         }
104 
105         public void visit(String name, Object value) {
106             ann.addParam(name, value);
107         }
108 
109         public void visitEnum(String name, String desc, String value) {
110             ann.addParam(name, new AnnEnum(desc, value));
111         }
112 
113         public AnnotationVisitor visitAnnotation(String name, String desc) {
114             Ann ann = new Ann(desc);
115             this.ann.addParam(name, ann);
116             return new AnnAnnReader(ann);
117         }
118 
119         public AnnotationVisitor visitArray(String name) {
120             return new AnnAnnArrayReader(ann, name);
121         }
122     }
123 
124     static class AnnAnnArrayReader extends AnnotationVisitor {
125 
126         private Ann ann;
127 
128         private String name;
129 
130         // TODO good enough for now, but does not cover general case
131         private ArrayList<String> array = new ArrayList<String>();
132 
133         public AnnAnnArrayReader(Ann ann, String name) {
134             super(Opcodes.ASM9);
135             this.ann = ann;
136             this.name = name;
137         }
138 
139         public void visit(String name, Object value) {
140             if (value instanceof String) {
141                 array.add((String) value);
142             }
143         }
144 
145         public void visitEnd() {
146             ann.addParam(name, array.toArray(new String[array.size()]));
147         }
148     }
149 }