Search in sources :

Example 1 with FileTree

use of org.gradle.api.file.FileTree 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 2 with FileTree

use of org.gradle.api.file.FileTree in project gradle by gradle.

the class CompositeFileTreeTest method matchingWithActionReturnsUnionOfFilteredSets.

@Test
public void matchingWithActionReturnsUnionOfFilteredSets() {
    final Action<PatternFilterable> action = Actions.doNothing();
    final FileTreeInternal filtered1 = context.mock(FileTreeInternal.class);
    final FileTreeInternal filtered2 = context.mock(FileTreeInternal.class);
    context.checking(new Expectations() {

        {
            oneOf(source1).matching(action);
            will(returnValue(filtered1));
            oneOf(source2).matching(action);
            will(returnValue(filtered2));
        }
    });
    FileTree filtered = tree.matching(action);
    assertThat(filtered, instanceOf(CompositeFileTree.class));
    CompositeFileTree filteredCompositeSet = (CompositeFileTree) filtered;
    assertThat(toList(filteredCompositeSet.getSourceCollections()), equalTo(toList(filtered1, filtered2)));
}
Also used : Expectations(org.jmock.Expectations) FileTree(org.gradle.api.file.FileTree) PatternFilterable(org.gradle.api.tasks.util.PatternFilterable) Test(org.junit.Test)

Example 3 with FileTree

use of org.gradle.api.file.FileTree in project gradle by gradle.

the class CompositeFileTreeTest method matchingWithPatternSetReturnsUnionOfFilteredSets.

@Test
public void matchingWithPatternSetReturnsUnionOfFilteredSets() {
    final PatternSet patternSet = new PatternSet();
    final FileTreeInternal filtered1 = context.mock(FileTreeInternal.class);
    final FileTreeInternal filtered2 = context.mock(FileTreeInternal.class);
    context.checking(new Expectations() {

        {
            oneOf(source1).matching(patternSet);
            will(returnValue(filtered1));
            oneOf(source2).matching(patternSet);
            will(returnValue(filtered2));
        }
    });
    FileTree filtered = tree.matching(patternSet);
    assertThat(filtered, instanceOf(CompositeFileTree.class));
    CompositeFileTree filteredCompositeSet = (CompositeFileTree) filtered;
    assertThat(toList(filteredCompositeSet.getSourceCollections()), equalTo(toList(filtered1, filtered2)));
}
Also used : Expectations(org.jmock.Expectations) FileTree(org.gradle.api.file.FileTree) PatternSet(org.gradle.api.tasks.util.PatternSet) Test(org.junit.Test)

Example 4 with FileTree

use of org.gradle.api.file.FileTree in project gradle by gradle.

the class CompositeFileTreeTest method plusReturnsUnionOfThisTreeAndSourceTree.

@Test
public void plusReturnsUnionOfThisTreeAndSourceTree() {
    FileTreeInternal other = context.mock(FileTreeInternal.class);
    FileTree sum = tree.plus(other);
    assertThat(sum, instanceOf(CompositeFileTree.class));
    UnionFileTree sumCompositeTree = (UnionFileTree) sum;
    assertThat(sumCompositeTree.getSourceCollections(), equalTo((Iterable) toList(source1, source2, other)));
}
Also used : FileTree(org.gradle.api.file.FileTree) Test(org.junit.Test)

Example 5 with FileTree

use of org.gradle.api.file.FileTree in project gradle by gradle.

the class SerializableCoffeeScriptCompileSpec method toRelativeFiles.

public static void toRelativeFiles(final FileCollection source, final List<RelativeFile> targets) {
    FileTree fileTree = source.getAsFileTree();
    fileTree.visit(new FileVisitor() {

        public void visitDir(FileVisitDetails dirDetails) {
        }

        public void visitFile(FileVisitDetails fileDetails) {
            targets.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
        }
    });
}
Also used : FileVisitDetails(org.gradle.api.file.FileVisitDetails) RelativeFile(org.gradle.api.internal.file.RelativeFile) FileTree(org.gradle.api.file.FileTree) FileVisitor(org.gradle.api.file.FileVisitor)

Aggregations

FileTree (org.gradle.api.file.FileTree)24 File (java.io.File)8 Test (org.junit.Test)6 GradleException (org.gradle.api.GradleException)5 Expectations (org.jmock.Expectations)5 IOException (java.io.IOException)4 FileCollection (org.gradle.api.file.FileCollection)4 TaskAction (org.gradle.api.tasks.TaskAction)4 PatternSet (org.gradle.api.tasks.util.PatternSet)4 Project (org.gradle.api.Project)2 DirectoryFileTree (org.gradle.api.internal.file.collections.DirectoryFileTree)2 MinimalFileSet (org.gradle.api.internal.file.collections.MinimalFileSet)2 TestClassProcessor (org.gradle.api.internal.tasks.testing.TestClassProcessor)2 TestFramework (org.gradle.api.internal.tasks.testing.TestFramework)2 WorkerTestClassProcessorFactory (org.gradle.api.internal.tasks.testing.WorkerTestClassProcessorFactory)2 MaxNParallelTestClassProcessor (org.gradle.api.internal.tasks.testing.processors.MaxNParallelTestClassProcessor)2 RestartEveryNTestClassProcessor (org.gradle.api.internal.tasks.testing.processors.RestartEveryNTestClassProcessor)2 TestMainAction (org.gradle.api.internal.tasks.testing.processors.TestMainAction)2 ForkingTestClassProcessor (org.gradle.api.internal.tasks.testing.worker.ForkingTestClassProcessor)2 SourceSet (org.gradle.api.tasks.SourceSet)2