Example usage for org.eclipse.jdt.apt.core.util AptConfig isEnabled

List of usage examples for org.eclipse.jdt.apt.core.util AptConfig isEnabled

Introduction

In this page you can find the example usage for org.eclipse.jdt.apt.core.util AptConfig isEnabled.

Prototype

public static boolean isEnabled(IJavaProject jproject) 

Source Link

Document

Is annotation processing turned on for this project?

Usage

From source file:org.jboss.tools.m2e.extras.AptProjectConfigurator.java

License:Open Source License

@Override
public AbstractBuildParticipant getBuildParticipant(IMavenProjectFacade projectFacade, MojoExecution execution,
        IPluginExecutionMetadata executionMetadata) {
    if (AptConfig.isEnabled(JavaCore.create(projectFacade.getProject()))) {
        // don't run the mojo if the Eclipse APT builder is enabled
        return null;
    }// ww  w.j a  va2s  .  c  om
    return new AptBuildParticipant(execution);
}

From source file:org.jboss.tools.maven.apt.AptProjectConfigurator.java

License:Open Source License

/**
 * {@inheritDoc}/*w ww .jav a2s.co m*/
 */
public void configureRawClasspath(ProjectConfigurationRequest request, IClasspathDescriptor classpath,
        IProgressMonitor monitor) throws CoreException {
    /*
     * We need to prevent/recover from the JavaProjectConfigurator removing the
     * generated annotation sources directory from the classpath: it will be added
     * when we configure the Eclipse APT preferences and then removed when the
     * JavaProjectConfigurator runs.
     */

    // Get the various project references we'll need
    IProject eclipseProject = request.getProject();
    MavenProject mavenProject = request.getMavenProject();
    IMavenProjectFacade projectFacade = request.getMavenProjectFacade();

    // If this isn't a Java project, we have nothing to do
    if (!eclipseProject.hasNature(JavaCore.NATURE_ID))
        return;

    //If APT is not enabled, nothing to do either
    IJavaProject javaProject = JavaCore.create(eclipseProject);
    if (!AptConfig.isEnabled(javaProject)) {
        return;
    }

    // If this project has no valid compiler plugin config, we have nothing to do
    File generatedSourcesDirectory = getGeneratedSourcesDirectory(request.getMavenSession(), projectFacade,
            monitor);
    if (generatedSourcesDirectory == null)
        return;

    // Get the generated annotation sources directory as an IFolder
    File generatedSourcesRelativeDirectory = convertToProjectRelativePath(eclipseProject,
            generatedSourcesDirectory);
    String generatedSourcesRelativeDirectoryPath = generatedSourcesRelativeDirectory.getPath();
    IFolder generatedSourcesFolder = eclipseProject.getFolder(generatedSourcesRelativeDirectoryPath);

    // Get the output folder to use as an IPath
    File outputFile = new File(mavenProject.getBuild().getOutputDirectory());
    File outputRelativeFile = convertToProjectRelativePath(eclipseProject, outputFile);
    IFolder outputFolder = eclipseProject.getFolder(outputRelativeFile.getPath());
    IPath outputPath = outputFolder.getFullPath();

    // Create the includes & excludes specifiers
    IPath[] includes = new IPath[] {};
    IPath[] excludes = new IPath[] {};

    // If the source folder exists and is non-nested, add it
    if (generatedSourcesFolder != null && generatedSourcesFolder.exists()
            && generatedSourcesFolder.getProject().equals(eclipseProject)) {
        IClasspathEntryDescriptor cped = getEnclosingEntryDescriptor(classpath,
                generatedSourcesFolder.getFullPath());
        if (cped == null) {
            classpath.addSourceEntry(generatedSourcesFolder.getFullPath(), outputPath, includes, excludes,
                    false);
        }
    } else {
        if (generatedSourcesFolder != null) {
            classpath.removeEntry(generatedSourcesFolder.getFullPath());
        }
    }
}

From source file:org.jboss.tools.maven.apt.internal.AbstractAptProjectConfigurator.java

License:Open Source License

/**
 * reconcile is enabled by default while enabling apt for maven-compiler-plugin,
 * As Annotation processing usually takes a long time for even a java file change,
 * and what's more, validate a jsp also triggers apt reconcile as jsp compiles into java,
 * this option is provided to switch off the "Processing on Edit" feature.
 * /*from  w w  w  .  j a v  a2  s.c  o m*/
 * @throws CoreException 
 */
