List of usage examples for org.objectweb.asm Opcodes IFEQ
int IFEQ
To view the source code for org.objectweb.asm Opcodes IFEQ.
Click Source Link
From source file:org.apache.aries.proxy.impl.common.AbstractWovenProxyMethodAdapter.java
License:Apache License
/** * This method unwraps woven proxy instances for use in the right-hand side * of equals methods/*from www . j a v a 2 s. c om*/ */ protected final void unwrapEqualsArgument() { //Create and initialize a local for our work int unwrapLocal = newLocal(OBJECT_TYPE); visitInsn(ACONST_NULL); storeLocal(unwrapLocal); Label startUnwrap = newLabel(); mark(startUnwrap); //Load arg and check if it is a WovenProxy instances loadArg(0); instanceOf(WOVEN_PROXY_IFACE_TYPE); Label unwrapFinished = newLabel(); //Jump if zero (false) visitJumpInsn(Opcodes.IFEQ, unwrapFinished); //Arg is a wovenProxy, if it is the same as last time then we're done loadLocal(unwrapLocal); loadArg(0); ifCmp(OBJECT_TYPE, EQ, unwrapFinished); //Not the same, store current arg in unwrapLocal for next loop loadArg(0); storeLocal(unwrapLocal); //So arg is a WovenProxy, but not the same as last time, cast it and store //the result of unwrap.call in the arg loadArg(0); checkCast(WOVEN_PROXY_IFACE_TYPE); //Now unwrap invokeInterface(WOVEN_PROXY_IFACE_TYPE, new Method("org_apache_aries_proxy_weaving_WovenProxy_unwrap", DISPATCHER_TYPE, NO_ARGS)); //Now we may have a Callable to invoke int callable = newLocal(DISPATCHER_TYPE); storeLocal(callable); loadLocal(callable); ifNull(unwrapFinished); loadLocal(callable); invokeInterface(DISPATCHER_TYPE, new Method("call", OBJECT_TYPE, NO_ARGS)); //Store the result and we're done (for this iteration) storeArg(0); goTo(startUnwrap); mark(unwrapFinished); }
From source file:org.apache.asterix.runtime.evaluators.staticcodegen.EvaluatorVisitor.java
License:Apache License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions); if (!METHOD_IDENTIFIER.equals(new MethodIdentifier(name, desc, signature))) { return mv; }//from w w w . j av a2 s.c o m if (mv != null) { return new MethodVisitor(Opcodes.ASM5, mv) { private FieldInsnNode fieldAccessNode = null; private List<AbstractInsnNode> instructionsAfterFieldAccess = new ArrayList<>(); @Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { mv.visitFieldInsn(opcode, owner, name, desc); fieldAccessNode = new FieldInsnNode(opcode, owner, name, desc); instructionsAfterFieldAccess.clear(); } @Override public void visitIincInsn(int var, int increment) { if (fieldAccessNode != null) { instructionsAfterFieldAccess.add(new IincInsnNode(var, increment)); } super.visitIincInsn(var, increment); } @Override public void visitInsn(int opcode) { if (fieldAccessNode != null) { instructionsAfterFieldAccess.add(new InsnNode(opcode)); } super.visitInsn(opcode); } @Override public void visitIntInsn(int opcode, int operand) { if (fieldAccessNode != null) { instructionsAfterFieldAccess.add(new IntInsnNode(opcode, operand)); } super.visitIntInsn(opcode, operand); } @Override public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { mv.visitMethodInsn(opcode, owner, name, desc, itf); if (fieldAccessNode == null || !METHOD_IDENTIFIER.equals(new MethodIdentifier(name, desc, signature))) { return; } // Loads "this". mv.visitVarInsn(Opcodes.ALOAD, 0); // Replays the field access instruction. fieldAccessNode.accept(mv); // Replays other instruction between the field access and the evaluator call. for (AbstractInsnNode instruction : instructionsAfterFieldAccess) { instruction.accept(mv); } // Loads the result IPointable. mv.visitVarInsn(Opcodes.ALOAD, 2); // Invokes the null check method. mv.visitMethodInsn(Opcodes.INVOKESTATIC, TYPECHECK_CLASS, IS_NULL, TYPECHECK_METHOD_DESC, false); Label notNull = new Label(); // Adds the if branch. mv.visitJumpInsn(Opcodes.IFEQ, notNull); mv.visitInsn(Opcodes.RETURN); mv.visitLabel(notNull); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); } }; } return null; }
From source file:org.apache.lucene.expressions.js.JavascriptCompiler.java
License:Apache License
/** * Sends the bytecode of class file to {@link ClassWriter}. *///from w w w. ja v a2 s .com private void generateClass(final ParseTree parseTree, final ClassWriter classWriter, final Map<String, Integer> externalsMap) throws ParseException { classWriter.visit(CLASSFILE_VERSION, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL, COMPILED_EXPRESSION_INTERNAL, null, EXPRESSION_TYPE.getInternalName(), null); final String clippedSourceText = (sourceText.length() <= MAX_SOURCE_LENGTH) ? sourceText : (sourceText.substring(0, MAX_SOURCE_LENGTH - 3) + "..."); classWriter.visitSource(clippedSourceText, null); final GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ACC_PUBLIC, EXPRESSION_CTOR, null, null, classWriter); constructor.loadThis(); constructor.loadArgs(); constructor.invokeConstructor(EXPRESSION_TYPE, EXPRESSION_CTOR); constructor.returnValue(); constructor.endMethod(); final GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC, EVALUATE_METHOD, null, null, classWriter); // to completely hide the ANTLR visitor we use an anonymous impl: new JavascriptBaseVisitor<Void>() { private final Deque<Type> typeStack = new ArrayDeque<>(); @Override public Void visitCompile(JavascriptParser.CompileContext ctx) { typeStack.push(Type.DOUBLE_TYPE); visit(ctx.expression()); typeStack.pop(); return null; } @Override public Void visitPrecedence(JavascriptParser.PrecedenceContext ctx) { visit(ctx.expression()); return null; } @Override public Void visitNumeric(JavascriptParser.NumericContext ctx) { if (ctx.HEX() != null) { pushLong(Long.parseLong(ctx.HEX().getText().substring(2), 16)); } else if (ctx.OCTAL() != null) { pushLong(Long.parseLong(ctx.OCTAL().getText().substring(1), 8)); } else if (ctx.DECIMAL() != null) { gen.push(Double.parseDouble(ctx.DECIMAL().getText())); gen.cast(Type.DOUBLE_TYPE, typeStack.peek()); } else { throw new IllegalStateException("Unknown operation specified: " + ctx.getText()); } return null; } @Override public Void visitExternal(JavascriptParser.ExternalContext ctx) { String text = ctx.VARIABLE().getText(); int arguments = ctx.expression().size(); boolean parens = ctx.LP() != null && ctx.RP() != null; Method method = parens ? functions.get(text) : null; try { if (method != null) { int arity = method.getParameterTypes().length; if (arguments != arity) { throw new ParseException("Invalid expression '" + sourceText + "': Expected (" + arity + ") arguments for function call (" + text + "), but found (" + arguments + ").", ctx.start.getStartIndex()); } typeStack.push(Type.DOUBLE_TYPE); for (int argument = 0; argument < arguments; ++argument) { visit(ctx.expression(argument)); } typeStack.pop(); gen.invokeStatic(Type.getType(method.getDeclaringClass()), org.objectweb.asm.commons.Method.getMethod(method)); gen.cast(Type.DOUBLE_TYPE, typeStack.peek()); } else if (!parens || arguments == 0 && text.contains(".")) { int index; text = normalizeQuotes(ctx.getText()); if (externalsMap.containsKey(text)) { index = externalsMap.get(text); } else { index = externalsMap.size(); externalsMap.put(text, index); } gen.loadArg(0); gen.push(index); gen.arrayLoad(FUNCTION_VALUES_TYPE); gen.invokeVirtual(FUNCTION_VALUES_TYPE, DOUBLE_VAL_METHOD); gen.cast(Type.DOUBLE_TYPE, typeStack.peek()); } else { throw new ParseException("Invalid expression '" + sourceText + "': Unrecognized function call (" + text + ").", ctx.start.getStartIndex()); } return null; } catch (ParseException e) { // The API doesn't allow checked exceptions here, so propagate up the stack. This is unwrapped // in getAntlrParseTree. throw new RuntimeException(e); } } @Override public Void visitUnary(JavascriptParser.UnaryContext ctx) { if (ctx.BOOLNOT() != null) { Label labelNotTrue = new Label(); Label labelNotReturn = new Label(); typeStack.push(Type.INT_TYPE); visit(ctx.expression()); typeStack.pop(); gen.visitJumpInsn(Opcodes.IFEQ, labelNotTrue); pushBoolean(false); gen.goTo(labelNotReturn); gen.visitLabel(labelNotTrue); pushBoolean(true); gen.visitLabel(labelNotReturn); } else if (ctx.BWNOT() != null) { typeStack.push(Type.LONG_TYPE); visit(ctx.expression()); typeStack.pop(); gen.push(-1L); gen.visitInsn(Opcodes.LXOR); gen.cast(Type.LONG_TYPE, typeStack.peek()); } else if (ctx.ADD() != null) { visit(ctx.expression()); } else if (ctx.SUB() != null) { typeStack.push(Type.DOUBLE_TYPE); visit(ctx.expression()); typeStack.pop(); gen.visitInsn(Opcodes.DNEG); gen.cast(Type.DOUBLE_TYPE, typeStack.peek()); } else { throw new IllegalStateException("Unknown operation specified: " + ctx.getText()); } return null; } @Override public Void visitMuldiv(JavascriptParser.MuldivContext ctx) { int opcode; if (ctx.MUL() != null) { opcode = Opcodes.DMUL; } else if (ctx.DIV() != null) { opcode = Opcodes.DDIV; } else if (ctx.REM() != null) { opcode = Opcodes.DREM; } else { throw new IllegalStateException("Unknown operation specified: " + ctx.getText()); } pushArith(opcode, ctx.expression(0), ctx.expression(1)); return null; } @Override public Void visitAddsub(JavascriptParser.AddsubContext ctx) { int opcode; if (ctx.ADD() != null) { opcode = Opcodes.DADD; } else if (ctx.SUB() != null) { opcode = Opcodes.DSUB; } else { throw new IllegalStateException("Unknown operation specified: " + ctx.getText()); } pushArith(opcode, ctx.expression(0), ctx.expression(1)); return null; } @Override public Void visitBwshift(JavascriptParser.BwshiftContext ctx) { int opcode; if (ctx.LSH() != null) { opcode = Opcodes.LSHL; } else if (ctx.RSH() != null) { opcode = Opcodes.LSHR; } else if (ctx.USH() != null) { opcode = Opcodes.LUSHR; } else { throw new IllegalStateException("Unknown operation specified: " + ctx.getText()); } pushShift(opcode, ctx.expression(0), ctx.expression(1)); return null; } @Override public Void visitBoolcomp(JavascriptParser.BoolcompContext ctx) { int opcode; if (ctx.LT() != null) { opcode = GeneratorAdapter.LT; } else if (ctx.LTE() != null) { opcode = GeneratorAdapter.LE; } else if (ctx.GT() != null) { opcode = GeneratorAdapter.GT; } else if (ctx.GTE() != null) { opcode = GeneratorAdapter.GE; } else { throw new IllegalStateException("Unknown operation specified: " + ctx.getText()); } pushCond(opcode, ctx.expression(0), ctx.expression(1)); return null; } @Override public Void visitBooleqne(JavascriptParser.BooleqneContext ctx) { int opcode; if (ctx.EQ() != null) { opcode = GeneratorAdapter.EQ; } else if (ctx.NE() != null) { opcode = GeneratorAdapter.NE; } else { throw new IllegalStateException("Unknown operation specified: " + ctx.getText()); } pushCond(opcode, ctx.expression(0), ctx.expression(1)); return null; } @Override public Void visitBwand(JavascriptParser.BwandContext ctx) { pushBitwise(Opcodes.LAND, ctx.expression(0), ctx.expression(1)); return null; } @Override public Void visitBwxor(JavascriptParser.BwxorContext ctx) { pushBitwise(Opcodes.LXOR, ctx.expression(0), ctx.expression(1)); return null; } @Override public Void visitBwor(JavascriptParser.BworContext ctx) { pushBitwise(Opcodes.LOR, ctx.expression(0), ctx.expression(1)); return null; } @Override public Void visitBooland(JavascriptParser.BoolandContext ctx) { Label andFalse = new Label(); Label andEnd = new Label(); typeStack.push(Type.INT_TYPE); visit(ctx.expression(0)); gen.visitJumpInsn(Opcodes.IFEQ, andFalse); visit(ctx.expression(1)); gen.visitJumpInsn(Opcodes.IFEQ, andFalse); typeStack.pop(); pushBoolean(true); gen.goTo(andEnd); gen.visitLabel(andFalse); pushBoolean(false); gen.visitLabel(andEnd); return null; } @Override public Void visitBoolor(JavascriptParser.BoolorContext ctx) { Label orTrue = new Label(); Label orEnd = new Label(); typeStack.push(Type.INT_TYPE); visit(ctx.expression(0)); gen.visitJumpInsn(Opcodes.IFNE, orTrue); visit(ctx.expression(1)); gen.visitJumpInsn(Opcodes.IFNE, orTrue); typeStack.pop(); pushBoolean(false); gen.goTo(orEnd); gen.visitLabel(orTrue); pushBoolean(true); gen.visitLabel(orEnd); return null; } @Override public Void visitConditional(JavascriptParser.ConditionalContext ctx) { Label condFalse = new Label(); Label condEnd = new Label(); typeStack.push(Type.INT_TYPE); visit(ctx.expression(0)); typeStack.pop(); gen.visitJumpInsn(Opcodes.IFEQ, condFalse); visit(ctx.expression(1)); gen.goTo(condEnd); gen.visitLabel(condFalse); visit(ctx.expression(2)); gen.visitLabel(condEnd); return null; } private void pushArith(int operator, ExpressionContext left, ExpressionContext right) { pushBinaryOp(operator, left, right, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE); } private void pushShift(int operator, ExpressionContext left, ExpressionContext right) { pushBinaryOp(operator, left, right, Type.LONG_TYPE, Type.INT_TYPE, Type.LONG_TYPE); } private void pushBitwise(int operator, ExpressionContext left, ExpressionContext right) { pushBinaryOp(operator, left, right, Type.LONG_TYPE, Type.LONG_TYPE, Type.LONG_TYPE); } private void pushBinaryOp(int operator, ExpressionContext left, ExpressionContext right, Type leftType, Type rightType, Type returnType) { typeStack.push(leftType); visit(left); typeStack.pop(); typeStack.push(rightType); visit(right); typeStack.pop(); gen.visitInsn(operator); gen.cast(returnType, typeStack.peek()); } private void pushCond(int operator, ExpressionContext left, ExpressionContext right) { Label labelTrue = new Label(); Label labelReturn = new Label(); typeStack.push(Type.DOUBLE_TYPE); visit(left); visit(right); typeStack.pop(); gen.ifCmp(Type.DOUBLE_TYPE, operator, labelTrue); pushBoolean(false); gen.goTo(labelReturn); gen.visitLabel(labelTrue); pushBoolean(true); gen.visitLabel(labelReturn); } private void pushBoolean(boolean truth) { switch (typeStack.peek().getSort()) { case Type.INT: gen.push(truth); break; case Type.LONG: gen.push(truth ? 1L : 0L); break; case Type.DOUBLE: gen.push(truth ? 1. : 0.); break; default: throw new IllegalStateException("Invalid expected type: " + typeStack.peek()); } } private void pushLong(long i) { switch (typeStack.peek().getSort()) { case Type.INT: gen.push((int) i); break; case Type.LONG: gen.push(i); break; case Type.DOUBLE: gen.push((double) i); break; default: throw new IllegalStateException("Invalid expected type: " + typeStack.peek()); } } }.visit(parseTree); gen.returnValue(); gen.endMethod(); classWriter.visitEnd(); }
From source file:org.apache.lucene.expressions.js.XJavascriptCompiler.java
License:Apache License
private void recursiveCompile(Tree current, Type expected) { int type = current.getType(); String text = current.getText(); switch (type) { case XJavascriptParser.AT_CALL: Tree identifier = current.getChild(0); String call = identifier.getText(); int arguments = current.getChildCount() - 1; Method method = functions.get(call); if (method == null) { throw new IllegalArgumentException("Unrecognized method call (" + call + ")."); }/*w w w. ja v a 2 s. com*/ int arity = method.getParameterTypes().length; if (arguments != arity) { throw new IllegalArgumentException("Expected (" + arity + ") arguments for method call (" + call + "), but found (" + arguments + ")."); } for (int argument = 1; argument <= arguments; ++argument) { recursiveCompile(current.getChild(argument), Type.DOUBLE_TYPE); } gen.invokeStatic(Type.getType(method.getDeclaringClass()), org.objectweb.asm.commons.Method.getMethod(method)); gen.cast(Type.DOUBLE_TYPE, expected); break; case XJavascriptParser.VARIABLE: int index; // normalize quotes text = normalizeQuotes(text); if (externalsMap.containsKey(text)) { index = externalsMap.get(text); } else { index = externalsMap.size(); externalsMap.put(text, index); } gen.loadArg(1); gen.push(index); gen.arrayLoad(FUNCTION_VALUES_TYPE); gen.loadArg(0); gen.invokeVirtual(FUNCTION_VALUES_TYPE, DOUBLE_VAL_METHOD); gen.cast(Type.DOUBLE_TYPE, expected); break; case XJavascriptParser.HEX: pushLong(expected, Long.parseLong(text.substring(2), 16)); break; case XJavascriptParser.OCTAL: pushLong(expected, Long.parseLong(text.substring(1), 8)); break; case XJavascriptParser.DECIMAL: gen.push(Double.parseDouble(text)); gen.cast(Type.DOUBLE_TYPE, expected); break; case XJavascriptParser.AT_NEGATE: recursiveCompile(current.getChild(0), Type.DOUBLE_TYPE); gen.visitInsn(Opcodes.DNEG); gen.cast(Type.DOUBLE_TYPE, expected); break; case XJavascriptParser.AT_ADD: pushArith(Opcodes.DADD, current, expected); break; case XJavascriptParser.AT_SUBTRACT: pushArith(Opcodes.DSUB, current, expected); break; case XJavascriptParser.AT_MULTIPLY: pushArith(Opcodes.DMUL, current, expected); break; case XJavascriptParser.AT_DIVIDE: pushArith(Opcodes.DDIV, current, expected); break; case XJavascriptParser.AT_MODULO: pushArith(Opcodes.DREM, current, expected); break; case XJavascriptParser.AT_BIT_SHL: pushShift(Opcodes.LSHL, current, expected); break; case XJavascriptParser.AT_BIT_SHR: pushShift(Opcodes.LSHR, current, expected); break; case XJavascriptParser.AT_BIT_SHU: pushShift(Opcodes.LUSHR, current, expected); break; case XJavascriptParser.AT_BIT_AND: pushBitwise(Opcodes.LAND, current, expected); break; case XJavascriptParser.AT_BIT_OR: pushBitwise(Opcodes.LOR, current, expected); break; case XJavascriptParser.AT_BIT_XOR: pushBitwise(Opcodes.LXOR, current, expected); break; case XJavascriptParser.AT_BIT_NOT: recursiveCompile(current.getChild(0), Type.LONG_TYPE); gen.push(-1L); gen.visitInsn(Opcodes.LXOR); gen.cast(Type.LONG_TYPE, expected); break; case XJavascriptParser.AT_COMP_EQ: pushCond(GeneratorAdapter.EQ, current, expected); break; case XJavascriptParser.AT_COMP_NEQ: pushCond(GeneratorAdapter.NE, current, expected); break; case XJavascriptParser.AT_COMP_LT: pushCond(GeneratorAdapter.LT, current, expected); break; case XJavascriptParser.AT_COMP_GT: pushCond(GeneratorAdapter.GT, current, expected); break; case XJavascriptParser.AT_COMP_LTE: pushCond(GeneratorAdapter.LE, current, expected); break; case XJavascriptParser.AT_COMP_GTE: pushCond(GeneratorAdapter.GE, current, expected); break; case XJavascriptParser.AT_BOOL_NOT: Label labelNotTrue = new Label(); Label labelNotReturn = new Label(); recursiveCompile(current.getChild(0), Type.INT_TYPE); gen.visitJumpInsn(Opcodes.IFEQ, labelNotTrue); pushBoolean(expected, false); gen.goTo(labelNotReturn); gen.visitLabel(labelNotTrue); pushBoolean(expected, true); gen.visitLabel(labelNotReturn); break; case XJavascriptParser.AT_BOOL_AND: Label andFalse = new Label(); Label andEnd = new Label(); recursiveCompile(current.getChild(0), Type.INT_TYPE); gen.visitJumpInsn(Opcodes.IFEQ, andFalse); recursiveCompile(current.getChild(1), Type.INT_TYPE); gen.visitJumpInsn(Opcodes.IFEQ, andFalse); pushBoolean(expected, true); gen.goTo(andEnd); gen.visitLabel(andFalse); pushBoolean(expected, false); gen.visitLabel(andEnd); break; case XJavascriptParser.AT_BOOL_OR: Label orTrue = new Label(); Label orEnd = new Label(); recursiveCompile(current.getChild(0), Type.INT_TYPE); gen.visitJumpInsn(Opcodes.IFNE, orTrue); recursiveCompile(current.getChild(1), Type.INT_TYPE); gen.visitJumpInsn(Opcodes.IFNE, orTrue); pushBoolean(expected, false); gen.goTo(orEnd); gen.visitLabel(orTrue); pushBoolean(expected, true); gen.visitLabel(orEnd); break; case XJavascriptParser.AT_COND_QUE: Label condFalse = new Label(); Label condEnd = new Label(); recursiveCompile(current.getChild(0), Type.INT_TYPE); gen.visitJumpInsn(Opcodes.IFEQ, condFalse); recursiveCompile(current.getChild(1), expected); gen.goTo(condEnd); gen.visitLabel(condFalse); recursiveCompile(current.getChild(2), expected); gen.visitLabel(condEnd); break; default: throw new IllegalStateException("Unknown operation specified: (" + current.getText() + ")."); } }
From source file:org.apache.maven.shared.dependency.analyzer.asm.DependencyVisitorTest.java
License:Apache License
public void testVisitJumpInsn() { visitor.visitJumpInsn(Opcodes.IFEQ, new Label()); assertNoClasses(); }
From source file:org.batoo.jpa.core.impl.instance.Enhancer.java
License:Open Source License
private static void createMethodCheck(final String enhancedClassName, final String descEnhancer, final ClassWriter cw) { final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PRIVATE, Enhancer.METHOD_ENHANCED_CHECK, Enhancer.makeDescription(Void.TYPE), null, null); mv.visitCode();/*from w w w. jav a 2 s . c o m*/ final Label lCheckInternal = new Label(); final Label lCheckInitialized = new Label(); final Label lReturn = new Label(); final Label lFind = new Label(); final Label lInitialized = new Label(); final Label lChanged = new Label(); final Label lOut = new Label(); // if (!this.__enhanced__$$__internal) { return } mv.visitLabel(lCheckInternal); mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {}); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INTERNAL, Enhancer.DESCRIPTOR_BOOLEAN); mv.visitJumpInsn(Opcodes.IFEQ, lCheckInitialized); mv.visitInsn(Opcodes.RETURN); // if (!this.__enhanced__$$__initialized) { mv.visitLabel(lCheckInitialized); mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {}); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED, Enhancer.DESCRIPTOR_BOOLEAN); mv.visitJumpInsn(Opcodes.IFNE, lChanged); // if (this.__enhanced_$$__session == null) mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION, Enhancer.DESCRIPTOR_SESSION); mv.visitJumpInsn(Opcodes.IFNONNULL, lFind); // throw new PersistenceException("No session to initialize the instance"); mv.visitTypeInsn(Opcodes.NEW, Enhancer.INTERNAL_PERSISTENCE_EXCEPTION); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn("No session to initialize the instance"); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Enhancer.INTERNAL_PERSISTENCE_EXCEPTION, Enhancer.CONSTRUCTOR_INIT, Enhancer.makeDescription(Void.TYPE, String.class)); mv.visitInsn(Opcodes.ATHROW); // this.__enhanced_$$__session.getEntityManager().find(this.__enhanced_$$__type, this.__enhanced__$$__id); mv.visitLabel(lFind); mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {}); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION, Enhancer.DESCRIPTOR_SESSION); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_SESSION, Enhancer.METHOD_GET_ENTITY_MANAGER, Enhancer.makeDescription(EntityManagerImpl.class)); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_TYPE, Enhancer.DESCRIPTOR_CLASS); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_ID, Enhancer.DESCRIPTOR_OBJECT); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_ENTITY_MANAGER, Enhancer.METHOD_FIND, Enhancer.makeDescription(Object.class, Class.class, Object.class)); mv.visitInsn(Opcodes.POP); // this.__enhanced__$$__initialized = true; mv.visitLabel(lInitialized); mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {}); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ICONST_1); mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED, Enhancer.DESCRIPTOR_BOOLEAN); // if (this.__enhanced_$$__session != null) mv.visitLabel(lChanged); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION, Enhancer.DESCRIPTOR_SESSION); mv.visitJumpInsn(Opcodes.IFNULL, lReturn); // this.__enhanced__$$__managedInstance.changed(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_MANAGED_INSTANCE, Enhancer.DESCRIPTOR_MANAGED_INSTANCE); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_MANAGED_INSTANCE, Enhancer.METHOD_CHANGED, Enhancer.makeDescription(Void.TYPE)); // return; mv.visitLabel(lReturn); mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {}); mv.visitInsn(Opcodes.RETURN); mv.visitLabel(lOut); mv.visitLocalVariable(Enhancer.THIS, descEnhancer, null, lCheckInternal, lOut, 0); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:org.boretti.drools.integration.drools5.DroolsClassVisitor.java
License:Open Source License
@Override public void visitEnd() { FieldVisitor fv = null;/*from w w w .ja v a 2 s . c om*/ if (isNeedChangeForBoth()) { fv = super.visitField(Opcodes.ACC_PRIVATE, DROOLS_FIELD_NAME, Type.BOOLEAN_TYPE.getDescriptor(), null, null); if (fv != null) { AnnotationVisitor av = fv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true); AnnotationVisitor value = av.visitArray("value"); value.visit("", "Generated by Drools5IntegrationHelper Maven plugin"); value.visitEnd(); av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current)); av.visitEnd(); fv.visitEnd(); } } if (isNeedChangeForField()) { fv = super.visitField(Opcodes.ACC_PRIVATE, DROOLS_FIELD_RULE, Type.getType(RuleBase.class).getDescriptor(), null, null); if (fv != null) { AnnotationVisitor av = fv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true); AnnotationVisitor value = av.visitArray("value"); value.visit("", "Generated by Drools5IntegrationHelper Maven plugin"); value.visitEnd(); av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current)); av.visitEnd(); fv.visitEnd(); } MethodVisitor mv = super.visitMethod(Opcodes.ACC_PRIVATE, DROOLS_METHOD_RUN, "()V", null, null); AnnotationVisitor av = mv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true); AnnotationVisitor value = av.visitArray("value"); value.visit("", "Generated by Drools5IntegrationHelper Maven plugin"); value.visitEnd(); av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current)); av.visitEnd(); mv.visitCode(); Label start = new Label(); mv.visitLabel(start); Label doIt = new Label(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_NAME, Type.BOOLEAN_TYPE.getDescriptor()); mv.visitJumpInsn(Opcodes.IFEQ, doIt); mv.visitInsn(Opcodes.RETURN); mv.visitLabel(doIt); mv.visitFrame(Opcodes.F_SAME, 1, new Object[] { Type.getObjectType(me).getInternalName() }, 0, new Object[] {}); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_RULE, Type.getType(RuleBase.class).getDescriptor()); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(RuleBase.class).getInternalName(), "newStatelessSession", "()Lorg/drools/StatelessSession;"); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn(FIELD_NAME_LOGGER); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(Object.class).getInternalName(), "getClass", "()Ljava/lang/Class;"); mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getType(org.apache.log4j.Logger.class).getInternalName(), "getLogger", "(Ljava/lang/Class;)Lorg/apache/log4j/Logger;"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(StatelessSession.class).getInternalName(), "setGlobal", "(Ljava/lang/String;Ljava/lang/Object;)V"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(StatelessSession.class).getInternalName(), "execute", "(Ljava/lang/Object;)V"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ICONST_1); mv.visitFieldInsn(Opcodes.PUTFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_NAME, Type.BOOLEAN_TYPE.getDescriptor()); mv.visitInsn(Opcodes.RETURN); Label end = new Label(); mv.visitLabel(end); mv.visitLocalVariable("this", Type.getObjectType(me).getDescriptor(), null, start, end, 0); mv.visitMaxs(4, 1); mv.visitEnd(); } super.visitEnd(); }
From source file:org.codehaus.groovy.runtime.callsite.CallSiteGenerator.java
License:Apache License
private static MethodVisitor writeMethod(ClassWriter cw, String name, int argumentCount, final String superClass, CachedMethod cachedMethod, String receiverType, String parameterDescription, boolean useArray) { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "call" + name, "(L" + receiverType + ";" + parameterDescription + ")Ljava/lang/Object;", null, null); mv.visitCode();/*from w w w . j a v a 2 s . c o m*/ final Label tryStart = new Label(); mv.visitLabel(tryStart); // call for checking if method is still valid for (int i = 0; i < argumentCount; ++i) mv.visitVarInsn(Opcodes.ALOAD, i); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, superClass, "checkCall", "(Ljava/lang/Object;" + parameterDescription + ")Z", false); Label l0 = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, l0); // valid method branch Class callClass = cachedMethod.getDeclaringClass().getTheClass(); boolean useInterface = callClass.isInterface(); String type = BytecodeHelper.getClassInternalName(callClass.getName()); String descriptor = BytecodeHelper.getMethodDescriptor(cachedMethod.getReturnType(), cachedMethod.getNativeParameterTypes()); // prepare call int invokeMethodCode = Opcodes.INVOKEVIRTUAL; if (cachedMethod.isStatic()) { invokeMethodCode = Opcodes.INVOKESTATIC; } else { mv.visitVarInsn(Opcodes.ALOAD, 1); BytecodeHelper.doCast(mv, callClass); if (useInterface) invokeMethodCode = Opcodes.INVOKEINTERFACE; } Class<?>[] parameters = cachedMethod.getPT(); int size = parameters.length; for (int i = 0; i < size; i++) { if (useArray) { // unpack argument from Object[] mv.visitVarInsn(Opcodes.ALOAD, 2); BytecodeHelper.pushConstant(mv, i); mv.visitInsn(Opcodes.AALOAD); } else { mv.visitVarInsn(Opcodes.ALOAD, i + 2); } // cast argument to parameter class, inclusive unboxing // for methods with primitive types BytecodeHelper.doCast(mv, parameters[i]); } // make call mv.visitMethodInsn(invokeMethodCode, type, cachedMethod.getName(), descriptor, useInterface); // produce result BytecodeHelper.box(mv, cachedMethod.getReturnType()); if (cachedMethod.getReturnType() == void.class) { mv.visitInsn(Opcodes.ACONST_NULL); } // return mv.visitInsn(Opcodes.ARETURN); // fall back after method change mv.visitLabel(l0); for (int i = 0; i < argumentCount; ++i) mv.visitVarInsn(Opcodes.ALOAD, i); if (!useArray) { mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ArrayUtil", "createArray", "(" + parameterDescription + ")[Ljava/lang/Object;", false); } mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/callsite/CallSiteArray", "defaultCall" + name, "(Lorg/codehaus/groovy/runtime/callsite/CallSite;L" + receiverType + ";[Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitInsn(Opcodes.ARETURN); // exception unwrapping for stackless exceptions final Label tryEnd = new Label(); mv.visitLabel(tryEnd); final Label catchStart = new Label(); mv.visitLabel(catchStart); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ScriptBytecodeAdapter", "unwrap", "(Lgroovy/lang/GroovyRuntimeException;)Ljava/lang/Throwable;", false); mv.visitInsn(Opcodes.ATHROW); mv.visitTryCatchBlock(tryStart, tryEnd, catchStart, "groovy/lang/GroovyRuntimeException"); mv.visitMaxs(0, 0); mv.visitEnd(); return mv; }
From source file:org.copperengine.core.instrument.TryCatchBlockHandler.java
License:Apache License
@SuppressWarnings("unchecked") public void instrument(ClassNode cn) { // if (1 == 1) return; for (MethodNode m : (List<MethodNode>) cn.methods) { if (!m.exceptions.contains(INTERRUPT_EXCEPTION_NAME) || m.tryCatchBlocks.isEmpty()) { continue; }/* w w w . j av a 2 s . c o m*/ logger.info("Instrument " + cn.name + "." + m.name); HashSet<Label> labels = new HashSet<Label>(); for (TryCatchBlockNode catchNode : (List<TryCatchBlockNode>) m.tryCatchBlocks) { if (labels.contains(catchNode.handler.getLabel())) { // some handlers share their handling code - check it out to prevent double instrumentation logger.info("skipping node"); continue; } labels.add(catchNode.handler.getLabel()); LabelNode labelNode = catchNode.handler; AbstractInsnNode lineNumberNode = labelNode.getNext() instanceof LineNumberNode ? labelNode.getNext() : labelNode; FrameNode frameNode = (FrameNode) lineNumberNode.getNext(); VarInsnNode varInsnNode = (VarInsnNode) frameNode.getNext(); AbstractInsnNode insertPoint = varInsnNode; if (catchNode.type == null) { // this is probably a finally block; if (insertPoint.getNext() != null && insertPoint.getNext() instanceof LabelNode) { insertPoint = insertPoint.getNext(); } } LabelNode labelNode4ifeg = new LabelNode(); InsnList newCode = new InsnList(); newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var)); newCode.add(new TypeInsnNode(Opcodes.INSTANCEOF, INTERRUPT_EXCEPTION_NAME)); newCode.add(new JumpInsnNode(Opcodes.IFEQ, labelNode4ifeg)); newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var)); newCode.add(new TypeInsnNode(Opcodes.CHECKCAST, INTERRUPT_EXCEPTION_NAME)); newCode.add(new InsnNode(Opcodes.ATHROW)); newCode.add(labelNode4ifeg); m.instructions.insert(insertPoint, newCode); } } }
From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java
License:Open Source License
@Override public void visitJumpInsn(final int opcode, final Label label) { T t = null;//from ww w .j av a 2 s .com Object oValue = null; final int targetPc = getPc(label); switch (opcode) { /******** * GOTO * ********/ case Opcodes.GOTO: { final GOTO op = new GOTO(this.ops.size(), opcode, this.line); add(op); op.setTargetPc(targetPc); if (targetPc < 0) { getUnresolved(label).add(op); } break; } /******** * JCMP * ********/ case Opcodes.IF_ACMPEQ: t = T.REF; oValue = CmpType.T_EQ; // fall through case Opcodes.IF_ACMPNE: if (t == null) { t = T.REF; oValue = CmpType.T_NE; } // fall through case Opcodes.IF_ICMPEQ: if (t == null) { t = T.AINT; // boolean too oValue = CmpType.T_EQ; } // fall through case Opcodes.IF_ICMPGE: if (t == null) { t = T.INT; oValue = CmpType.T_GE; } // fall through case Opcodes.IF_ICMPGT: if (t == null) { t = T.INT; oValue = CmpType.T_GT; } // fall through case Opcodes.IF_ICMPLE: if (t == null) { t = T.INT; oValue = CmpType.T_LE; } // fall through case Opcodes.IF_ICMPLT: if (t == null) { t = T.INT; oValue = CmpType.T_LT; } // fall through case Opcodes.IF_ICMPNE: if (t == null) { t = T.AINT; // boolean too oValue = CmpType.T_NE; } { assert oValue instanceof CmpType; final JCMP op = new JCMP(this.ops.size(), opcode, this.line, t, (CmpType) oValue); add(op); op.setTargetPc(targetPc); if (targetPc < 0) { getUnresolved(label).add(op); } } break; /******** * JCND * ********/ case Opcodes.IFNULL: t = T.REF; oValue = CmpType.T_EQ; // fall through case Opcodes.IFNONNULL: if (t == null) { t = T.REF; oValue = CmpType.T_NE; } // fall through case Opcodes.IFEQ: if (t == null) { t = T.AINT; // boolean too oValue = CmpType.T_EQ; } // fall through case Opcodes.IFGE: if (t == null) { t = T.INT; oValue = CmpType.T_GE; } // fall through case Opcodes.IFGT: if (t == null) { t = T.INT; oValue = CmpType.T_GT; } // fall through case Opcodes.IFLE: if (t == null) { t = T.INT; oValue = CmpType.T_LE; } // fall through case Opcodes.IFLT: if (t == null) { t = T.INT; oValue = CmpType.T_LT; } // fall through case Opcodes.IFNE: if (t == null) { t = T.AINT; // boolean too oValue = CmpType.T_NE; } { assert oValue instanceof CmpType; final JCND op = new JCND(this.ops.size(), opcode, this.line, t, (CmpType) oValue); add(op); op.setTargetPc(targetPc); if (targetPc < 0) { getUnresolved(label).add(op); } } break; /******* * JSR * *******/ case Opcodes.JSR: { final JSR op = new JSR(this.ops.size(), opcode, this.line); add(op); op.setTargetPc(targetPc); if (targetPc < 0) { getUnresolved(label).add(op); } break; } default: log.warn(getM() + ": Unknown jump insn opcode '" + opcode + "'!"); } }