Search in sources :

Example 1 with UnloadedClass

use of org.hibernate.bytecode.enhance.spi.UnloadedClass in project hibernate-orm by hibernate.

the class MavenEnhancePlugin method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    if (!shouldApply()) {
        getLog().warn("Skipping Hibernate bytecode enhancement plugin execution since no feature is enabled");
        return;
    }
    if (!dir.startsWith(base)) {
        throw new MojoExecutionException("The enhancement directory 'dir' (" + dir + ") is no subdirectory of 'base' (" + base + ")");
    }
    // Perform a depth first search for sourceSet
    File root = new File(this.dir);
    if (!root.exists()) {
        getLog().info("Skipping Hibernate enhancement plugin execution since there is no classes dir " + dir);
        return;
    }
    walkDir(root);
    if (sourceSet.isEmpty()) {
        getLog().info("Skipping Hibernate enhancement plugin execution since there are no classes to enhance on " + dir);
        return;
    }
    getLog().info("Starting Hibernate enhancement for classes on " + dir);
    final ClassLoader classLoader = toClassLoader(Collections.singletonList(new File(base)));
    EnhancementContext enhancementContext = new DefaultEnhancementContext() {

        @Override
        public ClassLoader getLoadingClassLoader() {
            return classLoader;
        }

        @Override
        public boolean doBiDirectionalAssociationManagement(UnloadedField field) {
            return enableAssociationManagement;
        }

        @Override
        public boolean doDirtyCheckingInline(UnloadedClass classDescriptor) {
            return enableDirtyTracking;
        }

        @Override
        public boolean hasLazyLoadableAttributes(UnloadedClass classDescriptor) {
            return enableLazyInitialization;
        }

        @Override
        public boolean isLazyLoadable(UnloadedField field) {
            return enableLazyInitialization;
        }

        @Override
        public boolean doExtendedEnhancement(UnloadedClass classDescriptor) {
            return enableExtendedEnhancement;
        }
    };
    if (enableExtendedEnhancement) {
        getLog().warn("Extended enhancement is enabled. Classes other than entities may be modified. You should consider access the entities using getter/setter methods and disable this property. Use at your own risk.");
    }
    final Enhancer enhancer = Environment.getBytecodeProvider().getEnhancer(enhancementContext);
    for (File file : sourceSet) {
        final byte[] enhancedBytecode = doEnhancement(file, enhancer);
        if (enhancedBytecode == null) {
            continue;
        }
        writeOutEnhancedClass(enhancedBytecode, file);
        getLog().info("Successfully enhanced class [" + file + "]");
    }
}
Also used : UnloadedClass(org.hibernate.bytecode.enhance.spi.UnloadedClass) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultEnhancementContext(org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext) Enhancer(org.hibernate.bytecode.enhance.spi.Enhancer) URLClassLoader(java.net.URLClassLoader) File(java.io.File) UnloadedField(org.hibernate.bytecode.enhance.spi.UnloadedField) DefaultEnhancementContext(org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext) EnhancementContext(org.hibernate.bytecode.enhance.spi.EnhancementContext)

Example 2 with UnloadedClass

use of org.hibernate.bytecode.enhance.spi.UnloadedClass in project hibernate-orm by hibernate.

the class HibernatePlugin method applyEnhancement.