private void configureAptReconcile(IProject project) throws CoreException {
    if (project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject jp = JavaCore.create(project);
        if (jp != null && AptConfig.isEnabled(jp)) {
            boolean shouldEnable = MavenJdtAptPlugin.getDefault().getPreferencesManager()
                    .shouldEnableAnnotationProcessDuringReconcile(project);
            if (shouldEnable && !AptConfig.shouldProcessDuringReconcile(jp)) {
                AptConfig.setProcessDuringReconcile(jp, true);
            }
            if (!shouldEnable && AptConfig.shouldProcessDuringReconcile(jp)) {
                AptConfig.setProcessDuringReconcile(jp, false);
            }
        }
    }
}

From source file:org.jboss.tools.maven.apt.internal.utils.ProjectUtils.java

License:Open Source License

/**
 * Disable JDT APT on this project//from w w  w.j  a  va 2 s .c o m
 */
public static void disableApt(IProject project) {
    if (project == null) {
        return;
    }

    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null && AptConfig.isEnabled(javaProject)) {
        AptConfig.setEnabled(javaProject, false);
    }
}

From source file:org.jboss.tools.maven.apt.tests.M2eAptProjectconfiguratorTest.java

License:Open Source License

public void testNoAnnotationProcessor() throws Exception {
    IProject p = importProject("projects/p0/pom.xml");
    waitForJobsToComplete();//from  w  w w.jav a  2s.c o m

    // Import doesn't build, so we trigger it manually
    p.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
    waitForJobsToComplete();

    IJavaProject javaProject = JavaCore.create(p);
    assertNotNull(javaProject);

    assertFalse("Annotation processing is enabled for " + p, AptConfig.isEnabled(javaProject));
    String expectedOutputFolder = "target/generated-sources/annotations";
    IFolder annotationsFolder = p.getFolder(expectedOutputFolder);
    assertFalse(annotationsFolder + " was generated", annotationsFolder.exists());
}

From source file:org.jboss.tools.maven.apt.tests.M2eAptProjectconfiguratorTest.java

License:Open Source License

public void testRuntimePluginDependency() throws Exception {

    IProject p = importProject("projects/eclipselink/pom.xml");
    waitForJobsToComplete();/*from www.  j  a  v  a 2s.  c  om*/

    // Import doesn't build, so we trigger it manually
    p.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
    waitForJobsToComplete();

    IJavaProject javaProject = JavaCore.create(p);
    assertNotNull(javaProject);

    assertTrue("Annotation processing is disabled for " + p, AptConfig.isEnabled(javaProject));
    String expectedOutputFolder = "target/generated-sources/annotations";
    IFolder annotationsFolder = p.getFolder(expectedOutputFolder);
    assertTrue(annotationsFolder + " was not generated", annotationsFolder.exists());

    FactoryPath factoryPath = (FactoryPath) AptConfig.getFactoryPath(javaProject);
    String modelGen = "org.eclipse.persistence.jpa.modelgen.processor-2.5.1.jar";
    boolean foundRuntimeDependency = false;
    for (FactoryContainer container : factoryPath.getEnabledContainers().keySet()) {
        if (("M2_REPO/org/eclipse/persistence/org.eclipse.persistence.jpa.modelgen.processor/2.5.1/" + modelGen)
                .equals(container.getId())) {
            foundRuntimeDependency = true;
            break;
        }
    }
    assertTrue(modelGen + " was not found", foundRuntimeDependency);

    /*
    There's an ugly bug in Tycho which makes 
    JavaModelManager.getJavaModelManager().createAnnotationProcessorManager() return null
    as a consequence, no annotation processors are run during Tycho builds
    See http://dev.eclipse.org/mhonarc/lists/tycho-user/msg02344.html
            
    For the time being, only APT configuration can be tested, not APT build outcomes
    */
    if (JavaModelManager.getJavaModelManager().createAnnotationProcessorManager() == null) {
        return;
    }

    IFile generatedFile = p.getFile(expectedOutputFolder + "/foo/bar/Dummy_.java");
    if (!generatedFile.exists()) {
        //APT was triggered during project configuration, i.e. before META-INF/persistence.xml was copied to 
        //target/classes by the maven-resource-plugin build participant. eclipselink modelgen could not find it 
        // and skipped model generation. Pretty annoying and I dunno how to fix that ... yet.

        //Let's check a nudge to Dummy.java fixes this.
        IFile dummy = p.getFile("src/main/java/foo/bar/Dummy.java");
        dummy.touch(monitor);
        p.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
        waitForJobsToComplete();
    }

    assertTrue(generatedFile + " was not generated", generatedFile.exists());
    assertNoErrors(p);
}

