Example usage for org.objectweb.asm Opcodes INVOKESTATIC

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

Introduction

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

Prototype

int INVOKESTATIC

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

Click Source Link

Usage

From source file:com.yahoo.yqlplus.engine.internal.compiler.CompareExpression.java

private void emitPrimitiveCompare(CodeEmitter code, TypeWidget type) {
    switch (type.getJVMType().getSort()) {
    case Type.BYTE:
    case Type.BOOLEAN:
    case Type.SHORT:
    case Type.INT:
    case Type.CHAR:
        code.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Integer.class),
                "compare", Type.getMethodDescriptor(Type.INT_TYPE, Type.INT_TYPE, Type.INT_TYPE), false);
        break;/*w  w w.  j  av  a2  s.com*/
    case Type.FLOAT:
        code.getMethodVisitor().visitInsn(Opcodes.FCMPG);
        break;
    case Type.LONG:
        code.getMethodVisitor().visitInsn(Opcodes.LCMP);
        break;
    case Type.DOUBLE:
        code.getMethodVisitor().visitInsn(Opcodes.DCMPG);
        break;
    default:
        throw new UnsupportedOperationException(
                "Unexpected primitive type: " + leftExpr.getType().getJVMType().getDescriptor());
    }
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.MethodGenerator.java

@Override
public GambitCreator.Invocable createInvocable() {
    int op = Opcodes.INVOKEVIRTUAL;
    if (Modifier.isStatic(modifiers)) {
        op = Opcodes.INVOKESTATIC;
    }//from w w w . j  a  v a  2 s.  c  o  m
    return ExactInvocation.exactInvoke(op, name, unit.getType(), getReturnType(), getArgumentTypes());
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.ClassAdapterPropertyValue.java

@Override
public BytecodeExpression read() {
    return new BaseTypeExpression(getType()) {
        @Override//  w w  w .  ja  v a  2 s.  co  m
        public void generate(CodeEmitter code) {
            target.generate(code);
            indexExpression.generate(code);
            code.cast(BaseTypeAdapter.STRING, indexExpression.getType());
            code.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, adapter.getInternalName(),
                    "getProperty", Type.getMethodDescriptor(AnyTypeWidget.getInstance().getJVMType(),
                            typeWidget.getJVMType(), BaseTypeAdapter.STRING.getJVMType()),
                    false);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.ClassAdapterPropertyValue.java

@Override
public BytecodeSequence write(final BytecodeExpression value) {
    return new BytecodeSequence() {
        @Override/*from w  ww .ja v  a 2 s .c o  m*/
        public void generate(CodeEmitter code) {
            target.generate(code);
            indexExpression.generate(code);
            code.cast(BaseTypeAdapter.STRING, indexExpression.getType());
            value.generate(code);
            code.box(value.getType());
            code.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, adapter.getInternalName(),
                    "putProperty",
                    Type.getMethodDescriptor(Type.VOID_TYPE, typeWidget.getJVMType(),
                            BaseTypeAdapter.STRING.getJVMType(), AnyTypeWidget.getInstance().getJVMType()),
                    false);

        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.ClassAdapterPropertyValue.java

@Override
public BytecodeSequence write(final TypeWidget top) {
    return new BytecodeSequence() {
        @Override/* ww  w  .  j a v a  2  s  . c om*/
        public void generate(CodeEmitter code) {
            target.generate(code);
            code.swap(top, target.getType());
            indexExpression.generate(code);
            code.cast(BaseTypeAdapter.STRING, indexExpression.getType());
            code.swap(BaseTypeAdapter.STRING, top);
            code.box(top);
            code.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, adapter.getInternalName(),
                    "putProperty",
                    Type.getMethodDescriptor(Type.VOID_TYPE, typeWidget.getJVMType(),
                            BaseTypeAdapter.STRING.getJVMType(), AnyTypeWidget.getInstance().getJVMType()),
                    false);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.InvokeExpression.java

@Override
public void generate(CodeEmitter code) {
    if (op != Opcodes.INVOKESTATIC) {
        target.generate(code);//from   w ww .j  ava  2  s.c o m
    }
    for (BytecodeExpression arg : arguments) {
        arg.generate(code);
    }
    MethodVisitor mv = code.getMethodVisitor();
    mv.visitMethodInsn(op, owner.getInternalName(), methodName, descriptor, op == Opcodes.INVOKEINTERFACE);
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.MapPropertyAdapter.java

@Override
public BytecodeExpression getPropertyNameIterable(BytecodeExpression target) {
    return new InvokeExpression(MapPropertyAdapter.class, Opcodes.INVOKESTATIC, "stringsOnly",
            Type.getMethodDescriptor(Type.getType(Iterable.class), Type.getType(Iterable.class)),
            new IterableTypeWidget(BaseTypeAdapter.STRING), null,
            ImmutableList.<BytecodeExpression>of(new InvokeExpression(Map.class, "keySet",
                    new SetTypeWidget(keyType), target, ImmutableList.<BytecodeExpression>of())));
}

From source file:de.dynamicfiles.projects.gradle.plugins.javafx.tasks.internal.MonkeyPatcher.java

License:Apache License

private static void doMonkeyPatchFileHandleLeak(ClassReader classReader, ClassWriter classWriter) {
    classReader.accept(new ClassVisitor(Opcodes.ASM5, classWriter) {
        @Override/*from ww  w .j  a v  a 2s  . c  om*/
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            if (!(name.equals(METHOD_TO_MONKEY_PATCH) && desc.equals(METHOD_SIGNATURE_TO_MONKEY_PATCH))) {
                return super.visitMethod(access, name, desc, signature, exceptions);
            }
            // helpful source: http://web.cs.ucla.edu/~msb/cs239-tutorial/
            // "We will do this using the Adapter Pattern. Adapters wrap an object, overriding some of its methods, and delegating to the others."
            // ugly adapter-pattern ... took me more time than I really can tell here <.<
            return getMonkeyPatchedFileHandleLeakMethodVisitor(access, name, desc, signature, exceptions);
        }

        private MethodVisitor getMonkeyPatchedFileHandleLeakMethodVisitor(int access, String name, String desc,
                String signature, String[] exceptions) {
            return new MethodVisitor(Opcodes.ASM5,
                    super.visitMethod(access, name, desc, signature, exceptions)) {

                /*
                 TODO improve detection of lambda-positions, numbers might vary on different compile-versions
                 */
                @Override
                public void visitCode() {
                    // This mostly got generated from ASM itself, except some adjustments for lambda-IDs and removed "visitMaxs()"-call
                    String javalangThrowable = "java/lang/Throwable";
                    String javaioFile = "java/io/File";
                    String javalangString = "java/lang/String";

                    Label l0 = new Label();
                    Label l1 = new Label();
                    Label l2 = new Label();
                    mv.visitTryCatchBlock(l0, l1, l2, javalangThrowable);
                    Label l3 = new Label();
                    Label l4 = new Label();
                    Label l5 = new Label();
                    mv.visitTryCatchBlock(l3, l4, l5, javalangThrowable);
                    Label l6 = new Label();
                    mv.visitTryCatchBlock(l3, l4, l6, null);
                    Label l7 = new Label();
                    Label l8 = new Label();
                    Label l9 = new Label();
                    mv.visitTryCatchBlock(l7, l8, l9, javalangThrowable);
                    Label l10 = new Label();
                    mv.visitTryCatchBlock(l5, l10, l6, null);
                    mv.visitInsn(Opcodes.ACONST_NULL);
                    mv.visitVarInsn(Opcodes.ASTORE, 3);
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    Label l11 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l11);
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javaioFile, "isDirectory", "()Z", false);
                    Label l12 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNE, l12);
                    mv.visitLabel(l11);
                    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { javalangString }, 0, null);
                    mv.visitTypeInsn(Opcodes.NEW, javaioFile);
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitLdcInsn("java.home");
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "getProperty",
                            "(Ljava/lang/String;)Ljava/lang/String;", false);
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, javaioFile, "<init>", "(Ljava/lang/String;)V",
                            false);
                    mv.visitVarInsn(Opcodes.ASTORE, 2);
                    mv.visitLabel(l12);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "com/oracle/tools/packager/windows/WinAppBundler",
                            "VS_VERS", "[Ljava/lang/String;");
                    mv.visitVarInsn(Opcodes.ASTORE, 4);
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitInsn(Opcodes.ARRAYLENGTH);
                    mv.visitVarInsn(Opcodes.ISTORE, 5);
                    mv.visitInsn(Opcodes.ICONST_0);
                    mv.visitVarInsn(Opcodes.ISTORE, 6);
                    Label l13 = new Label();
                    mv.visitLabel(l13);
                    mv.visitFrame(Opcodes.F_APPEND, 3,
                            new Object[] { "[Ljava/lang/String;", Opcodes.INTEGER, Opcodes.INTEGER }, 0, null);
                    mv.visitVarInsn(Opcodes.ILOAD, 6);
                    mv.visitVarInsn(Opcodes.ILOAD, 5);
                    Label l14 = new Label();
                    mv.visitJumpInsn(Opcodes.IF_ICMPGE, l14);
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitVarInsn(Opcodes.ILOAD, 6);
                    mv.visitInsn(Opcodes.AALOAD);
                    mv.visitVarInsn(Opcodes.ASTORE, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    mv.visitVarInsn(Opcodes.ALOAD, 1);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/oracle/tools/packager/windows/WinAppBundler",
                            "copyMSVCDLLs", "(Ljava/io/File;Ljava/lang/String;)Z", false);
                    Label l15 = new Label();
                    mv.visitJumpInsn(Opcodes.IFEQ, l15);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitVarInsn(Opcodes.ASTORE, 3);
                    mv.visitJumpInsn(Opcodes.GOTO, l14);
                    mv.visitLabel(l15);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitIincInsn(6, 1);
                    mv.visitJumpInsn(Opcodes.GOTO, l13);
                    mv.visitLabel(l14);
                    mv.visitFrame(Opcodes.F_CHOP, 3, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 3);
                    Label l16 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNONNULL, l16);
                    mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitLdcInsn("Not found MSVC dlls");
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
                            "(Ljava/lang/String;)V", false);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l16);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/atomic/AtomicReference");
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/concurrent/atomic/AtomicReference",
                            "<init>", "()V", false);
                    mv.visitVarInsn(Opcodes.ASTORE, 4);
                    mv.visitVarInsn(Opcodes.ALOAD, 3);
                    mv.visitVarInsn(Opcodes.ASTORE, 5);
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javaioFile, "toPath", "()Ljava/nio/file/Path;",
                            false);
                    mv.visitLdcInsn("bin");
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/nio/file/Path", "resolve",
                            "(Ljava/lang/String;)Ljava/nio/file/Path;", true);
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/nio/file/Files", "list",
                            "(Ljava/nio/file/Path;)Ljava/util/stream/Stream;", false);
                    mv.visitVarInsn(Opcodes.ASTORE, 6);
                    mv.visitInsn(Opcodes.ACONST_NULL);
                    mv.visitVarInsn(Opcodes.ASTORE, 7);
                    mv.visitLabel(l3);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitInvokeDynamicInsn("test", "()Ljava/util/function/Predicate;", new Handle(
                            Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
                            "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                            new Object[] { Type.getType("(Ljava/lang/Object;)Z"),
                                    new Handle(Opcodes.H_INVOKESTATIC,
                                            "com/oracle/tools/packager/windows/WinAppBundler",
                                            "lambda$copyMSVCDLLs$261", "(Ljava/nio/file/Path;)Z"),
                                    Type.getType("(Ljava/nio/file/Path;)Z") }); // modified lambda-name
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "filter",
                            "(Ljava/util/function/Predicate;)Ljava/util/stream/Stream;", true);
                    mv.visitVarInsn(Opcodes.ALOAD, 5);
                    mv.visitInvokeDynamicInsn("test", "(Ljava/lang/String;)Ljava/util/function/Predicate;",
                            new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory",
                                    "metafactory",
                                    "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                            new Object[] { Type.getType("(Ljava/lang/Object;)Z"), new Handle(
                                    Opcodes.H_INVOKESTATIC, "com/oracle/tools/packager/windows/WinAppBundler",
                                    "lambda$copyMSVCDLLs$262", "(Ljava/lang/String;Ljava/nio/file/Path;)Z"),
                                    Type.getType("(Ljava/nio/file/Path;)Z") }); // modified lambda-name
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "filter",
                            "(Ljava/util/function/Predicate;)Ljava/util/stream/Stream;", true);
                    mv.visitVarInsn(Opcodes.ALOAD, 1);
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitInvokeDynamicInsn("accept",
                            "(Ljava/io/File;Ljava/util/concurrent/atomic/AtomicReference;)Ljava/util/function/Consumer;",
                            new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory",
                                    "metafactory",
                                    "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                            new Object[] { Type.getType("(Ljava/lang/Object;)V"), new Handle(
                                    Opcodes.H_INVOKESTATIC, "com/oracle/tools/packager/windows/WinAppBundler",
                                    "lambda$copyMSVCDLLs$263",
                                    "(Ljava/io/File;Ljava/util/concurrent/atomic/AtomicReference;Ljava/nio/file/Path;)V"),
                                    Type.getType("(Ljava/nio/file/Path;)V") }); // modified lambda-name
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "forEach",
                            "(Ljava/util/function/Consumer;)V", true);
                    mv.visitLabel(l4);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    Label l17 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l17);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    Label l18 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l18);
                    mv.visitLabel(l0);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitLabel(l1);
                    mv.visitJumpInsn(Opcodes.GOTO, l17);
                    mv.visitLabel(l2);
                    mv.visitFrame(Opcodes.F_FULL, 8,
                            new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile,
                                    javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference",
                                    javalangString, "java/util/stream/Stream", javalangThrowable },
                            1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 8);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 8);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javalangThrowable, "addSuppressed",
                            "(Ljava/lang/Throwable;)V", false);
                    mv.visitJumpInsn(Opcodes.GOTO, l17);
                    mv.visitLabel(l18);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitJumpInsn(Opcodes.GOTO, l17);
                    mv.visitLabel(l5);
                    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 8);
                    mv.visitVarInsn(Opcodes.ALOAD, 8);
                    mv.visitVarInsn(Opcodes.ASTORE, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 8);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l6);
                    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 9);
                    mv.visitLabel(l10);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    Label l19 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l19);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    Label l20 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l20);
                    mv.visitLabel(l7);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitLabel(l8);
                    mv.visitJumpInsn(Opcodes.GOTO, l19);
                    mv.visitLabel(l9);
                    mv.visitFrame(Opcodes.F_FULL, 10,
                            new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile,
                                    javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference",
                                    javalangString, "java/util/stream/Stream", javalangThrowable, Opcodes.TOP,
                                    javalangThrowable },
                            1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 10);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 10);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javalangThrowable, "addSuppressed",
                            "(Ljava/lang/Throwable;)V", false);
                    mv.visitJumpInsn(Opcodes.GOTO, l19);
                    mv.visitLabel(l20);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitLabel(l19);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 9);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l17);
                    mv.visitFrame(Opcodes.F_FULL, 6,
                            new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile,
                                    javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference",
                                    javalangString },
                            0, new Object[] {});
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicReference",
                            "get", "()Ljava/lang/Object;", false);
                    mv.visitTypeInsn(Opcodes.CHECKCAST, "java/io/IOException");
                    mv.visitVarInsn(Opcodes.ASTORE, 6);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    Label l21 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l21);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l21);
                    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/io/IOException" }, 0, null);
                    mv.visitInsn(Opcodes.RETURN);
                }

            };
        }

    }, ClassReader.EXPAND_FRAMES); // ClassReader.EXPAND_FRAMES required for Java 8
}

