Search in sources :

Example 6 with MethodVisitor

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

the class ASMClassWriter method createGetCommandResourcePaths.

@Override
public void createGetCommandResourcePaths(List<CommandResourceMetaData> commandMetaData) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getCommandResourcesPaths", "()[[Ljava/lang/String;", null, null);
    mv.visitCode();
    int size = 0;
    for (CommandResourceMetaData metaData : commandMetaData) {
        if (ResourceUtil.commandIsPresent(habitat, metaData.command)) {
            size++;
        }
    }
    // 11 number of entries
    mv.visitIntInsn(BIPUSH, size);
    // first outer array
    mv.visitTypeInsn(ANEWARRAY, "[Ljava/lang/String;");
    int index = -1;
    for (CommandResourceMetaData metaData : commandMetaData) {
        if (ResourceUtil.commandIsPresent(habitat, metaData.command)) {
            index++;
            switch(// inner array has 3 strings,
            index) {
                case 0:
                    mv.visitInsn(DUP);
                    mv.visitInsn(ICONST_0);
                    mv.visitInsn(ICONST_3);
                    break;
                case 1:
                    mv.visitInsn(DUP);
                    mv.visitInsn(ICONST_1);
                    mv.visitInsn(ICONST_3);
                    break;
                case 2:
                    mv.visitInsn(DUP);
                    mv.visitInsn(ICONST_2);
                    mv.visitInsn(ICONST_3);
                    break;
                case 3:
                    mv.visitInsn(DUP);
                    mv.visitInsn(ICONST_3);
                    mv.visitInsn(ICONST_3);
                    break;
                case 4:
                    mv.visitInsn(DUP);
                    mv.visitInsn(ICONST_4);
                    mv.visitInsn(ICONST_3);
                    break;
                case 5:
                    mv.visitInsn(DUP);
                    mv.visitInsn(ICONST_5);
                    mv.visitInsn(ICONST_3);
                    break;
                // 6 and bigger is DIFFERENT!!!
                default:
                    mv.visitInsn(DUP);
                    mv.visitIntInsn(BIPUSH, index);
                    mv.visitInsn(ICONST_3);
                    break;
            }
            // switch
            // inner array
            mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
            mv.visitInsn(DUP);
            mv.visitInsn(ICONST_0);
            mv.visitLdcInsn(metaData.resourcePath);
            mv.visitInsn(AASTORE);
            mv.visitInsn(DUP);
            mv.visitInsn(ICONST_1);
            mv.visitLdcInsn(metaData.httpMethod);
            mv.visitInsn(AASTORE);
            mv.visitInsn(DUP);
            mv.visitInsn(ICONST_2);
            mv.visitLdcInsn(metaData.command);
            mv.visitInsn(AASTORE);
            mv.visitInsn(AASTORE);
        }
    }
    // for
    mv.visitInsn(ARETURN);
    mv.visitMaxs(7, 1);
    mv.visitEnd();
}
Also used : MethodVisitor(org.glassfish.hk2.external.org.objectweb.asm.MethodVisitor)

Example 7 with MethodVisitor

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

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

the class ASMClassWriter method createCustomResourceMapping.

@Override
public void createCustomResourceMapping(String resourceClassName, String mappingPath) {
    // gen in custom package!
    String completeName = "org/glassfish/admin/rest/resources/custom/" + resourceClassName;
    String baseClassName = generatedPath + className;
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "get" + resourceClassName, "()L" + completeName + ";", null, null);
    RestLogging.restLogger.log(Level.FINE, "Creating resource with path {0} (2)", mappingPath);
    AnnotationVisitor av0 = mv.visitAnnotation("Ljavax/ws/rs/Path;", true);
    av0.visit("value", mappingPath + "/");
    av0.visitEnd();
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, baseClassName, INJECTOR_FIELD, FORNAME_INJECTOR_TYPE);
    mv.visitLdcInsn(Type.getType("L" + completeName + ";"));
    mv.visitMethodInsn(INVOKEINTERFACE, INTERFACE_INJECTOR_TYPE, CREATE_AND_INITIALIZE, CREATE_AND_INITIALIZE_SIG);
    mv.visitTypeInsn(CHECKCAST, completeName);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, baseClassName, "getEntity", "()Lorg/jvnet/hk2/config/Dom;");
    mv.visitMethodInsn(INVOKEVIRTUAL, completeName, "setEntity", "(Lorg/jvnet/hk2/config/Dom;)V");
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}
Also used : AnnotationVisitor(org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor) MethodVisitor(org.glassfish.hk2.external.org.objectweb.asm.MethodVisitor)

Example 9 with MethodVisitor

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

the class ASMClassWriter method createGetCommandResource.

@Override
public void createGetCommandResource(String commandResourceClassName, String resourcePath) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "get" + commandResourceClassName, "()L" + generatedPath + commandResourceClassName + ";", null, null);
    RestLogging.restLogger.log(Level.FINE, "Creating resource with path {0} (3)", resourcePath);
    AnnotationVisitor av0 = mv.visitAnnotation("Ljavax/ws/rs/Path;", true);
    av0.visit("value", resourcePath + "/");
    av0.visitEnd();
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, generatedPath + className, INJECTOR_FIELD, FORNAME_INJECTOR_TYPE);
    mv.visitLdcInsn(Type.getType("L" + generatedPath + commandResourceClassName + ";"));
    mv.visitMethodInsn(INVOKEINTERFACE, INTERFACE_INJECTOR_TYPE, CREATE_AND_INITIALIZE, CREATE_AND_INITIALIZE_SIG);
    mv.visitTypeInsn(CHECKCAST, generatedPath + commandResourceClassName);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}
Also used : AnnotationVisitor(org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor) MethodVisitor(org.glassfish.hk2.external.org.objectweb.asm.MethodVisitor)

Example 10 with MethodVisitor

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

the class ASMClassWriter method createGetPostCommand.

@Override
public void createGetPostCommand(String commandName) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getPostCommand", "()Ljava/lang/String;", null, null);
    mv.visitCode();
    mv.visitLdcInsn(commandName);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}
Also used : MethodVisitor(org.glassfish.hk2.external.org.objectweb.asm.MethodVisitor)

Aggregations

MethodVisitor (org.glassfish.hk2.external.org.objectweb.asm.MethodVisitor)13 AnnotationVisitor (org.glassfish.hk2.external.org.objectweb.asm.AnnotationVisitor)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JsonString (javax.json.JsonString)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 ParameterMap (org.glassfish.api.admin.ParameterMap)2 JsonObject (javax.json.JsonObject)1