List of usage examples for org.objectweb.asm Opcodes CHECKCAST
int CHECKCAST
To view the source code for org.objectweb.asm Opcodes CHECKCAST.
Click Source Link
From source file:org.jacoco.core.runtime.LoggerRuntime.java
License:Open Source License
public int generateDataAccessor(final long classid, final String classname, final int probecount, final MethodVisitor mv) { // The data accessor performs the following steps: ////from ww w .ja v a2s . co m // final Object[] args = new Object[3]; // args[0] = Long.valueOf(classid); // args[1] = classname; // args[2] = Integer.valueOf(probecount); // Logger.getLogger(CHANNEL).log(Level.INFO, key, args); // final byte[] probedata = (byte[]) args[0]; // // Note that local variable 'args' is used at two places. As were not // allowed to allocate local variables we have to keep this value with // DUP and SWAP operations on the operand stack. // 1. Create parameter array: RuntimeData.generateArgumentArray(classid, classname, probecount, mv); // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.DUP); // Stack[1]: [Ljava/lang/Object; // Stack[0]: [Ljava/lang/Object; // 2. Call Logger: mv.visitLdcInsn(CHANNEL); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/logging/Logger", "getLogger", "(Ljava/lang/String;)Ljava/util/logging/Logger;"); // Stack[2]: Ljava/util/logging/Logger; // Stack[1]: [Ljava/lang/Object; // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.SWAP); // Stack[2]: [Ljava/lang/Object; // Stack[1]: Ljava/util/logging/Logger; // Stack[0]: [Ljava/lang/Object; mv.visitFieldInsn(Opcodes.GETSTATIC, "java/util/logging/Level", "INFO", "Ljava/util/logging/Level;"); // Stack[3]: Ljava/util/logging/Level; // Stack[2]: [Ljava/lang/Object; // Stack[1]: Ljava/util/logging/Logger; // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.SWAP); // Stack[3]: [Ljava/lang/Object; // Stack[2]: Ljava/util/logging/Level; // Stack[1]: Ljava/util/logging/Logger; // Stack[0]: [Ljava/lang/Object; mv.visitLdcInsn(key); // Stack[4]: Ljava/lang/String; // Stack[3]: [Ljava/lang/Object; // Stack[2]: Ljava/util/logging/Level; // Stack[1]: Ljava/util/logging/Logger; // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.SWAP); // Stack[4]: [Ljava/lang/Object; // Stack[3]: Ljava/lang/String; // Stack[2]: Ljava/util/logging/Level; // Stack[1]: Ljava/util/logging/Logger; // Stack[0]: [Ljava/lang/Object; mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/logging/Logger", "log", "(Ljava/util/logging/Level;Ljava/lang/String;[Ljava/lang/Object;)V"); // Stack[0]: [Ljava/lang/Object; // 3. Load data structure from parameter array: mv.visitInsn(Opcodes.ICONST_0); mv.visitInsn(Opcodes.AALOAD); mv.visitTypeInsn(Opcodes.CHECKCAST, InstrSupport.DATAFIELD_DESC); // Stack[0]: [Z return 5; // Maximum local stack size is 5 }
From source file:org.jacoco.core.runtime.URLStreamHandlerRuntime.java
License:Open Source License
public int generateDataAccessor(final long classid, final String classname, final int probecount, final MethodVisitor mv) { // The data accessor performs the following steps: //// ww w .j av a 2 s . c om // final URL url = new URL(protocol, null, ""); // final URLConnection connection = url.openConnection(); // final Object[] args = new Object[3]; // args[0] = Long.valueOf(classid); // args[1] = classname; // args[2] = Integer.valueOf(probecount); // connection.equals(args); // final byte[] probedata = (byte[]) args[0]; RuntimeData.generateArgumentArray(classid, classname, probecount, mv); mv.visitInsn(Opcodes.DUP); // Stack[1]: [Ljava/lang/Object; // Stack[0]: [Ljava/lang/Object; mv.visitTypeInsn(Opcodes.NEW, "java/net/URL"); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn(protocol); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitLdcInsn(""); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/net/URL", "<init>", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); // Stack[2]: [Ljava/net/URL; // Stack[1]: [Ljava/lang/Object; // Stack[0]: [Ljava/lang/Object; mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/net/URL", "openConnection", "()Ljava/net/URLConnection;"); // Stack[2]: [Ljava/net/URLConnection; // Stack[1]: [Ljava/lang/Object; // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.SWAP); // Stack[2]: [Ljava/lang/Object; // Stack[1]: [Ljava/net/URLConnection; // Stack[0]: [Ljava/lang/Object; mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z"); // Stack[1]: Z; // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.POP); // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.ICONST_0); mv.visitInsn(Opcodes.AALOAD); mv.visitTypeInsn(Opcodes.CHECKCAST, InstrSupport.DATAFIELD_DESC); return 7; }
From source file:org.jboss.arquillian.extension.jacoco.container.ArquillianRuntime.java
License:Apache License
public int generateDataAccessor(long classid, String classname, int probecount, MethodVisitor mv) { // 1. Create parameter array: generateArgumentArray(classid, classname, probecount, mv); // stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.DUP);//w w w . j a v a 2s . c om // stack[1]: [Ljava/lang/Object; // stack[0]: [Ljava/lang/Object; // 2. Invoke ArquillianRuntime: mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/jboss/arquillian/extension/jacoco/container/ArquillianRuntime", "getInstance", "()Lorg/jboss/arquillian/extension/jacoco/container/ArquillianRuntime;"); // stack[2]: LArquillianRuntime; // stack[1]: [Ljava/lang/Object; // stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.SWAP); // stack[2]: [Ljava/lang/Object; // stack[1]: LArquillianRuntime; // stack[0]: [Ljava/lang/Object; // 3. Invoke ArquillianRuntime swapExecutionData, gets the boolean[] in Object[0]: mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/jboss/arquillian/extension/jacoco/container/ArquillianRuntime", "swapExecutionData", "([Ljava/lang/Object;)V"); // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.ICONST_0); mv.visitInsn(Opcodes.AALOAD); mv.visitTypeInsn(Opcodes.CHECKCAST, InstrSupport.DATAFIELD_DESC); // Stack[0]: [Z; return 5; }
From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java
License:Open Source License
/** * Generates the instruction to check that the top stack value is of the * given type./*from ww w.j ava2s.com*/ * * @param type a class or interface type. */ public void checkCast(final Type type) { if (!type.equals(OBJECT_TYPE)) { typeInsn(Opcodes.CHECKCAST, type); } }
From source file:org.jboss.byteman.rule.binding.Binding.java
License:Open Source License
public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException { if (alias != null) { alias.compile(mv, compileContext); } else if (isBindVar()) { // push the current helper instance i.e. this -- adds 1 to stack height mv.visitVarInsn(Opcodes.ALOAD, 0); // push the variable name -- adds 1 to stack height mv.visitLdcInsn(name);// w ww . jav a 2 s. c o m // increment stack count compileContext.addStackCount(2); // compile the rhs expression for the binding -- adds 1 to stack height value.compile(mv, compileContext); // make sure value is boxed if necessary if (type.isPrimitive()) { compileBox(Type.boxType(type), mv, compileContext); } else if (doCheckCast) { mv.visitTypeInsn(Opcodes.CHECKCAST, type.getInternalName()); } // compile a setBinding call pops 3 from stack height mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.internalName(HelperAdapter.class), "setBinding", "(Ljava/lang/String;Ljava/lang/Object;)V"); compileContext.addStackCount(-3); } }
From source file:org.jboss.byteman.rule.compiler.Compiler.java
License:Open Source License
private static byte[] compileBytes(Rule rule, Class helperClass, String helperName, String compiledHelperName, boolean compileToBytecode) throws Exception { ClassWriter cw = new ClassWriter(0); FieldVisitor fv;/*from w w w. ja v a2 s.c om*/ MethodVisitor mv; AnnotationVisitor av0; // create the class as a subclass of the rule helper class, appending Compiled to the front // of the class name and a unique number to the end of the class helperName // also ensure it implements the HelperAdapter interface // // public class foo.bar.Compiled_<helper>_<NNN> extends foo.bar.<helper> implements HelperAdapter cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, compiledHelperName, null, helperName, new String[] { "org/jboss/byteman/rule/helper/HelperAdapter" }); // we need to install the source file name { String fullFileName = rule.getFile(); int idx = fullFileName.lastIndexOf(java.io.File.separatorChar); String basicFileName = (idx < 0 ? fullFileName : fullFileName.substring(idx + 1)); String debug = "// compiled from: " + fullFileName + "\n// generated by Byteman\n"; cw.visitSource(basicFileName, debug); } { // we need a Hashmap field to hold the bindings // // private HashMap<String, Object> bindingMap; fv = cw.visitField(ACC_PRIVATE, "bindingMap", "Ljava/util/HashMap;", "Ljava/util/HashMap<Ljava/lang/String;Ljava/lang/Object;>;", null); fv.visitEnd(); } { // and a rule field to hold the rule // // private Rule rule; fv = cw.visitField(ACC_PRIVATE, "rule", "Lorg/jboss/byteman/rule/Rule;", "Lorg/jboss/byteman/rule/Rule;", null); fv.visitEnd(); } { // we need a constructor which takes a Rule as argument // if the helper implements a constructor which takes a Rule as argument then we invoke it // otherwise we invoke the empty helper constructor Constructor superConstructor = null; try { superConstructor = helperClass.getDeclaredConstructor(Rule.class); } catch (NoSuchMethodException e) { // hmm, ok see if there is an empty constructor } catch (SecurityException e) { throw new CompileException("Compiler.compileBytes : unable to access constructor for helper class " + helperClass.getCanonicalName()); } boolean superWantsRule = (superConstructor != null); if (!superWantsRule) { try { superConstructor = helperClass.getDeclaredConstructor(); } catch (NoSuchMethodException e) { throw new CompileException( "Compiler.compileBytes : no valid constructor found for helper class " + helperClass.getCanonicalName()); } catch (SecurityException e) { throw new CompileException( "Compiler.compileBytes : unable to access constructor for helper class " + helperClass.getCanonicalName()); } } // // public Compiled<helper>_<NNN>()Rule rule) mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Lorg/jboss/byteman/rule/Rule;)V", null, null); mv.visitCode(); // super(); // // or // // super(Rule); if (superWantsRule) { mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, helperName, "<init>", "(Lorg/jboss/byteman/rule/Rule;)V"); } else { mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, helperName, "<init>", "()V"); } // bindingMap = new HashMap<String, Object); mv.visitVarInsn(ALOAD, 0); mv.visitTypeInsn(NEW, "java/util/HashMap"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashMap", "<init>", "()V"); mv.visitFieldInsn(PUTFIELD, compiledHelperName, "bindingMap", "Ljava/util/HashMap;"); // this.rule = rule mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, compiledHelperName, "rule", "Lorg/jboss/byteman/rule/Rule;"); // return; mv.visitInsn(RETURN); mv.visitMaxs(3, 2); mv.visitEnd(); } { // create the execute method // // public void execute(Bindings bindings, Object recipient, Object[] args) throws ExecuteException mv = cw.visitMethod(ACC_PUBLIC, "execute", "(Ljava/lang/Object;[Ljava/lang/Object;)V", null, new String[] { "org/jboss/byteman/rule/exception/ExecuteException" }); mv.visitCode(); // if (Transformer.isVerbose()) mv.visitMethodInsn(INVOKESTATIC, "org/jboss/byteman/agent/Transformer", "isVerbose", "()Z"); Label l0 = new Label(); mv.visitJumpInsn(IFEQ, l0); // then // System.out.println(rule.getName() + " execute"); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "rule", "Lorg/jboss/byteman/rule/Rule;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/jboss/byteman/rule/Rule", "getName", "()Ljava/lang/String;"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;"); mv.visitLdcInsn(" execute()"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); // end if mv.visitLabel(l0); Bindings bindings = rule.getBindings(); Iterator<Binding> iterator = bindings.iterator(); while (iterator.hasNext()) { Binding binding = iterator.next(); String name = binding.getName(); if (binding.isAlias()) { // lookups and updates will use the aliased name continue; } if (binding.isHelper()) { // bindingMap.put(name, this); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "bindingMap", "Ljava/util/HashMap;"); mv.visitLdcInsn(name); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/HashMap", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(POP); } else if (binding.isRecipient()) { // bindingMap.put(name, recipient); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "bindingMap", "Ljava/util/HashMap;"); mv.visitLdcInsn(name); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/HashMap", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(POP); // } else if (binding.isParam() || binding.isLocalVar() || binding.isReturn() || // binding.isThrowable() || binding.isParamCount() || binding.isParamArray()) { } else if (!binding.isBindVar()) { // bindingMap.put(name, args[binding.getCallArrayIndex()]); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "bindingMap", "Ljava/util/HashMap;"); mv.visitLdcInsn(name); mv.visitVarInsn(ALOAD, 2); mv.visitLdcInsn(binding.getCallArrayIndex()); mv.visitInsn(AALOAD); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/HashMap", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(POP); } } // execute0() mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, compiledHelperName, "execute0", "()V"); // now restore update bindings iterator = bindings.iterator(); while (iterator.hasNext()) { Binding binding = iterator.next(); if (binding.isAlias()) { continue; } String name = binding.getName(); if (binding.isUpdated()) { // if (binding.isParam() || binding.isLocalVar() || binding.isReturn()) { if (!binding.isBindVar()) { int idx = binding.getCallArrayIndex(); // Object value = bindingMap.get(name); // args[idx] = value; mv.visitVarInsn(ALOAD, 2); // args mv.visitLdcInsn(idx); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "bindingMap", "Ljava/util/HashMap;"); mv.visitLdcInsn(name); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/HashMap", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(AASTORE); } } } // return mv.visitInsn(RETURN); mv.visitMaxs(4, 3); mv.visitEnd(); } { // create the setBinding method // // public void setBinding(String name, Object value) mv = cw.visitMethod(ACC_PUBLIC, "setBinding", "(Ljava/lang/String;Ljava/lang/Object;)V", null, null); mv.visitCode(); // bindingMap.put(name, value); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "bindingMap", "Ljava/util/HashMap;"); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/HashMap", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(POP); // return mv.visitInsn(RETURN); mv.visitMaxs(3, 3); mv.visitEnd(); } { // create the getBinding method // // public Object getBinding(String name) mv = cw.visitMethod(ACC_PUBLIC, "getBinding", "(Ljava/lang/String;)Ljava/lang/Object;", null, null); mv.visitCode(); // {TOS} <== bindingMap.get(name); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "bindingMap", "Ljava/util/HashMap;"); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/HashMap", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); // return {TOS} mv.visitInsn(ARETURN); mv.visitMaxs(2, 2); mv.visitEnd(); } { // create the getName method // // public String getName() mv = cw.visitMethod(ACC_PUBLIC, "getName", "()Ljava/lang/String;", null, null); mv.visitCode(); // {TOS} <== rule.getName() mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "rule", "Lorg/jboss/byteman/rule/Rule;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/jboss/byteman/rule/Rule", "getName", "()Ljava/lang/String;"); // return {TOS} mv.visitInsn(ARETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } // create the getAccessibleField method // // public Object getAccessibleField(Object owner, int fieldIndex) { mv = cw.visitMethod(ACC_PUBLIC, "getAccessibleField", "(Ljava/lang/Object;I)Ljava/lang/Object;", null, null); mv.visitCode(); // {TOS} <== rule.getAccessibleField(owner, fieldIndex); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "rule", "Lorg/jboss/byteman/rule/Rule;"); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ILOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "org/jboss/byteman/rule/Rule", "getAccessibleField", "(Ljava/lang/Object;I)Ljava/lang/Object;"); // return {TOS} mv.visitInsn(ARETURN); mv.visitMaxs(3, 3); mv.visitEnd(); } // create the setAccessibleField method // // public void setAccessibleField(Object owner, Object value, int fieldIndex) // rule.setAccessibleField(owner, value, fieldIndex); { mv = cw.visitMethod(ACC_PUBLIC, "setAccessibleField", "(Ljava/lang/Object;Ljava/lang/Object;I)V", null, null); mv.visitCode(); // rule.setAccessibleField(owner, value, fieldIndex); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "rule", "Lorg/jboss/byteman/rule/Rule;"); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ILOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, "org/jboss/byteman/rule/Rule", "setAccessibleField", "(Ljava/lang/Object;Ljava/lang/Object;I)V"); // return mv.visitInsn(RETURN); mv.visitMaxs(4, 4); mv.visitEnd(); } // create the invokeAccessibleMethod method // // public Object invokeAccessibleMethod(Object target, Object[] args, int methodIndex) // {TOS} <== rule.invokeAccessibleMethod(target, args, methodIndex); { mv = cw.visitMethod(ACC_PUBLIC, "invokeAccessibleMethod", "(Ljava/lang/Object;[Ljava/lang/Object;I)Ljava/lang/Object;", null, null); mv.visitCode(); // rule.invokeAccessibleMethod(target, args, fieldIndex); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "rule", "Lorg/jboss/byteman/rule/Rule;"); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ILOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, "org/jboss/byteman/rule/Rule", "invokeAccessibleMethod", "(Ljava/lang/Object;[Ljava/lang/Object;I)Ljava/lang/Object;"); // return {TOS} mv.visitInsn(ARETURN); mv.visitMaxs(4, 4); mv.visitEnd(); } if (compileToBytecode) { // we generate a single execute0 method if we want to run compiled and get // the event, condiiton and action to insert the relevant bytecode to implement // bind(), test() and fire() { // create the execute0() method // // private void execute0() mv = cw.visitMethod(ACC_PRIVATE, "execute0", "()V", null, new String[] { "org/jboss/byteman/rule/exception/ExecuteException" }); mv.visitCode(); CompileContext compileContext = new CompileContext(mv); // make sure we set the first line number before generating any code compileContext.notifySourceLine(rule.getLine()); compileContext.addLocalCount(3); // for this and 2 object args // bind(); rule.getEvent().compile(mv, compileContext); // if (test()) rule.getCondition().compile(mv, compileContext); Label l0 = new Label(); mv.visitJumpInsn(IFEQ, l0); compileContext.addStackCount(-1); // then rule.getAction().compile(mv, compileContext); // fire(); // end if mv.visitLabel(l0); // this will match the ENDRULE line compileContext.notifySourceEnd(); // return mv.visitInsn(RETURN); // need to specify correct Maxs values mv.visitMaxs(compileContext.getStackMax(), compileContext.getLocalMax()); mv.visitEnd(); } } else { // we generate the following methods if we want to run interpreted { // create the execute0() method // // private void execute0() mv = cw.visitMethod(ACC_PRIVATE, "execute0", "()V", null, new String[] { "org/jboss/byteman/rule/exception/ExecuteException" }); mv.visitCode(); // bind(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, compiledHelperName, "bind", "()V"); // if (test()) mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, compiledHelperName, "test", "()Z"); Label l0 = new Label(); mv.visitJumpInsn(IFEQ, l0); // then // fire(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, compiledHelperName, "fire", "()V"); // end if mv.visitLabel(l0); // return mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { // create the bind method // // private void bind() mv = cw.visitMethod(ACC_PRIVATE, "bind", "()V", null, new String[] { "org/jboss/byteman/rule/exception/ExecuteException" }); mv.visitCode(); // rule.getEvent().interpret(this); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "rule", "Lorg/jboss/byteman/rule/Rule;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/jboss/byteman/rule/Rule", "getEvent", "()Lorg/jboss/byteman/rule/Event;"); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "org/jboss/byteman/rule/Event", "interpret", "(Lorg/jboss/byteman/rule/helper/HelperAdapter;)Ljava/lang/Object;"); mv.visitInsn(RETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } { // create the test method // // private boolean test() mv = cw.visitMethod(ACC_PRIVATE, "test", "()Z", null, new String[] { "org/jboss/byteman/rule/exception/ExecuteException" }); mv.visitCode(); // {TOS} <== rule.getCondition().interpret(this); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "rule", "Lorg/jboss/byteman/rule/Rule;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/jboss/byteman/rule/Rule", "getCondition", "()Lorg/jboss/byteman/rule/Condition;"); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "org/jboss/byteman/rule/Condition", "interpret", "(Lorg/jboss/byteman/rule/helper/HelperAdapter;)Ljava/lang/Object;"); mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Boolean"); // unbox the returned Boolean mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z"); // return {TOS} mv.visitInsn(IRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } { // create the fire method // // private void fire() mv = cw.visitMethod(ACC_PRIVATE, "fire", "()V", null, new String[] { "org/jboss/byteman/rule/exception/ExecuteException" }); mv.visitCode(); // rule.getAction().interpret(this); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, compiledHelperName, "rule", "Lorg/jboss/byteman/rule/Rule;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/jboss/byteman/rule/Rule", "getAction", "()Lorg/jboss/byteman/rule/Action;"); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "org/jboss/byteman/rule/Action", "interpret", "(Lorg/jboss/byteman/rule/helper/HelperAdapter;)Ljava/lang/Object;"); // return mv.visitInsn(RETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } } cw.visitEnd(); return cw.toByteArray(); }
From source file:org.jboss.byteman.rule.RuleElement.java
License:Open Source License
protected void compileObjectConversion(Type fromType, Type toType, MethodVisitor mv, CompileContext compileContext) throws CompileException { // ensure any primitive type is boxed before we go any further if (fromType.isPrimitive()) { Type boxType = Type.boxType(fromType); compileBox(boxType, mv, compileContext); fromType = boxType;/*from w w w.j a v a2 s. c o m*/ } if (toType.isAssignableFrom(fromType)) { // special case -- isAssignableFrom says yes if we are trying to assign to a String but // we may still need to do a toString cobversion all the same if (toType == Type.STRING && fromType != Type.STRING) { compileStringConversion(fromType, toType, mv, compileContext); } else { // nothing more to do } } else { // this happens when we downcast a bound variable from Object to the variable's type assert fromType.isAssignableFrom(toType); mv.visitTypeInsn(Opcodes.CHECKCAST, toType.getInternalName(false, true)); } }
From source file:org.jooby.internal.apitool.BytecodeRouteParser.java
License:Apache License
private java.lang.reflect.Type parameterType(final ClassLoader loader, final AbstractInsnNode n) { if (n instanceof MethodInsnNode) { MethodInsnNode node = (MethodInsnNode) n; if (mutantValue().test(node)) { /** value(); intValue(); booleanValue(); */ return TypeDescriptorParser.parse(loader, node.desc); } else if (mutantToSomething().test(node) || getOrCreateKotlinClass().test(node)) { /** to(String.class); toOptional; toList(); */ String owner = Type.getReturnType(node.desc).getClassName(); AbstractInsnNode prev = node.getPrevious(); if (prev instanceof FieldInsnNode && ((MethodInsnNode) n).name.equals("toEnum")) { /** toEnum(Letter.A); */ return loadType(loader, ((FieldInsnNode) prev).owner); }/*from www . jav a 2s. c o m*/ java.lang.reflect.Type toType = String.class; if (prev instanceof LdcInsnNode) { /** to(Foo.class); */ Object cst = ((LdcInsnNode) prev).cst; if (cst instanceof Type) { toType = loadType(loader, ((Type) cst).getClassName()); } } else if (prev instanceof FieldInsnNode) { toType = loadType(loader, ((FieldInsnNode) prev).owner); } // JoobyKt.toOptional AbstractInsnNode next = node.getNext(); if (next instanceof MethodInsnNode) { String joobyKt = ((MethodInsnNode) next).owner; String methodName = ((MethodInsnNode) next).name; if ("toOptional".equals(methodName) && "org/jooby/JoobyKt".equals(joobyKt)) { owner = Optional.class.getName(); } } Set<String> skipOwners = ImmutableSet.of(Object.class.getName(), Enum.class.getName(), "kotlin.reflect.KClass"); if (skipOwners.contains(owner)) { return toType; } /** toList(Foo.class); */ return Types.newParameterizedType(loadType(loader, owner), toType); } } else if (n instanceof VarInsnNode) { return new Insn<>(null, n).prev().filter(is(MethodInsnNode.class)).findFirst() .map(MethodInsnNode.class::cast).filter(file()).map(m -> { return m.name.equals("files") ? Types.newParameterizedType(List.class, File.class) : File.class; }).orElse(Object.class); } else if (n instanceof TypeInsnNode) { TypeInsnNode typeInsn = (TypeInsnNode) n; if (typeInsn.getOpcode() == Opcodes.CHECKCAST) { return loadType(loader, typeInsn.desc); } } else if (n != null && Opcodes.DUP == n.getOpcode()) { // Kotlin 1.2.x // mv.visitInsn(DUP); // mv.visitLdcInsn("req.param(\"p1\")"); // mv.visitMethodInsn(INVOKESTATIC, "kotlin/jvm/internal/Intrinsics", "checkExpressionValueIsNotNull", "(Ljava/lang/Object;Ljava/lang/String;)V", false); // mv.visitMethodInsn(INVOKESTATIC, "org/jooby/JoobyKt", "getValue", "(Lorg/jooby/Mutant;)Ljava/lang/String;", false); AbstractInsnNode next = new Insn<>(null, n).next().filter(MethodInsnNode.class::isInstance).skip(1) .findFirst().orElse(null); java.lang.reflect.Type result = parameterType(loader, next); if (result == Object.class) { next = new Insn<>(null, n).next().filter(TypeInsnNode.class::isInstance).findFirst().orElse(null); result = parameterType(loader, next); } return result; } else if (n instanceof FieldInsnNode) { AbstractInsnNode next = n.getNext(); if (next instanceof MethodInsnNode) { if (((MethodInsnNode) next).name.equals("toOptional")) { return Types.newParameterizedType(Optional.class, loadType(loader, ((FieldInsnNode) n).owner)); } else if (((MethodInsnNode) next).name.equals("getOrCreateKotlinClass")) { return loadType(loader, ((FieldInsnNode) n).owner); } } } return Object.class; }
From source file:org.lambdamatic.analyzer.ast.LambdaExpressionReader.java
License:Open Source License
/** * Checks if an operation of type OpsCode.CHECKCAST is following this operation, in which case it * provides valuable info about the actual returned type of the method * //from ww w . jav a2 s.c om * @param insnCursor the {@link InsnCursor} * @param javaMethod the current Java {@link Method} * @return the return type of the given method. */ private static Class<?> findReturnType(final InsnCursor insnCursor, final Method javaMethod) { if (insnCursor.hasNext()) { final AbstractInsnNode nextOp = insnCursor.getNext(); if (nextOp.getOpcode() == Opcodes.CHECKCAST) { final TypeInsnNode checkCastInsnNode = (TypeInsnNode) nextOp; try { return Class.forName(Type.getObjectType(checkCastInsnNode.desc).getClassName()); } catch (ClassNotFoundException e) { throw new AnalyzeException("Failed to retrieve class for " + checkCastInsnNode.desc, e); } } else { // move cursor position backwards insnCursor.getPrevious(); } } return javaMethod.getReturnType(); }
From source file:org.mbte.groovypp.compiler.transformers.ConstructorCallExpressionTransformer.java
License:Apache License
private Expression createDynamicCall(final ConstructorCallExpression exp, CompilerTransformer compiler, TupleExpression newArgs) {//from ww w .j a v a 2 s.c o m final List<Expression> args = newArgs.getExpressions(); for (int i = 0; i != args.size(); ++i) { BytecodeExpr arg = compiler.transformSynthetic((BytecodeExpr) args.get(i)); if (arg instanceof CompiledClosureBytecodeExpr) { compiler.processPendingClosure((CompiledClosureBytecodeExpr) arg); } args.set(i, arg); } return new BytecodeExpr(exp, exp.getType()) { protected void compile(MethodVisitor mv) { mv.visitLdcInsn(BytecodeHelper.getClassLoadingTypeDescription(getType())); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;"); mv.visitLdcInsn(args.size()); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); for (int j = 0; j != args.size(); ++j) { mv.visitInsn(DUP); mv.visitLdcInsn(j); BytecodeExpr arg = (BytecodeExpr) args.get(j); arg.visit(mv); box(arg.getType(), mv); mv.visitInsn(AASTORE); } mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/InvokerHelper", "invokeConstructorOf", "(Ljava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitTypeInsn(Opcodes.CHECKCAST, BytecodeHelper.getClassInternalName(getType())); } }; }