Package org.jpox.enhancer.asm

Source Code of org.jpox.enhancer.asm.ASMClassEnhancer$MyClassVisitor

/**********************************************************************
Copyright (c) 2007 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
    ...
**********************************************************************/
package org.jpox.enhancer.asm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import org.jpox.ClassLoaderResolver;
import org.jpox.enhancer.AbstractClassEnhancer;
import org.jpox.enhancer.ClassField;
import org.jpox.enhancer.asm.method.InitFieldFlags;
import org.jpox.enhancer.asm.method.InitFieldNames;
import org.jpox.enhancer.asm.method.InitFieldTypes;
import org.jpox.enhancer.asm.method.InitPersistenceCapableSuperclass;
import org.jpox.enhancer.asm.method.JdoCopyField;
import org.jpox.enhancer.asm.method.JdoCopyFields;
import org.jpox.enhancer.asm.method.JdoCopyKeyFieldsFromObjectId;
import org.jpox.enhancer.asm.method.JdoCopyKeyFieldsFromObjectId2;
import org.jpox.enhancer.asm.method.JdoCopyKeyFieldsToObjectId;
import org.jpox.enhancer.asm.method.JdoCopyKeyFieldsToObjectId2;
import org.jpox.enhancer.asm.method.JdoGetInheritedFieldCount;
import org.jpox.enhancer.asm.method.JdoGetManagedFieldCount;
import org.jpox.enhancer.asm.method.JdoGetObjectId;
import org.jpox.enhancer.asm.method.JdoGetPersistenceManager;
import org.jpox.enhancer.asm.method.JdoGetTransactionalObjectId;
import org.jpox.enhancer.asm.method.JdoGetVersion;
import org.jpox.enhancer.asm.method.JdoIsDeleted;
import org.jpox.enhancer.asm.method.JdoIsDetached;
import org.jpox.enhancer.asm.method.JdoIsDirty;
import org.jpox.enhancer.asm.method.JdoIsNew;
import org.jpox.enhancer.asm.method.JdoIsPersistent;
import org.jpox.enhancer.asm.method.JdoIsTransactional;
import org.jpox.enhancer.asm.method.JdoMakeDirty;
import org.jpox.enhancer.asm.method.JdoNewInstance1;
import org.jpox.enhancer.asm.method.JdoNewInstance2;
import org.jpox.enhancer.asm.method.JdoNewObjectIdInstance1;
import org.jpox.enhancer.asm.method.JdoNewObjectIdInstance2;
import org.jpox.enhancer.asm.method.JdoPreSerialize;
import org.jpox.enhancer.asm.method.JdoProvideField;
import org.jpox.enhancer.asm.method.JdoProvideFields;
import org.jpox.enhancer.asm.method.JdoReplaceDetachedState;
import org.jpox.enhancer.asm.method.JdoReplaceField;
import org.jpox.enhancer.asm.method.JdoReplaceFields;
import org.jpox.enhancer.asm.method.JdoReplaceFlags;
import org.jpox.enhancer.asm.method.JdoReplaceStateManager;
import org.jpox.enhancer.asm.method.JdoSuperClone;
import org.jpox.enhancer.asm.method.LoadClass;
import org.jpox.exceptions.JPOXException;
import org.jpox.metadata.ClassMetaData;
import org.jpox.metadata.ClassPersistenceModifier;
import org.jpox.util.JPOXLogger;
import org.jpox.util.StringUtils;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

/**
* Class enhancer using ASM (http://asm.objectweb.org).
* Assumes that the version of ASM is 3.0 or above.
* ASM operates using a SAXParser-like "visitor-pattern". We utilise this as follows :-
* <ul>
* <li><b>enhance</b> : start with a ClassReader for the class to be enhanced, and create a JdoClassAdapter
* (attached to a ClassWriter) that will perform the modifications, and use that as a visitor for the reader
* so that the reader sends its events to the adapter. Within the JdoClassAdapter we also make use of a
* JdoMethodAdapter to update individual methods</li>
* <li><b>check</b> : take a ClassReader, and create a JdoClassChecker that performs the checks. We then set
* the checker as a visitor for the reader so that the reader sends its events to the checker.</li>
* </ul>
* @version $Revision: 1.23 $
*/
public class ASMClassEnhancer extends AbstractClassEnhancer
{
    /** Resource name of the input class (only when the class exists in a class file). */
    protected String inputResourceName;

