Example usage for org.objectweb.asm ClassWriter visitAnnotation

List of usage examples for org.objectweb.asm ClassWriter visitAnnotation

Introduction

In this page you can find the example usage for org.objectweb.asm ClassWriter visitAnnotation.

Prototype

@Override
    public final AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) 

Source Link

Usage

From source file:ataspectj.UnweavableTest.java

License:Open Source License

ISome getJit() {
    ClassWriter cw = new ClassWriter(true, true);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "ataspectj/ISomeGen", null, "java/lang/Object",
            new String[] { "ataspectj/UnweavableTest$ISome" });
    AnnotationVisitor av = cw.visitAnnotation("Lataspectj/UnweavableTest$ASome;", true);
    av.visitEnd();// w w  w.ja v a  2 s. c om

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "giveOne", "()I", null, new String[0]);
    mv.visitInsn(Opcodes.ICONST_2);
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(0, 0);
    cw.visitEnd();

    try {
        ClassLoader loader = this.getClass().getClassLoader();
        Method def = ClassLoader.class.getDeclaredMethod("defineClass",
                new Class[] { String.class, byte[].class, int.class, int.class });
        def.setAccessible(true);
        Class gen = (Class) def.invoke(loader, "ataspectj.ISomeGen", cw.toByteArray(), 0,
                cw.toByteArray().length);
        return (ISome) gen.newInstance();
    } catch (Throwable t) {
        fail(t.toString());
        return null;
    }
}

From source file:ch.raffael.contracts.processor.pmap.ParameterMapAnnotationProcessor.java

License:Apache License

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Building parameter maps");
    for (Element elem : roundEnv.getRootElements()) {
        if (elem.getKind() == ElementKind.CLASS || elem.getKind() == ElementKind.INTERFACE) {
            // FIXME: enums & enum constants
            try {
                TypeElement typeElem = (TypeElement) elem;
                processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
                        "Processing: " + ((TypeElement) elem).getQualifiedName());
                String className = typeElem.getQualifiedName().toString();
                if (className.endsWith(CONTRACTS_CLASS_SUFFIX)) {
                    continue;
                }//from  w ww  . ja v  a  2 s. c  o m
                String ctrClassName = className + CONTRACTS_CLASS_SUFFIX;
                JavaFileObject contractsFile = processingEnv.getFiler().createClassFile(ctrClassName, elem);
                ClassWriter classWriter = new ClassWriter(0);
                classWriter.visit(V1_7, ACC_PUBLIC + ACC_SUPER, toInternalName(ctrClassName), null, null, null);
                MethodVisitor ctor = classWriter.visitMethod(ACC_PRIVATE, "<init>", "()V", null, null);
                ctor.visitCode();
                ctor.visitLabel(new Label());
                ctor.visitEnd();
                //classWriter.visitOuterClass(toInternalName(className), null, null);
                AnnotationVisitor parameterMap = classWriter
                        .visitAnnotation(Type.getDescriptor(ParameterMap.class), false);
                writeType(parameterMap, typeElem, null);
                parameterMap.visitEnd();
                classWriter.visitEnd();
                try (OutputStream output = contractsFile.openOutputStream()) {
                    output.write(classWriter.toByteArray());
                }
            } catch (IOException e) {
                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getLocalizedMessage());
            }
        }
    }
    return false;
}

From source file:co.cask.cdap.internal.app.runtime.service.http.HttpHandlerGenerator.java

License:Apache License

/**
 * Inspects the given type and copy/rewrite handler methods from it into the newly generated class.
 *
 * @param delegateType The user handler type
 * @param inspectType The type that needs to be inspected. It's either the delegateType or one of its parents
 *///  w w  w  .ja  va2s.c  om
