Example usage for org.objectweb.asm Opcodes INVOKEVIRTUAL

List of usage examples for org.objectweb.asm Opcodes INVOKEVIRTUAL

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes INVOKEVIRTUAL.

Prototype

int INVOKEVIRTUAL

To view the source code for org.objectweb.asm Opcodes INVOKEVIRTUAL.

Click Source Link

Usage

From source file:com.codename1.tools.translator.bytecodes.CustomInvoke.java

License:Open Source License

public boolean appendExpression(StringBuilder b) {
    // special case for clone on an array which isn't a real method invocation
    if (name.equals("clone") && owner.indexOf('[') > -1) {
        if (targetObjectLiteral != null) {
            b.append("cloneArray(").append(targetObjectLiteral).append(")");
        } else {/* w  ww  .  j a va 2s .  c  o  m*/
            b.append("cloneArray(POP_OBJ(1))");
        }
        return true;
    }

    StringBuilder bld = new StringBuilder();
    if (origOpcode == Opcodes.INVOKEINTERFACE || origOpcode == Opcodes.INVOKEVIRTUAL) {
        //b.append("    ");
        bld.append("virtual_");
    }

    if (origOpcode == Opcodes.INVOKESTATIC) {
        // find the actual class of the static method to workaround javac not defining it correctly
        ByteCodeClass bc = Parser.getClassObject(owner.replace('/', '_').replace('$', '_'));
        owner = findActualOwner(bc);
    }

    bld.append(owner.replace('/', '_').replace('$', '_'));
    bld.append("_");
    if (name.equals("<init>")) {
        bld.append("__INIT__");
    } else {
        if (name.equals("<clinit>")) {
            bld.append("__CLINIT__");
        } else {
            bld.append(name);
        }
    }
    bld.append("__");
    ArrayList<String> args = new ArrayList<String>();
    String returnVal = BytecodeMethod.appendMethodSignatureSuffixFromDesc(desc, bld, args);
    int numLiteralArgs = this.getNumLiteralArgs();
    if (numLiteralArgs > 0) {
        b.append("/* CustomInvoke */");
    }
    boolean noPop = false;
    b.append(bld);

    b.append("(threadStateData");

    if (origOpcode != Opcodes.INVOKESTATIC) {
        if (targetObjectLiteral == null) {
            //b.append(", SP[-");
            //b.append(args.size() + 1 - numLiteralArgs);
            //b.append("].data.o");
            return false;
        } else {
            b.append(", ").append(targetObjectLiteral);
            numLiteralArgs++;
        }
    }
    //int offset = args.size();
    //int numArgs = offset;
    int argIndex = 0;
    for (String a : args) {

        b.append(", ");
        if (literalArgs != null && literalArgs[argIndex] != null) {
            b.append(literalArgs[argIndex]);
        } else {
            return false;
            //b.append("SP[-");
            //b.append(offset);
            //b.append("].data.");
            //b.append(a);
            //offset--;
        }
        argIndex++;
    }
    if (returnVal == null) {
        return false;
    }

    b.append(")");

    return true;

}

From source file:com.codename1.tools.translator.bytecodes.CustomInvoke.java

License:Open Source License