    /** Bytes of the input class (only when enhancing generated classes with no class file). */
    protected byte[] inputBytes;

    /** Class that is being enhanced. */
    protected final Class cls;

    /** Bytes of the class (after enhancing). */
    protected byte[] classBytes = null;

    /** ASM Class name for this class (replace . with /). */
    protected String asmClassName = null;

    /** Class descriptor for this class. */
    protected String classDescriptor = null;

    /**
     * Constructor.
     * @param cmd MetaData for the class to be enhanced
     * @param clr ClassLoader resolver
     */
    public ASMClassEnhancer(ClassMetaData cmd, ClassLoaderResolver clr)
    {
        super(cmd, clr);

        cls = clr.classForName(cmd.getFullClassName());
        asmClassName = cmd.getFullClassName().replace('.', '/');
        classDescriptor = Type.getDescriptor(cls);
        inputResourceName = "/" + className.replace('.','/') + ".class";
    }

    /**
     * Constructor.
     * @param cmd MetaData for the class to be enhanced
     * @param clr ClassLoader resolver
     */
    public ASMClassEnhancer(ClassMetaData cmd, ClassLoaderResolver clr, byte[] classBytes)
    {
        super(cmd, clr);

        cls = clr.classForName(cmd.getFullClassName());
        asmClassName = cmd.getFullClassName().replace('.', '/');
        classDescriptor = Type.getDescriptor(cls);
        inputBytes = classBytes;
    }

    /**
     * Convenience accessor for the class name that is stored in a particular class.
     * @param filename Name of the file
     * @return The class name
     */
    public static String getClassNameForFileName(String filename)
    {
        MyClassVisitor vis = new MyClassVisitor();
        try
        {
            new ClassReader(new FileInputStream(filename)).accept(vis, 0);
            return vis.getClassName();
        }
        catch (IOException ioe)
        {
            return null;
        }
    }

    /** Convenience class to look up the class name for a file. */
    public static class MyClassVisitor implements ClassVisitor
    {
        String className = null;
        public String getClassName()
        {
            return className;
        }

        public void visitInnerClass(String name, String outerName, String innerName, int access) { }
        public void visit(int version, int access, String name, String sig, String supername, String[] intfs)
        {
            className = name.replace('/', '.');
        }

