Example usage for org.objectweb.asm Opcodes GETSTATIC

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

Introduction

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

Prototype

int GETSTATIC

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

Click Source Link

Usage

From source file:com.mebigfatguy.junitflood.jvm.OperandStack.java

License:Apache License

public void performFieldInsn(int opcode, String owner, String name, String desc) {
    switch (opcode) {
    case Opcodes.GETSTATIC:
    case Opcodes.GETFIELD: {
        Operand op = fields.get(owner + ":" + name);
        if (op == null) {
            op = new Operand(owner + ":" + name, desc);
        }/*  w  w w.j  a v a  2s .c  o  m*/
        push(op);
    }
        break;

    case Opcodes.PUTSTATIC:
    case Opcodes.PUTFIELD:
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            fields.put(owner + ":" + name, op);
        }
        break;
    }
}

From source file:com.microsoft.applicationinsights.agent.internal.agent.AdvancedAdviceAdapter.java

License:Open Source License

/**
 * This method calls a Java enum method with its expected argumentes
 * @param clazz The enum class to activate
 * @param methodName The method of the class to activate
 * @param methodSignature The method signature
 * @param args The arguments to pass to the method
 *//*  www .j  a v a  2 s  . co  m*/
protected void activateEnumMethod(Class<?> clazz, String methodName, String methodSignature, Object... args) {
    String internalName = Type.getInternalName(clazz);
    super.visitFieldInsn(Opcodes.GETSTATIC, internalName, "INSTANCE", "L" + internalName + ";");
    for (Object arg : args) {
        if (arg instanceof TempVar) {
            loadLocal(((TempVar) arg).tempVarIndex);
        } else if (arg instanceof TempArrayVar) {
            super.visitVarInsn(Opcodes.ALOAD, ((TempArrayVar) arg).tempVarIndex);
        } else {
            super.visitLdcInsn(arg);
        }
    }
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, internalName, methodName, methodSignature, false);
}

From source file:com.microsoft.applicationinsights.agent.internal.agent.HttpClientMethodVisitor.java

License:Open Source License

@Override
protected void byteCodeForMethodExit(int opcode) {
    String internalName = Type.getInternalName(ImplementationsCoordinator.class);
    switch (translateExitCode(opcode)) {
    case EXIT_WITH_RETURN_VALUE:
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "nanoTime", "()J", false);
        mv.visitVarInsn(LLOAD, deltaInNS);
        mv.visitInsn(LSUB);//from   w ww. jav a2  s.c om
        mv.visitVarInsn(LSTORE, deltaInNS);

        TempVar resultOfMethod = duplicateTopStackToTempVariable(Type.getType(Object.class));
        mv.visitVarInsn(ALOAD, resultOfMethod.tempVarIndex);
        mv.visitMethodInsn(INVOKEINTERFACE, "org/apache/http/client/methods/CloseableHttpResponse",
                "getStatusLine", "()Lorg/apache/http/StatusLine;", true);
        int statusLineLocal = this.newLocal(Type.getType(Object.class));
        mv.visitVarInsn(ASTORE, statusLineLocal);

        mv.visitVarInsn(ALOAD, statusLineLocal);
        mv.visitMethodInsn(INVOKEINTERFACE, "org/apache/http/StatusLine", "getStatusCode", "()I", true);
        int statusCodeLocal = this.newLocal(Type.INT_TYPE);
        mv.visitVarInsn(ISTORE, statusCodeLocal);

        mv.visitFieldInsn(Opcodes.GETSTATIC, internalName, "INSTANCE", "L" + internalName + ";");
        mv.visitLdcInsn(getMethodName());
        mv.visitVarInsn(ALOAD, methodLocal);
        mv.visitVarInsn(ALOAD, uriLocal);
        mv.visitVarInsn(ILOAD, statusCodeLocal);
        mv.visitVarInsn(LLOAD, deltaInNS);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, internalName, FINISH_DETECT_METHOD_NAME,
                FINISH_METHOD_RETURN_SIGNATURE, false);
        return;

    default:
        return;
    }
}

From source file:com.microsoft.Malmo.OverclockingClassTransformer.java

License:Open Source License

private static void overclockServer(ClassNode node, boolean isObfuscated) {
    // We're attempting to replace this code (from the heart of MinecraftServer.run):
    /*       /*from w  w w  .  jav a2 s .  co m*/
    {
        while (i > 50L)
        {
            i -= 50L;
            this.tick();
        }
    }
            
    Thread.sleep(Math.max(1L, 50L - i));
    */

    // With this:
    /*       
    {
    while (i > TimeHelper.serverTickLength)
    {
        i -= TimeHelper.serverTickLength;
        this.tick();
    }
    }
            
    Thread.sleep(Math.max(1L, TimeHelper.serverTickLength - i));
    */
    // This allows us to alter the tick length via TimeHelper.

    final String methodName = "run";
    final String methodDescriptor = "()V"; // No params, returns void.

    System.out.println("MALMO: Found MinecraftServer, attempting to transform it");

    for (MethodNode method : node.methods) {
        if (method.name.equals(methodName) && method.desc.equals(methodDescriptor)) {
            System.out.println("MALMO: Found MinecraftServer.run() method, attempting to transform it");
            for (AbstractInsnNode instruction : method.instructions.toArray()) {
                if (instruction.getOpcode() == Opcodes.LDC) {
                    Object cst = ((LdcInsnNode) instruction).cst;
                    if ((cst instanceof Long) && (Long) cst == 50) {
                        System.out.println("MALMO: Transforming LDC");
                        AbstractInsnNode replacement = new FieldInsnNode(Opcodes.GETSTATIC,
                                "com/microsoft/Malmo/Utils/TimeHelper", "serverTickLength", "J");
                        method.instructions.set(instruction, replacement);
                    }
                }
            }
        }
    }
}

