1 30 31 package oracle.toplink.libraries.asm.util.attrs; 32 33 import java.util.List ; 34 import java.util.Map ; 35 36 import oracle.toplink.libraries.asm.Attribute; 37 import oracle.toplink.libraries.asm.ClassReader; 38 import oracle.toplink.libraries.asm.Label; 39 import oracle.toplink.libraries.asm.Type; 40 import oracle.toplink.libraries.asm.attrs.Annotation; 41 import oracle.toplink.libraries.asm.attrs.AnnotationDefaultAttribute; 42 import oracle.toplink.libraries.asm.attrs.Annotation.EnumConstValue; 43 44 49 50 public class ASMAnnotationDefaultAttribute extends AnnotationDefaultAttribute 51 implements ASMifiable 52 { 53 54 protected Attribute read (ClassReader cr, int off, 55 int len, char[] buf, int codeOff, Label[] labels) 56 { 57 AnnotationDefaultAttribute attr = 58 (AnnotationDefaultAttribute)super.read( 59 cr, off, len, buf, codeOff, labels); 60 61 ASMAnnotationDefaultAttribute result = new ASMAnnotationDefaultAttribute(); 62 result.defaultValue = attr.defaultValue; 63 return result; 64 } 65 66 public void asmify (StringBuffer buf, String varName, Map labelNames) { 67 buf.append("AnnotationDefaultAttribute ").append(varName) 68 .append(" = new AnnotationDefaultAttribute();\n"); 69 String val = asmify(defaultValue, buf, varName + "Val"); 70 buf.append(varName).append(".defaultValue = ") 71 .append(val).append(";\n"); 72 } 73 74 static void asmifyAnnotations (StringBuffer buf, String varName, List annotations) { 75 if (annotations.size() > 0) { 76 buf.append("{\n"); 77 for (int i = 0; i < annotations.size(); i++) { 78 String val = varName + "ann" + i; 79 asmify((Annotation)annotations.get(i), buf, val); 80 buf.append(varName).append(".annotations.add( ").append(val).append(");\n"); 81 } 82 buf.append("}\n"); 83 } 84 } 85 86 static void asmifyParameterAnnotations (StringBuffer buf, String varName, List parameters) { 87 if (parameters.size() > 0) { 88 buf.append("{\n"); 89 for (int i = 0; i < parameters.size(); i++) { 90 String val = varName + "param" + i; 91 buf.append( "List ").append( val).append( " = new ArrayList();\n"); 92 List annotations = (List )parameters.get(i); 93 if (annotations.size() > 0) { 94 buf.append("{\n"); 95 for (int i1 = 0; i1 < annotations.size(); i1++) { 96 String val1 = val + "ann" + i1; 97 asmify((Annotation)annotations.get(i1), buf, val1); 98 buf.append(val).append(".add( ").append(val1).append(");\n"); 99 } 100 buf.append("}\n"); 101 } 102 buf.append(varName).append(".parameters.add( ").append(val).append(");\n\n"); 103 } 104 buf.append("}\n"); 105 } 106 } 107 108 static String asmify (Annotation a, StringBuffer buf, String varName) { 109 buf.append("Annotation ").append(varName) 110 .append(" = new Annotation(\"").append(a.type).append("\");\n"); 111 List elementValues = a.elementValues; 112 if (elementValues.size() > 0) { 113 for (int i = 0; i < elementValues.size(); i++) { 115 Object [] values = (Object [])elementValues.get(i); 116 String val = asmify(values[1], buf, varName + "val" + i); 117 buf.append(varName).append(".add( \"") 118 .append(values[0]).append("\", ").append(val).append(");\n"); 119 } 120 } 122 return varName; 123 } 124 125 static String asmify (Object value, StringBuffer buf, String valName) { 126 if (value instanceof String ) { 127 return "\""+value+"\""; 128 129 } else if (value instanceof Integer ) { 130 return "new Integer("+value+")"; 131 132 } else if (value instanceof Byte ) { 133 return "new Byte((byte)"+value+")"; 134 135 } else if (value instanceof Character ) { 136 return "new Character((char)"+((int)((Character )value).charValue())+")"; 137 138 } else if (value instanceof Double ) { 139 return "new Double(\""+value+"\")"; 140 141 } else if (value instanceof Float ) { 142 return "new Float(\""+value+"\")"; 143 144 } else if (value instanceof Long ) { 145 return "new Long("+value+"L)"; 146 147 } else if (value instanceof Short ) { 148 return "new Short((short)"+value+")"; 149 150 } else if (value instanceof Boolean ) { 151 return "new Boolean("+value+")"; 152 153 } else if (value instanceof EnumConstValue) { 154 EnumConstValue e = (EnumConstValue) value; 155 return "new Annotation.EnumConstValue(\""+e.typeName+"\", \""+e.constName+"\")"; 156 157 } else if (value instanceof Type) { 158 Type t = (Type)value; 159 return "Type.getType(\""+t.getDescriptor()+"\")"; 160 161 } else if (value instanceof Annotation) { 162 return asmify((Annotation)value, buf, valName); 163 164 } else if (value instanceof Object []) { 165 Object [] v = (Object [])value; 166 buf.append("Object[] ").append(valName) 167 .append(" = new Object[").append(v.length).append("];\n"); 168 for (int i = 0; i < v.length; i++) { 169 String val = asmify(v[i], buf, valName+"Arr"+i); 170 buf.append(valName+"["+i+"] = ").append(val).append(";\n"); 171 } 172 return valName; 173 174 } else if( value instanceof byte[]) { 175 byte[] v = (byte[])value; 176 StringBuffer sb = new StringBuffer ( "new byte[] {"); 177 String sep = ""; 178 for (int i = 0; i < v.length; i++) { 179 sb.append(sep).append(v[i]); 180 sep = ", "; 181 } 182 sb.append("}"); 183 return sb.toString(); 184 185 } else if( value instanceof char[]) { 186 char[] v = (char[])value; 187 StringBuffer sb = new StringBuffer ( "new char[] {"); 188 String sep = ""; 189 for (int i = 0; i < v.length; i++) { 190 sb.append(sep).append("(char)").append((int)v[i]); 191 sep = ", "; 192 } 193 sb.append("}"); 194 return sb.toString(); 195 196 } else if( value instanceof short[]) { 197 short[] v = (short[])value; 198 StringBuffer sb = new StringBuffer ( "new short[] {"); 199 String sep = ""; 200 for (int i = 0; i < v.length; i++) { 201 sb.append(sep).append("(short)").append(v[i]); 202 sep = ", "; 203 } 204 sb.append("}"); 205 return sb.toString(); 206 207 } else if( value instanceof long[]) { 208 long[] v = (long[])value; 209 StringBuffer sb = new StringBuffer ( "new long[] {"); 210 String sep = ""; 211 for (int i = 0; i < v.length; i++) { 212 sb.append(sep).append(v[i]).append("L"); 213 sep = ", "; 214 } 215 sb.append("}"); 216 return sb.toString(); 217 218 } else if( value instanceof int[]) { 219 int[] v = (int[])value; 220 StringBuffer sb = new StringBuffer ( "new int[] {"); 221 String sep = ""; 222 for (int i = 0; i < v.length; i++) { 223 sb.append(sep).append(v[i]); 224 sep = ", "; 225 } 226 sb.append("}"); 227 return sb.toString(); 228 229 } else if( value instanceof boolean[]) { 230 boolean[] v = (boolean[])value; 231 StringBuffer sb = new StringBuffer ( "new boolean[] {"); 232 String sep = ""; 233 for (int i = 0; i < v.length; i++) { 234 sb.append(sep).append(v[i]); 235 sep = ", "; 236 } 237 sb.append("}"); 238 return sb.toString(); 239 240 } else if( value instanceof float[]) { 241 float[] v = (float[])value; 242 StringBuffer sb = new StringBuffer ( "new float[] {"); 243 String sep = ""; 244 for (int i = 0; i < v.length; i++) { 245 sb.append(sep).append(v[i]).append("f"); 246 sep = ", "; 247 } 248 sb.append("}"); 249 return sb.toString(); 250 251 } else if( value instanceof double[]) { 252 double[] v = (double[])value; 253 StringBuffer sb = new StringBuffer ( "new double[] {"); 254 String sep = ""; 255 for (int i = 0; i < v.length; i++) { 256 sb.append(sep).append(v[i]).append("d"); 257 sep = ", "; 258 } 259 sb.append("}"); 260 return sb.toString(); 261 262 } else { 263 throw new IllegalArgumentException ( "Invalid value: "+value.getClass().getName()+" : "+value); 264 265 } 266 } 268 } 269 | Popular Tags |