Search in sources :

Example 1 with Attribute

use of org.eclipse.persistence.internal.libraries.asm.Attribute in project eclipselink by eclipse-ee4j.

the class MetadataAsmFactory method buildClassMetadata.

/**
 * Build the class metadata for the class name using ASM to read the class
 * byte codes.
 */
protected void buildClassMetadata(MetadataClass metadataClass, String className, boolean isLazy) {
    ClassMetadataVisitor visitor = new ClassMetadataVisitor(metadataClass, isLazy);
    InputStream stream = null;
    try {
        String resourceString = className.replace('.', '/') + ".class";
        if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
            final String f_resourceString = resourceString;
            stream = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {

                @Override
                public InputStream run() {
                    return m_loader.getResourceAsStream(f_resourceString);
                }
            });
        } else {
            stream = m_loader.getResourceAsStream(resourceString);
        }
        ClassReader reader = new ClassReader(stream);
        Attribute[] attributes = new Attribute[0];
        reader.accept(visitor, attributes, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    } catch (IllegalArgumentException iae) {
        // class was probably compiled with some newer than officially
        // supported and tested JDK
        // in such case log a warning and try to re-read the class
        // without class version check
        SessionLog log = getLogger().getSession() != null ? getLogger().getSession().getSessionLog() : AbstractSessionLog.getLog();
        if (log.shouldLog(SessionLog.WARNING, SessionLog.METADATA)) {
            SessionLogEntry entry = new SessionLogEntry(getLogger().getSession(), SessionLog.WARNING, SessionLog.METADATA, iae);
            entry.setMessage(ExceptionLocalization.buildMessage("unsupported_classfile_version", new Object[] { className }));
            log.log(entry);
        }
        if (stream != null) {
            try {
                ClassReader reader = new EclipseLinkClassReader(stream);
                Attribute[] attributes = new Attribute[0];
                reader.accept(visitor, attributes, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
            } catch (Exception e) {
                // our fall-back failed, this is severe
                if (log.shouldLog(SessionLog.SEVERE, SessionLog.METADATA)) {
                    SessionLogEntry entry = new SessionLogEntry(getLogger().getSession(), SessionLog.SEVERE, SessionLog.METADATA, e);
                    entry.setMessage(ExceptionLocalization.buildMessage("unsupported_classfile_version", new Object[] { className }));
                    log.log(entry);
                }
                addMetadataClass(getVirtualMetadataClass(className));
            }
        } else {
            addMetadataClass(getVirtualMetadataClass(className));
        }
    } catch (Exception exception) {
        SessionLog log = getLogger().getSession() != null ? getLogger().getSession().getSessionLog() : AbstractSessionLog.getLog();
        if (log.shouldLog(SessionLog.FINEST, SessionLog.METADATA)) {
            log.logThrowable(SessionLog.FINEST, SessionLog.METADATA, exception);
        }
        addMetadataClass(getVirtualMetadataClass(className));
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException ignore) {
        // Ignore.
        }
    }
}
Also used : Attribute(org.eclipse.persistence.internal.libraries.asm.Attribute) InputStream(java.io.InputStream) EclipseLinkClassReader(org.eclipse.persistence.internal.libraries.asm.EclipseLinkClassReader) IOException(java.io.IOException) SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry) IOException(java.io.IOException) SessionLog(org.eclipse.persistence.logging.SessionLog) AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) PrivilegedAction(java.security.PrivilegedAction) ClassReader(org.eclipse.persistence.internal.libraries.asm.ClassReader) EclipseLinkClassReader(org.eclipse.persistence.internal.libraries.asm.EclipseLinkClassReader)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrivilegedAction (java.security.PrivilegedAction)1 Attribute (org.eclipse.persistence.internal.libraries.asm.Attribute)1 ClassReader (org.eclipse.persistence.internal.libraries.asm.ClassReader)1 EclipseLinkClassReader (org.eclipse.persistence.internal.libraries.asm.EclipseLinkClassReader)1 AbstractSessionLog (org.eclipse.persistence.logging.AbstractSessionLog)1 SessionLog (org.eclipse.persistence.logging.SessionLog)1 SessionLogEntry (org.eclipse.persistence.logging.SessionLogEntry)1