private void applyEnhancement(final Project project, final HibernateExtension hibernateExtension) {
    if (!hibernateExtension.enhance.shouldApply()) {
        project.getLogger().warn("Skipping Hibernate bytecode enhancement since no feature is enabled");
        return;
    }
    for (final SourceSet sourceSet : hibernateExtension.getSourceSets()) {
        project.getLogger().debug("Applying Hibernate enhancement action to SourceSet.{}", sourceSet.getName());
        final Task compileTask = project.getTasks().findByName(sourceSet.getCompileJavaTaskName());
        compileTask.doLast(new Action<Task>() {

            @Override
            public void execute(Task task) {
                project.getLogger().debug("Starting Hibernate enhancement on SourceSet.{}", sourceSet.getName());
                final ClassLoader classLoader = toClassLoader(sourceSet.getRuntimeClasspath());
                EnhancementContext enhancementContext = new DefaultEnhancementContext() {

                    @Override
                    public ClassLoader getLoadingClassLoader() {
                        return classLoader;
                    }

                    @Override
                    public boolean doBiDirectionalAssociationManagement(UnloadedField field) {
                        return hibernateExtension.enhance.getEnableAssociationManagement();
                    }

                    @Override
                    public boolean doDirtyCheckingInline(UnloadedClass classDescriptor) {
                        return hibernateExtension.enhance.getEnableDirtyTracking();
                    }

                    @Override
                    public boolean hasLazyLoadableAttributes(UnloadedClass classDescriptor) {
                        return hibernateExtension.enhance.getEnableLazyInitialization();
                    }

                    @Override
                    public boolean isLazyLoadable(UnloadedField field) {
                        return hibernateExtension.enhance.getEnableLazyInitialization();
                    }

                    @Override
                    public boolean doExtendedEnhancement(UnloadedClass classDescriptor) {
                        return hibernateExtension.enhance.getEnableExtendedEnhancement();
                    }
                };
                if (hibernateExtension.enhance.getEnableExtendedEnhancement()) {
                    logger.warn("Extended enhancement is enabled. Classes other than entities may be modified. You should consider access the entities using getter/setter methods and disable this property. Use at your own risk.");
                }
                final Enhancer enhancer = Environment.getBytecodeProvider().getEnhancer(enhancementContext);
                final FileTree fileTree = project.fileTree(sourceSet.getOutput().getClassesDir());
                for (File file : fileTree) {
                    if (!file.getName().endsWith(".class")) {
                        continue;
                    }
                    final byte[] enhancedBytecode = doEnhancement(sourceSet.getOutput().getClassesDir(), file, enhancer);
                    if (enhancedBytecode != null) {
                        writeOutEnhancedClass(enhancedBytecode, file);
                        logger.info("Successfully enhanced class [" + file + "]");
                    } else {
                        logger.info("Skipping class [" + file.getAbsolutePath() + "], not an entity nor embeddable");
                    }
                }
            }
        });
    }
}
Also used : SourceSet(org.gradle.api.tasks.SourceSet) Task(org.gradle.api.Task) UnloadedClass(org.hibernate.bytecode.enhance.spi.UnloadedClass) DefaultEnhancementContext(org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext) Enhancer(org.hibernate.bytecode.enhance.spi.Enhancer) URLClassLoader(java.net.URLClassLoader) FileTree(org.gradle.api.file.FileTree) File(java.io.File) UnloadedField(org.hibernate.bytecode.enhance.spi.UnloadedField) DefaultEnhancementContext(org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext) EnhancementContext(org.hibernate.bytecode.enhance.spi.EnhancementContext)

Example 3 with UnloadedClass

use of org.hibernate.bytecode.enhance.spi.UnloadedClass in project hibernate-orm by hibernate.

the class EnhancementTask method execute.