        public AnnotationVisitor visitAnnotation(String desc, boolean visible)
        {
            return null;
        }
        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value)
        {
            return null;
        }
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] excpts)
        {
            return null;
        }
        public void visitAttribute(Attribute attr) { }
        public void visitOuterClass(String owner, String name, String desc) { }
        public void visitSource(String source, String debug) { }
        public void visitEnd() { }
    }

    /**
     * Accessor for the class being enhanced.
     * @return Class being enhanced
     */
    public Class getClassEnhanced()
    {
        return cls;
    }

    /**
     * Accessor for the ASM class name
     * @return ASM class name
     */
    public String getASMClassName()
    {
        return asmClassName;
    }

    /**
     * Accessor for the class descriptor for the class being enhanced
     * @return class descriptor
     */
    public String getClassDescriptor()
    {
        return classDescriptor;
    }

    /**
     * Method to initialise the list of methods to add.
     */
    protected void initialiseMethodsList()
    {
        if (cmd.getPersistenceCapableSuperclass() == null)
        {
            // Root persistent class methods
            methodsToAdd.add(JdoCopyKeyFieldsFromObjectId.getInstance(this));
            methodsToAdd.add(JdoCopyKeyFieldsFromObjectId2.getInstance(this));
            methodsToAdd.add(JdoCopyKeyFieldsToObjectId.getInstance(this));
            methodsToAdd.add(JdoCopyKeyFieldsToObjectId2.getInstance(this));
            methodsToAdd.add(JdoGetObjectId.getInstance(this));
            methodsToAdd.add(JdoGetVersion.getInstance(this));
            methodsToAdd.add(JdoPreSerialize.getInstance(this));
            methodsToAdd.add(JdoGetPersistenceManager.getInstance(this));
            methodsToAdd.add(JdoGetTransactionalObjectId.getInstance(this));
            methodsToAdd.add(JdoIsDeleted.getInstance(this));
            methodsToAdd.add(JdoIsDirty.getInstance(this));
            methodsToAdd.add(JdoIsNew.getInstance(this));
            methodsToAdd.add(JdoIsPersistent.getInstance(this));
            methodsToAdd.add(JdoIsTransactional.getInstance(this));
            methodsToAdd.add(JdoMakeDirty.getInstance(this));
            methodsToAdd.add(JdoNewObjectIdInstance1.getInstance(this));
            methodsToAdd.add(JdoNewObjectIdInstance2.getInstance(this));
            methodsToAdd.add(JdoProvideFields.getInstance(this));
            methodsToAdd.add(JdoReplaceFields.getInstance(this));
            methodsToAdd.add(JdoReplaceFlags.getInstance(this));
            methodsToAdd.add(JdoReplaceStateManager.getInstance(this));
        }

        if (requiresDetachable())
        {
            methodsToAdd.add(JdoReplaceDetachedState.getInstance(this));
        }
        if (cmd.isDetachable() && cmd.getPersistenceCapableSuperclass() != null)
        {
            methodsToAdd.add(JdoMakeDirty.getInstance(this));
        }

        methodsToAdd.add(JdoIsDetached.getInstance(this));
        methodsToAdd.add(JdoNewInstance1.getInstance(this));
        methodsToAdd.add(JdoNewInstance2.getInstance(this));
        methodsToAdd.add(JdoReplaceField.getInstance(this));
        methodsToAdd.add(JdoProvideField.getInstance(this));
        methodsToAdd.add(JdoCopyField.getInstance(this));
        methodsToAdd.add(JdoCopyFields.getInstance(this));
        methodsToAdd.add(InitFieldNames.getInstance(this));
        methodsToAdd.add(InitFieldTypes.getInstance(this));
        methodsToAdd.add(InitFieldFlags.getInstance(this));
        methodsToAdd.add(JdoGetInheritedFieldCount.getInstance(this));
        methodsToAdd.add(JdoGetManagedFieldCount.getInstance(this));
        methodsToAdd.add(InitPersistenceCapableSuperclass.getInstance(this));
        methodsToAdd.add(LoadClass.getInstance(this));
        methodsToAdd.add(JdoSuperClone.getInstance(this));
    }

    /**
     * Method to initialise the list of fields to add.
     */
    protected void initialiseFieldsList()
    {
        if (cmd.getPersistenceCapableSuperclass() == null)
        {
            // Root persistent class fields
            fieldsToAdd.add(new ClassField(this, FN_StateManager,
                Opcodes.ACC_PROTECTED | Opcodes.ACC_TRANSIENT, javax.jdo.spi.StateManager.class));
            fieldsToAdd.add(new ClassField(this, FN_Flag,
                Opcodes.ACC_PROTECTED | Opcodes.ACC_TRANSIENT, byte.class));
        }

        if (requiresDetachable())
        {
            // Detachable fields
            fieldsToAdd.add(new ClassField(this, FN_JdoDetachedState,
                Opcodes.ACC_PROTECTED, Object[].class));
        }

        fieldsToAdd.add(new ClassField(this, FN_FieldFlags,
            Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, byte[].class));
        fieldsToAdd.add(new ClassField(this, FN_PersistenceCapableSuperclass,
            Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, Class.class));
        fieldsToAdd.add(new ClassField(this, FN_FieldTypes,
            Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, Class[].class));
        fieldsToAdd.add(new ClassField(this, FN_FieldNames,
            Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, String[].class));
        fieldsToAdd.add(new ClassField(this, FN_JdoInheritedFieldCount,
            Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, int.class));
    }

    /**
     * Method to enhance a classes definition.
     * @return Whether it was enhanced with no errors
     */
    public boolean enhance()
    {
        if (cmd.getPersistenceModifier() != ClassPersistenceModifier.PERSISTENCE_CAPABLE &&
            cmd.getPersistenceModifier() != ClassPersistenceModifier.PERSISTENCE_AWARE)
        {
            return false;
        }

        initialise();

        if (checkClassIsEnhanced(false))
        {
            // Already enhanced
            JPOXLogger.ENHANCER.info(LOCALISER.msg("Enhancer.ClassIsAlreadyEnhanced", className));
            return true;
        }

        try
        {
            // Create an adapter using a writer
            // [ASM Note : In 2.2 this should be "new ClassWriter(true, true);"]
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            JdoClassAdapter cv = new JdoClassAdapter(cw, this);
            ClassReader cr = null;

            // Create a reader for the class and tell it to visit the adapter, performing the changes
            if (inputBytes != null)
            {
                cr = new ClassReader(inputBytes);
            }
            else
            {
                cr = new ClassReader(getClass().getResourceAsStream(inputResourceName));
            }
            cr.accept(cv, 0); // [ASM Note : In 2.2 this should be "cr.accept(cv, false);"]

            // Save the bytes
            classBytes = cw.toByteArray();
        }
        catch (Exception e)
        {
            JPOXLogger.ENHANCER.error("Error thrown enhancing with ASMClassEnhancer", e);
            return false;
        }

        update = true;
        return true;
    }

    /**
     * Accessor for the class bytes.
     * Only has relevance to be called after enhance().
     * @return The class bytes
     */
    public byte[] getBytes()
    {
        return classBytes;
    }

    /**
     * Method to save the class definition bytecode into a class file.
     * If directoryName is specified it will be written to $directoryName/className.class
     * else will overwrite the existing class.
     * @param directoryName Name of a directory (or null to overwrite the class)
     * @throws IOException If an I/O error occurs in the write.
     */
    public void save(String directoryName)
    throws IOException
    {
        if (!update)
        {
            // Not updated so nothing to do here
            return;
        }

        File file = null;
        if (directoryName != null)
        {
            File baseDir = new File(directoryName);
            if (!baseDir.exists())
            {
                baseDir.mkdirs();
            }
            else if (!baseDir.isDirectory())
            {
                throw new RuntimeException("not directory " + directoryName);
            }

            String sep = System.getProperty("file.separator");
            String name = cmd.getFullClassName();
            name = name.replace('.', sep.charAt(0));
            name = name + ".class";
            file = new File(directoryName, name);
            file.getParentFile().mkdirs();
            JPOXLogger.ENHANCER.info(LOCALISER.msg("Enhancer.UpdateClass", file.getCanonicalPath()));
        }
        else
        {
            URL classURL = clr.getResource(className.replace('.','/') + ".class", null);
            JPOXLogger.ENHANCER.info(LOCALISER.msg("Enhancer.UpdateClass", classURL));

            URL convertedPath = this.cmd.getMetaDataManager().getOMFContext().getPluginManager().resolveURLAsFileURL(classURL);
            if (!convertedPath.toString().equals(classURL.toString()))
            {
                JPOXLogger.ENHANCER.info(LOCALISER.msg("Enhancer.UpdateClass", classURL));
            }

            file = StringUtils.getFileForFilename(convertedPath.getFile());
        }

        FileOutputStream out = null;
        try
        {
            out = new FileOutputStream(file);
            out.write(getBytes());
        }
        finally
        {
            try
            {
                out.close();
                out = null;
            }
            catch (Exception ignore)
            {
                // ignore exception in closing the stream
            }
        }
    }

    /* (non-Javadoc)
     * @see org.jpox.enhancer.ClassEnhancer#checkEnhanced()
     */
    public boolean checkEnhanced()
    {
        if (cmd.getPersistenceModifier() != ClassPersistenceModifier.PERSISTENCE_CAPABLE &&
            cmd.getPersistenceModifier() != ClassPersistenceModifier.PERSISTENCE_AWARE)
        {
            return false;
        }

        initialise();

        return checkClassIsEnhanced(true);
    }

    /**
     * Verify mode for class files.
     * ASM ClassEnhancer doesn't support this so just throws an exception.
     * @throws Exception Thrown if an error occurs.
     */
    public void verify() throws Exception
    {
        throw new JPOXException("ASM ClassEnhancer doesnt support a verify mode");
    }

    /**
     * Convenience method to return if a class is enhanced.
     * @param logErrors Whether to log any errors (missing methods etc) as errors (otherwise info/debug)
     * @return Whether the class is enhanced
     */
    protected boolean checkClassIsEnhanced(boolean logErrors)
    {
        try
        {
            // Create an adapter using a writer
            JdoClassChecker checker = new JdoClassChecker(this, logErrors);

            // Create a reader for the class and visit it using the checker
            ClassReader cr = null;
            if (inputBytes != null)
            {
                cr = new ClassReader(inputBytes);
            }
            else
            {
                cr = new ClassReader(getClass().getResourceAsStream(inputResourceName));
            }
            cr.accept(checker, 0); // [ASM Note : In 2.2 this should be "cr.accept(checker, false);"]

            return checker.isEnhanced();
        }
        catch (Exception e)
        {
            JPOXLogger.ENHANCER.error("Error thrown enhancing with ASMClassEnhancer", e);
        }
        return false;
    }
}
TOP

Related Classes of org.jpox.enhancer.asm.ASMClassEnhancer$MyClassVisitor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.