Search in sources :

Example 1 with AnnotationVisitor

use of org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor in project Payara by payara.

the class ASMClassWriter method createGetChildResourceForListResources.

@Override
public void createGetChildResourceForListResources(String keyAttributeName, String childResourceClassName) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "get" + childResourceClassName, "(Ljava/lang/String;)L" + generatedPath + childResourceClassName + ";", null, null);
    RestLogging.restLogger.log(Level.FINE, "Creating resource with path {0} (5)", keyAttributeName);
    AnnotationVisitor av0 = mv.visitAnnotation("Ljavax/ws/rs/Path;", true);
    av0.visit("value", "{" + keyAttributeName + "}/");
    av0.visitEnd();
    av0 = mv.visitParameterAnnotation(0, "Ljavax/ws/rs/PathParam;", true);
    av0.visit("value", keyAttributeName);
    av0.visitEnd();
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, generatedPath + "List" + childResourceClassName, INJECTOR_FIELD, FORNAME_INJECTOR_TYPE);
    mv.visitLdcInsn(Type.getType("L" + generatedPath + childResourceClassName + ";"));
    mv.visitMethodInsn(INVOKEINTERFACE, INTERFACE_INJECTOR_TYPE, CREATE_AND_INITIALIZE, CREATE_AND_INITIALIZE_SIG);
    mv.visitTypeInsn(CHECKCAST, generatedPath + childResourceClassName);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, generatedPath + "List" + childResourceClassName, "entity", "Ljava/util/List;");
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, generatedPath + "List" + childResourceClassName, "tagName", "Ljava/lang/String;");
    mv.visitMethodInsn(INVOKEVIRTUAL, generatedPath + childResourceClassName, "setBeanByKey", "(Ljava/util/List;Ljava/lang/String;Ljava/lang/String;)V");
    mv.visitVarInsn(ALOAD, 2);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 3);
    mv.visitEnd();
}
Also used : AnnotationVisitor(org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor) MethodVisitor(org.glassfish.hk2.external.org.objectweb.asm.MethodVisitor)

Example 2 with AnnotationVisitor

use of org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor in project Payara by payara.

the class BtraceClientGenerator method generateBtraceClientClassData.

public static byte[] generateBtraceClientClassData(int clientID, Collection<FlashlightProbe> probes) {
    // create a unique name.  It does not matter what the name is.
    String generatedClassName = "com/sun/btrace/flashlight/BTrace_Flashlight_" + clientID;
    // Start of writing a class using ASM, which will be our BTrace Client
    int cwFlags = ClassWriter.COMPUTE_FRAMES + ClassWriter.COMPUTE_MAXS;
    ClassWriter cw = new ClassWriter(cwFlags);
    // Define the access identifiers for the BTrace Client class
    int access = Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL;
    cw.visit(Opcodes.V1_5, access, generatedClassName, null, "java/lang/Object", null);
    // Need a @OnMethod annotation, so prepare your Annotation Visitor for that
    cw.visitAnnotation("Lcom/sun/btrace/annotations/BTrace;", true);
    // Iterate through the probes, so you will create one method for each probe
    int methodCounter = 0;
    for (FlashlightProbe probe : probes) {
        // Preparing the class method header and params (type) for @OnMethod annotation
        StringBuilder typeDesc = new StringBuilder("void ");
        StringBuilder methodDesc = new StringBuilder("void __");
        methodDesc.append(probe.getProviderJavaMethodName()).append("__");
        methodDesc.append(clientID).append("_").append(methodCounter).append("_");
        methodDesc.append("(");
        typeDesc.append("(");
        String delim = "";
        String typeDelim = "";
        Class[] paramTypes = probe.getParamTypes();
        for (int index = 0; index < paramTypes.length; index++) {
            Class paramType = paramTypes[index];
            methodDesc.append(delim).append(paramType.getName());
            // Dont add the param type for type desc, if self is the first index
            if (!(probe.hasSelf() && (index == 0))) {
                typeDesc.append(typeDelim).append(paramType.getName());
                typeDelim = ",";
            }
            delim = ", ";
        }
        methodDesc.append(")");
        typeDesc.append(")");
        // Creating the class method
        Method m = Method.getMethod(methodDesc.toString());
        GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, m, null, null, cw);
        // Add the @Self annotation
        if (probe.hasSelf()) {
            String[] paramNames = probe.getProbeParamNames();
            for (int index = 0; index < paramNames.length; index++) {
                if (paramNames[index].equalsIgnoreCase(FlashlightProbe.SELF)) {
                    AnnotationVisitor paramVisitor = gen.visitParameterAnnotation(index, "Lcom/sun/btrace/annotations/Self;", true);
                    paramVisitor.visitEnd();
                }
            }
        }
        // Add the @OnMethod annotation to this method
        AnnotationVisitor av = gen.visitAnnotation("Lcom/sun/btrace/annotations/OnMethod;", true);
        av.visit("clazz", "" + probe.getProviderClazz().getName());
        av.visit("method", probe.getProviderJavaMethodName());
        av.visit("type", typeDesc.toString());
        av.visitEnd();
        // Add the body
        gen.push(probe.getId());
        gen.loadArgArray();
        gen.invokeStatic(Type.getType(ProbeRegistry.class), Method.getMethod("void invokeProbe(int, Object[])"));
        gen.returnValue();
        gen.endMethod();
        methodCounter++;
    }
    BtraceClientGenerator.generateConstructor(cw);
    cw.visitEnd();
    byte[] classData = cw.toByteArray();
    writeClass(classData, generatedClassName);
    return classData;
}
Also used : FlashlightProbe(org.glassfish.flashlight.provider.FlashlightProbe) ProbeRegistry(org.glassfish.flashlight.provider.ProbeRegistry) Method(org.glassfish.hk2.external.org.objectweb.asm.commons.Method) ClassWriter(org.glassfish.hk2.external.org.objectweb.asm.ClassWriter) AnnotationVisitor(org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor) GeneratorAdapter(org.glassfish.hk2.external.org.objectweb.asm.commons.GeneratorAdapter)

