1 25 26 package org.netbeans.modules.classfile; 27 28 import java.io.*; 29 import java.util.*; 30 31 36 public class Annotation { 37 ClassName type; 38 AnnotationComponent[] components; 39 boolean runtimeVisible; 40 41 45 static void load(DataInputStream in, ConstantPool pool, 46 boolean visible, Map<ClassName,Annotation> map) throws IOException { 47 int nattrs = in.readUnsignedShort(); 48 for (int i = 0; i < nattrs; i++) { 49 Annotation ann = loadAnnotation(in, pool, visible); 50 map.put(ann.getType(), ann); 51 } 52 } 53 54 static Annotation loadAnnotation(DataInputStream in, ConstantPool pool, 55 boolean visible) throws IOException { 56 final ClassName type; 57 CPEntry entry = pool.get(in.readUnsignedShort()); 58 if (entry.getTag() == ConstantPool.CONSTANT_Class) 59 type = ((CPClassInfo)entry).getClassName(); 61 else { 62 String s = ((CPName)entry).getName(); 63 type = ClassName.getClassName(s); 64 } 65 int npairs = in.readUnsignedShort(); 66 List<AnnotationComponent> pairList = new ArrayList<AnnotationComponent>(); 67 for (int j = 0; j < npairs; j++) 68 pairList.add(AnnotationComponent.load(in, pool, visible)); 69 AnnotationComponent[] acs = 70 new AnnotationComponent[pairList.size()]; 71 pairList.toArray(acs); 72 return new Annotation(pool, type, acs, visible); 73 } 74 75 Annotation(ConstantPool pool, ClassName type, 76 AnnotationComponent[] components, boolean runtimeVisible) { 77 this.type = type; 78 this.components = components; 79 this.runtimeVisible = runtimeVisible; 80 } 81 82 85 public final ClassName getType() { 86 return type; 87 } 88 89 93 public final AnnotationComponent[] getComponents() { 94 return components.clone(); 95 } 96 97 101 public final AnnotationComponent getComponent(String name) { 102 for (int i = 0; i < components.length; i++) { 103 AnnotationComponent comp = components[i]; 104 if (comp.getName().equals(name)) 105 return comp; 106 } 107 return null; 108 } 109 110 114 public boolean isRuntimeVisible() { 115 return runtimeVisible; 116 } 117 118 public String toString() { 119 StringBuffer sb = new StringBuffer ("@"); 120 sb.append(type); 121 sb.append(" runtimeVisible="); 122 sb.append(runtimeVisible); 123 int n = components.length; 124 if (n > 0) { 125 sb.append(" { "); 126 for (int i = 0; i < n; i++) { 127 sb.append(components[i]); 128 if (i < n - 1) 129 sb.append(", "); 130 } 131 sb.append(" }"); 132 } 133 return sb.toString(); 134 } 135 } 136 | Popular Tags |