private void inspectHandler(final TypeToken<?> delegateType, final TypeToken<?> inspectType,
        final String pathPrefix, final Type classType, final ClassWriter classWriter,
        final List<Class<?>> preservedClasses) throws IOException {
    Class<?> rawType = inspectType.getRawType();

    // Visit the delegate class, copy and rewrite handler method, with method body just do delegation
    try (InputStream sourceBytes = rawType.getClassLoader()
            .getResourceAsStream(Type.getInternalName(rawType) + ".class")) {
        ClassReader classReader = new ClassReader(sourceBytes);
        classReader.accept(new ClassVisitor(Opcodes.ASM5) {

            // Only need to visit @Path at the class level if we are inspecting the user handler class
            private final boolean inspectDelegate = delegateType.equals(inspectType);
            private boolean visitedPath = !inspectDelegate;

            @Override
            public void visit(int version, int access, String name, String signature, String superName,
                    String[] interfaces) {
                super.visit(version, access, name, signature, superName, interfaces);
            }

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                // Copy the class annotation if it is @Path. Only do it for one time
                Type type = Type.getType(desc);
                if (inspectDelegate && type.equals(Type.getType(Path.class))) {
                    visitedPath = true;
                    AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(desc, visible);
                    return new AnnotationVisitor(Opcodes.ASM5, annotationVisitor) {
                        @Override
                        public void visit(String name, Object value) {
                            // "value" is the key for the Path annotation string.
                            if (name.equals("value")) {
                                super.visit(name, pathPrefix + value.toString());
                            } else {
                                super.visit(name, value);
                            }
                        }
                    };

                } else {
                    return super.visitAnnotation(desc, visible);
                }
            }

            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                    String[] exceptions) {
                // Create a class-level annotation with the prefix, if the user has not specified any class-level
                // annotation.
                if (!visitedPath) {
                    String pathDesc = Type.getType(Path.class).getDescriptor();
                    AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(pathDesc, true);
                    annotationVisitor.visit("value", pathPrefix);
                    annotationVisitor.visitEnd();
                    visitedPath = true;
                }

                // Copy the method if it is public and annotated with one of the HTTP request method
                MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
                if (!Modifier.isPublic(access)) {
                    return mv;
                }
                return new HandlerMethodVisitor(delegateType, mv, desc, signature, access, name, exceptions,
                        classType, classWriter, preservedClasses);
            }
        }, ClassReader.SKIP_DEBUG);
    }
}

From source file:com.facebook.buck.java.abi.AnnotationMirror.java

License:Apache License