From source file:de.jiac.micro.config.analysis.MethodAnalyser.java

License:Open Source License

@Override
public void visitEnd() {
    final HashSet<String> obtainedHandles = new HashSet<String>();
    final ClassInfo ci = parent.classInfo;
    final MethodNode method = (MethodNode) mv;

    ci.referencedClassesInMethods.put(methodKey, new HashSet<String>());

    // insert argument types
    processType(Type.getReturnType(method.desc));
    for (Type type : Type.getArgumentTypes(method.desc)) {
        processType(type);/* w  w  w.  j  a  va  2 s.co  m*/
    }

    Interpreter interpreter = new BasicGuesser() {
        @Override
        public Value newOperation(AbstractInsnNode insn) {
            final int op = insn.getOpcode();
            String className = null;

            if (op == GETSTATIC) {
                className = ((FieldInsnNode) insn).owner.replace('/', '.');
            } else if (op == NEW) {
                className = Type.getObjectType(((TypeInsnNode) insn).desc).getClassName();
            }

            parent.registerDependencyForMethod(methodKey, className);
            return super.newOperation(insn);
        }

        @Override
        public Value unaryOperation(AbstractInsnNode insn, Value value) throws AnalyzerException {
            if (insn.getOpcode() == ARETURN) {
                if (methodKey.equals(GET_HANDLE) || methodKey.equals(GET_NODE_HANDLE)) {
                    RuntimeGuessValue guessedValue = (RuntimeGuessValue) value;
                    Type type = guessedValue.getType();

                    if (type != null && guessedValue != RuntimeGuessValue.NULL_CONSTANT) {
                        parent.classInfo.directHandle = type.getClassName();
                    }
                }
            }
            return super.unaryOperation(insn, value);
        }

        @Override
        public Value naryOperation(AbstractInsnNode insn, List values) throws AnalyzerException {
            if (insn instanceof MethodInsnNode) {
                MethodInsnNode minsn = (MethodInsnNode) insn;
                final int opCode = minsn.getOpcode();
                if (minsn.desc.endsWith(")Ljava/lang/Class;") && (minsn.name.equals("class$")
                        || (minsn.owner.equals("java/lang/Class") && minsn.name.equals("forName"))
                        || (minsn.owner.endsWith("ClassLoader") && minsn.name.equals("loadClass")))) {
                    // pattern for class loading
                    RuntimeGuessValue guessedValue = (RuntimeGuessValue) values
                            .get(opCode == Opcodes.INVOKESTATIC ? 0 : 1);
                    String className = (String) guessedValue.getValue();

                    if (className != null) {
                        return new RuntimeGuessValue(Type.getObjectType("java/lang/Class"), className);
                    }
                } else if (HANDLE_SOURCES.contains(minsn.owner)
                        && minsn.desc.equals("(Ljava/lang/Class;)Lde/jiac/micro/core/IHandle;")
                        && (minsn.name.equals("getHandle") || minsn.name.equals("getScopeHandle"))) {
                    // pattern for obtaining a handle
                    RuntimeGuessValue guessedValue = (RuntimeGuessValue) values
                            .get(opCode == Opcodes.INVOKESTATIC ? 0 : 1);
                    String className = (String) guessedValue.getValue();

                    if (className != null) {
                        obtainedHandles.add(className);
                        parent.registerDependencyForMethod(methodKey, className);
                        String internalName = className.replace('.', '/');
                        return new RuntimeGuessValue(Type.getObjectType(internalName), null);
                    }
                } else if (HANDLE_SOURCES.contains(minsn.owner) && minsn.name.equals("addHandle")
                        && minsn.desc.equals("(Lde/jiac/micro/core/IHandle;)V")) {
                    // pattern for adding a handle explicitly
                    RuntimeGuessValue guessValue = (RuntimeGuessValue) values.get(1);
                    Type type = guessValue.getType();

                    if (type != null) {
                        parent.registerDependencyForMethod(methodKey, type.getClassName());
                        parent.classInfo.indirectHandles.add(type.getClassName());
                    }
                }
            }

            return super.naryOperation(insn, values);
        }
    };

    Analyzer a = new Analyzer(interpreter);
    try {
        a.analyze(parent.owner, method);
    } catch (AnalyzerException e) {
        e.printStackTrace();
    }

    if (obtainedHandles.size() > 0) {
        ci.referencedHandlesInMethods.put(methodKey, obtainedHandles);
    }
}

