List of usage examples for org.objectweb.asm Opcodes INVOKEVIRTUAL
int INVOKEVIRTUAL
To view the source code for org.objectweb.asm Opcodes INVOKEVIRTUAL.
Click Source Link
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java
License:Open Source License
/** * This method could be used to generate debug outputs in the generated code in the form: <br> * <code>//w w w.j a v a 2s.c o m * Sytsem.out.println(message); * </code> * @param message * @return */ protected InsnList getInstructionsForDebugOutput(String message) { InsnList instructions = new InsnList(); instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;")); instructions.add(new LdcInsnNode(message)); instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false)); return instructions; }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AddGlobalTeamActivationAdapter.java
License:Open Source License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { synchronized (AddGlobalTeamActivationAdapter.class) { if (!done && isMainMethod(name, desc, access)) { done = true;// w ww.j ava 2s .c o m final MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, null, null); return new AdviceAdapter(this.api, methodVisitor, access, name, desc) { @Override protected void onMethodEnter() { List<String> teams = getTeamsFromConfigFile(); for (String aTeam : teams) { Label start, end, typeHandler, ctorHandler, after; String aTeamSlash = aTeam.replace('.', '/'); // new SomeTeam(): methodVisitor.visitLabel(start = new Label()); methodVisitor.visitTypeInsn(Opcodes.NEW, aTeamSlash); // .activate(Team.ALL_THREADS): methodVisitor.visitInsn(Opcodes.DUP); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, aTeamSlash, "<init>", "()V", false); methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, ClassNames.TEAM_SLASH, "ALL_THREADS", "Ljava/lang/Thread;"); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, aTeamSlash, "activate", "(Ljava/lang/Thread;)V", false); methodVisitor.visitLabel(end = new Label()); methodVisitor.visitJumpInsn(Opcodes.GOTO, after = new Label()); // catch (ClassNotFoundException, NoClassDefFoundError): // System.err.println(...) methodVisitor.visitLabel(typeHandler = new Label()); methodVisitor.visitInsn(Opcodes.POP); // discard the exception methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;"); methodVisitor.visitLdcInsn("Config error: Team class '" + aTeam + "' in config file '" + TEAM_CONFIG_FILE + "' can not be found!"); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); methodVisitor.visitJumpInsn(Opcodes.GOTO, after); methodVisitor.visitTryCatchBlock(start, end, typeHandler, "java/lang/ClassNotFoundException"); // dup to avoid stackmap errors (ASM bug at 1.8) methodVisitor.visitLabel(typeHandler = new Label()); methodVisitor.visitInsn(Opcodes.POP); // discard the exception methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;"); methodVisitor.visitLdcInsn("Config error: Team class '" + aTeam + "' in config file '" + TEAM_CONFIG_FILE + "' can not be found!"); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); methodVisitor.visitJumpInsn(Opcodes.GOTO, after); // methodVisitor.visitTryCatchBlock(start, end, typeHandler, "java/lang/NoClassDefFoundError"); // catch (NoSuchMethodError): // System.err.println(...) methodVisitor.visitLabel(ctorHandler = new Label()); methodVisitor.visitInsn(Opcodes.POP); // discard the exception methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;"); methodVisitor.visitLdcInsn( "Activation failed: Team class '" + aTeam + "' has no default constuctor!"); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); methodVisitor.visitTryCatchBlock(start, end, ctorHandler, "java/lang/NoSuchMethodError"); methodVisitor.visitLabel(after); } } @Override public void visitMaxs(int maxStack, int maxLocals) { super.visitMaxs(Math.max(maxStack, 3), maxLocals); } }; } return null; } }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AsmTypeHelper.java
License:Open Source License
public static AbstractInsnNode getUnboxingInstructionForType(Type primitiveType, String objectType) { String methodName = primitiveType.getClassName() + "Value"; String desc = Type.getMethodDescriptor(primitiveType, new Type[] {}); return new MethodInsnNode(Opcodes.INVOKEVIRTUAL, objectType, methodName, desc, false); }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateAddRemoveRoleMethod.java
License:Open Source License
@Override protected boolean transform() { // void _OT$addRemoveRole(Object role, boolean isAdding) { MethodNode method = getMethod(ConstantMembers.addOrRemoveRole); final int ROLE_SLOT = 1, IS_ADDING_SLOT = 2; Label start = new Label(), end = new Label(); method.instructions.clear();//from w w w . ja va 2 s . c om method.visitLabel(start); // set = <initialized _OT$roleSet;> final int SET_SLOT = 3; method.visitLocalVariable("set", "Ljava/util/Set;", null, start, end, SET_SLOT); genGetInitializedRoleSet(method.instructions, SET_SLOT); // if (isAdding) { method.instructions.add(new IntInsnNode(Opcodes.ILOAD, IS_ADDING_SLOT)); LabelNode jumpToRemove = new LabelNode(); method.instructions.add(new JumpInsnNode(Opcodes.IFEQ, jumpToRemove)); // set.add(role); method.instructions.add(new IntInsnNode(Opcodes.ALOAD, SET_SLOT)); method.instructions.add(new IntInsnNode(Opcodes.ALOAD, ROLE_SLOT)); method.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ClassNames.HASH_SET_SLASH, "add", "(Ljava/lang/Object;)Z", false)); method.instructions.add(new InsnNode(Opcodes.POP)); LabelNode jumpToEnd = new LabelNode(); method.instructions.add(new JumpInsnNode(Opcodes.GOTO, jumpToEnd)); // } else { method.instructions.add(jumpToRemove); // set.remove(role); method.instructions.add(new IntInsnNode(Opcodes.ALOAD, SET_SLOT)); method.instructions.add(new IntInsnNode(Opcodes.ALOAD, ROLE_SLOT)); method.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ClassNames.HASH_SET_SLASH, "remove", "(Ljava/lang/Object;)Z", false)); method.instructions.add(new InsnNode(Opcodes.POP)); method.instructions.add(jumpToEnd); // } method.instructions.add(new InsnNode(Opcodes.RETURN)); method.visitLabel(end); // } // maxs are computed, maxStack from flow, maxLocals from localVariable-slots return true; }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateCallAllBindingsCallInOrgMethod.java
License:Open Source License
@Override public boolean transform() { MethodNode method = getMethod(orgMethod); if ((method.access & Opcodes.ACC_ABSTRACT) != 0) return false; // if (method.name.equals("<init>")) { // int size = method.instructions.size(); // for (int i = 0; i < size; i++) { // AbstractInsnNode insn = method.instructions.get(i); // System.out.println(insn+" "+insn.getOpcode()); // }//from w w w . ja v a2 s. c o m // } // System.out.println("================"); AbstractInsnNode insertBefore = null; if (orgMethod.getName().equals("<init>")) { // keep instructions, find insertion point: int last = method.instructions.size(); LabelNode callAll = new LabelNode(); while (--last >= 0) { if (method.instructions.get(last).getOpcode() == Opcodes.RETURN) { AbstractInsnNode ret = method.instructions.get(last); method.instructions.set(ret, callAll); insertBefore = callAll; break; } } if (insertBefore == null) throw new IllegalStateException("Insertion point for weaving into ctor not found!!!"); // FIXME: triggers NPE in MethodVisitor.visitMaxs // // replace RETURN with GOTO // for (int i=0; i<last; i++) { // AbstractInsnNode current = method.instructions.get(i); // if (current.getOpcode() == Opcodes.RETURN) // method.instructions.set(current, new JumpInsnNode(Opcodes.GOTO, callAll)); // } } else { method.instructions.clear(); } // start of try-block: InsnList newInstructions = new InsnList(); LabelNode start = new LabelNode(); newInstructions.add(start); // put this on the stack newInstructions.add(new IntInsnNode(Opcodes.ALOAD, 0)); // put boundMethodId on the stack newInstructions.add(createLoadIntConstant(boundMethodId)); Type[] args = Type.getArgumentTypes(method.desc); // box the arguments newInstructions.add(getBoxingInstructions(args, false)); // this.callAllBindings(boundMethodId, args); newInstructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, this.name, ConstantMembers.callAllBindingsClient.getName(), ConstantMembers.callAllBindingsClient.getSignature())); Type returnType = Type.getReturnType(method.desc); newInstructions.add(getUnboxingInstructionsForReturnValue(returnType)); if (insertBefore != null) { method.instructions.insertBefore(insertBefore, newInstructions); method.instructions.remove(insertBefore); // remove extra RETURN } else { method.instructions.add(newInstructions); } // if (method.name.equals("<init>")) { // int size = method.instructions.size(); // for (int i = 0; i < size; i++) { // AbstractInsnNode insn = method.instructions.get(i); // System.out.println(insn+" "+insn.getOpcode()); // } // } // catch and unwrap SneakyException: addCatchSneakyException(method, start); int localSlots = 0; int maxArgSize = 1; for (Type type : args) { int size = type.getSize(); localSlots += size; if (size == 2) maxArgSize = 2; } method.maxStack = args.length > 0 ? 5 + maxArgSize : 3; method.maxLocals = localSlots + 1; return true; }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateCallAllBindingsCallInOrgMethod.java
License:Open Source License
TryCatchBlockNode getCatchBlock(InsnList instructions, LabelNode start, Method method) { // end (exclusive) of try-block LabelNode end = new LabelNode(); instructions.add(end);/*from ww w.ja va2 s . c om*/ // catch (SneakyException e) { e.rethrow(); } LabelNode catchSneaky = new LabelNode(); instructions.add(catchSneaky); instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ClassNames.SNEAKY_EXCEPTION_SLASH, ClassNames.RETHROW_SELECTOR, ClassNames.RETHROW_SIGNATURE, false)); // never reached, just to please the verifier: Type returnType = Type.getReturnType(method.getSignature()); instructions.add(getReturnInsn(returnType)); return new TryCatchBlockNode(start, end, catchSneaky, ClassNames.SNEAKY_EXCEPTION_SLASH); }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateMethodAccessAdapter.java
License:Open Source License
@Override public boolean transform() { MethodNode methodNode = getMethod(method); InsnList instructions = new InsnList(); if (isConstructor) { // create empty object for constructor invocation: instructions.add(new TypeInsnNode(Opcodes.NEW, name)); instructions.add(new InsnNode(Opcodes.DUP)); } else if (!method.isStatic()) { //put "this" on the stack for a non-static method instructions.add(new IntInsnNode(Opcodes.ALOAD, 0)); }/*w w w . j a v a2 s . c om*/ //Unbox arguments Type[] args = Type.getArgumentTypes(methodNode.desc); if (args.length > 0) { for (int i = 0; i < args.length; i++) { instructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + 2)); instructions.add(createLoadIntConstant(i)); instructions.add(new InsnNode(Opcodes.AALOAD)); Type arg = args[i]; if (arg.getSort() != Type.ARRAY && arg.getSort() != Type.OBJECT) { String objectType = AsmTypeHelper.getObjectType(arg); instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType)); instructions.add(AsmTypeHelper.getUnboxingInstructionForType(arg, objectType)); } else { instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, arg.getInternalName())); } } } //call original method int opcode = Opcodes.INVOKEVIRTUAL; if (method.isStatic()) { opcode = Opcodes.INVOKESTATIC; } else if (isConstructor) { opcode = Opcodes.INVOKESPECIAL; } instructions.add(new MethodInsnNode(opcode, name, method.getName(), method.getSignature())); //box return value Type returnType = Type.getReturnType(methodNode.desc); if (returnType.getSort() != Type.OBJECT && returnType.getSort() != Type.ARRAY && returnType.getSort() != Type.VOID) { instructions.add(AsmTypeHelper.getBoxingInstructionForType(returnType)); instructions.add(new InsnNode(Opcodes.ARETURN)); } else if (returnType.getSort() == Type.VOID && !isConstructor) { instructions.add(new InsnNode(Opcodes.ACONST_NULL)); instructions.add(new InsnNode(Opcodes.ARETURN)); } else { instructions.add(new InsnNode(Opcodes.ARETURN)); } //add the instructions to a new label in the existing switch MethodNode access = getMethod(this.access); addNewLabelToSwitch(access.instructions, instructions, accessId); return true; }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateSwitchForAccessAdapter.java
License:Open Source License
@Override protected void addPreSwitchInstructions(MethodNode method) { // put "accessId" on the stack method.instructions.add(new IntInsnNode(Opcodes.ILOAD, getFirstArgIndex())); // put "caller".getClass() on the stack method.instructions.add(new IntInsnNode(Opcodes.ALOAD, getFirstArgIndex() + 3)); method.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false)); // call "getMemberId(accessId, callerClass) method.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ClassNames.TEAM_MANAGER_SLASH, ConstantMembers.getMemberId.getName(), ConstantMembers.getMemberId.getSignature(), false)); }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateSwitchForCallAllBindingsNode.java
License:Open Source License
@Override protected void addPostSwitchInstructions(MethodNode method) { method.instructions.add(gotoLabel);/*from www. j a v a2 s.c o m*/ method.instructions.add(new IntInsnNode(Opcodes.ALOAD, 0)); args = Type.getArgumentTypes(method.desc); int length = args.length; for (int i = 0; i < length; i++) { Type arg = args[i]; method.instructions.add(new IntInsnNode(arg.getOpcode(Opcodes.ILOAD), i + 1)); } // return callOrig(boundMethodId, args); method.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, name, ConstantMembers.callOrig.getName(), ConstantMembers.callOrig.getSignature())); method.instructions.add(new InsnNode(Opcodes.ARETURN)); }
From source file:org.elasticsearch.plan.a.Caster.java
License:Apache License
void writeTransform(final MethodVisitor visitor, final Transform transform) { final Class clazz = transform.method.owner.clazz; final java.lang.reflect.Method method = transform.method.method; final String name = method.getName(); final String internal = transform.method.owner.internal; final String descriptor = transform.method.descriptor; final Type upcast = transform.upcast; final Type downcast = transform.downcast; if (upcast != null) { visitor.visitTypeInsn(Opcodes.CHECKCAST, upcast.internal); }/*from w ww .j a v a 2 s . c o m*/ if (java.lang.reflect.Modifier.isStatic(method.getModifiers())) { visitor.visitMethodInsn(Opcodes.INVOKESTATIC, internal, name, descriptor, false); } else if (java.lang.reflect.Modifier.isInterface(clazz.getModifiers())) { visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, internal, name, descriptor, true); } else { visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, internal, name, descriptor, false); } if (downcast != null) { visitor.visitTypeInsn(Opcodes.CHECKCAST, downcast.internal); } }