List of usage examples for org.objectweb.asm Opcodes INVOKESPECIAL
int INVOKESPECIAL
To view the source code for org.objectweb.asm Opcodes INVOKESPECIAL.
Click Source Link
From source file:org.jacoco.core.runtime.RuntimeDataTest.java
License:Open Source License
@Test public void testGenerateAccessCall() throws Exception { final boolean[] probes = data.getExecutionData(Long.valueOf(1234), "Sample", 5).getProbes(); final ClassWriter writer = new ClassWriter(0); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object", new String[] { Type.getInternalName(Callable.class) }); // Constructor MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, new String[0]); mv.visitCode();//from ww w . j ava 2s . co m mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitFieldInsn(Opcodes.PUTFIELD, "Sample", "access", "Ljava/lang/Object;"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); // call() mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "Sample", "access", "Ljava/lang/Object;"); RuntimeData.generateAccessCall(1234, "Sample", 5, mv); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(6, 1); mv.visitEnd(); writer.visitField(Opcodes.ACC_PRIVATE, "access", "Ljava/lang/Object;", null, null); writer.visitEnd(); final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray()); Callable<?> callable = (Callable<?>) loader.getTargetClass().getConstructor(Object.class).newInstance(data); assertSame(probes, callable.call()); }
From source file:org.jacoco.core.runtime.URLStreamHandlerRuntime.java
License:Open Source License
public int generateDataAccessor(final long classid, final String classname, final int probecount, final MethodVisitor mv) { // The data accessor performs the following steps: ////from www . j a va2s .c om // final URL url = new URL(protocol, null, ""); // final URLConnection connection = url.openConnection(); // final Object[] args = new Object[3]; // args[0] = Long.valueOf(classid); // args[1] = classname; // args[2] = Integer.valueOf(probecount); // connection.equals(args); // final byte[] probedata = (byte[]) args[0]; RuntimeData.generateArgumentArray(classid, classname, probecount, mv); mv.visitInsn(Opcodes.DUP); // Stack[1]: [Ljava/lang/Object; // Stack[0]: [Ljava/lang/Object; mv.visitTypeInsn(Opcodes.NEW, "java/net/URL"); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn(protocol); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitLdcInsn(""); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/net/URL", "<init>", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); // Stack[2]: [Ljava/net/URL; // Stack[1]: [Ljava/lang/Object; // Stack[0]: [Ljava/lang/Object; mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/net/URL", "openConnection", "()Ljava/net/URLConnection;"); // Stack[2]: [Ljava/net/URLConnection; // Stack[1]: [Ljava/lang/Object; // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.SWAP); // Stack[2]: [Ljava/lang/Object; // Stack[1]: [Ljava/net/URLConnection; // Stack[0]: [Ljava/lang/Object; mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z"); // Stack[1]: Z; // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.POP); // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.ICONST_0); mv.visitInsn(Opcodes.AALOAD); mv.visitTypeInsn(Opcodes.CHECKCAST, InstrSupport.DATAFIELD_DESC); return 7; }
From source file:org.jboss.as.hibernate.MethodAdapter.java
License:Open Source License
@Override public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { if (rewriteSessionImplementor && hasSessionImplementor(desc) && (opcode == Opcodes.INVOKESPECIAL || opcode == Opcodes.INVOKEVIRTUAL)) { // if we have a user type calling a method from org.hibernate, we rewrite it to use SharedSessionContractImplementor logger.debugf("Deprecated Hibernate51CompatibilityTransformer transformed application classes in '%s', " + "class '%s' is calling method %s.%s, which must be changed to use SharedSessionContractImplementor as parameter.", moduleName, className, owner, name); mv.visitMethodInsn(opcode, owner, name, replaceSessionImplementor(desc), itf); transformedState.setClassTransformed(true); } else if (opcode == Opcodes.INVOKEINTERFACE && (owner.equals("org/hibernate/Session") || owner.equals("org/hibernate/BasicQueryContract")) && name.equals("getFlushMode") && desc.equals("()Lorg/hibernate/FlushMode;")) { logger.debugf("Deprecated Hibernate51CompatibilityTransformer transformed application classes in '%s', " + "class '%s' is calling %s.getFlushMode, which must be changed to call getHibernateFlushMode().", moduleName, className, owner); name = "getHibernateFlushMode"; mv.visitMethodInsn(opcode, owner, name, desc, itf); transformedState.setClassTransformed(true); } else if (opcode == Opcodes.INVOKEINTERFACE && owner.equals("org/hibernate/Query") && name.equals("getFirstResult") && desc.equals("()Ljava/lang/Integer;")) { logger.debugf("Deprecated Hibernate51CompatibilityTransformer transformed application classes in '%s', " + "class '%s', is calling org.hibernate.Query.getFirstResult, which must be changed to call getHibernateFirstResult() " + "so null can be returned when the value is uninitialized. Please note that if a negative value was set using " + "org.hibernate.Query.setFirstResult, then getHibernateFirstResult() will return 0.", moduleName, className);// w w w.j a va 2 s . co m name = "getHibernateFirstResult"; mv.visitMethodInsn(opcode, owner, name, desc, itf); transformedState.setClassTransformed(true); } else if (opcode == Opcodes.INVOKEINTERFACE && owner.equals("org/hibernate/Query") && name.equals("getMaxResults") && desc.equals("()Ljava/lang/Integer;")) { logger.debugf("Deprecated Hibernate51CompatibilityTransformer transformed application classes in '%s', " + "class '%s', is calling org.hibernate.Query.getMaxResults, which must be changed to call getHibernateMaxResults() " + "so that null will be returned when the value is uninitialized or ORM 5.1 org.hibernate.Query#setMaxResults was " + "used to set a value <= 0", moduleName, className); name = "getHibernateMaxResults"; mv.visitMethodInsn(opcode, owner, name, desc, itf); transformedState.setClassTransformed(true); } else if (!disableAmbiguousChanges && opcode == Opcodes.INVOKEINTERFACE && owner.equals("org/hibernate/Query") && name.equals("setFirstResult") && desc.equals("(I)Lorg/hibernate/Query;")) { logger.debugf("Deprecated Hibernate51CompatibilityTransformer transformed application classes in '%s', " + "class '%s', is calling org.hibernate.Query.setFirstResult, which must be changed to call setHibernateFirstResult() " + "so setting a value < 0 results in pagination starting with the 0th row as was done in Hibernate ORM 5.1 " + "(instead of throwing IllegalArgumentException as specified by JPA).", moduleName, className); name = "setHibernateFirstResult"; mv.visitMethodInsn(opcode, owner, name, desc, itf); transformedState.setClassTransformed(true); } else if (!disableAmbiguousChanges && opcode == Opcodes.INVOKEINTERFACE && owner.equals("org/hibernate/Query") && name.equals("setMaxResults") && desc.equals("(I)Lorg/hibernate/Query;")) { logger.debugf("Deprecated Hibernate51CompatibilityTransformer transformed application classes in '%s', " + "class '%s', is calling org.hibernate.Query.setMaxResults, which must be changed to call setHibernateMaxResults() " + "so that values <= 0 are treated the same as uninitialized. Review Hibernate ORM migration doc ", moduleName, className); name = "setHibernateMaxResults"; mv.visitMethodInsn(opcode, owner, name, desc, itf); transformedState.setClassTransformed(true); } else { mv.visitMethodInsn(opcode, owner, name, desc, itf); } }
From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java
License:Open Source License
/** * Generates the instruction to invoke a constructor. * * @param type the class in which the constructor is defined. * @param method the constructor to be invoked. *///from ww w.j ava 2 s. c om public void invokeConstructor(final Type type, final Method method) { invokeInsn(Opcodes.INVOKESPECIAL, type, method); }
From source file:org.jboss.byteman.agent.adapter.RuleTriggerAdapter.java
License:Open Source License
protected boolean isSuperOrSiblingConstructorCall(int opcode, String owner, String name) { return (opcode == Opcodes.INVOKESPECIAL && name.equals("<init>") && (superName.equals(owner) || className.equals(owner))); }
From source file:org.jboss.byteman.rule.expression.NewExpression.java
License:Open Source License
public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException { // make sure we are at the right source line compileContext.notifySourceLine(line); int currentStack = compileContext.getStackCount(); int expected = 1; int extraParams = 0; if (arrayDimCount == 0) { // ok, we need to create the new instance and then initialise it. // create the new instance -- adds 1 to stack String instantiatedClassName = type.getInternalName(); mv.visitTypeInsn(Opcodes.NEW, instantiatedClassName); compileContext.addStackCount(1); // copy the exception so we can init it mv.visitInsn(Opcodes.DUP);//from www . j a va2 s . com compileContext.addStackCount(1); int argCount = arguments.size(); // stack each of the arguments to the constructor for (int i = 0; i < argCount; i++) { Type argType = argumentTypes.get(i); Type paramType = paramTypes.get(i); int paramCount = (paramType.getNBytes() > 4 ? 2 : 1); // track extra storage used after type conversion extraParams += (paramCount); arguments.get(i).compile(mv, compileContext); compileTypeConversion(argType, paramType, mv, compileContext); } // construct the exception mv.visitMethodInsn(Opcodes.INVOKESPECIAL, instantiatedClassName, "<init>", getDescriptor()); // modify the stack height to account for the removed exception and params compileContext.addStackCount(-(extraParams + 1)); } else { // TODO !!! implement compilation for array types !!! if (arrayDimCount == 1) { // we can use a NEWARRAY or ANEWARRAY Type baseType = type.getBaseType(); // compile first array dimension adds 1 to stack arrayDims.get(0).compile(mv, compileContext); // compile new array op -- pops 1 and adds 1 to stack if (baseType.isObject()) { mv.visitTypeInsn(Opcodes.ANEWARRAY, baseType.getInternalName()); // } else if (baseType.isArray()) { // cannot happen!!! } else { int operand = 0; if (baseType.equals(Type.Z)) { operand = Opcodes.T_BOOLEAN; } else if (baseType.equals(Type.B)) { operand = Opcodes.T_BYTE; } else if (baseType.equals(Type.S)) { operand = Opcodes.T_SHORT; } else if (baseType.equals(Type.C)) { operand = Opcodes.T_CHAR; } else if (baseType.equals(Type.I)) { operand = Opcodes.T_INT; } else if (baseType.equals(Type.J)) { operand = Opcodes.T_LONG; } else if (baseType.equals(Type.F)) { operand = Opcodes.T_FLOAT; } else if (baseType.equals(Type.D)) { operand = Opcodes.T_DOUBLE; } mv.visitIntInsn(Opcodes.NEWARRAY, operand); } } else { // we need to use MULTIANEWARRAY for (int i = 0; i < arrayDimDefinedCount; i++) { // compile next array dimension adds 1 to stack arrayDims.get(i).compile(mv, compileContext); } // execute the MULTIANEWARRAY operation -- pops arrayDims operands and pushes 1 mv.visitMultiANewArrayInsn(type.getInternalName(), arrayDimDefinedCount); compileContext.addStackCount(1 - arrayDimDefinedCount); } } if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("NewExpression.compile : invalid stack height " + compileContext.getStackCount() + " expecting " + (currentStack + expected)); } }
From source file:org.jboss.byteman.rule.expression.ReturnExpression.java
License:Open Source License
public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException { // make sure we are at the right source line compileContext.notifySourceLine(line); Type valueType = (returnValue == null ? Type.VOID : returnValue.getType()); int currentStack = compileContext.getStackCount(); int expected = 1; // ok, we need to create the EarlyReturnException instance and then // initialise it using the appropriate return value or null if no // return value is needed. strictly we should maybe delay the // new until after computing the return expression so we avoid a new // if the expression throws an error. but that means we end up doing // stack manipulations so lets do it the easy way. // create am EarlyReturnException -- adds 1 to stack String exceptionClassName = Type.internalName(EarlyReturnException.class); mv.visitTypeInsn(Opcodes.NEW, exceptionClassName); compileContext.addStackCount(1);//from w ww. j a v a 2 s .c o m // copy the exception so we can initialise it -- adds 1 to stack mv.visitInsn(Opcodes.DUP); compileContext.addStackCount(1); // stack a string constant to initialise the exception with -- adds 1 to stack mv.visitLdcInsn("return from " + rule.getName()); compileContext.addStackCount(1); // stack any required return value or null -- adds 1 to stack but may use 2 slots if (returnValue != null) { returnValue.compile(mv, compileContext); // we may need to convert from the value type to the return type if (valueType != type) { compileTypeConversion(valueType, type, mv, compileContext); } if (type.isPrimitive()) { // we need an object not a primitive compileBox(Type.boxType(type), mv, compileContext); } } else { // just push null mv.visitInsn(Opcodes.ACONST_NULL); compileContext.addStackCount(1); } // construct the exception -- pops 3 mv.visitMethodInsn(Opcodes.INVOKESPECIAL, exceptionClassName, "<init>", "(Ljava/lang/String;Ljava/lang/Object;)V"); compileContext.addStackCount(-3); // check current stack and increment max stack if necessary if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("ReturnExpression.compile : invalid stack height " + compileContext.getStackCount() + " expecting " + (currentStack + expected)); } // now insert the throw instruction and decrement the stack height accordingly mv.visitInsn(Opcodes.ATHROW); compileContext.addStackCount(-1); }
From source file:org.jboss.byteman.rule.expression.ThrowExpression.java
License:Open Source License
public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException { // make sure we are at the right source line compileContext.notifySourceLine(line); int currentStack = compileContext.getStackCount(); int expected = 1; int extraParams = 0; // ok, we need to create the thrown exception instance and then // initialise it. // create the thrown exception instance -- adds 1 to stack String exceptionClassName = type.getInternalName(); mv.visitTypeInsn(Opcodes.NEW, exceptionClassName); compileContext.addStackCount(1);//from www .j a v a 2s.co m // copy the exception so we can init it mv.visitInsn(Opcodes.DUP); compileContext.addStackCount(1); int argCount = arguments.size(); // stack each of the arguments to the constructor for (int i = 0; i < argCount; i++) { Type argType = argumentTypes.get(i); Type paramType = paramTypes.get(i); int paramCount = (paramType.getNBytes() > 4 ? 2 : 1); // track extra storage used after type conversion extraParams += (paramCount); arguments.get(i).compile(mv, compileContext); compileTypeConversion(argType, paramType, mv, compileContext); } // construct the exception mv.visitMethodInsn(Opcodes.INVOKESPECIAL, exceptionClassName, "<init>", getDescriptor()); // modify the stack height to account for the removed exception and params compileContext.addStackCount(-(extraParams + 1)); if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("ThrowExpression.compile : invalid stack height " + compileContext.getStackCount() + " expecting " + (currentStack + expected)); } // now create a ThrowException to wrap the user exception // create the thrown exception instance -- adds 1 to stack [UE] --> [UE, THE] exceptionClassName = "org/jboss/byteman/rule/exception/ThrowException"; mv.visitTypeInsn(Opcodes.NEW, exceptionClassName); compileContext.addStackCount(1); // copy the ThrowException so we can init it [UE, THE] --> [THE, UE, THE] mv.visitInsn(Opcodes.DUP_X1); compileContext.addStackCount(1); // reverse the order of the top two words [THE, UE, THE] --> [THE, THE, UE] mv.visitInsn(Opcodes.SWAP); // construct the exception [THE, THE, UE] --> [UE] mv.visitMethodInsn(Opcodes.INVOKESPECIAL, exceptionClassName, "<init>", "(Ljava/lang/Throwable;)V"); // we should now have just the ThrowException on the stack compileContext.addStackCount(-2); if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("ThrowExpression.compile : invalid stack height " + compileContext.getStackCount() + " expecting " + (currentStack + expected)); } // now throw the exception and decrement the stack height mv.visitInsn(Opcodes.ATHROW); compileContext.addStackCount(-1); }
From source file:org.jetbrains.jet.codegen.JetTypeMapper.java
License:Apache License
public CallableMethod mapToCallableMethod(FunctionDescriptor functionDescriptor, boolean superCall, OwnerKind kind) {/*from w w w .j a v a 2 s. co m*/ if (functionDescriptor == null) return null; final DeclarationDescriptor functionParent = functionDescriptor.getOriginal().getContainingDeclaration(); JvmMethodSignature descriptor = mapSignature(functionDescriptor.getOriginal(), true, kind); String owner; String ownerForDefaultImpl; String ownerForDefaultParam; int invokeOpcode; ClassDescriptor thisClass; if (functionParent instanceof NamespaceDescriptor) { assert !superCall; boolean namespace = true; if (functionParent instanceof JavaNamespaceDescriptor) { namespace = ((JavaNamespaceDescriptor) functionParent).isNamespace(); } owner = NamespaceCodegen.getJVMClassName(DescriptorUtils.getFQName(functionParent), namespace); ownerForDefaultImpl = ownerForDefaultParam = owner; invokeOpcode = INVOKESTATIC; thisClass = null; } else if (functionDescriptor instanceof ConstructorDescriptor) { assert !superCall; ClassDescriptor containingClass = (ClassDescriptor) functionParent; owner = mapType(containingClass.getDefaultType(), OwnerKind.IMPLEMENTATION).getInternalName(); ownerForDefaultImpl = ownerForDefaultParam = owner; invokeOpcode = INVOKESPECIAL; thisClass = null; } else if (functionParent instanceof ClassDescriptor) { FunctionDescriptor declarationFunctionDescriptor = findAnyDeclaration(functionDescriptor); ClassDescriptor currentOwner = (ClassDescriptor) functionParent; ClassDescriptor declarationOwner = (ClassDescriptor) declarationFunctionDescriptor .getContainingDeclaration(); boolean originalIsInterface = CodegenUtil.isInterface(declarationOwner); boolean currentIsInterface = CodegenUtil.isInterface(currentOwner); ClassDescriptor receiver; if (currentIsInterface && !originalIsInterface) { receiver = declarationOwner; } else { receiver = currentOwner; } ClassDescriptor containingClass = (ClassDescriptor) functionParent; boolean isInterface = originalIsInterface && currentIsInterface; OwnerKind kind1 = isInterface && superCall ? OwnerKind.TRAIT_IMPL : OwnerKind.IMPLEMENTATION; Type type = mapType(receiver.getDefaultType(), OwnerKind.IMPLEMENTATION); owner = type.getInternalName(); ownerForDefaultParam = mapType(((ClassDescriptor) declarationOwner).getDefaultType(), OwnerKind.IMPLEMENTATION).getInternalName(); ownerForDefaultImpl = ownerForDefaultParam + (originalIsInterface ? JvmAbi.TRAIT_IMPL_SUFFIX : ""); invokeOpcode = isInterface ? (superCall ? Opcodes.INVOKESTATIC : Opcodes.INVOKEINTERFACE) : (superCall ? Opcodes.INVOKESPECIAL : Opcodes.INVOKEVIRTUAL); if (isInterface && superCall) { descriptor = mapSignature(functionDescriptor, false, OwnerKind.TRAIT_IMPL); owner += JvmAbi.TRAIT_IMPL_SUFFIX; } thisClass = receiver; } else { throw new UnsupportedOperationException("unknown function parent"); } final CallableMethod result = new CallableMethod(owner, ownerForDefaultImpl, ownerForDefaultParam, descriptor, invokeOpcode); result.setNeedsThis(thisClass); if (functionDescriptor.getReceiverParameter().exists()) { result.setNeedsReceiver(functionDescriptor); } return result; }
From source file:org.jvnet.jax_ws_commons.beans_generator.ambassador.impl.asm.ASMRequestBeanGenerator.java
License:Open Source License
/** * //from ww w . j av a2 s . c o m */ private void generateNoArgsConstructor() { MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); }