Example 3 with AnnotationVisitor

use of org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor in project Payara by payara.

the class AttributeMethodVisitor method visitAnnotation.

/**
 * Visits an annotation of this method.
 *
 * @param desc the class descriptor of the annotation class.
 * @param visible <tt>true</tt> if the annotation is visible at runtime.
 *
 * @return a visitor to visit the annotation values, or <tt>null</tt> if this visitor is not interested in visiting
 *         this annotation.
 */
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
    duckTyped |= "Lorg/jvnet/hk2/config/DuckTyped;".equals(desc);
    AnnotationVisitor visitor = null;
    if ("Lorg/jvnet/hk2/config/Attribute;".equals(desc) || "Lorg/jvnet/hk2/config/Element;".equals(desc)) {
        try {
            final Class<?> configurable = Thread.currentThread().getContextClassLoader().loadClass(def.getDef());
            final Attribute annotation = configurable.getMethod(name).getAnnotation(Attribute.class);
            def.addAttribute(name, annotation);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    } else if ("Lorg/glassfish/api/admin/config/PropertiesDesc;".equals(desc)) {
        try {
            final Class<?> configurable = Thread.currentThread().getContextClassLoader().loadClass(def.getDef());
            final PropertiesDesc annotation = configurable.getMethod(name).getAnnotation(PropertiesDesc.class);
            final PropertyDesc[] propertyDescs = annotation.props();
            for (PropertyDesc prop : propertyDescs) {
                def.addProperty(prop);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    return visitor;
}
Also used : PropertiesDesc(org.glassfish.api.admin.config.PropertiesDesc) Attribute(org.jvnet.hk2.config.Attribute) AnnotationVisitor(org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor) PropertyDesc(org.glassfish.api.admin.config.PropertyDesc)

Example 4 with AnnotationVisitor

use of org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor in project Payara by payara.

the class CompositeUtil method visitClass.

/**
 * This method starts the class definition, adding the JAX-B annotations to allow for marshalling via JAX-RS
 */
private void visitClass(ClassWriter classWriter, String className, Set<Class<?>> ifaces, Map<String, Map<String, Object>> properties) {
    String[] ifaceNames = new String[ifaces.size() + 1];
    int i = 1;
    ifaceNames[0] = getInternalName(RestModel.class.getName());
    for (Class<?> iface : ifaces) {
        ifaceNames[i++] = iface.getName().replace(".", "/");
    }
    className = getInternalName(className);
    classWriter.visit(V1_6, ACC_PUBLIC + ACC_SUPER, className, null, "org/glassfish/admin/rest/composite/RestModelImpl", ifaceNames);
    // Add @XmlRootElement
    classWriter.visitAnnotation("Ljavax/xml/bind/annotation/XmlRootElement;", true).visitEnd();
    // Add @XmlAccessType
    AnnotationVisitor annotation = classWriter.visitAnnotation("Ljavax/xml/bind/annotation/XmlAccessorType;", true);
    annotation.visitEnum("value", "Ljavax/xml/bind/annotation/XmlAccessType;", "FIELD");
    annotation.visitEnd();
}
Also used : AnnotationVisitor(org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor) JsonString(javax.json.JsonString)

Example 5 with AnnotationVisitor

use of org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor in project Payara by payara.

the class CompositeUtil method createGettersAndSetters.

/**
 * Create getters and setters for the given field
 */
private void createGettersAndSetters(ClassWriter cw, Class c, String className, String name, Map<String, Object> props) {
    Class<?> type = (Class<?>) props.get("type");
    String internalType = getInternalTypeString(type);
    className = getInternalName(className);
    // Create the getter
    MethodVisitor getter = cw.visitMethod(ACC_PUBLIC, "get" + name, "()" + internalType, null, null);
    getter.visitCode();
    getter.visitVarInsn(ALOAD, 0);
    getter.visitFieldInsn(GETFIELD, className, getPropertyName(name), internalType);
    getter.visitInsn(type.isPrimitive() ? Primitive.getPrimitive(internalType).getReturnOpcode() : ARETURN);
    getter.visitMaxs(0, 0);
    getter.visitEnd();
    Map<String, Map<String, Object>> annotations = (Map<String, Map<String, Object>>) props.get("annotations");
    if (annotations != null) {
        for (Map.Entry<String, Map<String, Object>> entry : annotations.entrySet()) {
            String annotationClass = entry.getKey();
            Map<String, Object> annotationValues = entry.getValue();
            AnnotationVisitor av = getter.visitAnnotation("L" + getInternalName(annotationClass) + ";", true);
            for (Map.Entry<String, Object> values : annotationValues.entrySet()) {
                final String paramName = values.getKey();
                Object paramValue = values.getValue();
                if (Class.class.isAssignableFrom(paramValue.getClass())) {
                    paramValue = org.glassfish.hk2.external.org.objectweb.asm.Type.getType("L" + getInternalName(paramValue.getClass().getName()) + ";");
                }
                if (paramValue.getClass().isArray() && (Array.getLength(paramValue) == 0)) {
                    continue;
                }
                av.visit(paramName, paramValue);
            }
            av.visitEnd();
        }
    }
    // Create the setter
    MethodVisitor setter = cw.visitMethod(ACC_PUBLIC, "set" + name, "(" + internalType + ")V", null, null);
    setter.visitCode();
    setter.visitVarInsn(ALOAD, 0);
    setter.visitVarInsn(type.isPrimitive() ? Primitive.getPrimitive(internalType).getSetOpCode() : ALOAD, 1);
    setter.visitFieldInsn(PUTFIELD, className, getPropertyName(name), internalType);
    setter.visitVarInsn(ALOAD, 0);
    setter.visitLdcInsn(name);
    setter.visitMethodInsn(INVOKEVIRTUAL, className, "fieldSet", "(Ljava/lang/String;)V");
    setter.visitInsn(RETURN);
    setter.visitMaxs(0, 0);
    setter.visitEnd();
}
Also used : AnnotationVisitor(org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) Map(java.util.Map) ParameterMap(org.glassfish.api.admin.ParameterMap) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MethodVisitor(org.glassfish.hk2.external.org.objectweb.asm.MethodVisitor)

Aggregations

AnnotationVisitor (org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor)8 MethodVisitor (org.glassfish.hk2.external.org.objectweb.asm.MethodVisitor)5 JsonString (javax.json.JsonString)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JsonObject (javax.json.JsonObject)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 ParameterMap (org.glassfish.api.admin.ParameterMap)1 PropertiesDesc (org.glassfish.api.admin.config.PropertiesDesc)1 PropertyDesc (org.glassfish.api.admin.config.PropertyDesc)1 FlashlightProbe (org.glassfish.flashlight.provider.FlashlightProbe)1 ProbeRegistry (org.glassfish.flashlight.provider.ProbeRegistry)1 ClassWriter (org.glassfish.hk2.external.org.objectweb.asm.ClassWriter)1 GeneratorAdapter (org.glassfish.hk2.external.org.objectweb.asm.commons.GeneratorAdapter)1 Method (org.glassfish.hk2.external.org.objectweb.asm.commons.Method)1 Attribute (org.jvnet.hk2.config.Attribute)1