@Override
public void appendInstruction(StringBuilder b) {
    // special case for clone on an array which isn't a real method invocation
    if (name.equals("clone") && owner.indexOf('[') > -1) {
        if (targetObjectLiteral != null) {
            b.append("    PUSH_OBJ(cloneArray(").append(targetObjectLiteral).append("));\n");
        } else {/*from  ww  w  .  jav a 2s  .  c  om*/
            b.append("    POP_MANY_AND_PUSH_OBJ(cloneArray(PEEK_OBJ(1)), 1);\n");
        }
        return;
    }

    StringBuilder bld = new StringBuilder();
    if (origOpcode == Opcodes.INVOKEINTERFACE || origOpcode == Opcodes.INVOKEVIRTUAL) {
        b.append("    ");
        bld.append("virtual_");
    } else {
        b.append("    ");
    }

    if (origOpcode == Opcodes.INVOKESTATIC) {
        // find the actual class of the static method to workaround javac not defining it correctly
        ByteCodeClass bc = Parser.getClassObject(owner.replace('/', '_').replace('$', '_'));
        owner = findActualOwner(bc);
    }
    //if(owner.replace('/', '_').replace('$', '_').equals("java_lang_System_1") && name.equals("sleep")) {
    //    System.out.println("Break");
    //}
    bld.append(owner.replace('/', '_').replace('$', '_'));
    bld.append("_");
    if (name.equals("<init>")) {
        bld.append("__INIT__");
    } else {
        if (name.equals("<clinit>")) {
            bld.append("__CLINIT__");
        } else {
            bld.append(name);
        }
    }
    bld.append("__");
    ArrayList<String> args = new ArrayList<String>();
    String returnVal = BytecodeMethod.appendMethodSignatureSuffixFromDesc(desc, bld, args);
    int numLiteralArgs = this.getNumLiteralArgs();
    if (numLiteralArgs > 0) {
        b.append("/* CustomInvoke */");
    }
    boolean noPop = false;
    if (returnVal == null || noReturn) {
        b.append(bld);
    } else {
        if (args.size() - numLiteralArgs == 0 && origOpcode == Opcodes.INVOKESTATIC) {
            // special case for static method
            if (returnVal.equals("JAVA_OBJECT")) {
                b.append("PUSH_OBJ");
            } else {
                if (returnVal.equals("JAVA_INT")) {
                    b.append("PUSH_INT");
                } else {
                    if (returnVal.equals("JAVA_LONG")) {
                        b.append("PUSH_LONG");
                    } else {
                        if (returnVal.equals("JAVA_DOUBLE")) {
                            b.append("PUSH_DOUBLE");
                        } else {
                            if (returnVal.equals("JAVA_FLOAT")) {
                                b.append("PUSH_FLOAT");
                            } else {
                                throw new UnsupportedOperationException("Unknown type: " + returnVal);
                            }
                        }
                    }
                }
            }
            //b.append(returnVal);
            noPop = true;
            b.append("(");
        } else {
            //b.append("POP_MANY_AND_");
            //b.append(returnVal);
            b.append("{ ");
            b.append(returnVal);
            b.append(" tmpResult = ");
        }
        b.append(bld);
    }
    b.append("(threadStateData");

    if (origOpcode != Opcodes.INVOKESTATIC) {
        if (targetObjectLiteral == null) {
            b.append(", SP[-");
            b.append(args.size() + 1 - numLiteralArgs);
            b.append("].data.o");
        } else {
            b.append(", " + targetObjectLiteral);
            numLiteralArgs++;
        }
    }
    int offset = args.size();
    //int numArgs = offset;
    int argIndex = 0;
    for (String a : args) {

        b.append(", ");
        if (literalArgs != null && literalArgs[argIndex] != null) {
            b.append(literalArgs[argIndex]);
        } else {
            b.append("SP[-");
            b.append(offset);
            b.append("].data.");
            b.append(a);
            offset--;
        }
        argIndex++;
    }
    if (noPop) {
        b.append("));\n");
        return;
    }
    if (returnVal != null && !noReturn) {
        b.append(");\n");
        if (origOpcode != Opcodes.INVOKESTATIC) {
            if (args.size() - numLiteralArgs > 0) {
                b.append("    SP -= ");
                b.append(args.size() - numLiteralArgs);
                b.append(";\n");
            }
        } else {
            if (args.size() - numLiteralArgs > 1) {
                b.append("    SP -= ");
                b.append(args.size() - numLiteralArgs - 1);
                b.append(";\n");
            }
        }
        if (targetObjectLiteral == null) {
            if (returnVal.equals("JAVA_OBJECT")) {
                b.append("    SP[-1].data.o = tmpResult; SP[-1].type = CN1_TYPE_OBJECT; }\n");
            } else {
                if (returnVal.equals("JAVA_INT")) {
                    b.append("    SP[-1].data.i = tmpResult; SP[-1].type = CN1_TYPE_INT; }\n");
                } else {
                    if (returnVal.equals("JAVA_LONG")) {
                        b.append("    SP[-1].data.l = tmpResult; SP[-1].type = CN1_TYPE_LONG; }\n");
                    } else {
                        if (returnVal.equals("JAVA_DOUBLE")) {
                            b.append("    SP[-1].data.d = tmpResult; SP[-1].type = CN1_TYPE_DOUBLE; }\n");
                        } else {
                            if (returnVal.equals("JAVA_FLOAT")) {
                                b.append("    SP[-1].data.f = tmpResult; SP[-1].type = CN1_TYPE_FLOAT; }\n");
                            } else {
                                throw new UnsupportedOperationException("Unknown type: " + returnVal);
                            }
                        }
                    }
                }
            }
        } else {
            if (returnVal.equals("JAVA_OBJECT")) {
                b.append("    PUSH_OBJ(tmpResult); }\n");
            } else {
                if (returnVal.equals("JAVA_INT")) {
                    b.append("    PUSH_INT(tmpResult); }\n");
                } else {
                    if (returnVal.equals("JAVA_LONG")) {
                        b.append("    PUSH_LONG(tmpResult); }\n");
                    } else {
                        if (returnVal.equals("JAVA_DOUBLE")) {
                            b.append("    PUSH_DOUBLE(tmpResult); }\n");
                        } else {
                            if (returnVal.equals("JAVA_FLOAT")) {
                                b.append("    PUSH_FLOAT(tmpResult); }\n");
                            } else {
                                throw new UnsupportedOperationException("Unknown type: " + returnVal);
                            }
                        }
                    }
                }
            }
        }

        return;
    }
    b.append("); ");
    int val;
    if (origOpcode != Opcodes.INVOKESTATIC) {
        val = args.size() + 1 - numLiteralArgs;
    } else {
        val = args.size() - numLiteralArgs;
    }
    if (val > 0) {
        b.append("    SP -= ");
        b.append(val);
        b.append(";\n");
    } else {
        b.append("\n");
    }
}

