List of usage examples for org.objectweb.asm MethodVisitor visitEnd
public void visitEnd()
From source file:blue.lapis.pore.event.PoreListenerGenerator.java
License:Open Source License
private static byte[] generate(String name, Class<?> pore, Class<?> sponge, EventPriority priority, Order order) {/*from ww w. j a v a 2s . c om*/ name = name.replace('.', '/'); String poreName = Type.getInternalName(pore); String spongeSignature = "(L" + Type.getInternalName(sponge) + ";)V"; ClassWriter cw = new ClassWriter(0); MethodVisitor mv; AnnotationVisitor av; cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, "java/lang/Object", null); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "on" + sponge.getSimpleName(), spongeSignature, null, null); { av = mv.visitAnnotation(ANNOTATION_DESCRIPTOR, true); av.visitEnum("order", ORDER_DESCRIPTOR, order.name()); av.visitEnd(); } mv.visitCode(); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "get", GET_CACHE, false); mv.visitTypeInsn(CHECKCAST, poreName); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); Label l0 = new Label(); mv.visitJumpInsn(IFNONNULL, l0); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(NEW, poreName); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, poreName, "<init>", spongeSignature, false); mv.visitInsn(DUP); mv.visitVarInsn(ASTORE, 2); mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "set", SET_CACHE, false); mv.visitLabel(l0); mv.visitFrame(F_APPEND, 1, new Object[] { poreName }, 0, null); mv.visitVarInsn(ALOAD, 2); mv.visitFieldInsn(GETSTATIC, PRIORITY_CLASS, priority.name(), PRIORITY_DESCRIPTOR); mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "call", CALL_EVENT, false); mv.visitInsn(RETURN); mv.visitMaxs(4, 3); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
From source file:bluejelly.PrimTransformer.java
License:BSD License
private void generate(PrimInfo pi) { for (int i = 0; i < pi.arity; i++) { Label start = new Label(); Label end = new Label(); String method = getMethodName(pi, i); String cont = getContName(pi, i); MethodVisitor v = cv.visitMethod(ACC_PUBLIC, method, DESC, null, null); generateAnn(v, pi, i);/* w ww. j av a2 s. c o m*/ v.visitCode(); v.visitLabel(start); if (i == 0) { v.visitIntInsn(ALOAD, 1); pushIntConst(v, pi.arity); v.visitMethodInsn(INVOKEVIRTUAL, CTX, "stackCheck", "(I)V"); } v.visitIntInsn(ALOAD, 1); pushIntConst(v, i << 1); v.visitLdcInsn(cont); v.visitMethodInsn(INVOKEVIRTUAL, CTX, "evalVar", "(ILjava/lang/String;)V"); v.visitLabel(end); v.visitInsn(RETURN); v.visitLocalVariable("ctx", Type.getDescriptor(ExecutionContext.class), null, start, end, 1); v.visitMaxs(3, 2); v.visitEnd(); } }
From source file:bluejelly.runtime.ModuleLoader.java
License:BSD License
/** * Generate a {@link Caf} subclass for the given method information. * /* w w w . j av a2 s .co m*/ * @param module module declaring the method to process * @param ci code info for the method to process * * @return * a {@link Caf} subclass whose <code>fastEnter</code> will call * module.ci.getMethod() with an {@link ExecutionContext} as argument. */ @SuppressWarnings("unchecked") private Class<Caf> generateCaf(Module module, CodeInfo ci) { String nodeName = this.genNodeName(ci); String cafName = Type.getInternalName(Caf.class); String moduleName = Type.getInternalName(module.getClass()); String methodName = ci.getMethod().getName(); ClassWriter cw = new ClassWriter(0); cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL, nodeName, null, cafName, null); { String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(Module.class), Type.getType(String.class) }); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", consDesc, null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKESPECIAL, cafName, "<init>", consDesc); mv.visitInsn(RETURN); mv.visitMaxs(3, 3); mv.visitEnd(); } this.generateFastEnter(cw, cafName, moduleName, methodName); cw.visitEnd(); byte[] b = cw.toByteArray(); Class<?> clazz = this.loader.defineClass(nodeName, b); return (Class<Caf>) clazz; }
From source file:bluejelly.runtime.ModuleLoader.java
License:BSD License
/** * Generate a {@link Code} subclass for the given method information. * // ww w. j a v a2 s . com * @param module module declaring the method to process * @param ci code info for the method to process * * @return * a {@link Code} subclass whose <code>fastEnter</code> will call * module.ci.getMethod() with an {@link ExecutionContext} as argument. */ @SuppressWarnings("unchecked") private Class<Code> generateCode(Module module, CodeInfo ci) { String nodeName = this.genNodeName(ci); String codeName = Type.getInternalName(Code.class); String moduleName = Type.getInternalName(module.getClass()); String methodName = ci.getMethod().getName(); ClassWriter cw = new ClassWriter(0); cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL, nodeName, null, codeName, null); { String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(int.class), Type.getType(Module.class), Type.getType(String.class) }); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", consDesc, null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ILOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKESPECIAL, codeName, "<init>", consDesc); mv.visitInsn(RETURN); mv.visitMaxs(4, 4); mv.visitEnd(); } this.generateFastEnter(cw, codeName, moduleName, methodName); cw.visitEnd(); byte[] b = cw.toByteArray(); Class<?> clazz = this.loader.defineClass(nodeName, b); return (Class<Code>) clazz; }
From source file:bluejelly.runtime.ModuleLoader.java
License:BSD License
/** * Override <code>fastEnter</code> method in class being visited * by <code>cw</code>, subclass of <code>superName</code>. Method * will call <code>methodName</code>. declared in class * <code>moduleName</code>. So , assuming class generated by * <code>cw</code> is named <code>X</code>, we will have: * //from ww w. j a v a 2s.co m * <p> * <code> * class X extends superName { * public void fastEnter(ExecutionContext ctx) { * ((moduleName)this.module).methodName(ctx); * } * } * </code> * * @param cw class writer generating class code * @param superName internal name of superclass: Code or Caf * @param moduleName internal name of module class * @param methodName name of method to invoke in module moduleName */ private void generateFastEnter(ClassWriter cw, String superName, String moduleName, String methodName) { String fieldDesc = Type.getDescriptor(Module.class); String methodDesc = "(" + Type.getDescriptor(ExecutionContext.class) + ")V"; MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "fastEnter", methodDesc, null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, superName, "module", fieldDesc); mv.visitTypeInsn(CHECKCAST, moduleName); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, moduleName, methodName, methodDesc); mv.visitInsn(RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); }
From source file:blusunrize.immersiveengineering.common.util.compat.jei.arcfurnace.ArcFurnaceRecipeWrapper.java
private static Class<? extends ArcFurnaceRecipeWrapper> createSubWrapper(String subtype) throws Exception { String entitySuperClassName = Type.getInternalName(ArcFurnaceRecipeWrapper.class); String entityProxySubClassName = ArcFurnaceRecipeWrapper.class.getSimpleName().concat(subtype); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, entityProxySubClassName, null, entitySuperClassName, null); cw.visitSource(entityProxySubClassName.concat(".java"), null); //create constructor String methodDescriptor = "(L" + Type.getInternalName(ArcFurnaceRecipe.class) + ";)V"; MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, null, null); mv.visitCode();/*from w ww .j a v a2 s . c o m*/ mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, entitySuperClassName, "<init>", methodDescriptor, false); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); cw.visitEnd(); return (Class<? extends ArcFurnaceRecipeWrapper>) new ProxyClassLoader( Thread.currentThread().getContextClassLoader(), cw.toByteArray()) .loadClass(entityProxySubClassName.replaceAll("/", ".")); }
From source file:boilerplate.processor.adapters.EqualsAdapter.java
License:Open Source License
private void addEqualsMethod(List<FieldInfo> fields) { // CODE: public boolean equals(Object obj) { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "equals", "(Ljava/lang/Object;)Z", null, null); mv.visitCode();/*from w ww .j ava2s . com*/ // CODE: if (this == obj) return true; mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); Label l0 = new Label(); mv.visitJumpInsn(IF_ACMPNE, l0); mv.visitInsn(ICONST_1); mv.visitInsn(IRETURN); mv.visitLabel(l0); mv.visitFrame(F_SAME, 0, null, 0, null); // CODE: if (!(obj instanceof Foo)) return false; mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(INSTANCEOF, typeInfo.type); Label l1 = new Label(); mv.visitJumpInsn(IFNE, l1); mv.visitInsn(ICONST_0); mv.visitInsn(IRETURN); mv.visitLabel(l1); mv.visitFrame(F_SAME, 0, null, 0, null); // CODE: final Foo other = (Foo) obj; mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, typeInfo.type); mv.visitVarInsn(ASTORE, 2); for (FieldInfo info : fields) { addTest(mv, info); } // CODE: return true; } mv.visitInsn(ICONST_1); mv.visitInsn(IRETURN); mv.visitMaxs(1, 2); mv.visitEnd(); }
From source file:boilerplate.processor.adapters.EqualsAdapter.java
License:Open Source License
private void addHashCodeMethod(List<FieldInfo> fields) { // CODE: public int hashCode() { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null); mv.visitCode();/*from w w w. ja va 2 s . c o m*/ // CODE: final int prime = 31; mv.visitIntInsn(BIPUSH, 31); mv.visitVarInsn(ISTORE, 1); mv.visitInsn(ICONST_1); // CODE: int result = 1; mv.visitVarInsn(ISTORE, 2); for (FieldInfo fieldInfo : fields) { addCalculation(mv, fieldInfo); } // CODE: return result; } mv.visitVarInsn(ILOAD, 2); mv.visitInsn(IRETURN); mv.visitMaxs(1, 3); mv.visitEnd(); }
From source file:boilerplate.processor.adapters.ToStringAdapter.java
License:Open Source License
private void addMethod() { List<String> fieldNames = new ArrayList<String>(typeInfo.fields.size()); Map<String, FieldInfo> fields = new HashMap<String, FieldInfo>(typeInfo.fields.size()); boolean hasAnnotatedFields = typeInfo.hasAnnotatedFields(); for (FieldInfo info : typeInfo.fields) { if (!(hasAnnotatedFields ^ info.mark == Mark.INCLUDE)) { fieldNames.add(info.name);/*w w w . j a va 2 s .com*/ fields.put(info.name, info); } } Collections.sort(fieldNames); MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V"); mv.visitVarInsn(ASTORE, 1); for (String name : fieldNames) { FieldInfo info = fields.get(name); addField(mv, info); } mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;"); mv.visitInsn(ARETURN); mv.visitMaxs(4, 2); mv.visitEnd(); }
From source file:br.usp.each.saeg.badua.core.internal.instr.ClassInstrumenter.java
License:Open Source License
@Override public void visitEnd() { // not instrument interfaces or // classes with probe count equals to zero if (interfaceType || classProbeCount == 0) { super.visitEnd(); return;/*from ww w .j ava2 s .co m*/ } final FieldVisitor fv = cv.visitField(InstrSupport.DATAFIELD_ACC, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC, null, null); fv.visitEnd(); final MethodVisitor mv = cv.visitMethod(InstrSupport.DATAMETHOD_ACC, InstrSupport.DATAMETHOD_NAME, InstrSupport.DATAMETHOD_DESC, null, null); mv.visitCode(); // Load the value of the static data field: mv.visitFieldInsn(Opcodes.GETSTATIC, className, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC); mv.visitInsn(Opcodes.DUP); // Stack[1]: [J // Stack[0]: [J // Skip initialization when we already have a data array: final Label alreadyInitialized = new Label(); mv.visitJumpInsn(Opcodes.IFNONNULL, alreadyInitialized); // Stack[0]: [J mv.visitInsn(Opcodes.POP); mv.visitLdcInsn(classId); InstrSupport.push(mv, classProbeCount); mv.visitMethodInsn(Opcodes.INVOKESTATIC, InstrSupport.RUNTIME_OWNER, InstrSupport.RUNTIME_NAME, InstrSupport.RUNTIME_DESC, false); // Stack[0]: [J mv.visitInsn(Opcodes.DUP); // Stack[1]: [J // Stack[0]: [J mv.visitFieldInsn(Opcodes.PUTSTATIC, className, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC); // Stack[0]: [J if (withFrames) { mv.visitFrame(Opcodes.F_NEW, 0, new Object[] {}, 1, new Object[] { InstrSupport.DATAFIELD_DESC }); } mv.visitLabel(alreadyInitialized); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(3, 0); mv.visitEnd(); super.visitEnd(); }