Example usage for org.objectweb.asm MethodVisitor visitLdcInsn

List of usage examples for org.objectweb.asm MethodVisitor visitLdcInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitLdcInsn.

Prototype

public void visitLdcInsn(final Object value) 

Source Link

Document

Visits a LDC instruction.

Usage

From source file:com.gargoylesoftware.js.nashorn.internal.codegen.types.NumberType.java

License:Open Source License

@Override
public Type ldc(final MethodVisitor method, final Object c) {
    assert c instanceof Double;

    final double value = (Double) c;

    if (Double.doubleToLongBits(value) == 0L) { // guard against -0.0
        method.visitInsn(DCONST_0);/*from w w  w . ja  v a 2  s . co m*/
    } else if (value == 1.0) {
        method.visitInsn(DCONST_1);
    } else {
        method.visitLdcInsn(value);
    }

    return NUMBER;
}

From source file:com.gargoylesoftware.js.nashorn.internal.codegen.types.ObjectType.java

License:Open Source License

@Override
public Type ldc(final MethodVisitor method, final Object c) {
    if (c == null) {
        method.visitInsn(ACONST_NULL);//from   ww  w  . ja va  2  s.c  om
    } else if (c instanceof Undefined) {
        return loadUndefined(method);
    } else if (c instanceof String) {
        method.visitLdcInsn(c);
        return STRING;
    } else if (c instanceof Handle) {
        method.visitLdcInsn(c);
        return Type.typeFor(MethodHandle.class);
    } else {
        throw new UnsupportedOperationException(
                "implementation missing for class " + c.getClass() + " value=" + c);
    }

    return Type.OBJECT;
}

From source file:com.github.bmsantos.core.cola.injector.ErrorsClassVisitor.java

License:Apache License

private void injectTestMethod() {

    final MethodVisitor mv = classVisitor.visitMethod(ACC_PUBLIC, METHOD_NAME, "()V", null, null);
    mv.visitCode();//from w w  w  . j  av  a2 s. co m
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(errors);
    mv.visitMethodInsn(INVOKESTATIC, "org/junit/Assert", "fail", "(Ljava/lang/String;)V", false);
    mv.visitInsn(RETURN);
    mv.visitAnnotation("Lorg/junit/Test;", true);
    mv.visitEnd();
    mv.visitMaxs(0, 0);
}

From source file:com.github.bmsantos.core.cola.injector.InjectorClassVisitor.java

License:Apache License

private void injectTestMethod(final String feature, final String scenario, final String story,
        final String projectionValues, final String reports) {

    final MethodVisitor mv = infoClassVisitor.visitMethod(ACC_PUBLIC,
            String.format(METHOD_NAME_FORMAT, feature, scenario), "()V", null, null);
    mv.visitCode();//from ww w .j a va  2s . co  m
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(feature);
    mv.visitLdcInsn(scenario);
    mv.visitLdcInsn(story);
    mv.visitLdcInsn(projectionValues);
    mv.visitLdcInsn(reports);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, "com/github/bmsantos/core/cola/story/processor/StoryProcessor", "process",
            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V",
            false);
    mv.visitInsn(RETURN);
    mv.visitAnnotation("Lorg/junit/Test;", true);
    mv.visitEnd();
    mv.visitMaxs(0, 0);
}

From source file:com.github.bmsantos.core.cola.injector.InjectorClassVisitor.java

License:Apache License

private void injectIgnoreMethod(final String feature, final String scenario) {
    final MethodVisitor mv = infoClassVisitor.visitMethod(ACC_PUBLIC,
            String.format(IGNORED_METHOD_NAME_FORMAT, feature, scenario), "()V", null, null);
    mv.visitCode();/*from   w ww. java 2  s.c  om*/
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(feature);
    mv.visitLdcInsn(scenario);
    mv.visitMethodInsn(INVOKESTATIC, "com/github/bmsantos/core/cola/story/processor/StoryProcessor", "ignore",
            "(Ljava/lang/String;Ljava/lang/String;)V", false);
    mv.visitInsn(RETURN);
    mv.visitAnnotation("Lorg/junit/Test;", true);
    mv.visitEnd();
    mv.visitMaxs(0, 0);
}

From source file:com.github.jasmo.obfuscate.ScrambleStrings.java

License:Open Source License

