Example usage for org.objectweb.asm MethodVisitor visitLineNumber

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

Introduction

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

Prototype

public void visitLineNumber(final int line, final Label start) 

Source Link

Document

Visits a line number declaration.

Usage

From source file:org.fabric3.monitor.impl.proxy.BytecodeMonitorProxyService.java

License:Open Source License

private void writeConstructor(ClassWriter cw, String proxyClassDescriptor) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();//  ww w. java2  s.  com
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(56, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, ABSTRACT_MONITOR_HANDLER, "<init>", "()V");
    mv.visitInsn(RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", proxyClassDescriptor, null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:org.forgerock.openidm.shell.felixgogo.FelixGogoCommandsServiceGenerator.java

License:Apache License

/**
 * Generate CommandProvider class and newBuilder for this class based on parameters.
 *
 * @param service  commands service/*from  w  w w  . j a va2  s. co  m*/
 * @param commands commands map (name=help)
 * @param suffix   unique class suffix
 * @return generated CommandProvider newBuilder
 * @throws Exception if something went wrong
 */
public static Object generate(CustomCommandScope service, Map<String, String> commands, String suffix)
        throws Exception {
    // generate class with unique name
    //javassist.CtClass ctClass = POOL.makeClass(AbstractFelixCommandsService.class.getName() + suffix);

    try {
        ClassWriter cw = new ClassWriter(0);
        MethodVisitor mv;
        AnnotationVisitor av0;
        String className = AbstractFelixCommandsService.class.getName().replace('.', '/') + suffix;
        cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, null,
                AbstractFelixCommandsService.class.getName().replace('.', '/'), null);

        //cw.visitSource("AbstractFelixCommandsServiceSample.java", null);

        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(10, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, AbstractFelixCommandsService.class.getName().replace('.', '/'),
                "<init>", "(Ljava/lang/Object;)V");
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(11, l1);
        mv.visitInsn(RETURN);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + className + ";", null, l0, l2, 0);
        mv.visitLocalVariable("service", "Ljava/lang/Object;", null, l0, l2, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();

        /*javassist.bytecode.ClassFile ccFile = ctClass.getClassFile();
        ccFile.setVersionToJava5();
        javassist.bytecode.ConstPool constPool = ccFile.getConstPool();
                
        // set superclass
        javassist.CtClass abstractCtClass = POOL.getCtClass(AbstractFelixCommandsService.class.getName());
        ctClass.setSuperclass(abstractCtClass);
                
        // add constructor
        javassist.CtClass serviceCtClass = POOL.getCtClass(Object.class.getName());
        javassist.CtConstructor ctConstructor = new javassist.CtConstructor(new javassist.CtClass[]{serviceCtClass},
            ctClass);
        ctConstructor.setModifiers(javassist.Modifier.PUBLIC);
        ctConstructor.setBody("super($1);");
        ctClass.addConstructor(ctConstructor);
                
        // add method for each command
        javassist.CtClass sessionCtClass = POOL.getCtClass(CommandSession.class.getName());
        javassist.CtClass stringArrayCtClass = POOL.getCtClass(String[].class.getName());*/
        Set<String> names = commands.keySet();
        for (String name : names) {
            if (isMethodAvailable(service, name)) {
                mv = cw.visitMethod(ACC_PUBLIC, name,
                        "(Lorg/apache/felix/service/command/CommandSession;[Ljava/lang/String;)V", null, null);

                av0 = mv.visitAnnotation("Lorg/apache/felix/service/command/Descriptor;", true);
                av0.visit("value", commands.get(name));
                av0.visitEnd();

                mv.visitCode();
                l0 = new Label();
                mv.visitLabel(l0);
                mv.visitLineNumber(15, l0);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitLdcInsn(name);
                mv.visitVarInsn(ALOAD, 1);
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKEVIRTUAL, className, "runCommand",
                        "(Ljava/lang/String;Lorg/apache/felix/service/command/CommandSession;"
                                + "[Ljava/lang/String;)V");
                l1 = new Label();
                mv.visitLabel(l1);
                mv.visitLineNumber(16, l1);
                mv.visitInsn(RETURN);
                l2 = new Label();
                mv.visitLabel(l2);
                mv.visitLocalVariable("this", "L" + className + ";", null, l0, l2, 0);
                mv.visitLocalVariable("session", "Lorg/apache/felix/service/command/CommandSession;", null, l0,
                        l2, 1);
                mv.visitLocalVariable("args", "[Ljava/lang/String;", null, l0, l2, 2);
                mv.visitMaxs(4, 3);
                mv.visitEnd();

                /*javassist.CtMethod ctMethod = new javassist.CtMethod(javassist.CtClass.voidType, name,
                    new javassist.CtClass[]{
                        sessionCtClass, stringArrayCtClass
                    }, ctClass);
                ctMethod.setModifiers(javassist.Modifier.PUBLIC);
                ctMethod.setBody("runCommand(\"" + name + "\", $1, $2);");
                ctClass.addMethod(ctMethod);
                        
                // add GoGo descriptor for this shell command
                javassist.bytecode.AnnotationsAttribute annotationsAttribute =
                    new javassist.bytecode.AnnotationsAttribute(constPool,
                        javassist.bytecode.AnnotationsAttribute.visibleTag);
                javassist.bytecode.annotation.Annotation annotation =
                    new javassist.bytecode.annotation.Annotation(Descriptor.class.getName(), constPool);
                annotation.addMemberValue("value",
                    new javassist.bytecode.annotation.StringMemberValue(commands.get(name), constPool));
                annotationsAttribute.addAnnotation(annotation);
                ctMethod.getMethodInfo().addAttribute(annotationsAttribute);*/
            }
        }

        cw.visitEnd();
        // create new newBuilder
        /*Class<?> aClass = ctClass.toClass(FelixGogoCommandsServiceGenerator.class.getClassLoader(), null);
        */
        Class<?> aClass = classLoader.defineClass(className, cw.toByteArray());
        Constructor<?> constructor = aClass.getConstructor(Object.class);
        return constructor.newInstance(service);
    } catch (Exception e) {
        //ctClass.detach();
        throw e;
    }
}

