Example usage for org.objectweb.asm MethodVisitor visitEnd

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

Introduction

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

Prototype

public void visitEnd() 

Source Link

Document

Visits the end of the method.

Usage

From source file:edu.illinois.nondex.instr.WeakHashMapShufflingAdder.java

License:Open Source License

public void addNextEntry() {
    MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC, "nextEntry", "()Ljava/util/WeakHashMap$Entry;",
            "()Ljava/util/WeakHashMap$Entry<TK;TV;>;", null);
    mv.visitCode();/*from  ww  w.  j  a va2s  .  c om*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "this$0",
            "Ljava/util/WeakHashMap;");
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap", "modCount", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "expectedModCount", "I");
    Label l0 = new Label();
    mv.visitJumpInsn(Opcodes.IF_ICMPEQ, l0);
    mv.visitTypeInsn(Opcodes.NEW, "java/util/ConcurrentModificationException");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ConcurrentModificationException", "<init>", "()V",
            false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitLabel(l0);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "iter", "Ljava/util/Iterator;");
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true);
    mv.visitTypeInsn(Opcodes.CHECKCAST, "java/util/WeakHashMap$Entry");
    mv.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "lastReturned",
            "Ljava/util/WeakHashMap$Entry;");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "lastReturned",
            "Ljava/util/WeakHashMap$Entry;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/WeakHashMap$Entry", "get", "()Ljava/lang/Object;",
            false);
    mv.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "currentKey",
            "Ljava/lang/Object;");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "lastReturned",
            "Ljava/util/WeakHashMap$Entry;");
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:edu.ubc.mirrors.holograms.HologramClassGenerator.java

License:Open Source License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {//  www  .  j  a v a  2  s.  c o m
    this.name = name;
    this.isInterface = (Opcodes.ACC_INTERFACE & access) != 0;
    this.superName = getHologramSuperclassName(isInterface, name, superName);
    interfaces = getHologramInterfaces(name, isInterface, interfaces);

    // Force everything to be public, since HologramClassLoader has to reflectively
    // construct holograms. Again, not a problem because the VM will see the original flags on the ClassMirror instead.
    // Also remove enum flags.
    int hologramAccess = forcePublic(access);
    // Also remove abstract flag. Shouldn't be necessary, but the VM (OpenJDK at least)
    // creates objects that claim to be an instance of VirtualMachineError, which is abstract.
    if (name.equals("hologram/java/lang/VirtualMachineError")) {
        hologramAccess = ~Opcodes.ACC_ABSTRACT & access;
    }

    // We need at least 1.5 to use class literal constants
    // TODO-RS: Work out a better way to interpret 45.X numbers correctly
    if (version == Opcodes.V1_1 || version < Opcodes.V1_5) {
        version = 49;
    }

    super.visit(version, hologramAccess, name, signature, this.superName, interfaces);

    if (this.name.equals(hologramThrowableType.getInternalName())) {
        // Generate aliases for the original superclass' fillInStackTrace and getStackTrace methods,
        // so we can call them in stubs without hitting hologram code.
        MethodVisitor v = super.visitMethod(Opcodes.ACC_PUBLIC, "superFillInStackTrace",
                Type.getMethodDescriptor(Type.VOID_TYPE), null, null);
        v.visitCode();
        v.visitVarInsn(Opcodes.ALOAD, 0);
        v.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Throwable.class), "fillInStackTrace",
                Type.getMethodDescriptor(Type.getType(Throwable.class)));
        v.visitInsn(Opcodes.RETURN);
        v.visitMaxs(1, 1);
        v.visitEnd();

        v = super.visitMethod(Opcodes.ACC_PUBLIC, "superGetStackTrace",
                Type.getMethodDescriptor(Type.getType(StackTraceElement[].class)), null, null);
        v.visitCode();
        v.visitVarInsn(Opcodes.ALOAD, 0);
        v.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Throwable.class), "getStackTrace",
                Type.getMethodDescriptor(Type.getType(StackTraceElement[].class)));
        v.visitInsn(Opcodes.ARETURN);
        v.visitMaxs(1, 1);
        v.visitEnd();
    }
}

From source file:edu.ubc.mirrors.holograms.HologramClassGenerator.java

License:Open Source License

@Override
public void visitEnd() {
    // Generate the static field used to store the corresponding ClassMirror
    int staticAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC;
    super.visitField(staticAccess, "classMirror", classMirrorType.getDescriptor(), null, null);

    // Generate the constructor that takes a mirror instance as an Object parameter
    String constructorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class));
    if (name.equals(getHologramType(Type.getType(Throwable.class), true).getInternalName())) {
        // This doesn't extend ObjectHologram so we have to set the field directly
        super.visitField(Opcodes.ACC_PUBLIC, "mirror", objectMirrorType.getDescriptor(), null, null);

        MethodVisitor methodVisitor = super.visitMethod(Opcodes.ACC_PUBLIC, "<init>", constructorDesc, null,
                null);//w w w  . ja v a2 s .com
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE));
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, objectMirrorType.getInternalName());
        methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, name, "mirror", Type.getDescriptor(ObjectMirror.class));
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(2, 2);
        methodVisitor.visitEnd();

        methodVisitor = super.visitMethod(Opcodes.ACC_PUBLIC, "getMirror",
                Type.getMethodDescriptor(objectMirrorType), null, null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitFieldInsn(Opcodes.GETFIELD, name, "mirror", Type.getDescriptor(ObjectMirror.class));
        methodVisitor.visitInsn(Opcodes.ARETURN);
        methodVisitor.visitMaxs(1, 1);
        methodVisitor.visitEnd();
    } else if (!isInterface) {
        MethodVisitor methodVisitor = super.visitMethod(Opcodes.ACC_PUBLIC, "<init>", constructorDesc, null,
                null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", constructorDesc);
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(2, 2);
        methodVisitor.visitEnd();
    }

    // Add a class initialization method to initialize the static fields,
    // if one doesn't exist already.
    if (!hasClinit) {
        InstructionAdapter mv = new InstructionAdapter(
                super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "<clinit>", "()V", null, null));
        mv.visitCode();
        HologramMethodGenerator.initializeStaticFields(Type.getObjectType(this.name), mv);
        mv.areturn(Type.VOID_TYPE);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }

    super.visitEnd();
}

From source file:edu.ubc.mirrors.holograms.HologramClassGenerator.java

License:Open Source License

public static void generateArray(ClassVisitor visitor, HologramClassLoader loader,
        HologramClassMirror hologramClassMirror) {
    boolean isInterface = !hologramClassMirror.isImplementationClass();
    ClassMirror classMirror = hologramClassMirror.getOriginal();

    Type originalType = Reflection.typeForClassMirror(classMirror);
    Type originalElementType = originalType.getElementType();
    int dims = originalType.getDimensions();

    String internalName = getHologramType(originalType, !isInterface).getInternalName();

    ClassMirror superClassMirror = null;
    String superName = isInterface ? Type.getInternalName(Object.class)
            : Type.getInternalName(ObjectArrayHologram.class);
    Set<String> interfaces = new HashSet<String>();
    int access = Opcodes.ACC_PUBLIC | (isInterface ? Opcodes.ACC_INTERFACE : 0);

    if (originalElementType.getSort() == Type.OBJECT || originalElementType.getSort() == Type.ARRAY) {
        ClassMirror elementClass = loader.loadOriginalClassMirror(originalElementType.getClassName());
        superClassMirror = elementClass.getSuperClassMirror();

        if (isInterface) {
            if (superClassMirror != null) {
                Type superType = Reflection.makeArrayType(dims,
                        Type.getObjectType(superClassMirror.getClassName().replace('.', '/')));
                String superInterfaceName = getHologramType(superType).getInternalName();
                interfaces.add(superInterfaceName);
            }//from  w  w w .jav a  2 s.  co  m

            for (ClassMirror interfaceMirror : elementClass.getInterfaceMirrors()) {
                Type superType = Reflection.makeArrayType(dims,
                        Type.getObjectType(interfaceMirror.getClassName().replace('.', '/')));
                String interfaceName = getHologramType(superType).getInternalName();
                interfaces.add(interfaceName);
            }

            interfaces.add(hologramType.getInternalName());

            Type nMinus1Type = Reflection.makeArrayType(dims - 1, Type.getType(Object.class));
            interfaces.add(getHologramType(nMinus1Type).getInternalName());
        }
    }
    if (!isInterface) {
        interfaces.add(getHologramType(originalType, false).getInternalName());
    }

    visitor.visit(Opcodes.V1_5, access, internalName, null, superName, interfaces.toArray(new String[0]));

    if (isInterface) {
        // Generate clone()
        String cloneDesc = Type.getMethodDescriptor(objectType);
        MethodVisitor mv = visitor.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, "clone", cloneDesc,
                null, null);
        mv.visitEnd();
    } else {
        // Generate thunk constructors
        String initDesc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(ObjectArrayMirror.class));
        MethodVisitor mv = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", initDesc, null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", initDesc);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();

        initDesc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.INT_TYPE);
        mv = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", initDesc, null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ILOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", initDesc);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }

    // Generate the static field used to store the corresponding ClassMirror and the static initializer to set it
    int staticAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC;
    visitor.visitField(staticAccess, "classMirror", classMirrorType.getDescriptor(), null, null);

    InstructionAdapter mv = new InstructionAdapter(
            visitor.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "<clinit>", "()V", null, null));
    mv.visitCode();
    HologramMethodGenerator.initializeStaticFields(Type.getObjectType(internalName), mv);
    mv.areturn(Type.VOID_TYPE);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    visitor.visitEnd();
}

From source file:edu.ubc.mirrors.holograms.MainEntryAdaptor.java

License:Open Source License

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

    MethodVisitor superVisitor = super.visitMethod(access, name, desc, signature, exceptions);
    if (superVisitor != null && name.equals("main") && (Opcodes.ACC_STATIC & access) != 0
            && desc.equals(mainDesc)) {
        superVisitor.visitCode();//from  w ww  . j av  a2  s . c o m
        superVisitor.visitLdcInsn(Type.getObjectType(className));
        superVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        superVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(ObjectHologram.class),
                "invokeHologramMainMethod", invokeHologramMainMethodDesc);
        superVisitor.visitInsn(Opcodes.RETURN);
        superVisitor.visitMaxs(2, 1);
        superVisitor.visitEnd();
        return null;
    } else {
        return superVisitor;
    }
}

From source file:edu.ubc.mirrors.raw.NativeClassGenerator.java

License:Open Source License

@Override
public void visitEnd() {
    if (!isInterface) {
        // Generate the no-argument constructor
        String constructorDesc = Type.getMethodDescriptor(Type.VOID_TYPE);
        MethodVisitor methodVisitor = super.visitMethod(Opcodes.ACC_PUBLIC, "<init>", constructorDesc, null,
                null);// w  ww  .  ja  v a 2s.c o  m
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", constructorDesc);
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(1, 1);
        methodVisitor.visitEnd();
    }

    super.visitEnd();
}

From source file:egovframework.rte.itl.webservice.service.impl.EgovWebServiceClassLoaderImpl.java

License:Apache License

private byte[] createRecordClass(final String className, final RecordType recordType)
        throws ClassNotFoundException {
    String asmClassName = className.replace('.', '/');

    // ClassWriter classWriter = new
    // ClassWriter(0);
    ClassWriter classWriter = new ClassWriter(0);
    classWriter.visit(V1_5, // version
            ACC_PUBLIC, // access
            asmClassName, // name
            null, // signature
            "java/lang/Object", // superName
            null); // interfaces

    // Create Annotation
    AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(DESC_OF_XML_ACCESSOR_TYPE, true);
    annotationVisitor.visitEnum("value", // name
            DESC_OF_XML_ACCESS_TYPE, // desc
            "FIELD"); // value
    annotationVisitor.visitEnd();/*  ww w .  j  a  v  a  2  s  . co  m*/

    // Create Fields
    for (Entry<String, Type> entry : recordType.getFieldTypes().entrySet()) {
        String fieldName = entry.getKey();
        Type fieldType = entry.getValue();

        Class<?> fieldTypeClass = loadClass(fieldType);
        String desc = org.objectweb.asm.Type.getDescriptor(fieldTypeClass);

        classWriter.visitField(ACC_PUBLIC, // access
                fieldName, // name
                desc, // desc
                null, // signature
                null); // value
    }

    // Create Constructor
    MethodVisitor methodVisitor = classWriter.visitMethod(ACC_PUBLIC, // access
            "<init>", // name
            "()V", // desc
            null, // signature
            null); // exceptions
    methodVisitor.visitCode();
    methodVisitor.visitVarInsn(ALOAD, 0);
    methodVisitor.visitMethodInsn(INVOKESPECIAL, // opcode
            "java/lang/Object", // owner
            "<init>", // name
            "()V"); // desc
    methodVisitor.visitInsn(RETURN);
    methodVisitor.visitMaxs(1, 1);
    methodVisitor.visitEnd();

    // Class finalize
    classWriter.visitEnd();

    // try
    // {
    // DataOutputStream dos = new DataOutputStream(
    // new FileOutputStream("EgovType" +
    // recordType.getId() + ".class"));
    // dos.write(classWriter.toByteArray());
    // dos.close();
    // }
    // catch (IOException e)
    // {
    // e.printStackTrace();
    // }

    return classWriter.toByteArray();
}

