Coverage Report - org.codehaus.plexus.metadata.ann.AnnReader
 
Classes in this File Line Coverage Branch Coverage Complexity
AnnReader
100 %
22/22
N/A
1,059
AnnReader$1
100 %
4/4
N/A
1,059
AnnReader$2
14 %
1/7
N/A
1,059
AnnReader$AnnAnnArrayReader
100 %
10/10
50 %
1/2
1,059
AnnReader$AnnAnnReader
54 %
6/11
N/A
1,059
 
 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  1
     super(Opcodes.ASM5);
 39  1
     this.annClass = annClass;
 40  1
   }
 41  
 
 42  
   public static AnnClass read(InputStream is, ClassLoader cl) throws IOException {
 43  1
     AnnClass annClass = new AnnClass(cl);
 44  1
     AnnReader cv = new AnnReader(annClass);
 45  1
     ClassReader r = new ClassReader(is);
 46  1
     r.accept(cv, ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);
 47  1
     return annClass;
 48  
   }
 49  
 
 50  
   public void visit(int version, int access, String name, String signature,
 51  
       String superName, String[] interfaces) {
 52  1
     annClass.setName(name);
 53  1
     annClass.setAccess(access);
 54  1
     annClass.setSuperName(superName);
 55  1
     annClass.setInterfaces(interfaces);
 56  1
   }
 57  
   
 58  
   public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
 59  1
     Ann ann = new Ann(desc);
 60  1
     annClass.addAnn(ann);
 61  1
     return new AnnAnnReader(ann);
 62  
   }
 63  
   
 64  
   public FieldVisitor visitField(int access, final String name, final String desc, String signature, Object value) {
 65  2
     final AnnField field = new AnnField(annClass, access, name, desc);
 66  2
     annClass.addField(field);
 67  2
     return new FieldVisitor(Opcodes.ASM5) {
 68  
 
 69  
       public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
 70  3
         Ann ann = new Ann(desc);
 71  3
         field.addAnn(ann);
 72  3
         return new AnnAnnReader(ann);
 73  
       }
 74  
     };
 75  
   }
 76  
 
 77  
   public MethodVisitor visitMethod(int access, final String mname, final String mdesc,
 78  
       String signature, String[] exceptions) {
 79  1
     final AnnMethod method = new AnnMethod(annClass, access, mname, mdesc);
 80  1
     annClass.addMethod(method);
 81  
     
 82  1
     return new MethodVisitor(Opcodes.ASM5) {
 83  
 
 84  
       public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
 85  0
         Ann ann = new Ann(desc);
 86  0
         method.addAnn(ann);
 87  0
         return new AnnAnnReader(ann);
 88  
       }
 89  
 
 90  
       public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
 91  0
         Ann ann = new Ann(desc);
 92  0
         method.addParamAnn(parameter, ann);
 93  0
         return new AnnAnnReader(ann);
 94  
       }
 95  
     };
 96  
   }
 97  
 
 98  
   static class AnnAnnReader extends AnnotationVisitor {
 99  
     private Ann ann;
 100  
 
 101  
     public AnnAnnReader(Ann ann) {
 102  4
       super(Opcodes.ASM5);
 103  4
       this.ann = ann;
 104  4
     }
 105  
 
 106  
     public void visit(String name, Object value) {
 107  6
       ann.addParam(name, value);
 108  6
     }
 109  
 
 110  
     public void visitEnum(String name, String desc, String value) {
 111  0
       ann.addParam(name, new AnnEnum(desc, value));
 112  0
     }
 113  
     
 114  
     public AnnotationVisitor visitAnnotation(String name, String desc) {
 115  0
       Ann ann = new Ann(desc);
 116  0
       this.ann.addParam(name, ann);
 117  0
       return new AnnAnnReader(ann);
 118  
     }
 119  
 
 120  
     public AnnotationVisitor visitArray(String name) {
 121  1
       return new AnnAnnArrayReader(ann, name);
 122  
     }
 123  
   }
 124  
   
 125  
   static class AnnAnnArrayReader extends AnnotationVisitor {
 126  
 
 127  
     private Ann ann;
 128  
 
 129  
     private String name;
 130  
 
 131  
     // TODO good enough for now, but does not cover general case
 132  1
     private ArrayList<String> array = new ArrayList<String>();
 133  
 
 134  
     public AnnAnnArrayReader(Ann ann, String name) {
 135  1
       super(Opcodes.ASM5);
 136  1
       this.ann = ann;
 137  1
       this.name = name;
 138  1
     }
 139  
 
 140  
     public void visit(String name, Object value) {
 141  3
       if(value instanceof String) {
 142  3
         array.add((String) value);
 143  
       }
 144  3
     }
 145  
 
 146  
     public void visitEnd() {
 147  1
       ann.addParam(name, array.toArray(new String[array.size()]));
 148  1
     }
 149  
   }
 150  
 }