List of usage examples for org.objectweb.asm MethodVisitor visitTryCatchBlock
public void visitTryCatchBlock(final Label start, final Label end, final Label handler, final String type)
From source file:Java6to2.java
License:Open Source License
private static byte[] transform(InputStream classfile) throws IOException { ClassReader cr = new ClassReader(classfile); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); ClassVisitor cv = new ClassVisitor(ASM7, cw) { String internalName;//from w ww. j a va 2s. co m boolean classLookupMethodGenerated; Set fieldsGenerated = new HashSet(); public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { /* Change class file version to 1.2 */ cv.visit(V1_2, access, name, signature, superName, interfaces); this.internalName = name; } /** * Generates the synthetic "class$" method, used to lookup classes via Class.forName(). This uses the exact same code as does JDK8 javac for target 1.2. */ void generateSyntheticClassLookupMethod() { MethodVisitor mv = cv.visitMethod(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, "class$", "(Ljava/lang/String;)Ljava/lang/Class;", null, null); { Label start = new Label(); Label end = new Label(); Label handler = new Label(); mv.visitTryCatchBlock(start, end, handler, "java/lang/ClassNotFoundException"); mv.visitLabel(start); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;", false); mv.visitLabel(end); mv.visitInsn(ARETURN); mv.visitLabel(handler); mv.visitVarInsn(ASTORE, 1); mv.visitTypeInsn(NEW, "java/lang/NoClassDefFoundError"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/NoClassDefFoundError", "<init>", "()V", false); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/NoClassDefFoundError", "initCause", "(Ljava/lang/Throwable;)Ljava/lang/Throwable;", false); mv.visitInsn(ATHROW); } mv.visitMaxs(2, 2); mv.visitEnd(); } public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); final ClassVisitor cv = this.cv; return new MethodVisitor(ASM7, mv) { /** * Intercepts class instantiations to see whether they instantiate a StringBuilder. Those instructions were generated by javac for string concatenations. But * StringBuilder is not available on JRE1.2, so we just replace it with StringBuffer. */ public void visitTypeInsn(int opcode, String type) { if (opcode == NEW && "java/lang/StringBuilder".equals(type)) { mv.visitTypeInsn(opcode, "java/lang/StringBuffer"); } else { mv.visitTypeInsn(opcode, type); } } /** * Intercepts method invocations to see whether they do something with StringBuilder. Those instructions were generated by javac for string concatenations. But * StringBuilder is not available on JRE1.2, so we just replace it with StringBuffer. */ public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { if ("java/lang/StringBuilder".equals(owner)) { mv.visitMethodInsn(opcode, "java/lang/StringBuffer", name, desc.replace("java/lang/StringBuilder", "java/lang/StringBuffer"), itf); } else { mv.visitMethodInsn(opcode, owner, name, desc, itf); } } /** * Intercepts LDC instructions and check whether they are used to load a class. This is not supported on Java 1.2, so we convert it to the same code used by the * JDK8 javac: * <ul> * <li>create synthetic fields holding the resolved class objects * <li>create a synthetic method called "class$" which does Class.forName * </ul> */ public void visitLdcInsn(Object cst) { if (cst instanceof Type) { Type t = (Type) cst; String syntheticField = "class$" + t.getInternalName().replace('/', '$').replace("[", ""); if (!classLookupMethodGenerated) { /* Emit the synthetic "class$" method, used to lookup classes via Class.forName() */ generateSyntheticClassLookupMethod(); classLookupMethodGenerated = true; } if (!fieldsGenerated.contains(syntheticField)) { /* Generate a synthetic field holding the resolved Class object */ cv.visitField(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, syntheticField, "Ljava/lang/Class;", null, null); fieldsGenerated.add(syntheticField); } mv.visitFieldInsn(GETSTATIC, internalName, syntheticField, "Ljava/lang/Class;"); Label nonNull = new Label(); mv.visitJumpInsn(IFNONNULL, nonNull); mv.visitLdcInsn(t.getInternalName().replace('/', '.')); mv.visitMethodInsn(INVOKESTATIC, internalName, "class$", "(Ljava/lang/String;)Ljava/lang/Class;", false); mv.visitInsn(DUP); mv.visitFieldInsn(PUTSTATIC, internalName, syntheticField, "Ljava/lang/Class;"); Label cnt = new Label(); mv.visitJumpInsn(GOTO, cnt); mv.visitLabel(nonNull); mv.visitFieldInsn(GETSTATIC, internalName, syntheticField, "Ljava/lang/Class;"); mv.visitLabel(cnt); } else { mv.visitLdcInsn(cst); } } }; } }; cr.accept(cv, ClassReader.SKIP_FRAMES); // <- Frames are not used in Java 1.2, so skip them return cw.toByteArray(); }
From source file:autostack.Transformer.java
License:Open Source License
public byte[] transform(ClassLoader loader, final String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { try {//from ww w . j a v a 2 s. c o m if (className == null || className.startsWith("java/") || className.startsWith("sun/") || className.startsWith("jdk/internal/") || className.startsWith("org/lwjgl/")) return null; for (String pack : packages) if (!className.startsWith(pack)) return null; ClassReader cr = new ClassReader(classfileBuffer); final Map<String, Integer> stackMethods = new HashMap<String, Integer>(); // Scan all methods that need auto-stack if (debugTransform) System.out.println("[autostack] scanning methods in class: " + className.replace('/', '.')); cr.accept(new ClassVisitor(ASM5) { public MethodVisitor visitMethod(final int access, final String methodName, final String methodDesc, String signature, String[] exceptions) { if ((access & (ACC_NATIVE | ACC_ABSTRACT)) != 0) { // Don't try to analyze native or abstract methods. return null; } MethodVisitor mv = new MethodVisitor(ASM5) { boolean mark, catches, notransform, nostackparam, forcestack; public AnnotationVisitor visitAnnotation(String desc, boolean visible) { if ("Lautostack/NoTransform;".equals(desc)) notransform = true; else if ("Lautostack/NoStackParam;".equals(desc)) nostackparam = true; else if ("Lautostack/UseNewStack;".equals(desc)) forcestack = true; return null; } public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { if (opcode == INVOKESTATIC && !itf && (owner.startsWith("org/lwjgl/") && (name.equals("mallocStack") || name.equals("callocStack")) || owner.equals(MEMORYSTACK) && (name.equals("stackGet") || name.equals("stackPop") || name.equals("stackPush") || name.startsWith("stackMalloc") || name.startsWith("stackCalloc") || name.equals("stackUTF8") || name.equals("stackASCII") || name.equals("stackUTF16") || name.equals("stackFloats") || name.equals("stackInts") || name.equals("stackBytes") || name.equals("stackShorts") || name.equals("stackPointers") || name.equals("stackLongs")))) { mark = true; } } public void visitTryCatchBlock(Label start, Label end, Label handler, String type) { catches = true; } public void visitEnd() { int flag = (access & ACC_PRIVATE) != 0 ? 8 : 0; flag |= nostackparam ? 16 : 0; if (mark || notransform || forcestack || nostackparam) { if (notransform) { flag |= 2; if (debugTransform) System.out.println("[autostack] will not transform method: " + className.replace('/', '.') + "." + methodName); } else { if (checkStack) flag |= 4; flag |= catches ? 1 : 0; if (debugTransform) System.out.println("[autostack] will transform method: " + className.replace('/', '.') + "." + methodName); } stackMethods.put(methodName + methodDesc, Integer.valueOf(flag)); } } }; return mv; } }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); if (stackMethods.isEmpty()) return null; // Now, transform all such methods if (debugTransform) System.out.println("[autostack] transforming methods in class: " + className.replace('/', '.')); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); cr.accept(new ClassVisitor(ASM5, cw) { boolean classDefaultNewStack = defaultNewStack; boolean classNoTransform; @Override public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { cv.visit(version, access, name, signature, superName, interfaces); if (!checkStack) { return; } /* Generate simple synthetic "compare stack pointers and throw if not equal" method */ MethodVisitor mv = cv.visitMethod(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, "$checkStack$", "(II)V", null, new String[] { "java/lang/AssertionError" }); { mv.visitCode(); mv.visitVarInsn(ILOAD, 0); mv.visitVarInsn(ILOAD, 1); Label l0 = new Label(); mv.visitJumpInsn(IF_ICMPEQ, l0); mv.visitTypeInsn(NEW, "java/lang/IllegalStateException"); mv.visitInsn(DUP); mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); mv.visitInsn(DUP); mv.visitLdcInsn("Stack pointers differ: "); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false); mv.visitVarInsn(ILOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;", false); mv.visitLdcInsn(" != "); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); mv.visitVarInsn(ILOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(RETURN); mv.visitMaxs(5, 2); mv.visitEnd(); } mv = cv.visitMethod(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, "$checkStackWithThrowable$", "(Ljava/lang/Throwable;II)Ljava/lang/Throwable;", null, null); { mv.visitCode(); mv.visitVarInsn(ILOAD, 1); mv.visitVarInsn(ILOAD, 2); Label l0 = new Label(); mv.visitJumpInsn(IF_ICMPEQ, l0); mv.visitTypeInsn(NEW, "java/lang/IllegalStateException"); mv.visitInsn(DUP); mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); mv.visitInsn(DUP); mv.visitLdcInsn("Stack pointers differ: "); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false); mv.visitVarInsn(ILOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;", false); mv.visitLdcInsn(" != "); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); mv.visitVarInsn(ILOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V", false); mv.visitInsn(ARETURN); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitInsn(ARETURN); mv.visitMaxs(5, 3); mv.visitEnd(); } } public AnnotationVisitor visitAnnotation(String desc, boolean visible) { if ("Lautostack/UseCallerStack;".equals(desc)) { if (debugTransform) System.out.println( "[autostack] class declares to use caller stack for all methods, unless overridden by method"); classDefaultNewStack = false; return null; } else if ("Lautostack/UseNewStack;".equals(desc)) { if (debugTransform) System.out.println( "[autostack] class declares to use new stack for all methods, unless overridden by method"); classDefaultNewStack = true; return null; } else if ("Lautostack/NoTransform;".equals(desc)) { if (debugTransform) System.out.println("[autostack] class declares to not transform any methods"); classNoTransform = true; return null; } return cv.visitAnnotation(desc, visible); } public MethodVisitor visitMethod(final int access, final String name, final String desc, String signature, String[] exceptions) { Integer info = stackMethods.get(name + desc); if (info == null) return super.visitMethod(access, name, desc, signature, exceptions); boolean catches = (info.intValue() & 1) == 1; final boolean notransform = classNoTransform || (info.intValue() & 2) == 2; if (debugTransform && !notransform) System.out.println( "[autostack] transform method: " + className.replace('/', '.') + "." + name); final boolean memoryStackParam = stackAsParameter && (access & ACC_PRIVATE) != 0 && (info.intValue() & 16) == 0; MethodVisitor mv; final Type[] paramTypes = Type.getArgumentTypes(desc); final boolean isStatic = (access & ACC_STATIC) != 0; final boolean isConstructor = "<init>".equals(name); if (memoryStackParam) { if (debugTransform) System.out.println( "[autostack] changing signature of method to add additional MemoryStack parameter"); // Add additional MemoryStack parameter to the method signature index of the local stays the same int paramEndIndex = desc.indexOf(')'); String beforeDesc = desc.substring(0, paramEndIndex); String afterDesc = desc.substring(paramEndIndex); mv = super.visitMethod(access | ACC_SYNTHETIC, name, beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, signature, exceptions); // Re-introduce the original method which just delegates if (debugTransform) System.out.println("[autostack] adding delegate method with original signature"); MethodVisitor omv = super.visitMethod(access, name, desc, signature, exceptions); omv.visitCode(); int param = 0; if (!isStatic) { omv.visitVarInsn(ALOAD, 0); param++; } for (int i = 0; i < paramTypes.length; i++) { omv.visitVarInsn(paramTypes[i].getOpcode(ILOAD), param); param += paramTypes[i].getSize(); } omv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, "stackGet", "()L" + MEMORYSTACK + ";", false); boolean isPrivate = (access & ACC_PRIVATE) != 0; int opcode = isStatic ? INVOKESTATIC : isPrivate ? INVOKESPECIAL : INVOKEVIRTUAL; omv.visitMethodInsn(opcode, className, name, beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, false); Type retType = Type.getReturnType(desc); omv.visitInsn(retType.getOpcode(IRETURN)); omv.visitMaxs(-1, -1); omv.visitEnd(); } else { mv = super.visitMethod(access, name, desc, signature, exceptions); } if (catches) mv = new TryCatchBlockSorter(mv, access, name, desc, signature, exceptions); mv = new MethodVisitor(ASM5, mv) { Label tryLabel = new Label(); Label finallyLabel = new Label(); int lastLine = 0; boolean newStack = classDefaultNewStack; int stackVarIndex; int stackPointerVarIndex; int firstAdditionalLocal; int additionalLocals; Object[] replacedLocals; public void visitInsn(int opcode) { if (notransform) { mv.visitInsn(opcode); return; } if (opcode >= IRETURN && opcode <= RETURN && (newStack || checkStack)) { if (debugRuntime && newStack && !checkStack) { mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("[autostack] restore stack pointer because of return at " + className.replace('/', '.') + "." + name + ":" + lastLine); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); } if (newStack && !checkStack) { mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitVarInsn(ILOAD, stackPointerVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "setPointer", "(I)V", false); } else if (checkStack) { mv.visitVarInsn(ILOAD, stackPointerVarIndex); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false); mv.visitMethodInsn(INVOKESTATIC, className, "$checkStack$", "(II)V", false); } } mv.visitInsn(opcode); } public AnnotationVisitor visitAnnotation(String desc, boolean visible) { if ("Lautostack/UseCallerStack;".equals(desc)) { if (!notransform) { if (debugTransform) System.out.println("[autostack] method declares to use caller stack"); newStack = false; } return null; } else if ("Lautostack/UseNewStack;".equals(desc)) { if (!notransform) { if (debugTransform) System.out.println("[autostack] method declares to use new stack"); newStack = true; } return null; } else if ("Lautostack/NoTransform;".equals(desc)) { return null; } else if ("Lautostack/NoStackParam;".equals(desc)) { return null; } return mv.visitAnnotation(desc, visible); } public void visitVarInsn(int opcode, int var) { if (notransform) { mv.visitVarInsn(opcode, var); return; } if (var >= firstAdditionalLocal) var += additionalLocals; mv.visitVarInsn(opcode, var); } public void visitIincInsn(int var, int increment) { if (notransform) { mv.visitIincInsn(var, increment); return; } if (var >= firstAdditionalLocal) var += additionalLocals; mv.visitIincInsn(var, increment); } public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) { if (notransform) { mv.visitFrame(type, nLocal, local, nStack, stack); return; } if (type == F_FULL) { int noThis = isStatic ? 0 : 1; Object[] locals = new Object[local.length + additionalLocals]; if (!isStatic) locals[0] = local[0]; int replacementLength = replacedLocals.length; System.arraycopy(replacedLocals, noThis, locals, noThis, replacementLength - noThis); int len = locals.length - replacementLength; System.arraycopy(local, replacementLength - additionalLocals, locals, replacementLength, len); mv.visitFrame(type, nLocal + additionalLocals, locals, nStack, stack); } else mv.visitFrame(type, nLocal, local, nStack, stack); } public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { if (notransform) { mv.visitLocalVariable(className, desc, signature, start, end, index); return; } if (index >= firstAdditionalLocal) index += additionalLocals; mv.visitLocalVariable(name, desc, signature, start, end, index); } private boolean doesNotTakeStackItself(String desc) { return desc.lastIndexOf("L" + MEMORYSTACK + ";)") == -1; } public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { String completeName = name + desc; Integer info = stackMethods.get(completeName); if (opcode != INVOKESTATIC || notransform) { mv.visitMethodInsn(opcode, owner, name, desc, itf); return; } if (stackAsParameter && info != null && (info.intValue() & 8) != 0 && (info.intValue() & 16) == 0) { /* Rewrite invocation to have additional MemoryStack parameter */ if (debugTransform) System.out.println( "[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> " + owner.replace('/', '.') + "." + name + "(..., MemoryStack)"); int paramEndIndex = desc.indexOf(')'); String beforeDesc = desc.substring(0, paramEndIndex); String afterDesc = desc.substring(paramEndIndex); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitMethodInsn(opcode, owner, name, beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, itf); return; } if (owner.startsWith("org/lwjgl/") && (name.equals("mallocStack") || name.equals("callocStack")) && doesNotTakeStackItself(desc)) { if (debugTransform) System.out.println( "[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokestatic " + owner.replace('/', '.') + "." + name); mv.visitVarInsn(ALOAD, stackVarIndex); int paramEndIndex = desc.indexOf(')'); String beforeDesc = desc.substring(0, paramEndIndex); String afterDesc = desc.substring(paramEndIndex); mv.visitMethodInsn(opcode, owner, name, beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, false); } else if (owner.equals(MEMORYSTACK) && name.equals("stackGet")) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex); mv.visitVarInsn(ALOAD, stackVarIndex); } else if (owner.equals(MEMORYSTACK) && (name.equals("stackPush") || name.equals("stackPop"))) { String newName = "p" + name.substring(6); if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (owner.equals(MEMORYSTACK) && (name.startsWith("stackMalloc") || name.startsWith("stackCalloc"))) { String newName = name.substring(5, 6).toLowerCase() + name.substring(6); if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (owner.equals(MEMORYSTACK) && (name.equals("stackASCII") || name.equals("stackUTF8") || name.equals("stackUTF16"))) { String newName = name.substring(5); boolean withBoolean = desc.startsWith("(Ljava/lang/CharSequence;Z"); if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); if (withBoolean) { mv.visitInsn(DUP_X2); mv.visitInsn(POP); } else { mv.visitInsn(SWAP); } mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (owner.equals(MEMORYSTACK) && (name.equals("stackFloats") || name.equals("stackInts") || name.equals("stackBytes") || name.equals("stackShorts") || name.equals("stackPointers") && (desc.startsWith("([Lorg/lwjgl/system/Pointer;") || desc.startsWith("(Lorg/lwjgl/system/Pointer;")))) { String newName = name.substring(5, 6).toLowerCase() + name.substring(6); Type[] argTypes = Type.getArgumentTypes(desc); if (argTypes.length == 1 && argTypes[0].getSort() == Type.ARRAY) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (argTypes.length == 1) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (argTypes.length == 2) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(DUP_X2); mv.visitInsn(POP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (argTypes.length == 3) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(DUP2_X2); mv.visitInsn(POP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); mv.visitInsn(SWAP); mv.visitInsn(POP); } else { if (debugTransform) System.out.println("[autostack] failed to rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + ". Not yet implemented."); /* Give up. Not possible without an additional local */ mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, name, desc, itf); } } else if (owner.equals(MEMORYSTACK) && name.equals("stackLongs")) { String newName = name.substring(5, 6).toLowerCase() + name.substring(6); Type[] argTypes = Type.getArgumentTypes(desc); if (argTypes.length == 1 && argTypes[0].getSort() == Type.ARRAY) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (argTypes.length == 1) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(DUP_X2); mv.visitInsn(POP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else { if (debugTransform) System.out.println("[autostack] failed to rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + ". Not yet implemented."); /* Give up. Not possible without an additional local */ mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, name, desc, itf); } } else { mv.visitMethodInsn(opcode, owner, name, desc, itf); } } public void visitLineNumber(int line, Label start) { mv.visitLineNumber(line, start); lastLine = line; } public void visitCode() { if (notransform) { mv.visitCode(); return; } additionalLocals = newStack || checkStack ? 2 : 1; replacedLocals = new Object[paramTypes.length + additionalLocals + (isStatic ? 0 : 1)]; if (!newStack && !checkStack) { replacedLocals[replacedLocals.length - 1] = MEMORYSTACK; } else { replacedLocals[replacedLocals.length - 2] = MEMORYSTACK; replacedLocals[replacedLocals.length - 1] = INTEGER; } if (!isStatic) replacedLocals[0] = isConstructor ? TOP : className; int var = isStatic ? 0 : 1; for (int t = 0, i = var; t < paramTypes.length; t++, i++) { Type type = paramTypes[t]; var += type.getSize(); switch (type.getSort()) { case Type.INT: case Type.BYTE: case Type.BOOLEAN: case Type.SHORT: case Type.CHAR: replacedLocals[i] = INTEGER; break; case Type.LONG: replacedLocals[i] = LONG; break; case Type.FLOAT: replacedLocals[i] = FLOAT; break; case Type.DOUBLE: replacedLocals[i] = DOUBLE; break; case Type.OBJECT: case Type.ARRAY: replacedLocals[i] = type.getInternalName(); break; default: throw new AssertionError("Unhandled parameter type: " + type); } } firstAdditionalLocal = var; stackVarIndex = var; stackPointerVarIndex = var + 1; mv.visitCode(); if (newStack && !checkStack || checkStack) { if (!memoryStackParam) { mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, "stackGet", "()L" + MEMORYSTACK + ";", false); mv.visitInsn(DUP); mv.visitVarInsn(ASTORE, stackVarIndex); } else { mv.visitVarInsn(ALOAD, stackVarIndex); } mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false); mv.visitVarInsn(ISTORE, stackPointerVarIndex); if (debugRuntime && newStack && !checkStack) { mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("[autostack] save stack pointer ["); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitVarInsn(ILOAD, stackPointerVarIndex); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("] at begin of " + className.replace('/', '.') + "." + name); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); } mv.visitLabel(tryLabel); if (!memoryStackParam) mv.visitFrame(F_APPEND, 2, new Object[] { MEMORYSTACK, INTEGER }, 0, null); else mv.visitFrame(F_APPEND, 1, new Object[] { INTEGER }, 0, null); } else if (!newStack && !checkStack) { if (!memoryStackParam) { mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, "stackGet", "()L" + MEMORYSTACK + ";", false); mv.visitVarInsn(ASTORE, stackVarIndex); } if (debugRuntime) { mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("[autostack] current stack pointer is ["); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("] at begin of " + className.replace('/', '.') + "." + name); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); } mv.visitLabel(tryLabel); if (!memoryStackParam) mv.visitFrame(F_APPEND, 1, new Object[] { MEMORYSTACK }, 0, null); } } public void visitMaxs(int maxStack, int maxLocals) { if (notransform) { mv.visitMaxs(maxStack, maxLocals); return; } if (newStack && !checkStack || checkStack) { mv.visitLabel(finallyLabel); mv.visitFrame(F_FULL, replacedLocals.length, replacedLocals, 1, new Object[] { "java/lang/Throwable" }); mv.visitTryCatchBlock(tryLabel, finallyLabel, finallyLabel, null); if (debugRuntime && newStack && !checkStack) { mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("[autostack] restore stack pointer because of throw ["); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitInsn(DUP); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn( "] at " + className.replace('/', '.') + "." + name + ":" + lastLine); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); } if (newStack && !checkStack) { mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitVarInsn(ILOAD, stackPointerVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "setPointer", "(I)V", false); } if (checkStack) { mv.visitVarInsn(ILOAD, stackPointerVarIndex); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false); mv.visitMethodInsn(INVOKESTATIC, className, "$checkStackWithThrowable$", "(Ljava/lang/Throwable;II)Ljava/lang/Throwable;", false); } mv.visitInsn(ATHROW); } mv.visitMaxs(-1, maxLocals + additionalLocals); } }; return mv; } }, 0); byte[] arr = cw.toByteArray(); if (trace) { cr = new ClassReader(arr); cr.accept(new TraceClassVisitor(new PrintWriter(System.out)), 0); } return arr; } catch (Throwable t) { t.printStackTrace(); throw new RuntimeException(t); } }
From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java
License:Open Source License
public void accept(MethodVisitor mv, boolean hasAnnotation) { db.log(LogLevel.INFO, "Instrumenting method %s#%s%s", className, mn.name, mn.desc); mv.visitAnnotation(ALREADY_INSTRUMENTED_DESC, true); final boolean handleProxyInvocations = HANDLE_PROXY_INVOCATIONS & hasSuspendableSuperCalls; mv.visitCode();//w ww. java 2s . c om Label lMethodStart = new Label(); Label lMethodStart2 = new Label(); Label lMethodEnd = new Label(); Label lCatchSEE = new Label(); Label lCatchUTE = new Label(); Label lCatchAll = new Label(); Label[] lMethodCalls = new Label[numCodeBlocks - 1]; for (int i = 1; i < numCodeBlocks; i++) lMethodCalls[i - 1] = new Label(); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitVarInsn(Opcodes.ASTORE, lvarInvocationReturnValue); // if (verifyInstrumentation) { // mv.visitInsn(Opcodes.ICONST_0); // mv.visitVarInsn(Opcodes.ISTORE, lvarSuspendableCalled); // } mv.visitTryCatchBlock(lMethodStart, lMethodEnd, lCatchSEE, EXCEPTION_NAME); if (handleProxyInvocations) mv.visitTryCatchBlock(lMethodStart, lMethodEnd, lCatchUTE, UNDECLARED_THROWABLE_NAME); // Prepare visitTryCatchBlocks for InvocationTargetException. // With reflective invocations, the SuspendExecution exception will be wrapped in InvocationTargetException. We need to catch it and unwrap it. // Note that the InvocationTargetException will be regenrated on every park, adding further overhead on top of the reflective call. // This must be done here, before all other visitTryCatchBlock, because the exception's handler // will be matched according to the order of in which visitTryCatchBlock has been called. Earlier calls take precedence. Label[][] refInvokeTryCatch = new Label[numCodeBlocks - 1][]; for (int i = 1; i < numCodeBlocks; i++) { FrameInfo fi = codeBlocks[i]; AbstractInsnNode in = mn.instructions.get(fi.endInstruction); if (mn.instructions.get(fi.endInstruction) instanceof MethodInsnNode) { MethodInsnNode min = (MethodInsnNode) in; if (isReflectInvocation(min.owner, min.name)) { Label[] ls = new Label[3]; for (int k = 0; k < 3; k++) ls[k] = new Label(); refInvokeTryCatch[i - 1] = ls; mv.visitTryCatchBlock(ls[0], ls[1], ls[2], "java/lang/reflect/InvocationTargetException"); } } } for (Object o : mn.tryCatchBlocks) { TryCatchBlockNode tcb = (TryCatchBlockNode) o; if (EXCEPTION_NAME.equals(tcb.type) && !hasAnnotation) // we allow catch of SuspendExecution in method annotated with @Suspendable. throw new UnableToInstrumentException("catch for SuspendExecution", className, mn.name, mn.desc); if (handleProxyInvocations && UNDECLARED_THROWABLE_NAME.equals(tcb.type)) // we allow catch of SuspendExecution in method annotated with @Suspendable. throw new UnableToInstrumentException("catch for UndeclaredThrowableException", className, mn.name, mn.desc); // if (INTERRUPTED_EXCEPTION_NAME.equals(tcb.type)) // throw new UnableToInstrumentException("catch for " + InterruptedException.class.getSimpleName(), className, mn.name, mn.desc); tcb.accept(mv); } if (mn.visibleParameterAnnotations != null) dumpParameterAnnotations(mv, mn.visibleParameterAnnotations, true); if (mn.invisibleParameterAnnotations != null) dumpParameterAnnotations(mv, mn.invisibleParameterAnnotations, false); if (mn.visibleAnnotations != null) { for (Object o : mn.visibleAnnotations) { AnnotationNode an = (AnnotationNode) o; an.accept(mv.visitAnnotation(an.desc, true)); } } mv.visitTryCatchBlock(lMethodStart, lMethodEnd, lCatchAll, null); mv.visitMethodInsn(Opcodes.INVOKESTATIC, STACK_NAME, "getStack", "()L" + STACK_NAME + ";"); mv.visitInsn(Opcodes.DUP); mv.visitVarInsn(Opcodes.ASTORE, lvarStack); // println(mv, "STACK: ", lvarStack); // dumpStack(mv); if (DUAL) { mv.visitJumpInsn(Opcodes.IFNULL, lMethodStart); mv.visitVarInsn(Opcodes.ALOAD, lvarStack); } emitStoreResumed(mv, true); // we'll assume we have been resumed mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "nextMethodEntry", "()I"); mv.visitTableSwitchInsn(1, numCodeBlocks - 1, lMethodStart2, lMethodCalls); mv.visitLabel(lMethodStart2); // the following code handles the case of an instrumented method called not as part of a suspendable code path // isFirstInStack will return false in that case. mv.visitVarInsn(Opcodes.ALOAD, lvarStack); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "isFirstInStackOrPushed", "()Z"); mv.visitJumpInsn(Opcodes.IFNE, lMethodStart); // if true mv.visitInsn(Opcodes.ACONST_NULL); mv.visitVarInsn(Opcodes.ASTORE, lvarStack); mv.visitLabel(lMethodStart); emitStoreResumed(mv, false); // no, we have not been resumed dumpCodeBlock(mv, 0, 0); for (int i = 1; i < numCodeBlocks; i++) { FrameInfo fi = codeBlocks[i]; MethodInsnNode min = (MethodInsnNode) (mn.instructions.get(fi.endInstruction)); if (isYieldMethod(min.owner, min.name)) { // special case - call to yield if (min.getOpcode() != Opcodes.INVOKESTATIC) throw new UnableToInstrumentException("invalid call to suspending method.", className, mn.name, mn.desc); final int numYieldArgs = TypeAnalyzer.getNumArguments(min.desc); final boolean yieldReturnsValue = (Type.getReturnType(min.desc) != Type.VOID_TYPE); emitStoreState(mv, i, fi, numYieldArgs); // we preserve the arguments for the call to yield on the operand stack emitStoreResumed(mv, false); // we have not been resumed // emitSuspendableCalled(mv); min.accept(mv); // we call the yield method if (yieldReturnsValue) mv.visitInsn(Opcodes.POP); // we ignore the returned value... mv.visitLabel(lMethodCalls[i - 1]); // we resume AFTER the call final Label afterPostRestore = new Label(); mv.visitVarInsn(Opcodes.ILOAD, lvarResumed); mv.visitJumpInsn(Opcodes.IFEQ, afterPostRestore); emitPostRestore(mv); mv.visitLabel(afterPostRestore); emitRestoreState(mv, i, fi, numYieldArgs); if (yieldReturnsValue) mv.visitVarInsn(Opcodes.ILOAD, lvarResumed); // ... and replace the returned value with the value of resumed dumpCodeBlock(mv, i, 1); // skip the call } else { final Label lbl = new Label(); if (DUAL) { mv.visitVarInsn(Opcodes.ALOAD, lvarStack); mv.visitJumpInsn(Opcodes.IFNULL, lbl); } // normal case - call to a suspendable method - resume before the call emitStoreState(mv, i, fi, 0); emitStoreResumed(mv, false); // we have not been resumed // emitPreemptionPoint(mv, PREEMPTION_CALL); mv.visitLabel(lMethodCalls[i - 1]); emitRestoreState(mv, i, fi, 0); if (DUAL) mv.visitLabel(lbl); if (isReflectInvocation(min.owner, min.name)) { // We catch the InvocationTargetException and unwrap it if it wraps a SuspendExecution exception. Label[] ls = refInvokeTryCatch[i - 1]; final Label startTry = ls[0]; final Label endTry = ls[1]; final Label startCatch = ls[2]; final Label endCatch = new Label(); final Label notSuspendExecution = new Label(); // mv.visitTryCatchBlock(startTry, endTry, startCatch, "java/lang/reflect/InvocationTargetException"); mv.visitLabel(startTry); // try { min.accept(mv); // method.invoke() mv.visitVarInsn(Opcodes.ASTORE, lvarInvocationReturnValue); // save return value mv.visitLabel(endTry); // } mv.visitJumpInsn(Opcodes.GOTO, endCatch); mv.visitLabel(startCatch); // catch(InvocationTargetException ex) { mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "getCause", "()Ljava/lang/Throwable;"); mv.visitTypeInsn(Opcodes.INSTANCEOF, EXCEPTION_NAME); mv.visitJumpInsn(Opcodes.IFEQ, notSuspendExecution); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "getCause", "()Ljava/lang/Throwable;"); mv.visitLabel(notSuspendExecution); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(endCatch); mv.visitVarInsn(Opcodes.ALOAD, lvarInvocationReturnValue); // restore return value dumpCodeBlock(mv, i, 1); // skip the call } else { // emitSuspendableCalled(mv); dumpCodeBlock(mv, i, 0); } } } mv.visitLabel(lMethodEnd); if (handleProxyInvocations) { mv.visitLabel(lCatchUTE); mv.visitInsn(Opcodes.DUP); // println(mv, "CTCH: "); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "getCause", "()Ljava/lang/Throwable;"); // println(mv, "CAUSE: "); mv.visitTypeInsn(Opcodes.INSTANCEOF, EXCEPTION_NAME); mv.visitJumpInsn(Opcodes.IFEQ, lCatchAll); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "getCause", "()Ljava/lang/Throwable;"); mv.visitJumpInsn(Opcodes.GOTO, lCatchSEE); } mv.visitLabel(lCatchAll); emitPopMethod(mv); mv.visitLabel(lCatchSEE); // println(mv, "THROW: "); mv.visitInsn(Opcodes.ATHROW); // rethrow shared between catchAll and catchSSE if (mn.localVariables != null) { for (Object o : mn.localVariables) ((LocalVariableNode) o).accept(mv); } mv.visitMaxs(mn.maxStack + ADD_OPERANDS, mn.maxLocals + NUM_LOCALS + additionalLocals); mv.visitEnd(); }
From source file:com.centimia.orm.jaqu.ext.asm.JaquClassAdapter.java
License:Open Source License
/** * Generates the new method body, copy the old to a new method and connect them. * //from w w w . j av a 2s . c o m * the structure of the new method is:<br> * <br><b><div style="background:lightgray"> * <pre> * public [CollectionType] [getterName]() { * if ([fieldName] == null){ * try { * if (null == db || db.isClosed()) * throw new RuntimeException("Cannot initialize a 'Relation' outside an open session!!!. Try initializing the field directly within the class."); * Method method = db.getClass().getDeclaredMethod("getRelationFromDb", String.class, Object.class, Class.class); * method.setAccessible(true); * children = (Collection<TestTable>)method.invoke(db, [fieldName], this, TestTable.class); * method.setAccessible(false); * } * catch (Exception e) { * if (e instanceof RuntimeException) * throw (RuntimeException)e; * throw new RuntimeException(e.getMessage(), e); * } * } * return $orig_[getterName](); * } * </pre> * </div> * * @param access * @param desc * @param signature * @param exceptions * @param name * @param newName * @param fieldName */ private void generateNewMethodBody(int access, String desc, String signature, String[] exceptions, String name, String newName, String fieldName) { MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions); String fieldSignature = signature.substring(signature.indexOf(')') + 1, signature.lastIndexOf('<')) + ";"; String type = signature.substring(signature.indexOf('<') + 1, signature.indexOf('>')); String cast = desc.substring(desc.indexOf("java/"), desc.indexOf(';')); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature); Label l3 = new Label(); mv.visitJumpInsn(IFNONNULL, l3); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); Label l4 = new Label(); mv.visitJumpInsn(IFNULL, l4); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "isClosed", "()Z", false); Label l5 = new Label(); mv.visitJumpInsn(IFEQ, l5); mv.visitLabel(l4); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitLdcInsn( "Cannot initialize a 'Relation' outside an open session!!!. Try initializing the field directly within the class."); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l5); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false); mv.visitLdcInsn("getRelationFromDb"); mv.visitInsn(ICONST_3); mv.visitTypeInsn(ANEWARRAY, "java/lang/Class"); mv.visitInsn(DUP); mv.visitInsn(ICONST_0); mv.visitLdcInsn(Type.getType("Ljava/lang/String;")); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_1); mv.visitLdcInsn(Type.getType("Ljava/lang/Object;")); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_2); mv.visitLdcInsn(Type.getType("Ljava/lang/Class;")); mv.visitInsn(AASTORE); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(ICONST_1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "setAccessible", "(Z)V", false); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitInsn(ICONST_3); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); mv.visitInsn(DUP); mv.visitInsn(ICONST_0); mv.visitLdcInsn(fieldName); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_1); mv.visitVarInsn(ALOAD, 0); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_2); mv.visitLdcInsn(Type.getType(type)); mv.visitInsn(AASTORE); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, cast); mv.visitFieldInsn(PUTFIELD, className, fieldName, fieldSignature); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(ICONST_0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "setAccessible", "(Z)V", false); mv.visitLabel(l1); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" }); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(INSTANCEOF, "java/lang/RuntimeException"); Label l6 = new Label(); mv.visitJumpInsn(IFEQ, l6); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, "java/lang/RuntimeException"); mv.visitInsn(ATHROW); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/Exception" }, 0, null); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "getMessage", "()Ljava/lang/String;", false); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, className, newName, desc, false); mv.visitInsn(ARETURN); mv.visitMaxs(7, 2); mv.visitEnd(); }
From source file:com.centimia.orm.jaqu.ext.asm.JaquClassAdapter.java
License:Open Source License
/** * Generates the new method body, copy the old to a new method and connect them. * the structure of the new method is:<br> * <br><b><div style="background:lightgray"> * <pre>/*from ww w . ja va2 s.co m*/ * public [entityType] [getterName]() { * if ([field] == null) { * try { * if (null == db) * throw new RuntimeException("Cannot initialize 'Relation' outside an open session!!!. Try initializing field directly within the class."); * * [parentType] parent = this.getClass().newInstance(); * [entityType] desc = TestB.class.newInstance(); * * // get the primary key * Object pk = db.getPrimaryKey(this); * * // get the object * [entityValue] = db.from(desc).innerJoin(parent).on(parent.[entityValue]).is(desc).where(db.getPrimaryKey(parent)).is(pk).selectFirst(); * } * catch (Exception e) { * if (e instanceof RuntimeException) * throw (RuntimeException)e; * throw new RuntimeException(e.getMessage(), e); * } * } * return $orig_[getterName](); * } * </pre> * </div> * * @param access * @param desc * @param exceptions * @param name - current method name * @param newName - new method name (the orig...) * @param fieldName */ public void generateLazyRelation(int access, String desc, String[] exceptions, String name, String newName, String fieldName) { MethodVisitor mv = cv.visitMethod(access, name, desc, null, exceptions); String fieldSignature = desc.substring(desc.indexOf(')') + 1); String fieldClassName = desc.substring(desc.indexOf(')') + 2, desc.length() - 1); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature); Label l3 = new Label(); mv.visitJumpInsn(IFNULL, l3); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature); mv.visitFieldInsn(GETFIELD, fieldClassName, "isLazy", "Z"); mv.visitJumpInsn(IFEQ, l3); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); Label l4 = new Label(); mv.visitJumpInsn(IFNULL, l4); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "isClosed", "()Z", false); Label l5 = new Label(); mv.visitJumpInsn(IFEQ, l5); mv.visitLabel(l4); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitLdcInsn( "Cannot initialize 'Relation' outside an open session!!!. Try initializing field directly within the class."); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l5); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "newInstance", "()Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, className); mv.visitVarInsn(ASTORE, 1); mv.visitLdcInsn(Type.getType(fieldSignature)); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "newInstance", "()Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, fieldClassName); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "getPrimaryKey", "(Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "from", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryInterface;", false); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEINTERFACE, "com/centimia/orm/jaqu/QueryInterface", "innerJoin", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoin;", true); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoin", "on", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoinCondition;", false); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoinCondition", "is", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoinWhere;", false); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "getPrimaryKey", "(Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoinWhere", "where", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryCondition;", false); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryCondition", "is", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryWhere;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryWhere", "selectFirst", "()Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, fieldClassName); mv.visitFieldInsn(PUTFIELD, className, fieldName, fieldSignature); mv.visitLabel(l1); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" }); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(INSTANCEOF, "java/lang/RuntimeException"); Label l6 = new Label(); mv.visitJumpInsn(IFEQ, l6); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, "java/lang/RuntimeException"); mv.visitInsn(ATHROW); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/Exception" }, 0, null); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "getMessage", "()Ljava/lang/String;", false); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, className, newName, desc, false); mv.visitInsn(ARETURN); mv.visitMaxs(4, 4); mv.visitEnd(); }
From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java
License:Open Source License
protected void generateReadStaticGroupForType(final Schema schema, ClassVisitor cv, final String genClassInternalName, final boolean javaClassCodec) { for (final GroupDef group : schema.getGroups()) { Object groupType = group.getGroupType(); String groupDescriptor = getTypeDescriptor(groupType, javaClassCodec); final MethodVisitor mv = cv.visitMethod(ACC_PRIVATE, "readStaticGroup_" + group.getName(), "(Lcom/cinnober/msgcodec/io/ByteSource;" + groupDescriptor + ")V", null, new String[] { "java/io/IOException" }); mv.visitCode();//from w w w. ja va2 s . co m final LocalVariable nextVar = new LocalVariable(3); // read fields of super group if (group.getSuperGroup() != null) { GroupDef superGroup = schema.getGroup(group.getSuperGroup()); Object superGroupType = superGroup.getGroupType(); String superGroupDescriptor = getTypeDescriptor(superGroupType, javaClassCodec); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "readStaticGroup_" + superGroup.getName(), "(Lcom/cinnober/msgcodec/io/ByteSource;" + superGroupDescriptor + ")V", false); } // fields for (final FieldDef field : group.getFields()) { final Class<?> javaClass = field.getJavaClass(); Runnable readValue = () -> { Label tryStart = new Label(); Label tryEnd = new Label(); Label tryCatch = new Label(); Label tryAfter = new Label(); mv.visitTryCatchBlock(tryStart, tryEnd, tryCatch, "java/lang/Exception"); mv.visitLabel(tryStart); mv.visitVarInsn(ALOAD, 1); // input stream generateDecodeValue(mv, 1, nextVar, field.isRequired(), field.getType(), javaClass, field.getComponentJavaClass(), schema, genClassInternalName, group.getName() + "_" + field.getName(), group.getName() + "." + field.getName(), javaClassCodec); mv.visitLabel(tryEnd); mv.visitJumpInsn(GOTO, tryAfter); mv.visitLabel(tryCatch); int caughtExVar = nextVar.next(); mv.visitVarInsn(ASTORE, caughtExVar); mv.visitTypeInsn(NEW, "com/cinnober/msgcodec/blink/FieldDecodeException"); mv.visitInsn(DUP); mv.visitLdcInsn(field.getName()); mv.visitVarInsn(ALOAD, caughtExVar); mv.visitMethodInsn(INVOKESPECIAL, "com/cinnober/msgcodec/blink/FieldDecodeException", "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(tryAfter); }; Accessor<?, ?> accessor = field.getAccessor(); if (isPublicFieldAccessor(accessor)) { Field f = ((FieldAccessor) accessor).getField(); mv.visitVarInsn(ALOAD, 2); // instance // value readValue.run(); // store mv.visitFieldInsn(PUTFIELD, Type.getInternalName(f.getDeclaringClass()), f.getName(), Type.getDescriptor(f.getType())); } else if (accessor.getClass() == CreateAccessor.class) { mv.visitVarInsn(ALOAD, 2); // instance Label tryStart = new Label(); Label tryEnd = new Label(); Label tryCatch = new Label(); Label tryAfter = new Label(); mv.visitTryCatchBlock(tryStart, tryEnd, tryCatch, "java/lang/Exception"); mv.visitLabel(tryStart); mv.visitVarInsn(ALOAD, 1); // input stream generateDecodeDummy(mv, 1, nextVar, field.isRequired(), field.getType(), javaClass, field.getComponentJavaClass(), schema, genClassInternalName, group.getName() + "." + field.getName(), javaClassCodec); mv.visitInsn(POP); mv.visitLabel(tryEnd); mv.visitJumpInsn(GOTO, tryAfter); mv.visitLabel(tryCatch); int caughtExVar = nextVar.next(); mv.visitVarInsn(ASTORE, caughtExVar); mv.visitTypeInsn(NEW, "com/cinnober/msgcodec/blink/FieldDecodeException"); mv.visitInsn(DUP); mv.visitLdcInsn(field.getName()); mv.visitVarInsn(ALOAD, caughtExVar); mv.visitMethodInsn(INVOKESPECIAL, "com/cinnober/msgcodec/blink/FieldDecodeException", "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(tryAfter); mv.visitInsn(POP); } else { // accessor mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, genClassInternalName, "accessor_" + group.getName() + "_" + field.getName(), "Lcom/cinnober/msgcodec/Accessor;"); // instance mv.visitVarInsn(ALOAD, 2); // instance // value readValue.run(); if (javaClass.isPrimitive()) { box(mv, javaClass); } // store mv.visitMethodInsn(INVOKEINTERFACE, "com/cinnober/msgcodec/Accessor", "setValue", "(Ljava/lang/Object;Ljava/lang/Object;)V", true); } } mv.visitInsn(RETURN); mv.visitMaxs(6, nextVar.get()); mv.visitEnd(); } }
From source file:com.facebook.presto.byteCode.control.TryCatch.java
License:Apache License
@Override public void accept(MethodVisitor visitor) { LabelNode tryStart = new LabelNode("tryStart"); LabelNode tryEnd = new LabelNode("tryEnd"); LabelNode handler = new LabelNode("handler"); LabelNode done = new LabelNode("done"); Block block = new Block(context); // try block/*from ww w . j a va2 s . com*/ block.visitLabel(tryStart).append(tryNode).visitLabel(tryEnd).gotoLabel(done); // handler block block.visitLabel(handler).append(catchNode); // all done block.visitLabel(done); block.accept(visitor); visitor.visitTryCatchBlock(tryStart.getLabel(), tryEnd.getLabel(), handler.getLabel(), exceptionName); }
From source file:com.github.stokito.gag.agent.ThisHadBetterGenerator.java
License:Apache License
private static void visitDeathStarPlans(MethodVisitor mv, boolean be, LocalVarInfo param, Label okay) { if (param.getType().getSort() != Type.OBJECT) { throw new AnnotationStateError("Unsupported type: " + param.getType()); }//from ww w . ja v a2s .c o m Label notOkay = new Label(); // See if the param type matches a DeathStarPlans type mv.visitVarInsn(ALOAD, param.getIndex()); mv.visitMethodInsn(INVOKEVIRTUAL, param.getType().getInternalName(), "getClass", "()Ljava/lang/Class;"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;"); mv.visitLdcInsn("DeathStarPlans|TheDeathStarPlans|.*[\\.$]DeathStarPlans|.*[\\.$]TheDeathStarPlans"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "matches", "(Ljava/lang/String;)Z"); if (be) { // If the param type does not match a DeathStarPlans type, that's not okay, go to exception. mv.visitJumpInsn(FALSE, notOkay); } else { // If the param type does not match a DeathStarPlans type, then that's okay, skip exception. mv.visitJumpInsn(FALSE, okay); } // At this point, the param type matches a DeathStarPlans type. // So check if the param has an isStolen method. Label start = new Label(); Label end = new Label(); Label handler = new Label(); mv.visitTryCatchBlock(start, end, handler, null); mv.visitLabel(start); mv.visitVarInsn(ALOAD, param.getIndex()); mv.visitMethodInsn(INVOKEVIRTUAL, param.getType().getInternalName(), "getClass", "()Ljava/lang/Class;"); mv.visitLdcInsn("isStolen"); mv.visitInsn(ACONST_NULL); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;"); mv.visitVarInsn(ALOAD, param.getIndex()); mv.visitInsn(ACONST_NULL); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z"); mv.visitJumpInsn(be ? TRUE : FALSE, okay); mv.visitLabel(end); mv.visitJumpInsn(GOTO, notOkay); mv.visitLabel(handler); mv.visitInsn(POP); mv.visitLabel(notOkay); }
From source file:com.github.wolf480pl.mias4j.MakeTestMH.java
License:Open Source License
public static byte[] dump() throws Exception { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); MethodVisitor mv; cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "TestMH", null, "java/lang/Object", null); cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", ACC_PUBLIC + ACC_FINAL + ACC_STATIC); {//from w w w. jav a2 s. c o m 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 + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Throwable"); mv.visitLabel(l0); /*mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodHandles", "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;", false); mv.visitLdcInsn(Type.getType("Ljava/io/PrintStream;")); mv.visitLdcInsn("println"); mv.visitFieldInsn(GETSTATIC, "java/lang/Void", "TYPE", "Ljava/lang/Class;"); mv.visitLdcInsn(Type.getType("Ljava/lang/String;")); mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodType", "methodType", "(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/invoke/MethodType;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findVirtual", "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;", false);*/ mv.visitLdcInsn(new Handle(H_INVOKEVIRTUAL, "java/io/PrintStream", "println", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class)))); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("hey"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invoke", "(Ljava/io/PrintStream;Ljava/lang/String;)V", false); mv.visitLabel(l1); Label l3 = new Label(); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable", "printStackTrace", "()V", false); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(RETURN); mv.visitMaxs(5, 2); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
From source file:com.googlecode.d2j.converter.IR2JConverter.java
License:Apache License
/** * an empty try-catch block will cause other crash, we check this by finding non-label stmts between * {@link Trap#start} and {@link Trap#end}. if find we add the try-catch or we drop the try-catch. * //from w w w . java 2s.com * @param ir * @param asm */ private void reBuildTryCatchBlocks(IrMethod ir, MethodVisitor asm) { for (Trap trap : ir.traps) { boolean needAdd = false; for (Stmt p = trap.start.getNext(); p != null && p != trap.end; p = p.getNext()) { if (p.st != ST.LABEL) { needAdd = true; break; } } if (needAdd) { for (int i = 0; i < trap.handlers.length; i++) { String type = trap.types[i]; asm.visitTryCatchBlock((Label) trap.start.tag, (Label) trap.end.tag, (Label) trap.handlers[i].tag, type == null ? null : toInternal(type)); } } } }