public void appendTo(ClassWriter writer) {
    AnnotationVisitor visitor = writer.visitAnnotation(desc, visible);
    visitValues(visitor);
    visitor.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();//from www  .j a  v a 2s .c  om

    // 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();//  w  w  w.j a v a 2 s  .c  o  m
    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();//  w w  w .  ja va 2 s  . c  om

    // 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:org.apache.cxf.jaxws.WrapperClassGenerator.java

License:Apache License

private void createWrapperClass(MessagePartInfo wrapperPart, MessageInfo messageInfo, OperationInfo op,
        Method method, boolean isRequest) {

    QName wrapperElement = messageInfo.getName();

    boolean anonymous = factory.getAnonymousWrapperTypes();

    ClassWriter cw = createClassWriter();
    String pkg = getPackageName(method) + ".jaxws_asm" + (anonymous ? "_an" : "");
    String className = pkg + "." + StringUtils.capitalize(op.getName().getLocalPart());
    if (!isRequest) {
        className = className + "Response";
    }//w w  w . j ava2  s .  com
    String pname = pkg + ".package-info";
    Class<?> def = findClass(pname, method.getDeclaringClass());
    if (def == null) {
        generatePackageInfo(pname, wrapperElement.getNamespaceURI(), method.getDeclaringClass());
    }

    def = findClass(className, method.getDeclaringClass());
    if (def != null) {
        wrapperPart.setTypeClass(def);
        wrapperBeans.add(def);
        return;
    }
    String classFileName = periodToSlashes(className);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, classFileName, null, "java/lang/Object",
            null);

    AnnotationVisitor av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlRootElement;", true);
    av0.visit("name", wrapperElement.getLocalPart());
    av0.visit("namespace", wrapperElement.getNamespaceURI());
    av0.visitEnd();

    av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlAccessorType;", true);
    av0.visitEnum("value", "Ljavax/xml/bind/annotation/XmlAccessType;", "FIELD");
    av0.visitEnd();

    av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlType;", true);
    if (!anonymous) {
        av0.visit("name", wrapperElement.getLocalPart());
        av0.visit("namespace", wrapperElement.getNamespaceURI());
    } else {
        av0.visit("name", "");
    }
    av0.visitEnd();

    // add constructor
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label lbegin = new Label();
    mv.visitLabel(lbegin);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    Label lend = new Label();
    mv.visitLabel(lend);
    mv.visitLocalVariable("this", "L" + classFileName + ";", null, lbegin, lend, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    for (MessagePartInfo mpi : messageInfo.getMessageParts()) {
        generateMessagePart(cw, mpi, method, classFileName);
    }

    cw.visitEnd();

    Class<?> clz = loadClass(className, method.getDeclaringClass(), cw.toByteArray());
    wrapperPart.setTypeClass(clz);
    wrapperBeans.add(clz);
}

From source file:org.apache.cxf.jaxws.WrapperClassGenerator.java

License:Apache License

private void generatePackageInfo(String className, String ns, Class clz) {
    ClassWriter cw = createClassWriter();
    String classFileName = periodToSlashes(className);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_ABSTRACT + Opcodes.ACC_INTERFACE, classFileName, null,
            "java/lang/Object", null);

    boolean q = qualified;
    SchemaInfo si = interfaceInfo.getService().getSchema(ns);
    if (si != null) {
        q = si.isElementFormQualified();
    }//w  ww  .  j a  v  a2s . c o  m
    AnnotationVisitor av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlSchema;", true);
    av0.visit("namespace", ns);
    av0.visitEnum("elementFormDefault", getClassCode(XmlNsForm.class), q ? "QUALIFIED" : "UNQUALIFIED");
    av0.visitEnd();
    cw.visitEnd();

    loadClass(className, clz, cw.toByteArray());
}

From source file:org.apache.deltaspike.partialbean.impl.proxy.AsmProxyClassGenerator.java

License:Apache License

private static byte[] generateProxyClassBytes(Class<?> targetClass,
        Class<? extends InvocationHandler> invocationHandlerClass, String proxyName,
        java.lang.reflect.Method[] redirectMethods, java.lang.reflect.Method[] interceptionMethods) {
    Class<?> superClass = targetClass;
    String[] interfaces = new String[] {};

    if (targetClass.isInterface()) {
        superClass = Object.class;
        interfaces = new String[] { Type.getInternalName(targetClass) };
    }//from  w w w  .  j a va 2s .  c o  m

    // add PartialBeanProxy as interface
    interfaces = Arrays.copyOf(interfaces, interfaces.length + 1);
    interfaces[interfaces.length - 1] = Type.getInternalName(PartialBeanProxy.class);

    Type superType = Type.getType(superClass);
    Type proxyType = Type.getObjectType(proxyName);
    Type invocationHandlerType = Type.getType(invocationHandlerClass);

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, proxyType.getInternalName(), null,
            superType.getInternalName(), interfaces);

    // copy annotations
    for (Annotation annotation : targetClass.getDeclaredAnnotations()) {
        cw.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true).visitEnd();
    }

    defineInvocationHandlerField(cw, invocationHandlerType);
    defineConstructor(cw, proxyType, superType);
    definePartialBeanProxyMethods(cw, proxyType, invocationHandlerType);

    for (java.lang.reflect.Method method : redirectMethods) {
        defineMethod(cw, method, proxyType, invocationHandlerType, superType, true);
    }

    for (java.lang.reflect.Method method : interceptionMethods) {
        defineMethod(cw, method, proxyType, invocationHandlerType, superType, false);
    }

    return cw.toByteArray();
}