@Override
public void execute() throws BuildException {
    if (!shouldApply()) {
        log("Skipping Hibernate bytecode enhancement task execution since no feature is enabled", Project.MSG_WARN);
        return;
    }
    if (!dir.startsWith(base)) {
        throw new BuildException("The enhancement directory 'dir' (" + dir + ") is no subdirectory of 'base' (" + base + ")");
    }
    // Perform a depth first search for sourceSet
    File root = new File(dir);
    if (!root.exists()) {
        log("Skipping Hibernate enhancement task execution since there is no classes dir " + dir, Project.MSG_INFO);
        return;
    }
    walkDir(root);
    if (sourceSet.isEmpty()) {
        log("Skipping Hibernate enhancement task execution since there are no classes to enhance on " + dir, Project.MSG_INFO);
        return;
    }
    log("Starting Hibernate enhancement task for classes on " + dir, Project.MSG_INFO);
    ClassLoader classLoader = toClassLoader(Collections.singletonList(new File(base)));
    EnhancementContext enhancementContext = new DefaultEnhancementContext() {

        @Override
        public ClassLoader getLoadingClassLoader() {
            return classLoader;
        }

        @Override
        public boolean doBiDirectionalAssociationManagement(UnloadedField field) {
            return enableAssociationManagement;
        }

        @Override
        public boolean doDirtyCheckingInline(UnloadedClass classDescriptor) {
            return enableDirtyTracking;
        }

        @Override
        public boolean hasLazyLoadableAttributes(UnloadedClass classDescriptor) {
            return enableLazyInitialization;
        }

        @Override
        public boolean isLazyLoadable(UnloadedField field) {
            return enableLazyInitialization;
        }

        @Override
        public boolean doExtendedEnhancement(UnloadedClass classDescriptor) {
            return enableExtendedEnhancement;
        }
    };
    if (enableExtendedEnhancement) {
        log("Extended enhancement is enabled. Classes other than entities may be modified. You should consider access the entities using getter/setter methods and disable this property. Use at your own risk.", Project.MSG_WARN);
    }
    Enhancer enhancer = Environment.getBytecodeProvider().getEnhancer(enhancementContext);
    for (File file : sourceSet) {
        byte[] enhancedBytecode = doEnhancement(file, enhancer);
        if (enhancedBytecode == null) {
            continue;
        }
        writeOutEnhancedClass(enhancedBytecode, file);
        log("Successfully enhanced class [" + file + "]", Project.MSG_INFO);
    }
}
Also used : UnloadedClass(org.hibernate.bytecode.enhance.spi.UnloadedClass) DefaultEnhancementContext(org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext) Enhancer(org.hibernate.bytecode.enhance.spi.Enhancer) URLClassLoader(java.net.URLClassLoader) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) UnloadedField(org.hibernate.bytecode.enhance.spi.UnloadedField) EnhancementContext(org.hibernate.bytecode.enhance.spi.EnhancementContext) DefaultEnhancementContext(org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext)

Example 4 with UnloadedClass

use of org.hibernate.bytecode.enhance.spi.UnloadedClass in project hibernate-orm by hibernate.

the class EnhancerTest method testMappedSuperclass.

@Test
@TestForIssue(jiraKey = "HHH-10646")
public void testMappedSuperclass() {
    EnhancerTestUtils.runEnhancerTestTask(MappedSuperclassTestTask.class);
    EnhancerTestUtils.runEnhancerTestTask(MappedSuperclassTestTask.class, new EnhancerTestContext() {

        @Override
        public boolean hasLazyLoadableAttributes(UnloadedClass classDescriptor) {
            // HHH-10981 - Without lazy loading, the generation of getters and setters has a different code path
            return false;
        }
    });
}
Also used : UnloadedClass(org.hibernate.bytecode.enhance.spi.UnloadedClass) EnhancerTestContext(org.hibernate.testing.bytecode.enhancement.EnhancerTestContext) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with UnloadedClass

use of org.hibernate.bytecode.enhance.spi.UnloadedClass in project hibernate-orm by hibernate.

the class EnhancerTest method testInherited.

@Test
@TestForIssue(jiraKey = "HHH-11284")
public void testInherited() {
    EnhancerTestUtils.runEnhancerTestTask(InheritedTestTask.class);
    EnhancerTestUtils.runEnhancerTestTask(InheritedTestTask.class, new EnhancerTestContext() {

        @Override
        public boolean hasLazyLoadableAttributes(UnloadedClass classDescriptor) {
            // HHH-10981 - Without lazy loading, the generation of getters and setters has a different code path
            return false;
        }
    });
}
Also used : UnloadedClass(org.hibernate.bytecode.enhance.spi.UnloadedClass) EnhancerTestContext(org.hibernate.testing.bytecode.enhancement.EnhancerTestContext) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

UnloadedClass (org.hibernate.bytecode.enhance.spi.UnloadedClass)5 File (java.io.File)3 URLClassLoader (java.net.URLClassLoader)3 DefaultEnhancementContext (org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext)3 EnhancementContext (org.hibernate.bytecode.enhance.spi.EnhancementContext)3 Enhancer (org.hibernate.bytecode.enhance.spi.Enhancer)3 UnloadedField (org.hibernate.bytecode.enhance.spi.UnloadedField)3 TestForIssue (org.hibernate.testing.TestForIssue)2 EnhancerTestContext (org.hibernate.testing.bytecode.enhancement.EnhancerTestContext)2 Test (org.junit.Test)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 BuildException (org.apache.tools.ant.BuildException)1 Task (org.gradle.api.Task)1 FileTree (org.gradle.api.file.FileTree)1 SourceSet (org.gradle.api.tasks.SourceSet)1