List of usage examples for org.objectweb.asm Opcodes IRETURN
int IRETURN
To view the source code for org.objectweb.asm Opcodes IRETURN.
Click Source Link
From source file:com.lucidtechnics.blackboard.TargetConstructor.java
License:Apache License
private static int getReturnOpcode(String _returnType) { int opCode = 0; char firstChar = _returnType.toCharArray()[0]; switch (firstChar) { case 'Z': case 'B': case 'C': case 'I': case 'S': opCode = Opcodes.IRETURN; break;/* w w w . j a va 2 s . com*/ case 'D': opCode = Opcodes.DRETURN; break; case 'F': opCode = Opcodes.FRETURN; break; case 'J': opCode = Opcodes.LRETURN; break; case 'L': opCode = Opcodes.ARETURN; break; case 'V': opCode = Opcodes.RETURN; break; case '[': opCode = Opcodes.ARETURN; break; default: if (true) throw new RuntimeException("Cannot handle return type identified by first character: " + firstChar); break; } return opCode; }
From source file:com.mebigfatguy.junitflood.jvm.OperandStack.java
License:Apache License
public void performInsn(int opcode) { switch (opcode) { case Opcodes.NOP: break;/*from w ww .j av a 2s .co m*/ case Opcodes.ACONST_NULL: { Operand op = new Operand(); op.setNull(true); push(op); } break; case Opcodes.ICONST_M1: { Operand op = new Operand(); op.setConstant(Integer.valueOf(-1)); push(op); } break; case Opcodes.ICONST_0: { Operand op = new Operand(); op.setConstant(Integer.valueOf(0)); push(op); } break; case Opcodes.ICONST_1: { Operand op = new Operand(); op.setConstant(Integer.valueOf(1)); push(op); } break; case Opcodes.ICONST_2: { Operand op = new Operand(); op.setConstant(Integer.valueOf(2)); push(op); } break; case Opcodes.ICONST_3: { Operand op = new Operand(); op.setConstant(Integer.valueOf(3)); push(op); } break; case Opcodes.ICONST_4: { Operand op = new Operand(); op.setConstant(Integer.valueOf(4)); push(op); } break; case Opcodes.ICONST_5: { Operand op = new Operand(); op.setConstant(Integer.valueOf(5)); push(op); } break; case Opcodes.LCONST_0: { Operand op = new Operand(); op.setConstant(Long.valueOf(0)); push(op); } break; case Opcodes.LCONST_1: { Operand op = new Operand(); op.setConstant(Long.valueOf(1)); push(op); } break; case Opcodes.FCONST_0: { Operand op = new Operand(); op.setConstant(Float.valueOf(0)); push(op); } break; case Opcodes.FCONST_1: { Operand op = new Operand(); op.setConstant(Float.valueOf(1)); push(op); } break; case Opcodes.FCONST_2: { Operand op = new Operand(); op.setConstant(Float.valueOf(2)); push(op); } break; case Opcodes.DCONST_0: { Operand op = new Operand(); op.setConstant(Double.valueOf(0)); push(op); } break; case Opcodes.DCONST_1: { Operand op = new Operand(); op.setConstant(Double.valueOf(1)); push(op); } break; case Opcodes.IALOAD: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LALOAD: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.FALOAD: { pop2(); Operand op = new Operand(); op.setSignature("F"); push(op); } break; case Opcodes.DALOAD: { pop2(); Operand op = new Operand(); op.setSignature("D"); push(op); } break; case Opcodes.AALOAD: { pop2(); Operand op = new Operand(); op.setSignature("Ljava/lang/Object;"); push(op); } break; case Opcodes.BALOAD: { pop2(); Operand op = new Operand(); op.setSignature("B"); push(op); } break; case Opcodes.CALOAD: { pop2(); Operand op = new Operand(); op.setSignature("C"); push(op); } break; case Opcodes.SALOAD: { pop2(); Operand op = new Operand(); op.setSignature("S"); push(op); } break; case Opcodes.IASTORE: case Opcodes.LASTORE: case Opcodes.FASTORE: case Opcodes.DASTORE: case Opcodes.AASTORE: case Opcodes.BASTORE: case Opcodes.CASTORE: case Opcodes.SASTORE: if (stack.size() < 2) { stack.clear(); } else { Operand value = stack.remove(stack.size() - 1); Operand reg = stack.remove(stack.size() - 1); registers.put(Integer.valueOf(reg.getRegister()), value); } break; case Opcodes.POP: pop(); break; case Opcodes.POP2: pop2(); break; case Opcodes.DUP: if (!stack.isEmpty()) { Operand op = stack.get(stack.size() - 1); push(op); } break; case Opcodes.DUP_X1: if (stack.size() >= 2) { Operand op = stack.get(stack.size() - 1); stack.add(stack.size() - 2, op); } break; case Opcodes.DUP_X2: if (stack.size() >= 2) { Operand op = stack.get(stack.size() - 2); String sig = op.getSignature(); op = stack.get(stack.size() - 1); if ("J".equals(sig) || "D".equals(sig)) { stack.add(stack.size() - 2, op); } else if (stack.size() >= 3) { stack.add(stack.size() - 3, op); } } break; case Opcodes.DUP2: if (stack.size() >= 2) { stack.add(stack.get(stack.size() - 2)); stack.add(stack.get(stack.size() - 2)); } break; case Opcodes.DUP2_X1: if (stack.size() >= 1) { Operand op = stack.get(stack.size() - 1); String sig = op.getSignature(); if ("J".equals(sig) || "D".equals(sig)) { if (stack.size() >= 3) { stack.add(stack.size() - 3, op); op = stack.get(stack.size() - 2); stack.add(stack.size() - 4, op); } } else { if (stack.size() >= 2) { stack.add(stack.size() - 2, op); } } } break; case Opcodes.DUP2_X2: if (stack.size() >= 1) { Operand op = stack.get(stack.size() - 1); String sig = op.getSignature(); if ("J".equals(sig) || "D".equals(sig)) { if (stack.size() >= 2) { op = stack.get(stack.size() - 2); sig = op.getSignature(); if ("J".equals(sig) || "D".equals(sig)) { op = stack.get(stack.size() - 1); stack.add(stack.size() - 2, op); } else { if (stack.size() >= 3) { op = stack.get(stack.size() - 1); stack.add(stack.size() - 3, op); } } } } else { if (stack.size() >= 3) { op = stack.get(stack.size() - 3); sig = op.getSignature(); if ("J".equals(sig) || "D".equals(sig)) { op = stack.get(stack.size() - 2); stack.add(stack.size() - 3, op); op = stack.get(stack.size() - 1); stack.add(stack.size() - 3, op); } else { if (stack.size() >= 4) { op = stack.get(stack.size() - 2); stack.add(stack.size() - 4, op); op = stack.get(stack.size() - 1); stack.add(stack.size() - 4, op); } } } } } break; case Opcodes.SWAP: if (stack.size() >= 2) { Operand op = stack.remove(stack.size() - 1); stack.add(stack.size() - 1, op); } break; case Opcodes.IADD: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LADD: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.FADD: { pop2(); Operand op = new Operand(); op.setSignature("F"); push(op); } break; case Opcodes.DADD: { pop2(); Operand op = new Operand(); op.setSignature("D"); push(op); } break; case Opcodes.ISUB: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LSUB: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.FSUB: { pop2(); Operand op = new Operand(); op.setSignature("F"); push(op); } break; case Opcodes.DSUB: { pop2(); Operand op = new Operand(); op.setSignature("D"); push(op); } break; case Opcodes.IMUL: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LMUL: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.FMUL: { pop2(); Operand op = new Operand(); op.setSignature("F"); push(op); } break; case Opcodes.DMUL: { pop2(); Operand op = new Operand(); op.setSignature("D"); push(op); } break; case Opcodes.IDIV: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LDIV: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.FDIV: { pop2(); Operand op = new Operand(); op.setSignature("F"); push(op); } break; case Opcodes.DDIV: { pop2(); Operand op = new Operand(); op.setSignature("D"); push(op); } break; case Opcodes.IREM: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LREM: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.FREM: { pop2(); Operand op = new Operand(); op.setSignature("F"); push(op); } break; case Opcodes.DREM: { pop2(); Operand op = new Operand(); op.setSignature("D"); push(op); } break; case Opcodes.INEG: { pop(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LNEG: { pop(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.FNEG: { pop(); Operand op = new Operand(); op.setSignature("F"); push(op); } break; case Opcodes.DNEG: { pop(); Operand op = new Operand(); op.setSignature("D"); push(op); } break; case Opcodes.ISHL: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LSHL: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.ISHR: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LSHR: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.IUSHR: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LUSHR: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.IAND: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LAND: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.IOR: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LOR: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.IXOR: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.LXOR: { pop2(); Operand op = new Operand(); op.setSignature("J"); push(op); } break; case Opcodes.I2L: { Operand lop = new Operand(); lop.setSignature("J"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { lop.setConstant(Long.valueOf(((Integer) o).longValue())); } } push(lop); } break; case Opcodes.I2F: { Operand fop = new Operand(); fop.setSignature("F"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { fop.setConstant(Float.valueOf(((Integer) o).floatValue())); } } push(fop); } break; case Opcodes.I2D: { Operand dop = new Operand(); dop.setSignature("D"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { dop.setConstant(Double.valueOf(((Integer) o).doubleValue())); } } push(dop); } break; case Opcodes.L2I: { Operand iop = new Operand(); iop.setSignature("I"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { iop.setConstant(Integer.valueOf(((Long) o).intValue())); } } push(iop); } break; case Opcodes.L2F: { Operand fop = new Operand(); fop.setSignature("F"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { fop.setConstant(Float.valueOf(((Long) o).floatValue())); } } push(fop); } break; case Opcodes.L2D: { Operand dop = new Operand(); dop.setSignature("D"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { dop.setConstant(Double.valueOf(((Long) o).doubleValue())); } } push(dop); } break; case Opcodes.F2I: { Operand iop = new Operand(); iop.setSignature("I"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { iop.setConstant(Integer.valueOf(((Float) o).intValue())); } } push(iop); } break; case Opcodes.F2L: { Operand lop = new Operand(); lop.setSignature("J"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { lop.setConstant(Long.valueOf(((Float) o).longValue())); } } push(lop); } break; case Opcodes.F2D: { Operand dop = new Operand(); dop.setSignature("D"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { dop.setConstant(Double.valueOf(((Float) o).doubleValue())); } } push(dop); } break; case Opcodes.D2I: { Operand iop = new Operand(); iop.setSignature("I"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { iop.setConstant(Integer.valueOf(((Double) o).intValue())); } } push(iop); } break; case Opcodes.D2L: { Operand lop = new Operand(); lop.setSignature("J"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { lop.setConstant(Long.valueOf(((Double) o).longValue())); } } push(lop); } break; case Opcodes.D2F: { Operand fop = new Operand(); fop.setSignature("F"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { fop.setConstant(Float.valueOf(((Double) o).floatValue())); } } push(fop); } break; case Opcodes.I2B: { Operand bop = new Operand(); bop.setSignature("B"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { bop.setConstant(Byte.valueOf(((Integer) o).byteValue())); } } push(bop); } break; case Opcodes.I2C: { Operand cop = new Operand(); cop.setSignature("C"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { cop.setConstant(Character.valueOf((char) ((Integer) o).intValue())); } } push(cop); } break; case Opcodes.I2S: { Operand sop = new Operand(); sop.setSignature("S"); if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); Object o = op.getConstant(); if (o != null) { sop.setConstant(Short.valueOf((short) ((Integer) o).intValue())); } } push(sop); } break; case Opcodes.LCMP: case Opcodes.FCMPL: case Opcodes.FCMPG: case Opcodes.DCMPL: case Opcodes.DCMPG: { pop2(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.IRETURN: case Opcodes.LRETURN: case Opcodes.FRETURN: case Opcodes.DRETURN: case Opcodes.ARETURN: pop(); break; case Opcodes.RETURN: //nop break; case Opcodes.ARRAYLENGTH: { pop(); Operand op = new Operand(); op.setSignature("I"); push(op); } break; case Opcodes.ATHROW: case Opcodes.MONITORENTER: case Opcodes.MONITOREXIT: pop(); break; } }
From source file:com.microsoft.applicationinsights.agent.internal.agent.AdvancedAdviceAdapter.java
License:Open Source License
protected ExitStatus translateExitCode(int opcode) { switch (opcode) { case Opcodes.ATHROW: return ExitStatus.EXIT_WITH_EXCEPTION; case Opcodes.IRETURN: case Opcodes.FRETURN: case Opcodes.LRETURN: case Opcodes.DRETURN: case Opcodes.ARETURN: return ExitStatus.EXIT_WITH_RETURN_VALUE; case Opcodes.RETURN: return ExitStatus.EXIT_VOID; default://ww w . ja va2s . c om return ExitStatus.EXIT_UNKNOWN; } }
From source file:com.mogujie.instantrun.IncrementalChangeVisitor.java
License:Apache License
public void addSupportMethod() { int access = Opcodes.ACC_PUBLIC; Method m = new Method("isSupport", "(I)Z"); MethodVisitor mv = super.visitMethod(access, m.getName(), m.getDescriptor(), null, null); mv.visitCode();/*from w ww . ja v a 2 s . c om*/ mv.visitVarInsn(Opcodes.ALOAD, 1); // mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false); int[] hashArray = new int[fixMtds.size()]; Label[] labelArray = new Label[fixMtds.size()]; Label l0 = new Label(); Label l1 = new Label(); for (int i = 0; i < fixMtds.size(); i++) { hashArray[i] = AcesoProguardMap.instance().getClassData(visitedClassName).getMtdIndex(fixMtds.get(i)); labelArray[i] = l0; } mv.visitLookupSwitchInsn(l1, hashArray, labelArray); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IRETURN); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(Opcodes.ICONST_0); mv.visitInsn(Opcodes.IRETURN); mv.visitMaxs(1, 2); mv.visitEnd(); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:com.mogujie.instantrun.SuperHelperVisitor.java
License:Apache License
public void start() { visit(Opcodes.V1_7, ACC_PUBLIC + ACC_SUPER, visitor.visitedClassName + "$helper", null, visitor.visitedSuperName, null); for (int nodeIndex = 0; nodeIndex < superNode.methods.size(); nodeIndex++) { MethodNode methodNode = (MethodNode) superNode.methods.get(nodeIndex); if ("<init>".equals(methodNode.name)) { String[] exceptions = null; if (methodNode.exceptions != null) { exceptions = (String[]) methodNode.exceptions.toArray(new String[0]); }/*from w w w . ja va 2s . com*/ MethodVisitor mv = visitMethod(ACC_PUBLIC, methodNode.name, methodNode.desc, methodNode.signature, exceptions); mv.visitCode(); Type[] args = Type.getArgumentTypes(methodNode.desc); List<LocalVariable> variables = ByteCodeUtils.toLocalVariables(Arrays.asList(args)); mv.visitVarInsn(ALOAD, 0); int local = 1; for (int i = 0; i < variables.size(); i++) { mv.visitVarInsn(variables.get(i).type.getOpcode(Opcodes.ILOAD), variables.get(i).var + 1); local = variables.get(i).var + 1 + variables.get(i).type.getSize(); } mv.visitMethodInsn(INVOKESPECIAL, superNode.name, methodNode.name, methodNode.desc, false); mv.visitInsn(RETURN); mv.visitMaxs(local, local); mv.visitEnd(); } } for (InstantMethod method : visitor.superMethods) { MethodVisitor mv = visitMethod(ACC_PUBLIC + ACC_STATIC, method.getName(), method.getDescriptor(), null, null); mv.visitCode(); Type[] args = Type.getArgumentTypes(method.getDescriptor()); List<LocalVariable> variables = ByteCodeUtils.toLocalVariables(Arrays.asList(args)); int totSize = 1; for (LocalVariable variable : variables) { mv.visitVarInsn(variable.type.getOpcode(Opcodes.ILOAD), variable.var); totSize = variable.var; } mv.visitMethodInsn(INVOKESPECIAL, method.getOwner(), method.getName(), method.getOriDesc(), false); Type returnType = Type.getReturnType(method.getDescriptor()); mv.visitInsn(returnType.getOpcode(Opcodes.IRETURN)); mv.visitMaxs(totSize + 1, totSize + 1); mv.visitEnd(); } visitEnd(); }
From source file:com.navercorp.pinpoint.profiler.instrument.ASMClassNodeAdapter.java
License:Apache License
public void addGetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) { if (methodName == null || fieldNode == null) { throw new IllegalArgumentException("method name or fieldNode annotation must not be null."); }/* w ww. j a v a 2 s . co m*/ // no argument is (). final String desc = "()" + fieldNode.getDesc(); final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null); if (methodNode.instructions == null) { methodNode.instructions = new InsnList(); } final InsnList instructions = methodNode.instructions; // load this. instructions.add(new VarInsnNode(Opcodes.ALOAD, 0)); // get fieldNode. instructions .add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc())); // return of type. final Type type = Type.getType(fieldNode.getDesc()); instructions.add(new InsnNode(type.getOpcode(Opcodes.IRETURN))); if (this.classNode.methods == null) { this.classNode.methods = new ArrayList<MethodNode>(); } this.classNode.methods.add(methodNode); }
From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodVariables.java
License:Apache License
boolean isReturnCode(final int opcode) { return opcode == Opcodes.IRETURN || opcode == Opcodes.LRETURN || opcode == Opcodes.FRETURN || opcode == Opcodes.DRETURN || opcode == Opcodes.ARETURN || opcode == Opcodes.RETURN; }
From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodVariables.java
License:Apache License
public void returnValue(final InsnList instructions) { instructions.add(new InsnNode(this.returnType.getOpcode(Opcodes.IRETURN))); }
From source file:com.nginious.http.xsp.expr.ExpressionCompiler.java
License:Apache License
/** * Creates a compiled expression from the specified tree value node expression. The class bytecode for the * compiled expression is generated at runtime. * //from w ww . j a v a 2s .c o m * @param uncompiled the uncompiled tree value node expression * @return the compiled expression * @throws ExpressionException if unable to compile expression */ public Expression compile(TreeExpression uncompiled) throws ExpressionException { ClassWriter writer = new ClassWriter(0); // Create class String className = classBaseName + classNameCounter.getAndIncrement(); String classIdentifier = className.replace('.', '/'); writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, classIdentifier, "L" + classIdentifier + ";", "com/nginious/http/xsp/expr/Expression", null); // Create constructor MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "<init>", "()V", null, null); visitor.visitCode(); visitor.visitVarInsn(Opcodes.ALOAD, 0); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/expr/Expression", "<init>", "()V"); visitor.visitInsn(Opcodes.RETURN); visitor.visitMaxs(1, 1); visitor.visitEnd(); // protected abstract boolean evaluateBoolean(); visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateBoolean", "()Z", null, null); if (uncompiled.getType() == Type.BOOLEAN) { uncompiled.compile(visitor); } else if (uncompiled.getType() == Type.DOUBLE) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D"); Label endLabel = new Label(); Label falseLabel = new Label(); visitor.visitLdcInsn(0.0d); visitor.visitInsn(Opcodes.DCMPL); visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel); visitor.visitLdcInsn(true); visitor.visitJumpInsn(Opcodes.GOTO, endLabel); visitor.visitLabel(falseLabel); visitor.visitLdcInsn(false); visitor.visitLabel(endLabel); } else if (uncompiled.getType() == Type.INT) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I"); Label endLabel = new Label(); Label falseLabel = new Label(); visitor.visitLdcInsn(0); visitor.visitJumpInsn(Opcodes.IFNE, falseLabel); visitor.visitLdcInsn(true); visitor.visitJumpInsn(Opcodes.GOTO, endLabel); visitor.visitLabel(falseLabel); visitor.visitLdcInsn(false); visitor.visitLabel(endLabel); } else if (uncompiled.getType() == Type.STRING) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString", "()Ljava/lang/String;"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "parseBoolean", "(Ljava/lang/String;)Z"); } visitor.visitInsn(Opcodes.IRETURN); visitor.visitMaxs(5, 5); visitor.visitEnd(); // protected abstract int evaluateInt(); visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateInt", "()I", null, null); if (uncompiled.getType() == Type.BOOLEAN) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z"); Label endLabel = new Label(); Label falseLabel = new Label(); visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel); visitor.visitLdcInsn(1); visitor.visitJumpInsn(Opcodes.GOTO, endLabel); visitor.visitLabel(falseLabel); visitor.visitLdcInsn(0); visitor.visitLabel(endLabel); } else if (uncompiled.getType() == Type.DOUBLE) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D"); visitor.visitInsn(Opcodes.D2I); } else if (uncompiled.getType() == Type.INT) { uncompiled.compile(visitor); } else if (uncompiled.getType() == Type.STRING) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString", "()Ljava/lang/String;"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I"); } visitor.visitInsn(Opcodes.IRETURN); visitor.visitMaxs(5, 5); visitor.visitEnd(); // protected abstract double evaluateDouble(); visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateDouble", "()D", null, null); if (uncompiled.getType() == Type.BOOLEAN) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z"); Label endLabel = new Label(); Label falseLabel = new Label(); visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel); visitor.visitLdcInsn(1.0d); visitor.visitJumpInsn(Opcodes.GOTO, endLabel); visitor.visitLabel(falseLabel); visitor.visitLdcInsn(0.0d); visitor.visitLabel(endLabel); } else if (uncompiled.getType() == Type.DOUBLE) { uncompiled.compile(visitor); } else if (uncompiled.getType() == Type.INT) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I"); visitor.visitInsn(Opcodes.I2D); } else if (uncompiled.getType() == Type.STRING) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString", "()Ljava/lang/String;"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "parseDouble", "(Ljava/lang/String;)D"); } visitor.visitInsn(Opcodes.DRETURN); visitor.visitMaxs(5, 5); visitor.visitEnd(); // protected abstract String evaluateString(); visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateString", "()Ljava/lang/String;", null, null); if (uncompiled.getType() == Type.BOOLEAN) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "toString", "(Z)Ljava/lang/String;"); } else if (uncompiled.getType() == Type.DOUBLE) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "toString", "(D)Ljava/lang/String;"); } else if (uncompiled.getType() == Type.INT) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;"); } else if (uncompiled.getType() == Type.STRING) { uncompiled.compile(visitor); } visitor.visitInsn(Opcodes.ARETURN); visitor.visitMaxs(6, 6); visitor.visitEnd(); // public abstract Type getType(); visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "getType", "()Lcom/nginious/http/xsp/expr/Type;", null, null); if (uncompiled.getType() == Type.BOOLEAN) { visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "BOOLEAN", "Lcom/nginious/http/xsp/expr/Type;"); visitor.visitInsn(Opcodes.ARETURN); } else if (uncompiled.getType() == Type.DOUBLE) { visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "DOUBLE", "Lcom/nginious/http/xsp/expr/Type;"); visitor.visitInsn(Opcodes.ARETURN); } else if (uncompiled.getType() == Type.INT) { visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "INT", "Lcom/nginious/http/xsp/expr/Type;"); visitor.visitInsn(Opcodes.ARETURN); } else if (uncompiled.getType() == Type.STRING) { visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "STRING", "Lcom/nginious/http/xsp/expr/Type;"); visitor.visitInsn(Opcodes.ARETURN); } visitor.visitMaxs(1, 1); visitor.visitEnd(); try { writer.visitEnd(); byte[] clazzBytes = writer.toByteArray(); Class<?> clazz = loadClass(className, clazzBytes); return (Expression) clazz.newInstance(); } catch (Exception e) { throw new ExpressionException("Can't instantiate compiled expression", e); } }
From source file:com.nginious.http.xsp.IfTagPart.java
License:Apache License
/** * Creates bytecode in a separate method for evaluating this if tag part. * //from w w w . jav a2 s . c om * @param intClassName the binary class name of the class being created * @param writer the class writer * @throws XspException if unable to create bytecode */ void compileMethod(String intClassName, ClassWriter writer) throws XspException { if (this.part == null) { throw new XspException( "Attribute test is missing for tag " + getName() + " at " + getLocationDescriptor()); } if (!part.isExpression()) { throw new XspException( "Attribute test is not an expression for tag " + getName() + " at " + getLocationDescriptor()); } String expression = part.getExpressionContent(); try { ExpressionParser parser = new ExpressionParser(); TreeExpression expr = parser.parse(expression); if (expr.getType() != Type.BOOLEAN) { throw new XspException("Expression in attribute test in tag " + getName() + " does not produce a boolean " + " at line " + getLocationDescriptor()); } MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PRIVATE, this.methodName, "()Z", null, null); visitor.visitCode(); expr.compile(visitor); visitor.visitInsn(Opcodes.IRETURN); visitor.visitMaxs(5, 5); visitor.visitEnd(); super.compileMethod(intClassName, writer); } catch (ExpressionException e) { throw new XspException("Invalid expression in attribute test in tag " + getName() + " at line " + getLocationDescriptor(), e); } }