From source file:com.netease.hearttouch.hthotfix.refclass.RefMethodVisitor.java

License:MIT License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    super.visitFieldInsn(opcode, owner, name, desc);

    //getstatic-b2-178
    //get a static field value of a class, where the field is identified by field reference in the constant pool index
    //static,static final(ldc)
    if (opcode == Opcodes.GETSTATIC) {
        context.checkInPatchClasses(owner);
    }/*ww w  .  j a  v a2s .  c  om*/
}

From source file:com.nginious.http.xsp.expr.ExpressionCompiler.java

License:Apache License

/**
 * Creates a compiled expression from the specified tree value node expression. The class bytecode for the 
 * compiled expression is generated at runtime.
 * //from  w w w. j  ava2 s  .  c o m
 * @param uncompiled the uncompiled tree value node expression
 * @return the compiled expression
 * @throws ExpressionException if unable to compile expression
 */
public Expression compile(TreeExpression uncompiled) throws ExpressionException {
    ClassWriter writer = new ClassWriter(0);

    // Create class
    String className = classBaseName + classNameCounter.getAndIncrement();
    String classIdentifier = className.replace('.', '/');
    writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, classIdentifier, "L" + classIdentifier + ";",
            "com/nginious/http/xsp/expr/Expression", null);

    // Create constructor
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "<init>", "()V", null, null);
    visitor.visitCode();
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/expr/Expression", "<init>", "()V");
    visitor.visitInsn(Opcodes.RETURN);
    visitor.visitMaxs(1, 1);
    visitor.visitEnd();

    // protected abstract boolean evaluateBoolean();
    visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateBoolean", "()Z", null, null);

    if (uncompiled.getType() == Type.BOOLEAN) {
        uncompiled.compile(visitor);
    } else if (uncompiled.getType() == Type.DOUBLE) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D");

        Label endLabel = new Label();
        Label falseLabel = new Label();
        visitor.visitLdcInsn(0.0d);
        visitor.visitInsn(Opcodes.DCMPL);
        visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel);
        visitor.visitLdcInsn(true);
        visitor.visitJumpInsn(Opcodes.GOTO, endLabel);
        visitor.visitLabel(falseLabel);
        visitor.visitLdcInsn(false);
        visitor.visitLabel(endLabel);
    } else if (uncompiled.getType() == Type.INT) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I");

        Label endLabel = new Label();
        Label falseLabel = new Label();
        visitor.visitLdcInsn(0);
        visitor.visitJumpInsn(Opcodes.IFNE, falseLabel);
        visitor.visitLdcInsn(true);
        visitor.visitJumpInsn(Opcodes.GOTO, endLabel);
        visitor.visitLabel(falseLabel);
        visitor.visitLdcInsn(false);
        visitor.visitLabel(endLabel);
    } else if (uncompiled.getType() == Type.STRING) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString",
                "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "parseBoolean",
                "(Ljava/lang/String;)Z");
    }

    visitor.visitInsn(Opcodes.IRETURN);
    visitor.visitMaxs(5, 5);
    visitor.visitEnd();

    // protected abstract int evaluateInt();
    visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateInt", "()I", null, null);

    if (uncompiled.getType() == Type.BOOLEAN) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z");

        Label endLabel = new Label();
        Label falseLabel = new Label();
        visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel);
        visitor.visitLdcInsn(1);
        visitor.visitJumpInsn(Opcodes.GOTO, endLabel);
        visitor.visitLabel(falseLabel);
        visitor.visitLdcInsn(0);
        visitor.visitLabel(endLabel);
    } else if (uncompiled.getType() == Type.DOUBLE) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D");
        visitor.visitInsn(Opcodes.D2I);
    } else if (uncompiled.getType() == Type.INT) {
        uncompiled.compile(visitor);
    } else if (uncompiled.getType() == Type.STRING) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString",
                "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I");
    }

    visitor.visitInsn(Opcodes.IRETURN);
    visitor.visitMaxs(5, 5);
    visitor.visitEnd();

    // protected abstract double evaluateDouble();
    visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateDouble", "()D", null, null);

    if (uncompiled.getType() == Type.BOOLEAN) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z");

        Label endLabel = new Label();
        Label falseLabel = new Label();
        visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel);
        visitor.visitLdcInsn(1.0d);
        visitor.visitJumpInsn(Opcodes.GOTO, endLabel);
        visitor.visitLabel(falseLabel);
        visitor.visitLdcInsn(0.0d);
        visitor.visitLabel(endLabel);
    } else if (uncompiled.getType() == Type.DOUBLE) {
        uncompiled.compile(visitor);
    } else if (uncompiled.getType() == Type.INT) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I");
        visitor.visitInsn(Opcodes.I2D);
    } else if (uncompiled.getType() == Type.STRING) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString",
                "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "parseDouble",
                "(Ljava/lang/String;)D");
    }

    visitor.visitInsn(Opcodes.DRETURN);
    visitor.visitMaxs(5, 5);
    visitor.visitEnd();

    // protected abstract String evaluateString();
    visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateString", "()Ljava/lang/String;", null, null);

    if (uncompiled.getType() == Type.BOOLEAN) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "toString", "(Z)Ljava/lang/String;");
    } else if (uncompiled.getType() == Type.DOUBLE) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "toString", "(D)Ljava/lang/String;");
    } else if (uncompiled.getType() == Type.INT) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;");
    } else if (uncompiled.getType() == Type.STRING) {
        uncompiled.compile(visitor);
    }

    visitor.visitInsn(Opcodes.ARETURN);
    visitor.visitMaxs(6, 6);
    visitor.visitEnd();

    // public abstract Type getType();        
    visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "getType", "()Lcom/nginious/http/xsp/expr/Type;", null,
            null);

    if (uncompiled.getType() == Type.BOOLEAN) {
        visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "BOOLEAN",
                "Lcom/nginious/http/xsp/expr/Type;");
        visitor.visitInsn(Opcodes.ARETURN);
    } else if (uncompiled.getType() == Type.DOUBLE) {
        visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "DOUBLE",
                "Lcom/nginious/http/xsp/expr/Type;");
        visitor.visitInsn(Opcodes.ARETURN);
    } else if (uncompiled.getType() == Type.INT) {
        visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "INT",
                "Lcom/nginious/http/xsp/expr/Type;");
        visitor.visitInsn(Opcodes.ARETURN);
    } else if (uncompiled.getType() == Type.STRING) {
        visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "STRING",
                "Lcom/nginious/http/xsp/expr/Type;");
        visitor.visitInsn(Opcodes.ARETURN);
    }

    visitor.visitMaxs(1, 1);
    visitor.visitEnd();

    try {
        writer.visitEnd();
        byte[] clazzBytes = writer.toByteArray();
        Class<?> clazz = loadClass(className, clazzBytes);
        return (Expression) clazz.newInstance();
    } catch (Exception e) {
        throw new ExpressionException("Can't instantiate compiled expression", e);
    }
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Generates instructions for printing out a string constant using {@link System#out}. This is useful for debugging. For example, you
 * can print out lines around your instrumented code to make sure that what you think is being run is actually being run.
 * @param text text to print out// www .j a  v a  2  s  . co  m
 * @return instructions to call System.out.println with a string constant
 * @throws NullPointerException if any argument is {@code null}
 */
public static InsnList debugPrint(String text) {
    Validate.notNull(text);

    InsnList ret = new InsnList();

    ret.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));
    ret.add(new LdcInsnNode(text));
    ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V",
            false));

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.DebugGenerators.java