From source file:com.codename1.tools.translator.bytecodes.Invoke.java

License:Open Source License

@Override
public void addDependencies(List<String> dependencyList) {
    String t = owner.replace('.', '_').replace('/', '_').replace('$', '_');
    t = unarray(t);// w w  w.j a va2  s  .  c om
    if (t != null && !dependencyList.contains(t)) {
        dependencyList.add(t);
    }

    StringBuilder bld = new StringBuilder();
    if (opcode != Opcodes.INVOKEINTERFACE && opcode != Opcodes.INVOKEVIRTUAL) {
        return;
    }
    bld.append(owner.replace('/', '_').replace('$', '_'));
    bld.append("_");
    if (name.equals("<init>")) {
        bld.append("__INIT__");
    } else {
        if (name.equals("<clinit>")) {
            bld.append("__CLINIT__");
        } else {
            bld.append(name);
        }
    }
    bld.append("__");
    ArrayList<String> args = new ArrayList<String>();
    String returnVal = BytecodeMethod.appendMethodSignatureSuffixFromDesc(desc, bld, args);
    String str = bld.toString();
    BytecodeMethod.addVirtualMethodsInvoked(str);
}

From source file:com.codename1.tools.translator.bytecodes.Invoke.java

License:Open Source License

@Override
public void appendInstruction(StringBuilder b) {
    // special case for clone on an array which isn't a real method invocation
    if (name.equals("clone") && owner.indexOf('[') > -1) {
        b.append("    POP_MANY_AND_PUSH_OBJ(cloneArray(PEEK_OBJ(1)), 1);\n");
        return;/*from   w  ww .  jav  a 2 s  .  co  m*/
    }

    StringBuilder bld = new StringBuilder();
    if (opcode == Opcodes.INVOKEINTERFACE || opcode == Opcodes.INVOKEVIRTUAL) {
        b.append("    ");
        bld.append("virtual_");
    } else {
        b.append("    ");
    }

    if (opcode == Opcodes.INVOKESTATIC) {
        // find the actual class of the static method to workaround javac not defining it correctly
        ByteCodeClass bc = Parser.getClassObject(owner.replace('/', '_').replace('$', '_'));
        owner = findActualOwner(bc);
    }
    //if(owner.replace('/', '_').replace('$', '_').equals("java_lang_System_1") && name.equals("sleep")) {
    //    System.out.println("Break");
    //}
    bld.append(owner.replace('/', '_').replace('$', '_'));
    bld.append("_");
    if (name.equals("<init>")) {
        bld.append("__INIT__");
    } else {
        if (name.equals("<clinit>")) {
            bld.append("__CLINIT__");
        } else {
            bld.append(name);
        }
    }
    bld.append("__");
    ArrayList<String> args = new ArrayList<String>();
    String returnVal = BytecodeMethod.appendMethodSignatureSuffixFromDesc(desc, bld, args);
    boolean noPop = false;
    if (returnVal == null) {
        b.append(bld);
    } else {
        if (args.size() == 0 && opcode == Opcodes.INVOKESTATIC) {
            // special case for static method
            if (returnVal.equals("JAVA_OBJECT")) {
                b.append("PUSH_OBJ");
            } else {
                if (returnVal.equals("JAVA_INT")) {
                    b.append("PUSH_INT");
                } else {
                    if (returnVal.equals("JAVA_LONG")) {
                        b.append("PUSH_LONG");
                    } else {
                        if (returnVal.equals("JAVA_DOUBLE")) {
                            b.append("PUSH_DOUBLE");
                        } else {
                            if (returnVal.equals("JAVA_FLOAT")) {
                                b.append("PUSH_FLOAT");
                            } else {
                                throw new UnsupportedOperationException("Unknown type: " + returnVal);
                            }
                        }
                    }
                }
            }
            //b.append(returnVal);
            noPop = true;
            b.append("(");
        } else {
            //b.append("POP_MANY_AND_");
            //b.append(returnVal);
            b.append("{ ");
            b.append(returnVal);
            b.append(" tmpResult = ");
        }
        b.append(bld);
    }
    b.append("(threadStateData");

    if (opcode != Opcodes.INVOKESTATIC) {
        b.append(", SP[-");
        b.append(args.size() + 1);
        b.append("].data.o");
    }
    int offset = args.size();
    //int numArgs = offset;
    int argIndex = 0;
    for (String a : args) {

        b.append(", ");

        b.append("SP[-");
        b.append(offset);
        b.append("].data.");
        b.append(a);
        offset--;

        argIndex++;
    }
    if (noPop) {
        b.append("));\n");
        return;
    }
    if (returnVal != null) {
        b.append(");\n");
        if (opcode != Opcodes.INVOKESTATIC) {
            if (args.size() > 0) {
                b.append("    SP-=");
                b.append(args.size());
                b.append(";\n");
            }
        } else {
            if (args.size() > 1) {
                b.append("    SP-=");
                b.append(args.size() - 1);
                b.append(";\n");
            }
        }
        if (returnVal.equals("JAVA_OBJECT")) {
            b.append("    SP[-1].data.o = tmpResult; SP[-1].type = CN1_TYPE_OBJECT; }\n");
        } else {
            if (returnVal.equals("JAVA_INT")) {
                b.append("    SP[-1].data.i = tmpResult; SP[-1].type = CN1_TYPE_INT; }\n");
            } else {
                if (returnVal.equals("JAVA_LONG")) {
                    b.append("    SP[-1].data.l = tmpResult; SP[-1].type = CN1_TYPE_LONG; }\n");
                } else {
                    if (returnVal.equals("JAVA_DOUBLE")) {
                        b.append("    SP[-1].data.d = tmpResult; SP[-1].type = CN1_TYPE_DOUBLE; }\n");
                    } else {
                        if (returnVal.equals("JAVA_FLOAT")) {
                            b.append("    SP[-1].data.f = tmpResult; SP[-1].type = CN1_TYPE_FLOAT; }\n");
                        } else {
                            throw new UnsupportedOperationException("Unknown type: " + returnVal);
                        }
                    }
                }
            }
        }

        /*if(opcode != Opcodes.INVOKESTATIC) {
        b.append(args.size() + 1);
        } else {
        b.append(args.size());
        }
        b.append(");\n");      */

        return;
    }
    b.append("); ");
    int val;
    if (opcode != Opcodes.INVOKESTATIC) {
        val = args.size() + 1;
    } else {
        val = args.size();
    }
    if (val > 0) {
        /*b.append("popMany(threadStateData, ");            
        b.append(val);
        b.append(", stack, &stackPointer); \n"); */
        b.append("    SP-= ");
        b.append(val);
        b.append(";\n");
    } else {
        b.append("\n");
    }
}