From source file:org.jboss.tools.maven.apt.tests.M2eAptProjectconfiguratorTest.java

License:Open Source License

public void testDisableAnnotationProcessingFromWorkspace() throws Exception {
    IPreferencesManager preferencesManager = MavenJdtAptPlugin.getDefault().getPreferencesManager();
    try {//  w ww  .j a  v a 2s  . c om
        preferencesManager.setAnnotationProcessorMode(null, AnnotationProcessingMode.disabled);
        IProject p = importProject("projects/p1/pom.xml");
        waitForJobsToComplete();
        IJavaProject javaProject = JavaCore.create(p);
        assertFalse("JDT APT support was enabled", AptConfig.isEnabled(javaProject));

        IFolder annotationsFolder = p.getFolder("target/generated-sources/annotations");
        assertFalse(annotationsFolder + " was generated", annotationsFolder.exists());

    } finally {
        preferencesManager.setAnnotationProcessorMode(null, AnnotationProcessingMode.jdt_apt);
    }
}

From source file:org.jboss.tools.maven.apt.tests.M2eAptProjectconfiguratorTest.java

License:Open Source License

public void testDisableAnnotationProcessingFromProject() throws Exception {
    IProject p = importProject("projects/p1/pom.xml");
    waitForJobsToComplete();//from  w w  w .  ja va  2 s.  c om
    IJavaProject javaProject = JavaCore.create(p);
    assertTrue("JDT APT support was not enabled", AptConfig.isEnabled(javaProject));

    //Manually disable APT support 
    AptConfig.setEnabled(javaProject, false);

    //Disable m2e-apt on the project
    IPreferencesManager preferencesManager = MavenJdtAptPlugin.getDefault().getPreferencesManager();
    preferencesManager.setAnnotationProcessorMode(p, AnnotationProcessingMode.disabled);

    //Update Maven Configuration
    updateProject(p);

    //Check APT support is still disabled
    assertFalse("JDT APT support was enabled", AptConfig.isEnabled(javaProject));

}

From source file:org.jboss.tools.maven.apt.tests.M2eAptProjectconfiguratorTest.java

License:Open Source License

public void testPluginExecutionDelegation() throws Exception {
    IPreferencesManager preferencesManager = MavenJdtAptPlugin.getDefault().getPreferencesManager();
    try {//  w w  w . ja  v a  2 s.  c  om
        preferencesManager.setAnnotationProcessorMode(null, AnnotationProcessingMode.maven_execution);
        IProject p = importProject("projects/p3/pom.xml");
        waitForJobsToComplete();

        p.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
        waitForJobsToComplete();

        IJavaProject javaProject = JavaCore.create(p);
        assertFalse("JDT APT support was enabled", AptConfig.isEnabled(javaProject));

        IFolder annotationsFolder = p.getFolder("target/generated-sources/apt");
        assertTrue(annotationsFolder + " was not generated", annotationsFolder.exists());

        IFolder testAnnotationsFolder = p.getFolder("target/generated-sources/apt-test");
        assertTrue(testAnnotationsFolder + " was not generated", testAnnotationsFolder.exists());

    } finally {
        preferencesManager.setAnnotationProcessorMode(null, AnnotationProcessingMode.jdt_apt);
    }
}

From source file:org.jboss.tools.maven.apt.tests.M2eAptProjectconfiguratorTest.java

License:Open Source License

private void testDisabledAnnotationProcessing(String projectName) throws Exception {
    IProject p = importProject("projects/" + projectName + "/pom.xml");
    waitForJobsToComplete();//from   w w  w .  j av a2  s.com
    IJavaProject javaProject = JavaCore.create(p);
    assertNotNull(javaProject);
    assertFalse(AptConfig.isEnabled(javaProject));
}