From source file:egovframework.rte.itl.webservice.service.impl.EgovWebServiceClassLoaderImpl.java

License:Apache License

private byte[] createServiceEndpointInterfaceClass(
        final ServiceEndpointInterfaceInfo serviceEndpointInterfaceInfo) throws ClassNotFoundException {
    // CHECKSTYLE:OFF
    String serviceEndpointInterfaceClassName = getServiceEndpointInterfaceClassName(
            serviceEndpointInterfaceInfo.getServiceName());

    String asmServiceEndpointInterfaceClassName = serviceEndpointInterfaceClassName.replace('.', '/');
    // CHECKSTYLE:ON
    // ClassWriter classWriter = new ClassWriter(false);
    ClassWriter classWriter = new ClassWriter(0);
    classWriter.visit(V1_5, // version
            ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, // access
            asmServiceEndpointInterfaceClassName, // name
            null, // signature
            "java/lang/Object", // superName
            null); // interfaces

    // Create Annotation
    AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(DESC_OF_WEB_SERVICE, true);
    annotationVisitor.visit("targetNamespace", serviceEndpointInterfaceInfo.getNamespace());
    annotationVisitor.visitEnd();//from www  .  ja  va  2 s . c om
    annotationVisitor = classWriter.visitAnnotation(DESC_OF_SOAP_BINDING, true);
    annotationVisitor.visitEnum("parameterStyle", DESC_OF_SOAP_BINDING_PARAMETER_STYLE, "BARE");

    // Create Method
    ServiceParamInfo returnInfo = serviceEndpointInterfaceInfo.getReturnInfo();
    Collection<ServiceParamInfo> paramInfos = serviceEndpointInterfaceInfo.getParamInfos();

    StringBuffer desc = new StringBuffer("(");
    StringBuffer signature = new StringBuffer("(");

    for (ServiceParamInfo info : paramInfos) {
        Class<?> paramClass = loadClass(info.getType());
        org.objectweb.asm.Type paramType = org.objectweb.asm.Type.getType(paramClass);
        String paramSign = paramType.getDescriptor();
        if (info.getMode().equals(OUT) || info.getMode().equals(INOUT)) {
            if (paramClass.isPrimitive()) {
                paramClass = wrapperClasses.get(paramClass);
                paramType = org.objectweb.asm.Type.getType(paramClass);
                paramSign = paramType.getDescriptor();
            }
            paramClass = Holder.class;
            paramType = TYPE_OF_HOLDER;
            paramSign = "Ljavax/xml/ws/Holder<" + paramSign + ">;";
        }
        desc.append(paramType.getDescriptor());
        signature.append(paramSign);
    }
    desc.append(")");
    signature.append(")");
    // CHECKSTYLE:OFF
    org.objectweb.asm.Type returnType = (returnInfo == null ? returnType = org.objectweb.asm.Type.VOID_TYPE
            : org.objectweb.asm.Type.getType(loadClass(returnInfo.getType())));
    // CHECKSTYLE:ON
    desc.append(returnType.getDescriptor());
    signature.append(returnType.getDescriptor());

    MethodVisitor methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, // access
            serviceEndpointInterfaceInfo.getOperationName(), // name
            desc.toString(), // desc
            signature.toString(), // signature
            null); // exceptions

    // @WebMethod
    annotationVisitor = methodVisitor.visitAnnotation(DESC_OF_WEB_METHOD, true);
    annotationVisitor.visit("operationName", serviceEndpointInterfaceInfo.getOperationName());
    annotationVisitor.visitEnd();

    // @WebResult
    if (returnInfo != null) {
        annotationVisitor = methodVisitor.visitAnnotation(DESC_OF_WEB_RESULT, true);
        annotationVisitor.visit("name", returnInfo.getName());
        // annotationVisitor.visit("partName",
        // returnInfo.getName());
        annotationVisitor.visit("header", returnInfo.isHeader());
        annotationVisitor.visit("targetNamespace", serviceEndpointInterfaceInfo.getNamespace());
        annotationVisitor.visitEnd();
    }

    // @WebParam
    int index = 0;
    for (ServiceParamInfo info : serviceEndpointInterfaceInfo.getParamInfos()) {
        annotationVisitor = methodVisitor.visitParameterAnnotation(index, DESC_OF_WEB_PARAM, true);
        annotationVisitor.visit("name", info.getName());
        // annotationVisitor.visit("partName",
        // info.getName());
        annotationVisitor.visitEnum("mode", DESC_OF_WEB_PARAM_MODE, info.getMode().toString());
        annotationVisitor.visit("header", info.isHeader());
        annotationVisitor.visit("targetNamespace", serviceEndpointInterfaceInfo.getNamespace());
        annotationVisitor.visitEnd();
        index++;
    }
    methodVisitor.visitEnd();

    // Class finalize
    classWriter.visitEnd();

    // try
    // {
    // DataOutputStream dos = new DataOutputStream(
    // new FileOutputStream("EgovType" +
    // serviceInfo.getServiceName() + ".class"));
    // dos.write(classWriter.toByteArray());
    // dos.close();
    // }
    // catch (IOException e)
    // {
    // e.printStackTrace();
    // }

    return classWriter.toByteArray();
}

