1 15 16 package javassist.bytecode.annotation; 17 18 import javassist.bytecode.ConstPool; 19 import javassist.bytecode.Descriptor; 20 import javassist.CtClass; 21 import javassist.CtMethod; 22 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 import java.util.Set ; 26 import java.util.Iterator ; 27 28 41 public class Annotation { 42 static class Pair { 43 int name; 44 MemberValue value; 45 } 46 47 ConstPool pool; 48 int typeIndex; 49 HashMap members; 52 64 public Annotation(int type, ConstPool cp) { 65 pool = cp; 66 typeIndex = type; 67 members = null; 68 } 69 70 79 public Annotation(String typeName, ConstPool cp) { 80 this(cp.addUtf8Info(Descriptor.of(typeName)), cp); 81 } 82 83 91 public Annotation(ConstPool cp, CtClass clazz) 92 throws javassist.NotFoundException 93 { 94 this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); 96 97 if (!clazz.isInterface()) 98 throw new RuntimeException ( 99 "Only interfaces are allowed for Annotation creation."); 100 101 CtMethod methods[] = clazz.getDeclaredMethods(); 102 if (methods.length > 0) 103 members = new HashMap (); 104 105 for (int i = 0; i < methods.length; i++) { 106 CtClass returnType = methods[i].getReturnType(); 107 addMemberValue(methods[i].getName(), 108 createMemberValue(cp, returnType)); 109 } 110 } 111 112 private MemberValue createMemberValue(ConstPool cp, CtClass returnType) 113 throws javassist.NotFoundException 114 { 115 if (returnType == CtClass.booleanType) 116 return new BooleanMemberValue(cp); 117 else if (returnType == CtClass.byteType) 118 return new ByteMemberValue(cp); 119 else if (returnType == CtClass.charType) 120 return new CharMemberValue(cp); 121 else if (returnType == CtClass.shortType) 122 return new ShortMemberValue(cp); 123 else if (returnType == CtClass.intType) 124 return new IntegerMemberValue(cp); 125 else if (returnType == CtClass.longType) 126 return new LongMemberValue(cp); 127 else if (returnType == CtClass.floatType) 128 return new FloatMemberValue(cp); 129 else if (returnType == CtClass.doubleType) 130 return new DoubleMemberValue(cp); 131 else if (returnType.getName().equals("java.lang.Class")) 132 return new ClassMemberValue(cp); 133 else if (returnType.getName().equals("java.lang.String")) 134 return new StringMemberValue(cp); 135 else if (returnType.isArray()) { 136 CtClass arrayType = returnType.getComponentType(); 137 MemberValue type = createMemberValue(cp, arrayType); 138 return new ArrayMemberValue(type, cp); 139 } 140 else if (returnType.isInterface()) { 141 Annotation info = new Annotation(cp, returnType); 142 return new AnnotationMemberValue(info, cp); 143 } 144 else { 145 EnumMemberValue emv = new EnumMemberValue(cp); 149 emv.setType(returnType.getName()); 150 return emv; 151 } 152 } 153 154 163 public void addMemberValue(int nameIndex, MemberValue value) { 164 Pair p = new Pair(); 165 p.name = nameIndex; 166 p.value = value; 167 addMemberValue(p); 168 } 169 170 176 public void addMemberValue(String name, MemberValue value) { 177 Pair p = new Pair(); 178 p.name = pool.addUtf8Info(name); 179 p.value = value; 180 if (members == null) 181 members = new HashMap (); 182 183 members.put(name, p); 184 } 185 186 private void addMemberValue(Pair pair) { 187 String name = pool.getUtf8Info(pair.name); 188 if (members == null) 189 members = new HashMap (); 190 191 members.put(name, pair); 192 } 193 194 197 public String toString() { 198 StringBuffer buf = new StringBuffer ("@"); 199 buf.append(getTypeName()); 200 if (members != null) { 201 buf.append("("); 202 Iterator mit = members.keySet().iterator(); 203 while (mit.hasNext()) { 204 String name = (String )mit.next(); 205 buf.append(name).append("=").append(getMemberValue(name)); 206 if (mit.hasNext()) 207 buf.append(", "); 208 } 209 buf.append(")"); 210 } 211 212 return buf.toString(); 213 } 214 215 218 public String getTypeName() { 219 return Descriptor.toClassName(pool.getUtf8Info(typeIndex)); 220 } 221 222 227 public Set getMemberNames() { 228 if (members == null) 229 return null; 230 else 231 return members.keySet(); 232 } 233 234 239 public MemberValue getMemberValue(String name) { 240 if (members == null) 241 return null; 242 else { 243 Pair p = (Pair)members.get(name); 244 if (p == null) 245 return null; 246 else 247 return p.value; 248 } 249 } 250 251 256 public void write(AnnotationsWriter writer) throws IOException { 257 if (members == null) { 258 writer.annotation(typeIndex, 0); 259 return; 260 } 261 262 writer.annotation(typeIndex, members.size()); 263 Iterator it = members.values().iterator(); 264 while (it.hasNext()) { 265 Pair pair = (Pair)it.next(); 266 writer.memberValuePair(pair.name); 267 pair.value.write(writer); 268 } 269 } 270 } 271 | Popular Tags |