Search code examples
javaeclipseeclipse-plugineclipse-jdtxtend

Programmatically compiling Xtend classes is not working


I am currently trying to compile programmatically generated Xtend classes. This is all part of an Eclipse plugin. This is what I do:

  • Adding the Xtend dependencies programmatically to the target project (works).
  • Creating programmatically some Xtend classes in the project with IProject.getFolder(), IFolder.getFile() and IFile.create() (JDT API).
  • Resfreshing the whole project with IProject.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
  • Compiling the project with IProject.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());

Now, as a result, I can see the generated classes in the Eclipse IDE. The problem is, there are no generated Java classes for the Xtend classes in the xtend-gen folder.

When I now open one of the generated Xtend classes manually in the Eclipse IDE, it will trigger the compilation. Now I can see the generated Java classes for the Xtend classes.

But I need to do that programmatically. Without opening one Xtend class manually. How can I do that? What is the problem here? Why am I not triggering the Xtend compilation?


Solution

  • Seems like I did not update the project description correctly. The Xtext builder was not set.

    This how I do that now:

    private static void updateProjectDescription(IProject project) {
        String builderName = "org.eclipse.xtext.ui.shared.xtextBuilder";
        String xtextNature = "org.eclipse.xtext.ui.shared.xtextNature";
        IProjectDescription description = null;
        try {
            description = project.getDescription();
        } catch (CoreException exception) {
            exception.printStackTrace();
        }
        // add xtext builder:
        ICommand[] commands = description.getBuildSpec();
        ICommand command = description.newCommand();
        command.setBuilderName(builderName);
        if (Arrays.asList(commands).contains(command)) {
            logger.warn(".project already contains " + builderName);
        } else {
            ICommand[] newCommands = new ICommand[commands.length + 1];
            System.arraycopy(commands, 0, newCommands, 0, commands.length);
            newCommands[commands.length] = command;
            description.setBuildSpec(newCommands);
        }
        // Add xtext nature:
        String[] natures = description.getNatureIds();
        if (Arrays.asList(natures).contains(xtextNature)) {
            logger.warn(".project already contains " + xtextNature);
        } else {
            String[] newNatures = new String[natures.length + 1];
            System.arraycopy(natures, 0, newNatures, 0, natures.length);
            newNatures[natures.length] = xtextNature;
            description.setNatureIds(newNatures);
        }
        try {
            project.setDescription(description, new ProgressMonitorAdapter(logger));
        } catch (CoreException exception) {
            logger.fatal(exception);
        }
    }