1 30 package org.objectweb.asm.util; 31 32 import java.util.HashMap ; 33 34 import org.objectweb.asm.AnnotationVisitor; 35 import org.objectweb.asm.Attribute; 36 import org.objectweb.asm.Type; 37 import org.objectweb.asm.util.attrs.ASMifiable; 38 39 44 public class ASMifierAbstractVisitor extends AbstractVisitor { 45 46 49 protected String name; 50 51 55 HashMap labelNames; 56 57 63 protected ASMifierAbstractVisitor(final String name) { 64 this.name = name; 65 } 66 67 74 public AnnotationVisitor visitAnnotation( 75 final String desc, 76 final boolean visible) 77 { 78 buf.setLength(0); 79 buf.append("{\n") 80 .append("av0 = ") 81 .append(name) 82 .append(".visitAnnotation("); 83 appendConstant(desc); 84 buf.append(", ").append(visible).append(");\n"); 85 text.add(buf.toString()); 86 ASMifierAnnotationVisitor av = new ASMifierAnnotationVisitor(0); 87 text.add(av.getText()); 88 text.add("}\n"); 89 return av; 90 } 91 92 97 public void visitAttribute(final Attribute attr) { 98 buf.setLength(0); 99 if (attr instanceof ASMifiable) { 100 buf.append("{\n"); 101 buf.append("// ATTRIBUTE\n"); 102 ((ASMifiable) attr).asmify(buf, "attr", labelNames); 103 buf.append(name).append(".visitAttribute(attr);\n"); 104 buf.append("}\n"); 105 } else { 106 buf.append("// WARNING! skipped a non standard attribute of type \""); 107 buf.append(attr.type).append("\"\n"); 108 } 109 text.add(buf.toString()); 110 } 111 112 115 public void visitEnd() { 116 buf.setLength(0); 117 buf.append(name).append(".visitEnd();\n"); 118 text.add(buf.toString()); 119 } 120 121 128 void appendConstant(final Object cst) { 129 appendConstant(buf, cst); 130 } 131 132 140 static void appendConstant(final StringBuffer buf, final Object cst) { 141 if (cst == null) { 142 buf.append("null"); 143 } else if (cst instanceof String ) { 144 appendString(buf, (String ) cst); 145 } else if (cst instanceof Type) { 146 buf.append("Type.getType(\""); 147 buf.append(((Type) cst).getDescriptor()); 148 buf.append("\")"); 149 } else if (cst instanceof Byte ) { 150 buf.append("new Byte((byte)").append(cst).append(")"); 151 } else if (cst instanceof Boolean ) { 152 buf.append("new Boolean(").append(cst).append(")"); 153 } else if (cst instanceof Short ) { 154 buf.append("new Short((short)").append(cst).append(")"); 155 } else if (cst instanceof Character ) { 156 int c = ((Character ) cst).charValue(); 157 buf.append("new Character((char)").append(c).append(")"); 158 } else if (cst instanceof Integer ) { 159 buf.append("new Integer(").append(cst).append(")"); 160 } else if (cst instanceof Float ) { 161 buf.append("new Float(\"").append(cst).append("\")"); 162 } else if (cst instanceof Long ) { 163 buf.append("new Long(").append(cst).append("L)"); 164 } else if (cst instanceof Double ) { 165 buf.append("new Double(\"").append(cst).append("\")"); 166 } else if (cst instanceof byte[]) { 167 byte[] v = (byte[]) cst; 168 buf.append("new byte[] {"); 169 for (int i = 0; i < v.length; i++) { 170 buf.append(i == 0 ? "" : ",").append(v[i]); 171 } 172 buf.append("}"); 173 } else if (cst instanceof boolean[]) { 174 boolean[] v = (boolean[]) cst; 175 buf.append("new boolean[] {"); 176 for (int i = 0; i < v.length; i++) { 177 buf.append(i == 0 ? "" : ",").append(v[i]); 178 } 179 buf.append("}"); 180 } else if (cst instanceof short[]) { 181 short[] v = (short[]) cst; 182 buf.append("new short[] {"); 183 for (int i = 0; i < v.length; i++) { 184 buf.append(i == 0 ? "" : ",").append("(short)").append(v[i]); 185 } 186 buf.append("}"); 187 } else if (cst instanceof char[]) { 188 char[] v = (char[]) cst; 189 buf.append("new char[] {"); 190 for (int i = 0; i < v.length; i++) { 191 buf.append(i == 0 ? "" : ",") 192 .append("(char)") 193 .append((int) v[i]); 194 } 195 buf.append("}"); 196 } else if (cst instanceof int[]) { 197 int[] v = (int[]) cst; 198 buf.append("new int[] {"); 199 for (int i = 0; i < v.length; i++) { 200 buf.append(i == 0 ? "" : ",").append(v[i]); 201 } 202 buf.append("}"); 203 } else if (cst instanceof long[]) { 204 long[] v = (long[]) cst; 205 buf.append("new long[] {"); 206 for (int i = 0; i < v.length; i++) { 207 buf.append(i == 0 ? "" : ",").append(v[i]).append("L"); 208 } 209 buf.append("}"); 210 } else if (cst instanceof float[]) { 211 float[] v = (float[]) cst; 212 buf.append("new float[] {"); 213 for (int i = 0; i < v.length; i++) { 214 buf.append(i == 0 ? "" : ",").append(v[i]).append("f"); 215 } 216 buf.append("}"); 217 } else if (cst instanceof double[]) { 218 double[] v = (double[]) cst; 219 buf.append("new double[] {"); 220 for (int i = 0; i < v.length; i++) { 221 buf.append(i == 0 ? "" : ",").append(v[i]).append("d"); 222 } 223 buf.append("}"); 224 } 225 } 226 } 227 | Popular Tags |