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:net.wpm.reflectasm.ClassAccess.java

License:Open Source License

static private MethodVisitor insertThrowExceptionForFieldNotFound(MethodVisitor mv) {
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);/*from w ww  .  ja  va 2s . com*/
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitLdcInsn("Field not found: ");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;",
            false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V",
            false);
    mv.visitInsn(ATHROW);
    return mv;
}

From source file:net.wpm.reflectasm.ClassAccess.java

License:Open Source License

static private MethodVisitor insertThrowExceptionForFieldType(MethodVisitor mv, String fieldType) {
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);//  www.  j a  va2s. com
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitLdcInsn("Field not declared as " + fieldType + ": ");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;",
            false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V",
            false);
    mv.visitInsn(ATHROW);
    return mv;
}

From source file:net.yrom.tools.ShrinkRClassVisitor.java

License:Apache License

private static void pushInt(MethodVisitor mv, int i) {
    if (0 <= i && i <= 5) {
        mv.visitInsn(Opcodes.ICONST_0 + i); //  ICONST_0 ~ ICONST_5
    } else if (i <= Byte.MAX_VALUE) {
        mv.visitIntInsn(Opcodes.BIPUSH, i);
    } else if (i <= Short.MAX_VALUE) {
        mv.visitIntInsn(Opcodes.SIPUSH, i);
    } else {//from www  .  j  a v  a 2  s .  c o m
        mv.visitLdcInsn(i);
    }
}

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.ComponentInjector.java

License:Open Source License

private Class<? extends T> construct(List<Class<? extends Component>> components) {

    // Map components to specified wrapped interfaces
    Map<Class<?>, Class<? extends Component>> intfComponentMap = new HashMap<>();
    for (Class<? extends Component> component : components) {
        for (Passthrough pt : component.getAnnotationsByType(Passthrough.class)) {
            Class<?> intf;/*from w  w w . j a va 2s.c  om*/
            try {
                intf = Class.forName(pt.value());
            } catch (ClassNotFoundException exec) {
                throw new ClassLoaderUtil.ClassLoaderException(
                        "Invalid passthrough \"%s\" on component %s, the specified interface doesn't exist.",
                        pt.value(), component);
            }
            if (!intf.isAssignableFrom(component)) {
                throw new ClassLoaderUtil.ClassLoaderException(
                        "Invalid passthrough \"%s\" on component %s, the specified interface isn't implemented.",
                        pt.value(), component);
            }
            if (intfComponentMap.containsKey(intf)) {
                throw new ClassLoaderUtil.ClassLoaderException(
                        "Duplicate Passthrough interface found: %s (%s, %s)", pt.value(), component,
                        intfComponentMap.get(intf));
            }
            intfComponentMap.put(intf, component);
        }
    }

    // Create new ClassNode from cached bytes
    ClassNode clazzNode = new ClassNode();
    String name = Type.getInternalName(baseClazz);
    String classname = name + "_$$_NOVA_" + cache.size();

    // Inject block field
    clazzNode.visit(V1_8, ACC_PUBLIC | ACC_SUPER, classname, null, name,
            intfComponentMap.keySet().stream().map(Type::getInternalName).toArray(s -> new String[s]));
    clazzNode.visitField(ACC_PRIVATE | ACC_FINAL, "$$_provider", Type.getDescriptor(ComponentProvider.class),
            null, null).visitEnd();

    // Add constructors
    for (Constructor<?> constructor : baseClazz.getConstructors()) {
        int mod = constructor.getModifiers();
        String descr = Type.getConstructorDescriptor(constructor);

        if (Modifier.isFinal(mod) || Modifier.isPrivate(mod)) {
            continue;
        }
        MethodVisitor mv = clazzNode.visitMethod(mod, "<init>", descr, null,
                ASMHelper.getExceptionTypes(constructor));

        // Call super constructor
        mv.visitCode();
        // load this
        mv.visitVarInsn(ALOAD, 0);
        Class<?>[] parameters = constructor.getParameterTypes();
        for (int i = 0; i < constructor.getParameterCount(); i++) {
            // variables
            mv.visitVarInsn(Type.getType(parameters[i]).getOpcode(ILOAD), i + 1);
        }
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(baseClazz), "<init>", descr, false);
        // return
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    // Add methods
    for (Class<?> intf : intfComponentMap.keySet()) {
        // Create class constant
        Type clazzConst = Type.getType(intf.getClass());

        for (Method m : intf.getMethods()) {
            boolean isVoid = m.getReturnType() == null;
            String descr = Type.getMethodDescriptor(m);

            MethodVisitor mv = clazzNode.visitMethod(ACC_PUBLIC, m.getName(), descr, null,
                    ASMHelper.getExceptionTypes(m));
            mv.visitCode();

            // load block instance
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, classname, "$$_provider", Type.getDescriptor(ComponentProvider.class));
            mv.visitLdcInsn(clazzConst);
            // load component instance
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(ComponentProvider.class), "get",
                    Type.getMethodDescriptor(Type.getType(Component.class), Type.getType(Class.class)), false);

            // add parameters
            Class<?>[] parameters = m.getParameterTypes();
            for (int i = 0; i < m.getParameterCount(); i++) {
                mv.visitVarInsn(Type.getType(parameters[i]).getOpcode(ILOAD), i + 1);
            }

            // invoke
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(intf), m.getName(), descr, true);
            mv.visitInsn(isVoid ? RETURN : Type.getType(m.getReturnType()).getOpcode(IRETURN));
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
    }

    clazzNode.visitEnd();

    return ASMHelper.defineClass(clazzNode, ClassWriter.COMPUTE_MAXS, baseClazz.getProtectionDomain());
}

