List of usage examples for org.objectweb.asm MethodVisitor visitJumpInsn
public void visitJumpInsn(final int opcode, final Label label)
From source file:org.jboss.byteman.rule.expression.ComparisonExpression.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 removed = 0; // evaluate the operands and ensure the reuslt is of the correct type for comparison adds 2 oper0.compile(mv, compileContext);//from w w w . j a va2s .co m compileTypeConversion(oper0.getType(), comparisonType, mv, compileContext); oper1.compile(mv, compileContext); compileTypeConversion(oper1.getType(), comparisonType, mv, compileContext); // now do the appropriate type of comparison if (comparisonType == type.B || comparisonType == type.S || comparisonType == type.S || comparisonType == type.I) { Label elsetarget = new Label(); Label endtarget = new Label(); // we remove 2 words from the stack and then add 1 back removed = 2; switch (oper) { case LT: mv.visitJumpInsn(Opcodes.IF_ICMPGE, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case LE: mv.visitJumpInsn(Opcodes.IF_ICMPGT, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case GT: mv.visitJumpInsn(Opcodes.IF_ICMPLE, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case GE: mv.visitJumpInsn(Opcodes.IF_ICMPLT, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case EQ: mv.visitJumpInsn(Opcodes.IF_ICMPNE, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case NE: mv.visitJumpInsn(Opcodes.IF_ICMPEQ, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; } } else if (comparisonType == type.J || comparisonType == type.F || comparisonType == type.D) { if (comparisonType == type.J) { mv.visitInsn(Opcodes.LCMP); // we remove four words from the stack and add 1 back removed = 4; } else if (comparisonType == type.F) { // we remove two words from the stack and add 1 back removed = 2; mv.visitInsn(Opcodes.FCMPG); } else if (comparisonType == type.D) { // we remove four words from the stack and add 1 back removed = 4; mv.visitInsn(Opcodes.DCMPG); } Label elsetarget = new Label(); Label endtarget = new Label(); switch (oper) { case LT: mv.visitJumpInsn(Opcodes.IFGE, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case LE: mv.visitJumpInsn(Opcodes.IFGT, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case GT: mv.visitJumpInsn(Opcodes.IFLE, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case GE: mv.visitJumpInsn(Opcodes.IFLT, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case EQ: mv.visitJumpInsn(Opcodes.IFNE, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case NE: mv.visitJumpInsn(Opcodes.IFEQ, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; } } else if (comparable) { // we add a further two words setting up the relevant test then remove them // and also remove the original two words replacing them with a single word removed = 4; compileContext.addStackCount(2); // we need to deal with null values correctly // if op1 == null || op2 == null // then // EQ: // push value1 == value2 // NE: // push value1 != value2 // ow: // push false // else // execute compareTo or equals and test for the desired outcome // end if Label splittarget = new Label(); // else Label jointarget = new Label(); // end if mv.visitInsn(Opcodes.DUP2); // [... op1, op2 ] ==> [... op1, op2, op1, op2] mv.visitInsn(Opcodes.POP); // [... op1, op2, op1, op2 ] ==> [... op1, op2, op1] // if op1 == null mv.visitJumpInsn(Opcodes.IFNULL, splittarget); // [... op1, op2, op1] ==> [... op1, op2] mv.visitInsn(Opcodes.DUP); // [... op1, op2 ] ==> [... op1, op2, op2] // || op2 == null mv.visitJumpInsn(Opcodes.IFNULL, splittarget); // [... op1, op2, op2] ==> [... op1, op2] // so, it is ok to call compareTo leaving an int or equals leaving a boolean if (oper != EQ && oper != NE) { mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Comparable", "compareTo", "(Ljava/lang/Object;)I"); } else { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z"); } // now if we did a compareTo we need to generate the required boolean Label elsetarget = new Label(); Label endtarget = new Label(); // if needed the convert the compareTo result to the required boolean outcome switch (oper) { case LT: mv.visitJumpInsn(Opcodes.IFGE, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case LE: mv.visitJumpInsn(Opcodes.IFGT, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case GT: mv.visitJumpInsn(Opcodes.IFLE, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case GE: mv.visitJumpInsn(Opcodes.IFLT, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); break; case NE: mv.visitJumpInsn(Opcodes.IFEQ, elsetarget); mv.visitLdcInsn(false); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(true); mv.visitLabel(endtarget); break; } // skip to the join point mv.visitJumpInsn(Opcodes.GOTO, jointarget); // label the split point mv.visitLabel(splittarget); if (oper == EQ) { elsetarget = new Label(); endtarget = new Label(); mv.visitJumpInsn(Opcodes.IF_ACMPEQ, elsetarget); mv.visitLdcInsn(false); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(true); mv.visitLabel(endtarget); } else if (oper == NE) { elsetarget = new Label(); endtarget = new Label(); mv.visitJumpInsn(Opcodes.IF_ACMPNE, elsetarget); mv.visitLdcInsn(false); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(true); mv.visitLabel(endtarget); } else { // pop the operands and stack false mv.visitInsn(Opcodes.POP2); mv.visitLdcInsn(false); } // label the join point mv.visitLabel(jointarget); } else if (comparisonType == Type.Z) { // unboxed booleans need special treatment // we remove two words replacing them with a single word removed = 2; Label elsetarget = new Label(); Label endtarget = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, elsetarget); // on this branch for EQ the stacked value is what we need and for NE // the stacked value needs flipping if (oper == NE) { Label elsetarget2 = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, elsetarget2); mv.visitLdcInsn(false); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget2); mv.visitLdcInsn(true); } mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); // on this branch for NE the stacked value is what we need and for EQ // the stacked value needs flipping if (oper == EQ) { Label elsetarget2 = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, elsetarget2); mv.visitLdcInsn(false); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget2); mv.visitLdcInsn(true); } mv.visitLabel(endtarget); } else if (comparisonType == Type.BOOLEAN) { // boxed booleans need special treatment // we remove two words replacing them with a single word removed = 2; Label elsetarget = new Label(); Label endtarget = new Label(); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java.lang.Boolean", "equals", "(Ljava/lang/Boolean;)Z"); if (oper == NE) { mv.visitJumpInsn(Opcodes.IFEQ, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); } } else { // we remove two words replacing them with a single word removed = 2; Label elsetarget = new Label(); Label endtarget = new Label(); if (oper == EQ) { mv.visitJumpInsn(Opcodes.IF_ACMPNE, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); } else { mv.visitJumpInsn(Opcodes.IF_ACMPEQ, elsetarget); mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endtarget); mv.visitLabel(elsetarget); mv.visitLdcInsn(false); mv.visitLabel(endtarget); } } compileContext.addStackCount(1 - removed); }
From source file:org.jboss.byteman.rule.expression.ConditionalEvalExpression.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); Expression oper2 = getOperand(2); int currentStack = compileContext.getStackCount(); int expected = (type.getNBytes() > 4 ? 2 : 1); // compile the first operand to a boolean and ensure it is primitive -- adds 1 to stack oper0.compile(mv, compileContext);// www .j av a 2 s. c o m if (oper0.getType() == Type.BOOLEAN) { compileBooleanConversion(Type.BOOLEAN, Type.Z, mv, compileContext); } // plant the test -- consumes 1 word Label elseLabel = new Label(); Label endLabel = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, elseLabel); compileContext.addStackCount(-1); // compile the if branch oper1.compile(mv, compileContext); // make sure we type convert to our result type so that either branch stacks the same thing compileTypeConversion(oper1.getType(), type, mv, compileContext); // plant a goto skipping over the else expression mv.visitJumpInsn(Opcodes.GOTO, endLabel); // check the stack height is what we expect, either 1 or 2 words depending upon the result type if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("ConditionalEvalExpression.compile : invalid true branch stack height " + compileContext.getStackCount() + " expecting " + currentStack + expected); } // ok, now reset stack height for false branch compileContext.addStackCount(-expected); // else starts here mv.visitLabel(elseLabel); // compile the else branch oper2.compile(mv, compileContext); // make sure we type convert to our result type so that either branch stacks the same thing compileTypeConversion(oper2.getType(), type, mv, compileContext); // the end is nigh mv.visitLabel(endLabel); // check the stack height is what we expect, either 1 or 2 words depending upon the result type if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("ConditionalEvalExpression.compile : invalid false branch stack height " + compileContext.getStackCount() + " expecting " + currentStack + expected); } // no need to check max stack height as teh left and right expressions will have exceeded anything // we stacked inside this call }
From source file:org.jboss.byteman.rule.expression.LogicalExpression.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(); // compile the first expression and make sure it is a boolean -- adds 1 to stack height oper0.compile(mv, compileContext);/*from w ww .ja va2s . c o m*/ if (oper0.getType() == Type.BOOLEAN) { compileBooleanConversion(Type.BOOLEAN, type.Z, mv, compileContext); } // plant a test and branch Label nextLabel = new Label(); Label endLabel = new Label(); if (oper == AND) { // only try next if we got true here mv.visitJumpInsn(Opcodes.IFNE, nextLabel); // ok, the first branch was false so stack a false for the result and skip to the end mv.visitLdcInsn(false); mv.visitJumpInsn(Opcodes.GOTO, endLabel); } else { // only try next if we got false here mv.visitJumpInsn(Opcodes.IFEQ, nextLabel); // ok, the first branch was true so stack a true for the result and skip to the end mv.visitLdcInsn(true); mv.visitJumpInsn(Opcodes.GOTO, endLabel); } // in either case if we get here the if test removed 1 from the stack compileContext.addStackCount(-1); // the else branch -- adds 1 to stack height mv.visitLabel(nextLabel); oper1.compile(mv, compileContext); if (oper0.getType() == Type.BOOLEAN) { compileBooleanConversion(Type.BOOLEAN, type.Z, mv, compileContext); } // the final result is the result of the second oper which is on the stack already // This is the end, my beau-tiful friend mv.visitLabel(endLabel); // in either case if we get here we should have one extra value on the stack // check stack height if (compileContext.getStackCount() != currentStack + 1) { throw new CompileException("LogicalExpression.compile : invalid stack height " + compileContext.getStackCount() + " expecting " + (currentStack + 1)); } }
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);//from w ww .jav a2s . c om 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.RuleElement.java
License:Open Source License
protected void compileStringConversion(Type fromType, Type toType, MethodVisitor mv, CompileContext compileContext) throws CompileException { assert toType == Type.STRING; if (fromType.isObject() || fromType.isArray() || (fromType.isNumeric() && !fromType.isPrimitive())) { // use the toString method if the object is non null otherwise just replace it with null Label elseLabel = new Label(); Label endLabel = new Label(); // if (object == null) mv.visitInsn(Opcodes.DUP);/*from ww w . j a v a2 s .c om*/ // the above dup bumps the stack height compileContext.addStackCount(1); mv.visitJumpInsn(Opcodes.IFNONNULL, elseLabel); compileContext.addStackCount(-1); // then string = "null" mv.visitInsn(Opcodes.POP); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitJumpInsn(Opcodes.GOTO, endLabel); // else string = object.toString() mv.visitLabel(elseLabel); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;"); mv.visitLabel(endLabel); } else if (fromType == Type.Z) { // use the toString method mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "toString", "(Z)Ljava/lang/String;"); } else if (fromType == Type.B) { // use the toString method mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "toString", "(B)Ljava/lang/String;"); } else if (fromType == Type.S) { // use the toString method mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "toString", "(S)Ljava/lang/String;"); } else if (fromType == Type.C) { // use the toString method mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "toString", "(C)Ljava/lang/String;"); } else if (fromType == Type.I) { // use the toString method mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;"); } else if (fromType == Type.J) { // use the toString method mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "toString", "(J)Ljava/lang/String;"); compileContext.addStackCount(-1); } else if (fromType == Type.F) { // use the toString method mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "toString", "(F)Ljava/lang/String;"); } else if (fromType == Type.D) { // use the toString method mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "toString", "(D)Ljava/lang/String;"); compileContext.addStackCount(-1); } }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a get Java primitive property method. * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method./*from w w w. j ava 2 s . co m*/ * @param propertyName The name of the property. * @param jsonPrimitiveType The type of the JSON primitive. * @param javaPrimitiveType The type of the Java primitive. */ private void generateGetJavaPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, String propertyName, Type jsonPrimitiveType, Type javaPrimitiveType) { MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label l0 = new Label(); Label l1 = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(propertyName); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getGetJsonPrimitivePropertyMethod(jsonPrimitiveType)); methodVisitor.visitVarInsn(ASTORE, 1); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitJumpInsn(IFNULL, l0); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonPrimitiveType, this.getToJavaPrimitiveMethod(javaPrimitiveType)); methodVisitor.visitJumpInsn(GOTO, l1); methodVisitor.visitLabel(l0); methodVisitor.visitFrame(Opcodes.F_APPEND, 1, new Object[] { jsonPrimitiveType }, 0, null); methodVisitor.visitInsn(this.getDefaultConstOpcode(javaPrimitiveType)); methodVisitor.visitLabel(l1); methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { this.getStackElement(javaPrimitiveType) }); methodVisitor.visitInsn(javaPrimitiveType.getOpcode(IRETURN)); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitLocalVariable("_" + propertyName, jsonPrimitiveType, null, start, end, 1); methodVisitor.visitMaxs(2, 2); methodVisitor.visitEnd(); }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a get Java character primitive property method. * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method./* w w w. j a va 2 s .co m*/ * @param propertyName The name of the property. */ private void generateGetJavaCharacterPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, String propertyName) { Type jsonStringPrimitiveType = Type.getType(String.class); MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label l0 = new Label(); Label l1 = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(propertyName); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getGetJsonPrimitivePropertyMethod(jsonStringPrimitiveType)); methodVisitor.visitVarInsn(ASTORE, 1); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitJumpInsn(IFNULL, l0); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonStringPrimitiveType, new Method("isEmpty", Type.BOOLEAN_TYPE, new Type[] {})); methodVisitor.visitJumpInsn(IFNE, l0); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitInsn(ICONST_0); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonStringPrimitiveType, new Method("charAt", Type.CHAR_TYPE, new Type[] { Type.INT_TYPE })); methodVisitor.visitJumpInsn(GOTO, l1); methodVisitor.visitLabel(l0); methodVisitor.visitFrame(Opcodes.F_APPEND, 1, new Object[] { jsonStringPrimitiveType }, 0, null); methodVisitor.visitInsn(ICONST_0); methodVisitor.visitLabel(l1); methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { Opcodes.INTEGER }); methodVisitor.visitInsn(IRETURN); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitLocalVariable("_" + propertyName, jsonStringPrimitiveType, null, start, end, 1); methodVisitor.visitMaxs(2, 2); methodVisitor.visitEnd(); }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a get composite JSON object property method. * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method.// w w w . java 2 s .co m * @param propertyName The name of the property. * @param jsonObjectType The type of the JSON object. * @param elementType The type of the element. */ private void generateGetCompositeJsonObjectPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, String propertyName, Type jsonObjectType, Type elementType) { MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label l0 = new Label(); Label l1 = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(propertyName); methodVisitor.visitLdcInsn(jsonObjectType); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getGetJsonObjectPropertyMethod()); methodVisitor.visitTypeInsn(CHECKCAST, jsonObjectType); methodVisitor.visitVarInsn(ASTORE, 1); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitJumpInsn(IFNULL, l0); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitLdcInsn(elementType); methodVisitor.visitMethodInsn(INVOKEINTERFACE, jsonObjectType, this.getCastCompositeJsonObjectElementMethod(jsonObjectType)); methodVisitor.visitJumpInsn(GOTO, l1); methodVisitor.visitLabel(l0); methodVisitor.visitFrame(Opcodes.F_APPEND, 1, new Object[] { jsonObjectType }, 0, null); methodVisitor.visitInsn(ACONST_NULL); methodVisitor.visitLabel(l1); methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { jsonObjectType }); methodVisitor.visitInsn(ARETURN); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitLocalVariable("_" + propertyName, jsonObjectType, null, start, end, 1); methodVisitor.visitMaxs(3, 2); methodVisitor.visitEnd(); }
From source file:org.lanternpowered.server.event.ClassEventListenerFactory.java
License:MIT License
private static byte[] generateClass(String name, Class<?> handle, Method method, Class<?> eventClass, Class<? extends EventFilter> filter) { name = name.replace('.', '/'); final String handleName = Type.getInternalName(handle); final String handleDescriptor = Type.getDescriptor(handle); final String filterName = Type.getInternalName(filter); String eventDescriptor = "("; for (int i = 0; i < method.getParameterCount(); i++) { eventDescriptor += Type.getDescriptor(method.getParameterTypes()[i]); }//from w ww.j ava 2s . com eventDescriptor += ")V"; ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); MethodVisitor mv; FieldVisitor fv; cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, BASE_HANDLER, null); { fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "FILTER", "L" + filterName + ";", null, null); fv.visitEnd(); } { mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, filterName); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, filterName, "<init>", "()V", false); mv.visitFieldInsn(PUTSTATIC, name, "FILTER", "L" + filterName + ";"); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "<init>", '(' + handleDescriptor + ")V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, BASE_HANDLER, "<init>", "(Ljava/lang/Object;)V", false); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "handle", HANDLE_METHOD_DESCRIPTOR, null, new String[] { "java/lang/Exception" }); mv.visitCode(); mv.visitFieldInsn(GETSTATIC, name, "FILTER", "L" + filterName + ";"); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(EventFilter.class), "filter", FILTER_DESCRIPTOR, true); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); Label l2 = new Label(); mv.visitJumpInsn(IFNULL, l2); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name, "handle", "Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, handleName); for (int i = 0; i < method.getParameterCount(); i++) { mv.visitVarInsn(ALOAD, 2); mv.visitIntInsn(BIPUSH, i); mv.visitInsn(AALOAD); Type paramType = Type.getType(method.getParameterTypes()[i]); GeneratorUtils.visitUnboxingMethod(mv, paramType); } mv.visitMethodInsn(INVOKEVIRTUAL, handleName, method.getName(), eventDescriptor, false); mv.visitLabel(l2); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
From source file:org.lanternpowered.server.event.filter.delegate.AfterCauseFilterSourceDelegate.java
License:MIT License
@Override protected void insertTransform(MethodVisitor mv, Parameter param, Class<?> targetType, int local) { mv.visitVarInsn(ALOAD, local);//from w ww. j a v a 2 s . co m Label failure = new Label(); Label success = new Label(); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Optional", "isPresent", "()Z", false); mv.visitJumpInsn(IFEQ, failure); // Unwrap the optional mv.visitVarInsn(ALOAD, local); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Optional", "get", "()Ljava/lang/Object;", false); mv.visitVarInsn(ASTORE, local); mv.visitVarInsn(ALOAD, local); mv.visitTypeInsn(INSTANCEOF, Type.getInternalName(targetType)); if (this.anno.typeFilter().length != 0) { mv.visitJumpInsn(IFEQ, failure); mv.visitVarInsn(ALOAD, local); // For each type we do an instance check and jump to either failure or success if matched for (int i = 0; i < this.anno.typeFilter().length; i++) { Class<?> filter = this.anno.typeFilter()[i]; if (i < this.anno.typeFilter().length - 1) { mv.visitInsn(DUP); } mv.visitTypeInsn(INSTANCEOF, Type.getInternalName(filter)); if (this.anno.inverse()) { mv.visitJumpInsn(IFNE, failure); } else { mv.visitJumpInsn(IFNE, success); } } if (this.anno.inverse()) { mv.visitJumpInsn(GOTO, success); } // If the annotation was not reversed then we fall into failure as no types were matched } else { mv.visitJumpInsn(IFNE, success); } mv.visitLabel(failure); mv.visitInsn(ACONST_NULL); mv.visitInsn(ARETURN); mv.visitLabel(success); }