List of usage examples for org.objectweb.asm MethodVisitor visitInsn
public void visitInsn(final int opcode)
From source file:com.googlecode.d2j.converter.IR2JConverter.java
License:Apache License
private static void reBuildE1Expression(E1Expr e1, MethodVisitor asm) { accept(e1.getOp(), asm);//from w w w.java 2 s . c om switch (e1.vt) { case STATIC_FIELD: { FieldExpr fe = (FieldExpr) e1; asm.visitFieldInsn(GETSTATIC, toInternal(fe.owner), fe.name, fe.type); break; } case FIELD: { FieldExpr fe = (FieldExpr) e1; asm.visitFieldInsn(GETFIELD, toInternal(fe.owner), fe.name, fe.type); break; } case NEW_ARRAY: { TypeExpr te = (TypeExpr) e1; switch (te.type.charAt(0)) { case '[': case 'L': asm.visitTypeInsn(ANEWARRAY, toInternal(te.type)); break; case 'Z': asm.visitIntInsn(NEWARRAY, T_BOOLEAN); break; case 'B': asm.visitIntInsn(NEWARRAY, T_BYTE); break; case 'S': asm.visitIntInsn(NEWARRAY, T_SHORT); break; case 'C': asm.visitIntInsn(NEWARRAY, T_CHAR); break; case 'I': asm.visitIntInsn(NEWARRAY, T_INT); break; case 'F': asm.visitIntInsn(NEWARRAY, T_FLOAT); break; case 'J': asm.visitIntInsn(NEWARRAY, T_LONG); break; case 'D': asm.visitIntInsn(NEWARRAY, T_DOUBLE); break; } } break; case CHECK_CAST: case INSTANCE_OF: { TypeExpr te = (TypeExpr) e1; asm.visitTypeInsn(e1.vt == VT.CHECK_CAST ? CHECKCAST : INSTANCEOF, toInternal(te.type)); } break; case CAST: { CastExpr te = (CastExpr) e1; cast2(e1.op.valueType, te.to, asm); } break; case LENGTH: asm.visitInsn(ARRAYLENGTH); break; case NEG: asm.visitInsn(getOpcode(e1, INEG)); break; } }
From source file:com.googlecode.d2j.converter.IR2JConverter.java
License:Apache License
private static void reBuildE2Expression(E2Expr e2, MethodVisitor asm) { String type = e2.op2.valueType; accept(e2.op1, asm);/*from ww w . j a va2 s.c om*/ if ((e2.vt == VT.ADD || e2.vt == VT.SUB) && e2.op2.vt == VT.CONSTANT) { // [x + (-1)] to [x - 1] // [x - (-1)] to [x + 1] Constant constant = (Constant) e2.op2; String t = constant.valueType; switch (t.charAt(0)) { case 'S': case 'B': case 'I': { int s = (Integer) constant.value; if (s < 0) { asm.visitLdcInsn(-s); asm.visitInsn(getOpcode(type, e2.vt == VT.ADD ? ISUB : IADD)); return; } } break; case 'F': { float s = (Float) constant.value; if (s < 0) { asm.visitLdcInsn(-s); asm.visitInsn(getOpcode(type, e2.vt == VT.ADD ? ISUB : IADD)); return; } } break; case 'J': { long s = (Long) constant.value; if (s < 0) { asm.visitLdcInsn(-s); asm.visitInsn(getOpcode(type, e2.vt == VT.ADD ? ISUB : IADD)); return; } } break; case 'D': { double s = (Double) constant.value; if (s < 0) { asm.visitLdcInsn(-s); asm.visitInsn(getOpcode(type, e2.vt == VT.ADD ? ISUB : IADD)); return; } } break; } } accept(e2.op2, asm); String tp1 = e2.op1.valueType; switch (e2.vt) { case ARRAY: String tp2 = e2.valueType; if (tp1.charAt(0) == '[') { asm.visitInsn(getOpcode(tp1.substring(1), IALOAD)); } else { asm.visitInsn(getOpcode(tp2, IALOAD)); } break; case ADD: asm.visitInsn(getOpcode(type, IADD)); break; case SUB: asm.visitInsn(getOpcode(type, ISUB)); break; case DIV: asm.visitInsn(getOpcode(type, IDIV)); break; case MUL: asm.visitInsn(getOpcode(type, IMUL)); break; case REM: asm.visitInsn(getOpcode(type, IREM)); break; case AND: asm.visitInsn(getOpcode(type, IAND)); break; case OR: asm.visitInsn(getOpcode(type, IOR)); break; case XOR: asm.visitInsn(getOpcode(type, IXOR)); break; case SHL: asm.visitInsn(getOpcode(tp1, ISHL)); break; case SHR: asm.visitInsn(getOpcode(tp1, ISHR)); break; case USHR: asm.visitInsn(getOpcode(tp1, IUSHR)); break; case LCMP: asm.visitInsn(LCMP); break; case FCMPG: asm.visitInsn(FCMPG); break; case DCMPG: asm.visitInsn(DCMPG); break; case FCMPL: asm.visitInsn(FCMPL); break; case DCMPL: asm.visitInsn(DCMPL); break; } }
From source file:com.googlecode.d2j.converter.IR2JConverter.java
License:Apache License
private static void cast2(String t1, String t2, MethodVisitor asm) { if (t1.equals(t2)) { return;/*from w w w. j a va2s . c o m*/ } switch (t1.charAt(0)) { case 'Z': case 'B': case 'C': case 'S': case 'I': { switch (t2.charAt(0)) { case 'F': asm.visitInsn(I2F); break; case 'J': asm.visitInsn(I2L); break; case 'D': asm.visitInsn(I2D); break; case 'C': asm.visitInsn(I2C); break; case 'B': asm.visitInsn(I2B); break; case 'S': asm.visitInsn(I2S); break; } } break; case 'J': { switch (t2.charAt(0)) { case 'I': asm.visitInsn(L2I); break; case 'F': asm.visitInsn(L2F); break; case 'D': asm.visitInsn(L2D); break; } } break; case 'D': { switch (t2.charAt(0)) { case 'I': asm.visitInsn(D2I); break; case 'F': asm.visitInsn(D2F); break; case 'J': asm.visitInsn(D2L); break; } } break; case 'F': { switch (t2.charAt(0)) { case 'I': asm.visitInsn(F2I); break; case 'J': asm.visitInsn(F2L); break; case 'D': asm.visitInsn(F2D); break; } break; } } }
From source file:com.googlecode.d2j.dex.BaseDexExceptionHandler.java
License:Apache License
@Override public void handleMethodTranslateException(Method method, DexMethodNode methodNode, MethodVisitor mv, Exception e) {//from w ww. j ava 2 s . c o m // replace the generated code with // 'return new RuntimeException("D2jFail translate: xxxxxxxxxxxxx");' StringWriter s = new StringWriter(); s.append("d2j fail translate: "); e.printStackTrace(new PrintWriter(s)); String msg = s.toString(); mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException"); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn(msg); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Runtime", "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(Opcodes.ATHROW); }
From source file:com.googlecode.ddom.weaver.asm.MethodVisitorTee.java
License:Apache License
public void visitInsn(int opcode) { for (MethodVisitor visitor : visitors) { visitor.visitInsn(opcode); } }
From source file:com.googlecode.ddom.weaver.compound.CompoundClassGenerator.java
License:Apache License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, name, desc, signature, exceptions); if (mv != null) { Type[] argumentTypes = Type.getArgumentTypes(desc); mv.visitCode();// ww w . j av a2 s. c o m Label l0 = new Label(); mv.visitLabel(l0); for (int i = 0; i < componentClasses.length; i++) { String componentClass = Util.classNameToInternalName(componentClasses[i]); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, className, "c" + i, "L" + componentClass + ";"); for (int j = 0; j < argumentTypes.length; j++) { mv.visitVarInsn(argumentTypes[j].getOpcode(Opcodes.ILOAD), j + 1); } mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, ifaceName, name, desc); } mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0); mv.visitMaxs(argumentTypes.length + 1, argumentTypes.length + 1); mv.visitEnd(); } return null; }
From source file:com.googlecode.ddom.weaver.compound.CompoundClassGenerator.java
License:Apache License
@Override public void visitEnd() { for (int i = 0; i < componentClasses.length; i++) { String componentClass = Util.classNameToInternalName(componentClasses[i]); FieldVisitor fv = cv.visitField(Opcodes.ACC_PRIVATE, "c" + i, "L" + componentClass + ";", null, null); if (fv != null) { fv.visitEnd();/* w ww . ja va 2 s. c o m*/ } } MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); for (int i = 0; i < componentClasses.length; i++) { String componentClass = Util.classNameToInternalName(componentClasses[i]); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitTypeInsn(Opcodes.NEW, componentClass); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, componentClass, "<init>", "()V"); mv.visitFieldInsn(Opcodes.PUTFIELD, className, "c" + i, "L" + componentClass + ";"); } mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0); mv.visitMaxs(3, 1); mv.visitEnd(); } cv.visitEnd(); }
From source file:com.googlecode.ddom.weaver.ext.ModelExtensionClass.java
License:Apache License
public void accept(ClassVisitor classVisitor) { ImplementationInfo implementationInfo = info.getImplementation().get(ImplementationInfo.class); String name = Util.classNameToInternalName(info.getClassName()); String superName = Util.classNameToInternalName(info.getSuperClassName()); classVisitor.visit(Opcodes.V1_5,//from ww w . ja v a 2s . c o m info.isAbstract() ? Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT : Opcodes.ACC_PUBLIC, name, null, superName, new String[] { Util.classNameToInternalName(info.getExtensionInterface().getClassInfo().getName()) }); for (ConstructorInfo constructor : implementationInfo.getConstructors()) { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", constructor.getDescriptor(), constructor.getSignature(), constructor.getExceptions()); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); Type[] argumentTypes = constructor.getArgumentTypes(); for (int i = 0; i < argumentTypes.length; i++) { mv.visitVarInsn(argumentTypes[i].getOpcode(Opcodes.ILOAD), i + 1); } mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", constructor.getDescriptor()); mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0); for (int i = 0; i < argumentTypes.length; i++) { mv.visitLocalVariable("arg" + i, argumentTypes[i].getDescriptor(), null, l0, l1, i + 1); } mv.visitMaxs(argumentTypes.length + 1, argumentTypes.length + 1); mv.visitEnd(); } } classVisitor.visitEnd(); }
From source file:com.googlecode.ddom.weaver.ext.ModelExtensionFactoryDelegateImplementation.java
License:Apache License
public void accept(ClassVisitor classVisitor) { String factoryName = Util .classNameToInternalName(modelExtensionClassInfo.getFactoryDelegateImplementationClassName()); classVisitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, factoryName, null, "java/lang/Object", new String[] { Util.classNameToInternalName(implementationInfo.getFactoryDelegateInterfaceName()) }); String className = Util.classNameToInternalName(modelExtensionClassInfo.getClassName()); {/*www . j a v a 2s. co m*/ MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } } for (ConstructorInfo constructor : implementationInfo.getConstructors()) { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "create", constructor.getFactoryDelegateMethodDescriptor(), constructor.getSignature(), constructor.getExceptions()); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitTypeInsn(Opcodes.NEW, className); mv.visitInsn(Opcodes.DUP); Type[] argumentTypes = constructor.getArgumentTypes(); for (int i = 0; i < argumentTypes.length; i++) { mv.visitVarInsn(argumentTypes[i].getOpcode(Opcodes.ILOAD), i + 1); } mv.visitMethodInsn(Opcodes.INVOKESPECIAL, className, "<init>", constructor.getDescriptor()); mv.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + factoryName + ";", null, l0, l1, 0); for (int i = 0; i < argumentTypes.length; i++) { mv.visitLocalVariable("arg" + i, argumentTypes[i].getDescriptor(), null, l0, l1, i + 1); } mv.visitMaxs(argumentTypes.length + 2, argumentTypes.length + 1); mv.visitEnd(); } } classVisitor.visitEnd(); }
From source file:com.googlecode.ddom.weaver.ext.ModelExtensionFactoryImplementation.java
License:Apache License
public void accept(ClassVisitor classVisitor) { // Note: the name chosen here must match what is expected in ExtensionFactoryLocator String name = Util.classNameToInternalName(implementationInfo.getFactoryInterface().getName() + "$$Impl"); String factoryInterfaceName = Util .classNameToInternalName(implementationInfo.getFactoryInterface().getName()); classVisitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object", new String[] { factoryInterfaceName }); {//from w w w .ja v a 2s . c o m FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC, "INSTANCE", "L" + factoryInterfaceName + ";", null, null); if (fw != null) { fw.visitEnd(); } } { FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "delegates", "Ljava/util/Map;", null, null); if (fw != null) { fw.visitEnd(); } } { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "<init>", "()V", null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); // Call constructor from superclass mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); // Create delegates map mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V"); mv.visitFieldInsn(Opcodes.PUTFIELD, name, "delegates", "Ljava/util/Map;"); // Populate delegates map for (ModelExtensionInfo modelExtensionInfo : implementationInfo.getModelExtensions()) { for (ModelExtensionInterfaceInfo extensionInterface : modelExtensionInfo .getExtensionInterfaces()) { if (!extensionInterface.isAbstract()) { // TODO: this is stupid; we should not recreate the info object here ModelExtensionClassInfo modelExtensionClassInfo = new ModelExtensionClassInfo( implementationInfo.getImplementation(), modelExtensionInfo.getRootInterface(), extensionInterface); String factoryDelegateImplName = Util.classNameToInternalName( modelExtensionClassInfo.getFactoryDelegateImplementationClassName()); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;"); mv.visitLdcInsn(Type.getObjectType(Util.classNameToInternalName( modelExtensionClassInfo.getExtensionInterface().getClassInfo().getName()))); mv.visitTypeInsn(Opcodes.NEW, factoryDelegateImplName); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, factoryDelegateImplName, "<init>", "()V"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(Opcodes.POP); } } } mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0); mv.visitMaxs(4, 1); mv.visitEnd(); } } String factoryDelegateInterfaceName = Util .classNameToInternalName(implementationInfo.getFactoryDelegateInterfaceName()); String getDelegateDesc = "(Ljava/lang/Class;)L" + factoryDelegateInterfaceName + ";"; { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "getDelegate", getDelegateDesc, null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;"); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitTypeInsn(Opcodes.CHECKCAST, factoryDelegateInterfaceName); mv.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0); mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l1, 1); mv.visitMaxs(2, 2); mv.visitEnd(); } } String implementationName = Util.classNameToInternalName(implementationInfo.getImplementation().getName()); for (ConstructorInfo constructor : implementationInfo.getConstructors()) { Type[] constructorArgumentTypes = constructor.getArgumentTypes(); Type[] argumentTypes = new Type[constructorArgumentTypes.length + 1]; argumentTypes[0] = Type.getObjectType("java/lang/Class"); System.arraycopy(constructorArgumentTypes, 0, argumentTypes, 1, constructorArgumentTypes.length); MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "create", Type.getMethodDescriptor(Type.getObjectType(implementationName), argumentTypes), null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 1); Label l1 = new Label(); mv.visitJumpInsn(Opcodes.IFNONNULL, l1); mv.visitTypeInsn(Opcodes.NEW, implementationName); mv.visitInsn(Opcodes.DUP); for (int i = 0; i < constructorArgumentTypes.length; i++) { mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2); } mv.visitMethodInsn(Opcodes.INVOKESPECIAL, implementationName, "<init>", constructor.getDescriptor()); mv.visitInsn(Opcodes.ARETURN); mv.visitLabel(l1); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, "getDelegate", getDelegateDesc); for (int i = 0; i < constructorArgumentTypes.length; i++) { mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2); } mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, factoryDelegateInterfaceName, "create", constructor.getFactoryDelegateMethodDescriptor()); mv.visitInsn(Opcodes.ARETURN); Label l3 = new Label(); mv.visitLabel(l3); mv.visitLocalVariable("this", "L" + name + ";", null, l0, l3, 0); mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l3, 1); for (int i = 0; i < constructorArgumentTypes.length; i++) { mv.visitLocalVariable("arg" + i, constructorArgumentTypes[i].getDescriptor(), null, l0, l3, i + 2); } mv.visitMaxs(argumentTypes.length + 1, argumentTypes.length + 1); mv.visitEnd(); } } { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); if (mv != null) { mv.visitCode(); mv.visitTypeInsn(Opcodes.NEW, name); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, name, "<init>", "()V"); mv.visitFieldInsn(Opcodes.PUTSTATIC, name, "INSTANCE", "L" + factoryInterfaceName + ";"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 0); mv.visitEnd(); } } classVisitor.visitEnd(); }