From source file:com.devexperts.aprof.transformer.AbstractMethodVisitor.java

License:Open Source License

@Override
public void visitMethodInsn(final int opcode, final String owner, final String name, final String desc,
        boolean intf) {
    if (context.isInternalLocation()) {
        // do not instrument method invocations in internal locations
        mv.visitMethodInsn(opcode, owner, name, desc, intf);
        return;//www  .  j  a v  a  2  s.com
    }

    String cname = owner.replace('/', '.'); // convert owner to dot-separated class name

    // check if it is eligible object.clone call (that can get dispatched to actual Object.clone method
    boolean isClone = opcode != Opcodes.INVOKESTATIC && name.equals(TransformerUtil.CLONE)
            && desc.equals(TransformerUtil.NOARG_RETURNS_OBJECT);
    boolean isArrayClone = isClone && owner.startsWith("[");
    boolean isObjectClone = isClone && AProfRegistry.isDirectCloneClass(cname);

    if (context.isMethodInvocationTracked(cname, opcode, owner, name, desc)) {
        visitTrackedMethodInsn(opcode, owner, name, desc, intf);
    } else {
        mv.visitMethodInsn(opcode, owner, name, desc, intf);
    }

    if (!context.getConfig().isReflect())
        return;
    if (context.isIntrinsicArraysCopyOf())
        return;

    if (opcode == Opcodes.INVOKEVIRTUAL && isObjectClone) {
        // INVOKEVIRTUAL needs runtime check of class that is being cloned
        visitAllocateReflectVClone();
        return;
    }

    if (opcode == Opcodes.INVOKESPECIAL && isObjectClone) {
        // Object.clone via super.clone (does not need runtime check)
        visitAllocateReflect(true);
        return;
    }

    if (isArrayClone) {
        // <array>.clone (usually via INVOKEVIRTUAL, but we don't care)
        visitAllocateReflect(false);
        return;
    }

    if (opcode == Opcodes.INVOKESTATIC && owner.equals("java/lang/reflect/Array") && name.equals("newInstance")
            && (desc.equals(TransformerUtil.CLASS_INT_RETURNS_OBJECT)
                    || desc.equals(TransformerUtil.CLASS_INT_ARR_RETURNS_OBJECT))) {
        // Array.newInstance
        visitAllocateReflect(false);
        return;
    }

    if (opcode == Opcodes.INVOKESTATIC && TransformerUtil.isIntrinsicArraysCopyOf(owner, name, desc)) {
        // HotSpot intrinsic for Arrays.copyOf and Arrays.copyOfRange
        visitAllocateReflect(false);
    }
}