From source file:de.sanandrew.core.manpack.transformer.TransformEntityThrowable.java

License:Creative Commons License

private static byte[] transformLqThrowable(byte[] bytes) {
    ClassNode classNode = ASMHelper.createClassNode(bytes);

    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PUBLIC, ASMNames.MD_SAP_CAN_IMPACT_ON_LIQUID);

    method.visitCode();/*from  w  w w  .j  a v a 2  s .c  om*/
    Label label1 = new Label();
    method.visitLabel(label1);
    method.visitInsn(Opcodes.ICONST_0);
    method.visitInsn(Opcodes.IRETURN);
    Label label2 = new Label();
    method.visitLabel(label2);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY_THROWABLE, null, label1, label2, 0);
    method.visitMaxs(0, 0);
    method.visitEnd();
    classNode.methods.add(method);

    method = ASMHelper.findMethod(classNode, ASMNames.MD_THROWABLE_ON_UPDATE);

    InsnList needle = new InsnList();
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_MOTION_Z));
    needle.add(new InsnNode(Opcodes.DADD));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESTATIC, ASMNames.MD_VEC3_CREATE_VECTOR_HELPER, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, 2));
    needle.add(new LabelNode());
    needle.add(new LineNumberNode(-1, new LabelNode()));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_WORLD_OBJ));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 1));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 2));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_WORLD_RAY_TRACE_BLOCKS, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, -1));

    VarInsnNode insertPoint = (VarInsnNode) ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList injectList = new InsnList();
    injectList.add(new LabelNode());
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_WORLD_OBJ));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 1));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 2));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_CAN_IMPACT_ON_LIQUID, false));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_WORLD_RAY_TRACE_BLOCKS_Z, false));
    injectList.add(new VarInsnNode(Opcodes.ASTORE, insertPoint.var));

    method.instructions.insert(insertPoint, injectList);

    return ASMHelper.createBytes(classNode, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);
}