License:Open Source License

/**
 * Generates instructions for generating marker instructions. These marker instructions are meant to be is useful for debugging
 * instrumented code. For example, you can spot a specific portion of instrumented code by looking for specific markers in the assembly
 * output./*from ww  w .  j  a  va2  s. co  m*/
 * @param markerType marker type (determines what kind of instructions are generated)
 * @param text text to print out
 * @return instructions to call System.out.println with a string constant
 * @throws NullPointerException if any argument is {@code null}
 */
public static InsnList debugMarker(MarkerType markerType, String text) {
    Validate.notNull(markerType);
    Validate.notNull(text);

    InsnList ret = new InsnList();

    switch (markerType) {
    case NONE:
        break;
    case CONSTANT:
        ret.add(new LdcInsnNode(text));
        ret.add(new InsnNode(Opcodes.POP));
        break;
    case STDOUT:
        ret.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));
        ret.add(new LdcInsnNode(text));
        ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                "(Ljava/lang/String;)V", false));
        break;
    default:
        throw new IllegalStateException();
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.DebugGenerators.java

License:Open Source License

/**
 * Generates instructions for printing out a string using {@link System#out}. This is useful for debugging. For example, you
 * can print out lines around your instrumented code to make sure that what you think is being run is actually being run.
 * @param text debug text generation instruction list -- must leave a String on the stack
 * @return instructions to call System.out.println with a string constant
 * @throws NullPointerException if any argument is {@code null}
 */// ww  w.  ja va2s  .c om
public static InsnList debugPrint(InsnList text) {
    Validate.notNull(text);

    InsnList ret = new InsnList();

    ret.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));
    ret.add(text);
    ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V",
            false));

    return ret;
}

From source file:com.sun.fortress.compiler.codegen.CodeGen.java

License:Open Source License

public void printString(CodeGenMethodVisitor mv, String s) {
    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    mv.visitLdcInsn(s);/*from   w w  w  .j  av  a  2  s  .  co  m*/
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
}