Search in sources :

Example 6 with ClassWriter

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

the class ProviderImplGenerator method generateConstructor.

private void generateConstructor(ClassWriter cw, String generatedClassName, FlashlightProbeProvider provider) {
    Method m = Method.getMethod("void <init> ()");
    GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, cw);
    gen.loadThis();
    gen.invokeConstructor(Type.getType(Object.class), m);
    Type probeRegType = Type.getType(ProbeRegistry.class);
    Type probeType = Type.getType(FlashlightProbe.class);
    gen.loadThis();
    for (FlashlightProbe probe : provider.getProbes()) {
        gen.dup();
        String fieldName = "_flashlight_" + probe.getProbeName();
        gen.push(probe.getId());
        gen.invokeStatic(probeRegType, Method.getMethod("org.glassfish.flashlight.provider.FlashlightProbe getProbeById(int)"));
        gen.visitFieldInsn(Opcodes.PUTFIELD, generatedClassName, fieldName, probeType.getDescriptor());
    }
    gen.pop();
    // return the value from constructor
    gen.returnValue();
    gen.endMethod();
}
Also used : Type(org.glassfish.hk2.external.org.objectweb.asm.Type) FlashlightProbe(org.glassfish.flashlight.provider.FlashlightProbe) GeneratorAdapter(org.glassfish.hk2.external.org.objectweb.asm.commons.GeneratorAdapter) Method(org.glassfish.hk2.external.org.objectweb.asm.commons.Method)

Example 7 with ClassWriter

use of org.glassfish.hk2.external.org.objectweb.asm.ClassWriter 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 8 with ClassWriter

use of org.glassfish.hk2.external.org.objectweb.asm.ClassWriter 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)

Example 9 with ClassWriter

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

the class CompositeUtil method createField.

/**
 * Add the field to the class, adding the @XmlAttribute annotation for marshalling purposes.
 */
private void createField(ClassWriter cw, String name, Class<?> type) {
    String internalType = getInternalTypeString(type);
    FieldVisitor field = cw.visitField(ACC_PRIVATE, getPropertyName(name), internalType, null, null);
    field.visitAnnotation("Ljavax/xml/bind/annotation/XmlAttribute;", true).visitEnd();
    field.visitEnd();
}
Also used : JsonString(javax.json.JsonString) FieldVisitor(org.glassfish.hk2.external.org.objectweb.asm.FieldVisitor)

Aggregations

JsonString (javax.json.JsonString)5 GeneratorAdapter (org.glassfish.hk2.external.org.objectweb.asm.commons.GeneratorAdapter)4 Method (org.glassfish.hk2.external.org.objectweb.asm.commons.Method)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)3 ParameterMap (org.glassfish.api.admin.ParameterMap)3 FlashlightProbe (org.glassfish.flashlight.provider.FlashlightProbe)3 AnnotationVisitor (org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor)3 ClassWriter (org.glassfish.hk2.external.org.objectweb.asm.ClassWriter)3 JsonObject (javax.json.JsonObject)2 MethodVisitor (org.glassfish.hk2.external.org.objectweb.asm.MethodVisitor)2 Type (org.glassfish.hk2.external.org.objectweb.asm.Type)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 JsonException (javax.json.JsonException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1