From source file:egovframework.rte.itl.webservice.service.impl.EgovWebServiceClassLoaderImpl.java

License:Apache License

private byte[] createServiceEndpointClass(final ServiceEndpointInfo serviceEndpointInfo)
        throws ClassNotFoundException {
    String serviceEndpointInterfaceClassName = getServiceEndpointInterfaceClassName(
            serviceEndpointInfo.getServiceName());
    String serviceEndpointClassName = getServiceEndpointClassName(serviceEndpointInfo.getServiceName());

    String asmServiceEndpointInterfaceClassName = serviceEndpointInterfaceClassName.replace('.', '/');
    String asmServiceEndpointClassName = serviceEndpointClassName.replace('.', '/');
    // CHECKSTYLE:ON
    // ClassWriter classWriter = new ClassWriter(false);
    ClassWriter classWriter = new ClassWriter(0);
    classWriter.visit(V1_5, // version
            ACC_PUBLIC, // access
            asmServiceEndpointClassName, // name
            null, // signature
            "java/lang/Object", // superName
            new String[] { asmServiceEndpointInterfaceClassName }); // interfaces
    // Create Annotation
    AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(DESC_OF_WEB_SERVICE, true);
    annotationVisitor.visit("endpointInterface", serviceEndpointInterfaceClassName);
    annotationVisitor.visit("targetNamespace", serviceEndpointInfo.getNamespace());
    // annotationVisitor.visit("name",
    // serviceProviderInfo.getServiceName());
    annotationVisitor.visit("serviceName", serviceEndpointInfo.getServiceName());
    annotationVisitor.visit("portName", serviceEndpointInfo.getPortName());
    annotationVisitor.visitEnd();//from w ww .  ja  va  2  s  .com

    // Create Attribute
    FieldVisitor fieldVisitor = classWriter.visitField(ACC_PUBLIC, // access
            FIELD_NAME_OF_SERVICE_BRIDGE, // name
            DESC_OF_SERVICE_BRIDGE_CLASS, // desc
            null, // signature
            null); // value
    fieldVisitor.visitEnd();

    // Create Constructor
    MethodVisitor methodVisitor = classWriter.visitMethod(ACC_PUBLIC, // access
            "<init>", // name
            "()V", // desc
            null, // signature
            null); // exceptions
    methodVisitor.visitCode();
    methodVisitor.visitVarInsn(ALOAD, 0);
    methodVisitor.visitMethodInsn(INVOKESPECIAL, // opcode
            "java/lang/Object", // owner
            "<init>", // name
            "()V"); // desc
    methodVisitor.visitInsn(RETURN);
    methodVisitor.visitMaxs(1, 1);
    methodVisitor.visitEnd();

    // Create Method
    ServiceParamInfo returnInfo = serviceEndpointInfo.getReturnInfo();
    Collection<ServiceParamInfo> paramInfos = serviceEndpointInfo.getParamInfos();

    StringBuffer desc = new StringBuffer("(");
    StringBuffer signature = new StringBuffer("(");

    for (ServiceParamInfo info : paramInfos) {
        Class<?> paramClass = loadClass(info.getType());
        org.objectweb.asm.Type paramType = org.objectweb.asm.Type.getType(paramClass);
        String paramSign = paramType.getDescriptor();
        if (info.getMode().equals(OUT) || info.getMode().equals(INOUT)) {
            if (paramClass.isPrimitive()) {
                paramClass = wrapperClasses.get(paramClass);
                paramType = org.objectweb.asm.Type.getType(paramClass);
                paramSign = paramType.getDescriptor();
            }
            paramClass = Holder.class;
            paramType = TYPE_OF_HOLDER;
            paramSign = "Ljavax/xml/ws/Holder<" + paramSign + ">;";
        }
        desc.append(paramType.getDescriptor());
        signature.append(paramSign);
    }
    desc.append(")");
    signature.append(")");
    // CHECKSTYLE:OFF
    org.objectweb.asm.Type returnType = (returnInfo == null ? returnType = org.objectweb.asm.Type.VOID_TYPE
            : org.objectweb.asm.Type.getType(loadClass(returnInfo.getType())));
    // CHECKSTYLE:ON
    desc.append(returnType.getDescriptor());
    signature.append(returnType.getDescriptor());

    methodVisitor = classWriter.visitMethod(ACC_PUBLIC, // access
            serviceEndpointInfo.getOperationName(), // name
            desc.toString(), // desc
            signature.toString(), // signature
            null); // exceptions

    int mapPosition = paramInfos.size() + 1;
    methodVisitor.visitCode();
    methodVisitor.visitTypeInsn(NEW, "java/util/HashMap");
    methodVisitor.visitInsn(DUP);
    methodVisitor.visitMethodInsn(INVOKESPECIAL, // opcode
            "java/util/HashMap", // owner
            "<init>", // name
            "()V"); // desc
    methodVisitor.visitVarInsn(ASTORE, mapPosition);
    int i = 1;
    for (ServiceParamInfo info : paramInfos) {
        methodVisitor.visitVarInsn(ALOAD, mapPosition);
        methodVisitor.visitLdcInsn(info.getName());
        methodVisitor.visitVarInsn(ALOAD, i);
        methodVisitor.visitMethodInsn(INVOKEINTERFACE, // opcode
                "java/util/Map", // owner
                "put", // name
                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); // desc
        methodVisitor.visitInsn(POP);
        i++;
    }
    methodVisitor.visitVarInsn(ALOAD, 0);
    methodVisitor.visitFieldInsn(GETFIELD, // opcode
            asmServiceEndpointClassName, // owner
            FIELD_NAME_OF_SERVICE_BRIDGE, // name
            DESC_OF_SERVICE_BRIDGE_CLASS); // desc
    methodVisitor.visitVarInsn(ALOAD, mapPosition);
    methodVisitor.visitMethodInsn(INVOKEINTERFACE, // opcode
            NAME_OF_SERVICE_BRIDGE_CLASS, // owner
            "doService", // name
            "(Ljava/util/Map;)Ljava/lang/Object;"); // desc
    if (returnInfo != null) {
        methodVisitor.visitTypeInsn(CHECKCAST, // opcode
                returnType.getInternalName()); // type
        methodVisitor.visitInsn(ARETURN);
    } else {
        methodVisitor.visitInsn(POP);
        methodVisitor.visitInsn(RETURN);
    }
    methodVisitor.visitMaxs(paramInfos.size(), paramInfos.size() + 2);
    methodVisitor.visitEnd();

    // Class finalize
    classWriter.visitEnd();

    // try
    // {
    // DataOutputStream dos = new DataOutputStream(
    // new FileOutputStream("EgovType" +
    // serviceProviderInfo.getServiceName() +
    // "Impl.class"));
    // dos.write(classWriter.toByteArray());
    // dos.close();
    // }
    // catch (IOException e)
    // {
    // e.printStackTrace();
    // }

    return classWriter.toByteArray();
}

From source file:enhancer.examples.generator.aop.internal.LoggingGenerator.java

License:Apache License

private void generateInit(ClassWriter cw, String proxyName, String fieldDesc) {
    /* Generation start */
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + fieldDesc + ")V", null, null);
    mv.visitCode();/*from ww  w. j a v  a2 s  . co m*/

    /* Call super constructor */
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");

    /* Set delegate field */
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, proxyName, DELEGATE, fieldDesc);

    /* Return */
    mv.visitInsn(RETURN);

    /* Generation end */
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}