private void createUnscramble() {
    MethodVisitor mv = unscrambleClass.visitMethod(ACC_PUBLIC | ACC_STATIC, CALL_NAME, CALL_DESC, null, null);
    mv.visitCode();/*from   ww w  . j  a  v  a2  s .  c  o m*/
    mv.visitTypeInsn(NEW, "java/lang/String");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESTATIC, "java/util/Base64", "getDecoder", "()Ljava/util/Base64$Decoder;", false);
    mv.visitFieldInsn(GETSTATIC, unscrambleClass.name, FIELD_NAME, "[Ljava/lang/String;");
    mv.visitVarInsn(ILOAD, 0);
    mv.visitInsn(AALOAD);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Base64$Decoder", "decode", "(Ljava/lang/String;)[B", false);
    mv.visitLdcInsn("UTF-8");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/String", "<init>", "([BLjava/lang/String;)V", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:com.github.megatronking.stringfog.plugin.StringFogClassVisitor.java

License:Apache License

@Override
public void visitEnd() {
    if (!mIgnoreClass && !isClInitExists && !mStaticFinalFields.isEmpty()) {
        MethodVisitor mv = super.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();//  w ww .j  a va  2 s .co  m
        // Here init static final fields.
        for (ClassStringField field : mStaticFinalFields) {
            if (!canEncrypted(field.value)) {
                continue;
            }
            String originValue = field.value;
            String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
            mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
            mv.visitLdcInsn(encryptValue);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                    "(Ljava/lang/String;)Ljava/lang/String;", false);
            mv.visitFieldInsn(Opcodes.PUTSTATIC, mClassName, field.name, ClassStringField.STRING_DESC);
        }
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 0);
        mv.visitEnd();
    }
    super.visitEnd();
}

From source file:com.github.rgcjonas.kuemmelgtr.core.Compiler.java

License:Open Source License