From source file:one.nio.gen.BytecodeGenerator.java

License:Apache License

public static void emitThrow(MethodVisitor mv, String exceptionClass, String message) {
    mv.visitTypeInsn(NEW, exceptionClass);
    mv.visitInsn(DUP);/*from   w ww. j av  a 2  s.  c  om*/
    mv.visitLdcInsn(message);
    mv.visitMethodInsn(INVOKESPECIAL, exceptionClass, "<init>", "(Ljava/lang/String;)V");
    mv.visitInsn(ATHROW);
}

From source file:one.nio.gen.BytecodeGenerator.java

License:Apache License

public static void emitInt(MethodVisitor mv, int c) {
    if (c >= -1 && c <= 5) {
        mv.visitInsn(ICONST_0 + c);/*w  ww .j a v  a 2s. c  o  m*/
    } else if (c >= Byte.MIN_VALUE && c <= Byte.MAX_VALUE) {
        mv.visitIntInsn(BIPUSH, c);
    } else if (c >= Short.MIN_VALUE && c <= Short.MAX_VALUE) {
        mv.visitIntInsn(SIPUSH, c);
    } else {
        mv.visitLdcInsn(c);
    }
}

From source file:one.nio.gen.BytecodeGenerator.java

License:Apache License

public static void emitLong(MethodVisitor mv, long c) {
    if (c >= 0 && c <= 1) {
        mv.visitInsn(LCONST_0 + (int) c);
    } else {//ww w. j a  v  a2  s  .  com
        mv.visitLdcInsn(c);
    }
}

From source file:one.nio.gen.BytecodeGenerator.java

License:Apache License

public static void emitFloat(MethodVisitor mv, float c) {
    if (c == 0.0f) {
        mv.visitInsn(FCONST_0);//from  ww w . j ava2  s  .co  m
    } else if (c == 1.0f) {
        mv.visitInsn(FCONST_1);
    } else if (c == 2.0f) {
        mv.visitInsn(FCONST_2);
    } else {
        mv.visitLdcInsn(c);
    }
}

From source file:one.nio.gen.BytecodeGenerator.java

License:Apache License

public static void emitDouble(MethodVisitor mv, double c) {
    if (c == 0.0) {
        mv.visitInsn(DCONST_0);/*from   w  ww . j  ava2  s .  c  o m*/
    } else if (c == 1.0) {
        mv.visitInsn(DCONST_1);
    } else {
        mv.visitLdcInsn(c);
    }
}

From source file:one.nio.http.gen.RequestHandlerGenerator.java

License:Apache License

private void setupParam(MethodVisitor mv, Class type, Param param) {
    String name = param.value();/* w ww . j  av a  2  s .c  o m*/
    String defaultValue = null;
    int eq = name.indexOf('=');
    if (eq > 0) {
        defaultValue = name.substring(eq + 1);
        name = name.substring(0, eq);
    }

    mv.visitVarInsn(ALOAD, 1);
    mv.visitLdcInsn(name + '=');

    boolean needNullCheck = false;
    if (param.required()) {
        mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/http/Request", "getRequiredParameter",
                "(Ljava/lang/String;)Ljava/lang/String;");
    } else if (defaultValue != null) {
        mv.visitLdcInsn(defaultValue);
        mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/http/Request", "getParameter",
                "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
    } else {
        mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/http/Request", "getParameter",
                "(Ljava/lang/String;)Ljava/lang/String;");
        needNullCheck = true;
    }

    convertArgument(mv, type, needNullCheck);
}