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

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

Introduction

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

Prototype

public static IFactoryPath getFactoryPath(IJavaProject jproj) 

Source Link

Document

Get the factory path for a given project or for the workspace.

Usage

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();// w  w w  . ja  v a 2  s . com

    // 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

private void defaultTest(String projectName, String expectedOutputFolder) throws Exception {

    IProject p = importProject("projects/" + projectName + "/pom.xml");
    waitForJobsToComplete();/*from  w  w  w .  j  a  v  a  2s  .c o m*/

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

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

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

    FactoryPath factoryPath = (FactoryPath) AptConfig.getFactoryPath(javaProject);
    Iterator<FactoryContainer> ite = factoryPath.getEnabledContainers().keySet().iterator();
    FactoryContainer jpaModelGen = ite.next();
    assertEquals(FactoryContainer.FactoryType.VARJAR, jpaModelGen.getType());
    assertEquals(
            "M2_REPO/org/hibernate/hibernate-jpamodelgen/1.1.1.Final/hibernate-jpamodelgen-1.1.1.Final.jar",
            jpaModelGen.getId());

    FactoryContainer jpaApi = ite.next();
    assertEquals(FactoryContainer.FactoryType.VARJAR, jpaApi.getType());
    assertEquals(
            "M2_REPO/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.0.Final/hibernate-jpa-2.0-api-1.0.0.Final.jar",
            jpaApi.getId());

    /*
    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");
    assertTrue(generatedFile + " was not generated", generatedFile.exists());
    assertNoErrors(p);
}

From source file:org.maven.ide.eclipse.annotations.AptConfigurator.java

License:Open Source License

/**
 * Configures maven project with associated annotation processors.
 *
 * Limitations:/*from   ww w. j  av a2 s  .c o m*/
 * <ul>
 *  <li>There can be only one outputDirectory.</li>
 *  <li>There can be only one configuration per project.</li>
 *  <li>Might not work for processors that must be run in batch mode.</li>
 * </ul>
 *
 * @author Ivica Loncar
 */
@Override
public void configure(final ProjectConfigurationRequest p_request, final IProgressMonitor p_monitor)
        throws CoreException {
    super.configure(p_request, p_monitor);

    IProject project = p_request.getProject();
    if (project.hasNature(JavaCore.NATURE_ID)) {
        // enable annotation processing
        IJavaProject javaProject = JavaCore.create(project);
        AptConfig.setEnabled(javaProject, true);

        // Associate jars containing annotation processors.
        ProcessorConfiguration processorConfiguration = getProcessorConfiguration(p_request, p_monitor);

        List<ArtifactRepository> remoteArtifactRepositories = p_request.getMavenProject()
                .getRemoteArtifactRepositories();
        IMaven maven = MavenPlugin.getMaven();

        IFactoryPath factoryPath = AptConfig.getFactoryPath(javaProject);

        ArtifactMetadata[] artifactsMetadata = processorConfiguration.getProcessorArtifacts();
        for (ArtifactMetadata artifactMetadata : artifactsMetadata) {
            // Q: what about transitive dependencies?
            Artifact artifact = maven.resolve(artifactMetadata.getGroupId(), artifactMetadata.getArtifactId(),
                    artifactMetadata.getVersion(), artifactMetadata.getType(), artifactMetadata.getClassifier(),
                    remoteArtifactRepositories, p_monitor);

            File file = artifact.getFile();
            if ((file == null) || !file.exists() || !file.canRead()) {
                throw new IllegalStateException("Cannot find file for artifact " + artifact + " file:" + file);
            }

            factoryPath.addExternalJar(file);
        }

        AptConfig.setFactoryPath(javaProject, factoryPath);

        Map<String, String> optionMap = processorConfiguration.getOptionMap();
        // we would like to override existing files
        optionMap.put("defaultOverride", "true");
        AptConfig.setProcessorOptions(optionMap, javaProject);

        // output directory for generated sources
        AptConfig.setGenSrcDir(javaProject, processorConfiguration.getOutputDirectory());
        // From http://www.eclipse.org/forums/index.php?t=rview&goto=533747:
        //
        // Batch mode is mainly intended to support processors that can't handle incremental processing:
        // for example, that rely on static variables being properly initialized at the beginning of the run,
        // or that rely on being able to process all the files in a single round rather than one at a time,
        // or that make assumptions about how many rounds of processing will occur before  they exit.
        // Sun's JPA processors do, I think, need to be run in batch mode;
    }
}

From source file:org.maven.ide.querydsl.AptConfigurator.java

License:Open Source License

/**
 * @author Ivica Loncar//from  ww  w  . j a  va2 s. c  o  m
 */
@Override
public void configure(final ProjectConfigurationRequest p_request, final IProgressMonitor p_monitor)
        throws CoreException {
    super.configure(p_request, p_monitor);

    IProject project = p_request.getProject();
    if (project.hasNature(JavaCore.NATURE_ID)) {
        // enable annotation processing
        IJavaProject javaProject = JavaCore.create(project);
        AptConfig.setEnabled(javaProject, true);

        MavenFacade mavenFacade = new MavenFacade(p_request, p_monitor);
        // Associate annotation processor jar. Since querydsl doesn't use generic apt plugin nor specifies extra dependencies we have to hardcode it.
        QueryDslConfiguration queryDslConfiguration = getQueryDslConfiguration(mavenFacade);
        // IRepositoryRegistry repositoryRegistry = MavenPlugin.getRepositoryRegistry();
        // List<IRepository> repositories = repositoryRegistry.getRepositories(IRepositoryRegistry.SCOPE_LOCAL | IRepositoryRegistry.SCOPE_SETTINGS | IRepositoryRegistry.SCOPE_PROJECT);

        IMaven maven = MavenPlugin.getMaven();

        IPath m2RepoVar = JavaCore.getClasspathVariable(M2_REPO);
        IFactoryPath factoryPath = AptConfig.getFactoryPath(javaProject);

        ArtifactMetadata[] artifactsMetadata = queryDslConfiguration.getProcessorArtifacts();
        for (ArtifactMetadata artifactMetadata : artifactsMetadata) {
            Artifact artifact = mavenFacade.resolve(artifactMetadata);

            File file = artifact.getFile();
            if ((file == null) || !file.exists() || !file.canRead()) {
                throw new IllegalStateException("Cannot find file for artifact " + artifact + " file:" + file);
            }
            String filePath = file.getAbsolutePath();
            IPath path = Path.fromOSString(filePath);

            IPath relativeToM2Repo = path.makeRelativeTo(m2RepoVar);

            IPath jarPath = Path.fromOSString("M2_REPO").append(relativeToM2Repo);

            factoryPath.addVarJar(jarPath);
        }

        AptConfig.setFactoryPath(javaProject, factoryPath);

        Map<String, String> options = new HashMap<String, String>() {
            {
                // we would like to override existing files
                put("defaultOverride", "true");
            }
        };
        AptConfig.setProcessorOptions(options, javaProject);

        AptConfig.setGenSrcDir(javaProject, queryDslConfiguration.getOutputDirectory());
    }
}