List of usage examples for org.objectweb.asm MethodVisitor visitLdcInsn
public void visitLdcInsn(final Object value)
From source file:org.jboss.byteman.rule.expression.NumericLiteral.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); if (type == Type.I) { int val = value.intValue(); // compile code to stack int value if (val >= -1 && val <= 5) { // we can use an iconst instruction mv.visitInsn(Opcodes.ICONST_0 + val); } else {//from www. jav a 2 s. c om // we have to add an integer constant to the constants pool mv.visitLdcInsn(value); } // we have only added 1 to the stack height compileContext.addStackCount(1); } else { // type = type.F float val = value.floatValue(); if (val == 0.0) { // we can use an fconst instruction mv.visitInsn(Opcodes.FCONST_0); } else if (val == 1.0) { // we can use an fconst instruction mv.visitInsn(Opcodes.FCONST_1); } else if (val == 2.0) { // we can use an fconst instruction mv.visitInsn(Opcodes.FCONST_2); } else { // we have to add a float constant to the constants pool mv.visitLdcInsn(value); } // we have only added 1 to the stack height compileContext.addStackCount(1); } }
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 w w . ja 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.StaticExpression.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; // compile a field access if (isPublicField) { String ownerType = Type.internalName(field.getDeclaringClass()); String fieldName = field.getName(); String fieldType = Type.internalName(field.getType(), true); mv.visitFieldInsn(Opcodes.GETSTATIC, ownerType, fieldName, fieldType); expected = (type.getNBytes() > 4 ? 2 : 1); compileContext.addStackCount(expected); } else {//from w ww . jav a 2 s. c o m // since this is a private field we need to do the access using reflection // stack the helper, a null owner and the field index mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitLdcInsn(fieldIndex); compileContext.addStackCount(3); // use the HelperAdapter method getAccessibleField to get the field value mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.internalName(HelperAdapter.class), "getAccessibleField", "(Ljava/lang/Object;I)Ljava/lang/Object;"); // we popped three words and added one object as result compileContext.addStackCount(-2); // convert Object to primitive or cast to subtype if required compileTypeConversion(Type.OBJECT, type, mv, compileContext); } }
From source file:org.jboss.byteman.rule.expression.StaticExpression.java
License:Open Source License
@Override public void compileAssign(MethodVisitor mv, CompileContext compileContext) throws CompileException { // make sure we are at the right source line compileContext.notifySourceLine(line); int currentStack = compileContext.getStackCount(); int size = (type.getNBytes() > 4 ? 2 : 1); // copy the value so we leave a result // increases stack height by size words if (size == 1) { mv.visitInsn(Opcodes.DUP);//from w w w .j a v a 2 s . c o m } else { mv.visitInsn(Opcodes.DUP2); } compileContext.addStackCount(size); // compile a static field update if (isPublicField) { String ownerType = Type.internalName(field.getDeclaringClass()); String fieldName = field.getName(); String fieldType = Type.internalName(field.getType(), true); compileContext.addStackCount(-size); mv.visitFieldInsn(Opcodes.PUTSTATIC, ownerType, fieldName, fieldType); } else { // since this is a private field we need to do the update using reflection // box the value to an object if necessary // [.. val(s) val(s) ==> val(s) valObj] if (type.isPrimitive()) { compileBox(Type.boxType(type), mv, compileContext); } // stack the helper and then swap it so it goes under the value // [.. val(s) valObj ==> val(s) valObj helper ==> val(s) helper valObj] mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.SWAP); // stack a null owner then swap it so it goes under the value // [val(s) helper valObj ==> val(s) helper valObj null ==> val(s) helper null valObj] mv.visitInsn(Opcodes.ACONST_NULL); mv.visitInsn(Opcodes.SWAP); // now stack the field index // [.. val(s) helper null valObj ==> val(s) helper null valObj index ] mv.visitLdcInsn(fieldIndex); // we added three more words compileContext.addStackCount(3); // use the HelperAdapter method setAccessibleField to set the field value mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.internalName(HelperAdapter.class), "setAccessibleField", "(Ljava/lang/Object;Ljava/lang/Object;I)V"); // we popped four args compileContext.addStackCount(-4); } if (compileContext.getStackCount() != currentStack) { throw new CompileException("StaticExpression.compileAssign : invalid stack height " + compileContext.getStackCount() + " expecting " + currentStack); } }
From source file:org.jboss.byteman.rule.expression.StringLiteral.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); // compile a load constant instruction mv.visitLdcInsn(text); compileContext.addStackCount(1);//from www . ja va 2 s . c o m }
From source file:org.jboss.byteman.rule.expression.StringPlusExpression.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); Expression oper0 = getOperand(0); Expression oper1 = getOperand(1); int currentStack = compileContext.getStackCount(); int expected = 1; // compile and type convert each operand n.b. the type conversion will ensure that // null operands are replaced with "null" oper0.compile(mv, compileContext);// w ww .ja va 2 s . c o m compileTypeConversion(oper0.getType(), type, mv, compileContext); oper1.compile(mv, compileContext); compileTypeConversion(oper1.getType(), type, mv, compileContext); // ok, we could optimize this for the case where the left or right operand is a String plus expression // by employing a StringBuffer but for now we will just evaluate the left and right operand and // then call concat to join them // add two strings leaving one string // if second operand is null replace it with "null" Label skiptarget = new Label(); // this adds a word then removes it -- do so to ensure the max height gets updated if need be mv.visitInsn(Opcodes.DUP); compileContext.addStackCount(1); // if it is not null we skip to the concat operation mv.visitJumpInsn(Opcodes.IFNONNULL, skiptarget); compileContext.addStackCount(-1); // it's null so we have to swap it fdr "null" mv.visitInsn(Opcodes.POP); mv.visitLdcInsn("null"); mv.visitLabel(skiptarget); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "concat", "(Ljava/lang/String;)Ljava/lang/String;"); compileContext.addStackCount(-1); if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("StringPlusExpression.compile : invalid stack height " + compileContext.getStackCount() + " expecting " + currentStack + expected); } }
From source file:org.jboss.byteman.rule.expression.Variable.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); // stack the current helper // stack the name for the variable // call the getBinding method mv.visitVarInsn(Opcodes.ALOAD, 0);//from www . j a va 2 s. co m mv.visitLdcInsn(name); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.internalName(HelperAdapter.class), "getBinding", "(Ljava/lang/String;)Ljava/lang/Object;"); // ok, we added 2 to the stack and then popped them leaving 1 compileContext.addStackCount(2); compileContext.addStackCount(-1); // perform any necessary type conversion if (type.isPrimitive()) { // cast down to the boxed type then do an unbox Type boxType = Type.boxType(type); compileObjectConversion(Type.OBJECT, boxType, mv, compileContext); compileUnbox(boxType, type, mv, compileContext); } else { // cast down to the required type compileObjectConversion(Type.OBJECT, type, mv, compileContext); } }
From source file:org.jboss.byteman.rule.expression.Variable.java
License:Open Source License
@Override public void compileAssign(MethodVisitor mv, CompileContext compileContext) throws CompileException { // make sure we are at the right source line compileContext.notifySourceLine(line); int currentStack = compileContext.getStackCount(); int size = ((type.getNBytes() > 4) ? 2 : 1); int max;//www .java 2 s.c o m // value to be assigned is TOS and will already be coerced to the correct value type // copy it so we leave it as a a return value on the stack if (size == 2) { // [... val1 val2 ==> ... val1 val2 val1 val2] mv.visitInsn(Opcodes.DUP2); } else { // [... val ==> ... val val] mv.visitInsn(Opcodes.DUP); } // stack the current helper then insert it below the value mv.visitVarInsn(Opcodes.ALOAD, 0); if (size == 2) { // use a DUP_X2 to push a copy below the value then pop the redundant value // [... val1 val2 val1 val2 helper ==> ... val1 val2 helper val1 val2 helper] mv.visitInsn(Opcodes.DUP_X2); // [... val1 val2 helper val1 val2 helper ==> ... val1 val2 helper val1 val2] mv.visitInsn(Opcodes.POP); } else { // we can just swap the two values // [... val val helper ==> ... val helper val] mv.visitInsn(Opcodes.SWAP); } // stack the name for the variable and swap below the value mv.visitLdcInsn(name); if (size == 2) { // use a DUP_X2 to push a copy below the value then pop the redundant value // [... val1 val2 helper val1 val2 name ==> [... val1 val2 helper name val1 val2 name] mv.visitInsn(Opcodes.DUP_X2); // this is the high water mark compileContext.addStackCount(5); // [... val1 val2 helper name val1 val2 name ==> [... val1 val2 helper name val1 val2] mv.visitInsn(Opcodes.POP); compileContext.addStackCount(-1); // and now we have the desired arrangement for the call[.. val1 val2 helper name val1 val2] } else { // this is the high water mark // at this point the stack has gone from [ .. val] to [.. val helper val name] compileContext.addStackCount(3); // we can just swap the two values // [... val helper val name ==> ... val helper name val] mv.visitInsn(Opcodes.SWAP); // and now we have the desired arrangement for the call[.. val helper name val] } // ensure we have an object compileObjectConversion(type, Type.OBJECT, mv, compileContext); // call the setBinding method mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.internalName(HelperAdapter.class), "setBinding", "(Ljava/lang/String;Ljava/lang/Object;)V"); // the call will remove 3 from the stack height compileContext.addStackCount(-3); // ok, the stack height should be as it was if (compileContext.getStackCount() != currentStack) { throw new CompileException("variable.compileAssignment : invalid stack height " + compileContext.getStackCount() + " expecting " + currentStack); } }
From source file:org.jephyr.activeobject.instrument.ActiveObjectClassAdapter.java
License:Open Source License
private void visitClinit(MethodVisitor mv) { mv.visitLdcInsn(mailbox); mv.visitInsn(ICONST_0);// w ww . j a va 2s .c om mv.visitTypeInsn(ANEWARRAY, "java/lang/Class"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getConstructor", "([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;", false); mv.visitInsn(ICONST_0); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Constructor", "newInstance", "([Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, "java/util/function/Supplier"); mv.visitFieldInsn(PUTSTATIC, name, "activeObject$mailboxSupplier", "Ljava/util/function/Supplier;"); }