Search in sources :

Example 1 with Label

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

the class ProviderImplGenerator method generateClassData.

public byte[] generateClassData(FlashlightProbeProvider provider, Class providerClazz, String generatedClassName) {
    Type classType = Type.getType(providerClazz);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("** classType: " + classType);
        logger.fine("** classDesc: " + Type.getDescriptor(providerClazz));
        logger.fine("Generating for: " + generatedClassName);
    }
    generatedClassName = generatedClassName.replace('.', '/');
    int cwFlags = ClassWriter.COMPUTE_FRAMES + ClassWriter.COMPUTE_MAXS;
    ClassWriter cw = new ClassWriter(cwFlags);
    int access = Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL;
    String[] interfaces = new String[] { providerClazz.getName().replace('.', '/') };
    cw.visit(Opcodes.V1_5, access, generatedClassName, null, "java/lang/Object", interfaces);
    for (FlashlightProbe probe : provider.getProbes()) {
        Type probeType = Type.getType(FlashlightProbe.class);
        int fieldAccess = Opcodes.ACC_PUBLIC;
        String fieldName = "_flashlight_" + probe.getProbeName();
        cw.visitField(fieldAccess, fieldName, probeType.getDescriptor(), null, null);
    }
    Type probeType = Type.getType(FlashlightProbe.class);
    for (FlashlightProbe probe : provider.getProbes()) {
        StringBuilder methodDesc = new StringBuilder();
        methodDesc.append("void ").append(probe.getProviderJavaMethodName());
        methodDesc.append("(");
        String delim = "";
        for (Class paramType : probe.getParamTypes()) {
            methodDesc.append(delim).append(paramType.getName());
            delim = ", ";
        }
        methodDesc.append(")");
        Method m = Method.getMethod(methodDesc.toString());
        GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, cw);
        String fieldName = "_flashlight_" + probe.getProbeName();
        gen.loadThis();
        gen.visitFieldInsn(Opcodes.GETFIELD, generatedClassName, fieldName, probeType.getDescriptor());
        int index = gen.newLocal(probeType);
        gen.storeLocal(index);
        gen.loadLocal(index);
        gen.invokeVirtual(probeType, Method.getMethod("boolean isEnabled()"));
        gen.push(true);
        Label enabledLabel = new Label();
        Label notEnabledLabel = new Label();
        gen.ifCmp(Type.getType(boolean.class), GeneratorAdapter.EQ, enabledLabel);
        gen.goTo(notEnabledLabel);
        gen.visitLabel(enabledLabel);
        gen.loadLocal(index);
        gen.loadArgArray();
        gen.invokeVirtual(probeType, Method.getMethod("void fireProbe(Object[])"));
        gen.visitLabel(notEnabledLabel);
        gen.returnValue();
        gen.endMethod();
    }
    generateConstructor(cw, generatedClassName, provider);
    cw.visitEnd();
    byte[] classData = cw.toByteArray();
    int index = generatedClassName.lastIndexOf('.');
    String clsName = generatedClassName.substring(index + 1);
    if (Boolean.parseBoolean(System.getenv("AS_DEBUG"))) {
        if (logger.isLoggable(Level.FINE))
            logger.fine("Generated ClassDATA " + clsName);
        // the path is horribly long.  Let's just write t directly into the
        // lib dir.  It is not for loading as a class but just for us humans
        // to decompile to figure out what is going on.  No need to make it even harder!
        clsName = clsName.replace('.', '/');
        // just in case Windows?  unlikely...
        clsName = clsName.replace('\\', '/');
        index = clsName.lastIndexOf("/");
        if (index >= 0)
            clsName = clsName.substring(index + 1);
        FileOutputStream fos = null;
        try {
            String rootPath = System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY) + File.separator + "lib" + File.separator;
            String fileName = rootPath + clsName + ".class";
            if (logger.isLoggable(Level.FINE))
                logger.fine("ClassFile: " + fileName);
            File file = new File(fileName);
            if (FileUtils.mkdirsMaybe(file.getParentFile())) {
                fos = new FileOutputStream(file);
                fos.write(classData);
                fos.flush();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (Exception e) {
            // nothing can be done...
            }
        }
    }
    return classData;
}
Also used : FlashlightProbe(org.glassfish.flashlight.provider.FlashlightProbe) Label(org.glassfish.hk2.external.org.objectweb.asm.Label) Method(org.glassfish.hk2.external.org.objectweb.asm.commons.Method) ClassWriter(org.glassfish.hk2.external.org.objectweb.asm.ClassWriter) PrivilegedActionException(java.security.PrivilegedActionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Type(org.glassfish.hk2.external.org.objectweb.asm.Type) FileOutputStream(java.io.FileOutputStream) GeneratorAdapter(org.glassfish.hk2.external.org.objectweb.asm.commons.GeneratorAdapter) File(java.io.File)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 FlashlightProbe (org.glassfish.flashlight.provider.FlashlightProbe)1 ClassWriter (org.glassfish.hk2.external.org.objectweb.asm.ClassWriter)1 Label (org.glassfish.hk2.external.org.objectweb.asm.Label)1 Type (org.glassfish.hk2.external.org.objectweb.asm.Type)1 GeneratorAdapter (org.glassfish.hk2.external.org.objectweb.asm.commons.GeneratorAdapter)1 Method (org.glassfish.hk2.external.org.objectweb.asm.commons.Method)1