1 30 package org.objectweb.asm.test.cases; 31 32 import java.io.IOException ; 33 34 import org.objectweb.asm.ClassWriter; 35 import org.objectweb.asm.FieldVisitor; 36 import org.objectweb.asm.MethodVisitor; 37 import org.objectweb.asm.Type; 38 39 44 public class Enum extends Generator { 45 46 public void generate(final String dir) throws IOException { 47 generate(dir, "pkg/Enum.class", dump()); 48 } 49 50 public byte[] dump() { 51 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); 52 FieldVisitor fv; 53 MethodVisitor mv; 54 55 cw.visit(V1_5, 56 ACC_PUBLIC + ACC_FINAL + ACC_SUPER + ACC_ENUM, 57 "pkg/Enum", 58 "Ljava/lang/Enum<Lpkg/Enum;>;", 59 "java/lang/Enum", 60 null); 61 62 fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM, 63 "V0", 64 "Lpkg/Enum;", 65 null, 66 null); 67 fv.visitEnd(); 68 69 fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM, 70 "V1", 71 "Lpkg/Enum;", 72 null, 73 null); 74 fv.visitEnd(); 75 76 fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM, 77 "V2", 78 "Lpkg/Enum;", 79 null, 80 null); 81 fv.visitEnd(); 82 83 fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC + ACC_SYNTHETIC, 84 "$VALUES", 85 "[Lpkg/Enum;", 86 null, 87 null); 88 fv.visitEnd(); 89 90 mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, 91 "values", 92 "()[Lpkg/Enum;", 93 null, 94 null); 95 mv.visitCode(); 96 mv.visitFieldInsn(GETSTATIC, "pkg/Enum", "$VALUES", "[Lpkg/Enum;"); 97 mv.visitMethodInsn(INVOKEVIRTUAL, 98 "[Lpkg/Enum;", 99 "clone", 100 "()Ljava/lang/Object;"); 101 mv.visitTypeInsn(CHECKCAST, "[Lpkg/Enum;"); 102 mv.visitInsn(ARETURN); 103 mv.visitMaxs(0, 0); 104 mv.visitEnd(); 105 106 mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, 107 "valueOf", 108 "(Ljava/lang/String;)Lpkg/Enum;", 109 null, 110 null); 111 mv.visitCode(); 112 mv.visitLdcInsn(Type.getType("Lpkg/Enum;")); 113 mv.visitVarInsn(ALOAD, 0); 114 mv.visitMethodInsn(INVOKESTATIC, 115 "java/lang/Enum", 116 "valueOf", 117 "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;"); 118 mv.visitTypeInsn(CHECKCAST, "pkg/Enum"); 119 mv.visitInsn(ARETURN); 120 mv.visitMaxs(0, 0); 121 mv.visitEnd(); 122 123 mv = cw.visitMethod(ACC_PRIVATE, 124 "<init>", 125 "(Ljava/lang/String;I)V", 126 "()V", 127 null); 128 mv.visitCode(); 129 mv.visitVarInsn(ALOAD, 0); 130 mv.visitVarInsn(ALOAD, 1); 131 mv.visitVarInsn(ILOAD, 2); 132 mv.visitMethodInsn(INVOKESPECIAL, 133 "java/lang/Enum", 134 "<init>", 135 "(Ljava/lang/String;I)V"); 136 mv.visitInsn(RETURN); 137 mv.visitMaxs(0, 0); 138 mv.visitEnd(); 139 140 mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); 141 mv.visitCode(); 142 mv.visitTypeInsn(NEW, "pkg/Enum"); 143 mv.visitInsn(DUP); 144 mv.visitLdcInsn("V0"); 145 mv.visitInsn(ICONST_0); 146 mv.visitMethodInsn(INVOKESPECIAL, 147 "pkg/Enum", 148 "<init>", 149 "(Ljava/lang/String;I)V"); 150 mv.visitFieldInsn(PUTSTATIC, "pkg/Enum", "V0", "Lpkg/Enum;"); 151 mv.visitTypeInsn(NEW, "pkg/Enum"); 152 mv.visitInsn(DUP); 153 mv.visitLdcInsn("V1"); 154 mv.visitInsn(ICONST_1); 155 mv.visitMethodInsn(INVOKESPECIAL, 156 "pkg/Enum", 157 "<init>", 158 "(Ljava/lang/String;I)V"); 159 mv.visitFieldInsn(PUTSTATIC, "pkg/Enum", "V1", "Lpkg/Enum;"); 160 mv.visitTypeInsn(NEW, "pkg/Enum"); 161 mv.visitInsn(DUP); 162 mv.visitLdcInsn("V2"); 163 mv.visitInsn(ICONST_2); 164 mv.visitMethodInsn(INVOKESPECIAL, 165 "pkg/Enum", 166 "<init>", 167 "(Ljava/lang/String;I)V"); 168 mv.visitFieldInsn(PUTSTATIC, "pkg/Enum", "V2", "Lpkg/Enum;"); 169 mv.visitInsn(ICONST_3); 170 mv.visitTypeInsn(ANEWARRAY, "pkg/Enum"); 171 mv.visitInsn(DUP); 172 mv.visitInsn(ICONST_0); 173 mv.visitFieldInsn(GETSTATIC, "pkg/Enum", "V0", "Lpkg/Enum;"); 174 mv.visitInsn(AASTORE); 175 mv.visitInsn(DUP); 176 mv.visitInsn(ICONST_1); 177 mv.visitFieldInsn(GETSTATIC, "pkg/Enum", "V1", "Lpkg/Enum;"); 178 mv.visitInsn(AASTORE); 179 mv.visitInsn(DUP); 180 mv.visitInsn(ICONST_2); 181 mv.visitFieldInsn(GETSTATIC, "pkg/Enum", "V2", "Lpkg/Enum;"); 182 mv.visitInsn(AASTORE); 183 mv.visitFieldInsn(PUTSTATIC, "pkg/Enum", "$VALUES", "[Lpkg/Enum;"); 184 mv.visitInsn(RETURN); 185 mv.visitMaxs(0, 0); 186 mv.visitEnd(); 187 188 cw.visitEnd(); 189 190 return cw.toByteArray(); 191 } 192 } 193 | Popular Tags |