private FieldVisitor declareStaticField(ClassVisitor visitor, String name, Class<?> fieldClass) { return visitor.visitField( ACC_PRIVATE | ACC_FINAL | ACC_STATIC | ACC_SYNTHETIC, name, Type.getDescriptor(fieldClass), null, null); }
@Override public void visitEnd() { // add fields for (ThreadLocalVar tlv : threadLocalVars) { super.visitField(Opcodes.ACC_PUBLIC, tlv.getName(), tlv.getTypeAsDesc(), null, null); } super.visitEnd(); }
public void visitField(FieldNode fieldNode) { cv.visitField( fieldNode.getModifiers(), fieldNode.getName(), BytecodeHelper.getTypeDescription(fieldNode.getType()), null, // fieldValue, //br all the sudden that one cannot init the field here. init is done // in static initializer and instance initializer. null); }
public String getNewStaticConstant(String type, String name_prefix) { ClassVisitor cv = getClassVisitor(); String realName; synchronized (this) { realName = "__" + constants++; } // declare the field cv.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, realName, type, null, null).visitEnd(); return realName; }
public String getNewField(String type, String name, Object init) { ClassVisitor cv = getClassVisitor(); // declare the field cv.visitField(ACC_PRIVATE, name, type, null, null).visitEnd(); if (init != null) { initMethod.aload(THIS); initMethod.ldc(init); initMethod.putfield(getClassname(), name, type); } return name; }
public String getNewConstant(String type, String name_prefix, Object init) { ClassVisitor cv = getClassVisitor(); String realName = getNewConstantName(); // declare the field cv.visitField(ACC_PRIVATE, realName, type, null, null).visitEnd(); if (init != null) { initMethod.aload(THIS); initMethod.ldc(init); initMethod.putfield(getClassname(), realName, type); } return realName; }
public void test() throws Exception { ClassWriter cw = new ClassWriter(true); ClassVisitor cv = new StaticInitMerger("$clinit$", cw); cv.visit(V1_1, ACC_PUBLIC, "A", null, "java/lang/Object", null); cv.visitField(ACC_PUBLIC + ACC_STATIC, "counter", "I", null, null); for (int i = 0; i < 5; ++i) { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<clinit>", "()V", null, null); mv.visitFieldInsn(GETSTATIC, "A", "counter", "I"); mv.visitInsn(ICONST_1); mv.visitInsn(IADD); mv.visitFieldInsn(PUTSTATIC, "A", "counter", "I"); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); } MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); cv.visitEnd(); Class c = LOADER.defineClass("A", cw.toByteArray()); assertEquals(c.getField("counter").getInt(c.newInstance()), 5); }
public void accept(ClassVisitor cv) { cv.visitField(access, name, type.getDescriptor(), null /*generics*/, null /*value*/); }
public static byte[] generate(EventClassModel model) { ClassWriter cwriter = new ClassWriter(ClassWriter.COMPUTE_MAXS); ClassVisitor cw; if (TRACE) { cw = new TraceClassVisitor(cwriter, new ASMifier(), new PrintWriter(System.out)); cw = new CheckClassAdapter(cw); } else { cw = cwriter; } FieldVisitor fv; MethodVisitor mv; String concreteClassName = model.getRootPackageInternalPrefix() + "event/" + model.technicalNameCapitalized + "Event"; String className = model.isCustomized ? model.getRootPackageInternalPrefix() + "event/Abstract" + model.technicalNameCapitalized + "Event" : concreteClassName; cw.visit( V1_7, ACC_PUBLIC + ACC_SUPER + (model.isCustomized ? ACC_ABSTRACT : 0), className, null, "org/instantlogic/interaction/flow/impl/SimpleFlowEvent", null); // public static final HomeEvent { fv = cw.visitField( ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "INSTANCE", "L" + concreteClassName + ";", null, null); fv.visitEnd(); } { mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode(); // INSTANCE = new HomeEvent(); mv.visitTypeInsn(NEW, concreteClassName); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, concreteClassName, "<init>", "()V"); mv.visitFieldInsn(PUTSTATIC, className, "INSTANCE", "L" + concreteClassName + ";"); mv.visitInsn(RETURN); mv.visitMaxs(9, 99); mv.visitEnd(); } // Default synthetic constructor { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn(model.name); mv.visitIntInsn(BIPUSH, model.parameters.size()); mv.visitTypeInsn(ANEWARRAY, "org/instantlogic/fabric/model/Entity"); int i = 0; for (String parameter : model.parameters) { mv.visitInsn(DUP); mv.visitIntInsn(BIPUSH, i); mv.visitFieldInsn( GETSTATIC, model.getRootPackageInternalPrefix() + "entity/" + parameter + "Entity", "INSTANCE", "L" + model.getRootPackageInternalPrefix() + "entity/" + parameter + "Entity;"); mv.visitInsn(AASTORE); i++; } mv.visitMethodInsn( INVOKESPECIAL, "org/instantlogic/interaction/flow/impl/SimpleFlowEvent", "<init>", "(Ljava/lang/String;[Lorg/instantlogic/fabric/model/Entity;)V"); mv.visitInsn(RETURN); mv.visitMaxs(9, 99); mv.visitEnd(); } cw.visitEnd(); return cwriter.toByteArray(); }
private void declareField(ClassVisitor visitor, String name, Class<?> fieldClass) { visitor.visitField( ACC_PRIVATE | ACC_FINAL | ACC_SYNTHETIC, name, Type.getDescriptor(fieldClass), null, null); }