From source file:com.devexperts.aprof.transformer.Context.java

License:Open Source License

public boolean isMethodInvocationTracked(String cname, int opcode, String owner, String name, String desc) {
    if (owner.startsWith("["))
        return false; // do not track array method invocations
    if (config.isMethodTracked(cname, name))
        return true; // direct invocation of tracked method through its actual class
    if (opcode != Opcodes.INVOKEVIRTUAL && opcode != Opcodes.INVOKEINTERFACE)
        return false; // special or static invocation cannot got though superclass/super interface
    // Check for invocation via super class or interface
    // Load target class information
    ClassInfo classInfo = ciCache.getClassInfo(owner, loader);
    if (classInfo == null)
        return false; // we don't have class info, which means we don't track it, then
    // Check if tracked method
    Set<String> descSet = classInfo.getTrackedMethodInvocations().get(name);
    return descSet != null && descSet.contains(desc);
}

From source file:com.devexperts.aprof.transformer.MethodTransformer.java

License:Open Source License

/**
 * @see com.devexperts.aprof.LocationStack#addInvokedMethod(int)
 *//*from   w w  w.ja  v a 2  s  . co m*/
@Override
protected void visitStartInvokedMethod() {
    assert !context.isInternalLocation() : context;
    startFinally = new Label();
    pushLocationStack();
    mv.push(AProfRegistry.registerLocation(context.getLocation()));
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TransformerUtil.LOCATION_STACK, "addInvokedMethod",
            TransformerUtil.INT_VOID, false);
    mv.visitLabel(startFinally);
}

From source file:com.devexperts.aprof.transformer.MethodTransformer.java

License:Open Source License

/**
 * @see com.devexperts.aprof.LocationStack#removeInvokedMethod()
 *///from  ww w.  j  av  a 2  s.  co  m
@Override
protected void visitReturnFromInvokedMethod() {
    assert !context.isInternalLocation() : context;
    pushLocationStack();
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TransformerUtil.LOCATION_STACK, "removeInvokedMethod",
            TransformerUtil.NOARG_VOID, false);
}

From source file:com.devexperts.aprof.transformer.MethodTransformer.java

License:Open Source License

private void visitMarkInvocationPoint() {
    pushLocationStack();//w  ww  .  j av  a 2 s.  co m
    mv.push(AProfRegistry.registerLocation(context.getLocation()));
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TransformerUtil.LOCATION_STACK, "addInvocationPoint",
            TransformerUtil.INT_VOID, false);
}

From source file:com.devexperts.aprof.transformer.MethodTransformer.java

License:Open Source License

private void visitUnmarkInvocationPoint() {
    pushLocationStack();/*  ww  w  . j  a v  a  2  s. c o m*/
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TransformerUtil.LOCATION_STACK, "removeInvocationPoint",
            TransformerUtil.NOARG_VOID, false);
}