From source file:org.glassfish.pfl.tf.tools.enhancer.StaticInitVisitor.java

License:Open Source License

private void generateTraceMsg(MethodVisitor mv, String msg, int num) {
    if (ENABLED && util.getDebug()) {
        final Label start = new Label();
        mv.visitLabel(start);// w  w  w. ja  v  a  2  s .co m
        mv.visitLineNumber(num, start);
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn(msg);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
    }
}

From source file:org.jboss.errai.BoringClassGenerator.java

License:Apache License

/**
 * Generates a class with a no-args public constructor.
 *
 * @param loader//from  www  . ja va 2  s . c o m
 *          ignored.
 * @param className
 *          The generated class has this package and name.
 * @param classBeingRedefined
 *          ignored.
 * @param protectionDomain
 *          ignored.
 * @param classfileBuffer
 *          ignored.
 * @return bytecode for an empty class whose package and class name are
 *         determined by the {@code className} parameter.
 */
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {

    if (!hiddenClassNamePattern.matcher(className).matches()) {
        if (debug) {
            System.out.println("client-local-class-hider: not hiding " + className);
        }
        return null;
    }

    if (debug) {
        System.out.println("client-local-class-hider: hiding " + className);
    }

    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", null);

    cw.visitSource("FakeClass__GENERATED_BY_JBoss_ClientLocalClassHider.java", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(15, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }

    cw.visitEnd();

    return cw.toByteArray();
}

From source file:org.jboss.errai.EmtpyClassGenerator.java

License:Apache License

/**
 * Generate an empty class: a trivial public subclass of java.lang.Object
 * which is in the expected package and has the expected name.
 * // ww  w .  j a v  a 2  s .c o m
 * @param className
 *          the fully qualified class name
 * @return byte array representing the empty class
 */
public static byte[] generateEmptyClass(String className) {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", null);

    cw.visitSource("FakeClass__GENERATED_BY_JBoss_ClientLocalClassHider.java", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(15, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }

    cw.visitEnd();

    return cw.toByteArray();
}

From source file:org.jruby.runtime.callback.InvocationCallbackFactory.java

License:LGPL

@Deprecated
private ClassWriter createCtor(String namePath) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, namePath, null, SUPER_CLASS, null);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();//from  w w  w.j a va 2 s  .  c o  m
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, SUPER_CLASS, "<init>", "()V");
    Label line = new Label();
    mv.visitLineNumber(0, line);
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    return cw;
}

From source file:org.jruby.runtime.callback.InvocationCallbackFactory.java

License:LGPL

@Deprecated
private ClassWriter createCtorFast(String namePath) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, namePath, null, FAST_SUPER_CLASS, null);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();//from   w  w w .  j a v a2s  .c  o m
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, FAST_SUPER_CLASS, "<init>", "()V");
    Label line = new Label();
    mv.visitLineNumber(0, line);
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    return cw;
}

From source file:org.jruby.runtime.callback.InvocationCallbackFactory.java

License:LGPL

@Deprecated
private MethodVisitor startCall(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "call", CALL_SIG, null, null);

    mv.visitCode();// w w  w . jav  a  2 s  . c  o m
    Label line = new Label();
    mv.visitLineNumber(0, line);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, typePath);
    return mv;
}

From source file:org.jruby.runtime.callback.InvocationCallbackFactory.java

License:LGPL

@Deprecated
private MethodVisitor startCallS(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "call", CALL_SIG, null, null);

    mv.visitCode();/*  w  w  w  . j  a  v  a  2  s .c o  m*/
    Label line = new Label();
    mv.visitLineNumber(0, line);
    mv.visitVarInsn(ALOAD, 1);
    checkCast(mv, IRubyObject.class);
    return mv;
}

From source file:org.jruby.runtime.callback.InvocationCallbackFactory.java

License:LGPL

@Deprecated
private MethodVisitor startCallFast(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "call", FAST_CALL_SIG, null, null);

    mv.visitCode();//from  w w  w.  j  ava 2  s .  c om
    Label line = new Label();
    mv.visitLineNumber(0, line);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, typePath);
    return mv;
}