private static void compileCommon(TokenSource<RPNToken> source, MethodVisitor method, String variable)
        throws ParsingError {
    method.visitCode();//from w ww  .ja v  a  2 s.  c  o  m

    // we now can compile our token stream
    int currentStackSize = 0; // we keep track of the current stack size to throw errors if we encounter an invalid instruction
    RPNToken t;
    while ((t = source.nextToken()) != null) {
        if (t instanceof RPNToken.Operand) {
            // we push it onto the stack
            method.visitLdcInsn(((RPNToken.Operand) t).getValue());
            currentStackSize++;
        } else if (t instanceof RPNToken.Operator) {
            RPNToken.Operator op = (RPNToken.Operator) t;

            if (currentStackSize < op.getNumOperands())
                throw new ParsingError(t.getSrcLine(), t.getSrcColumn(), "Missing operand(s)");

            switch (op.getName()) {
            case "_add":
                method.visitInsn(Opcodes.DADD);
                currentStackSize--;
                break;
            case "_mult":
                method.visitInsn(Opcodes.DMUL);
                currentStackSize--;
                break;
            case "_sub":
                method.visitInsn(Opcodes.DSUB);
                currentStackSize--;
                break;
            case "_div":
                method.visitInsn(Opcodes.DDIV);
                currentStackSize--;
                break;
            case "_pow":
                method.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Math", "pow", "(DD)D");
                currentStackSize--;
                break;
            default:
                //HACK: support every function in java/lang/Math. Will be way more performant than any lookup-and-evaluate
                //TODO: implement more functions inline
                if (currentStackSize < 1)
                    throw new ParsingError(t.getSrcLine(), t.getSrcColumn(), "Missing operand");
                try {
                    Method meth = Math.class.getDeclaredMethod(op.getName(), double.class); // just check if it's available and hope it returns a double
                    if (meth.getReturnType() != double.class)
                        throw new NoSuchMethodException(); // we don't want to blow up at runtime, do we?

                    method.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Math", op.getName(), "(D)D");
                } catch (NoSuchMethodException e) {
                    // we do not give up. The method may be available at runtime.
                    method.visitVarInsn(Opcodes.ALOAD, 0);
                    method.visitInsn(Opcodes.DUP_X2); // swap the this pointer and double
                    method.visitInsn(Opcodes.POP);
                    method.visitLdcInsn(op.getName());
                    method.visitLdcInsn((int) op.getSrcLine());
                    method.visitLdcInsn((int) op.getSrcColumn());
                    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                            CompiledClassBase.class.getName().replace('.', '/'), "resolveAndEvaluateFunction",
                            "(DLjava/lang/String;II)D");
                }
            }
        } else if (t instanceof RPNToken.VariableRecall) {
            // maybe, maybe the variable is an argument we can load
            if (variable != null && ((RPNToken.VariableRecall) t).getName().equalsIgnoreCase(variable)) {
                method.visitVarInsn(Opcodes.DLOAD, 1);
            } else {
                // we let our parent class do the hard work
                method.visitVarInsn(Opcodes.ALOAD, 0);
                method.visitLdcInsn(((RPNToken.VariableRecall) t).getName());
                method.visitLdcInsn(t.getSrcLine());
                method.visitLdcInsn(t.getSrcColumn());
                method.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        CompiledClassBase.class.getName().replace('.', '/'), "getVariable",
                        "(Ljava/lang/String;II)D");
            }
            currentStackSize++;
        } else if (t instanceof RPNToken.VariableAssignment) {
            // also defer to our parent class
            // we do not give up. The method may be available at runtime.
            method.visitVarInsn(Opcodes.ALOAD, 0);
            method.visitInsn(Opcodes.DUP_X2); // swap the this pointer and double
            method.visitInsn(Opcodes.POP);
            method.visitLdcInsn(((RPNToken.VariableAssignment) t).getVariableName());
            method.visitLdcInsn(t.getSrcLine());
            method.visitLdcInsn(t.getSrcColumn());
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CompiledClassBase.class.getName().replace('.', '/'),
                    "setVariable", "(DLjava/lang/String;II)D");
        } else {
            throw new ParsingError(t.getSrcLine(), t.getSrcColumn(), "Unknown instruction: " + t);
        }
    }

    if (currentStackSize != 1)
        throw new ParsingError(0, 0, "Expected stack to be one value, found " + currentStackSize);

    method.visitInsn(Opcodes.DRETURN);

    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.github.stokito.gag.agent.AnswerToLifeGenerator.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) {

    MethodVisitor mv = writer().visitMethod(access, name, desc, sig, exceptions);
    mv.visitCode();/*ww  w.j  a  v  a2s .com*/

    MethodInfo method = classInfo().getMethod(name, desc);
    for (LocalVarInfo param : method.getLocalVars()) {
        AnnoInfo anno = param.getAnnoFor(ANSWER_TYPE);
        if (anno == null) {
            continue;
        }

        Type paramType = param.getType();
        switch (paramType.getSort()) {
        case Type.INT:
        case Type.BYTE:
        case Type.CHAR:
        case Type.SHORT:
            mv.visitLdcInsn(FORTY_TWO);
            mv.visitVarInsn(ISTORE, param.getIndex());
            break;
        case Type.LONG:
            mv.visitLdcInsn((long) FORTY_TWO);
            mv.visitVarInsn(LSTORE, param.getIndex());
            break;
        case Type.DOUBLE:
            mv.visitLdcInsn((double) FORTY_TWO);
            mv.visitVarInsn(DSTORE, param.getIndex());
            break;
        case Type.FLOAT:
            mv.visitLdcInsn((float) FORTY_TWO);
            mv.visitVarInsn(FSTORE, param.getIndex());
            break;
        case Type.OBJECT:
            visitObject(mv, param);
            break;
        default:
            throwUnsupportedException(param);
        }

        setInstrumented(true);
    }

    mv.visitEnd();
    return mv;
}

From source file:com.github.stokito.gag.agent.AnswerToLifeGenerator.java

License:Apache License

/** TODO: Support BigDecimal and BigInteger. */
private void visitObject(MethodVisitor mv, LocalVarInfo param) {
    Type paramType = param.getType();
    if (Type.getType(Character.class).equals(paramType)) {
        mv.visitLdcInsn((char) FORTY_TWO);
        mv.visitMethodInsn(INVOKESTATIC, paramType.getInternalName(), "valueOf",
                "(C)L" + paramType.getInternalName() + ";");
        mv.visitVarInsn(ASTORE, param.getIndex());
    } else if (supportedValueOfTypes.contains(paramType)) {
        mv.visitLdcInsn(String.valueOf(FORTY_TWO));
        mv.visitMethodInsn(INVOKESTATIC, paramType.getInternalName(), "valueOf",
                "(Ljava/lang/String;)L" + paramType.getInternalName() + ";");
        mv.visitVarInsn(ASTORE, param.getIndex());
    } else {//from w  ww .  j  a  v a  2 s  .c o  m
        throwUnsupportedException(param);
    }
}