0

Typically, when you are parsing Java code with JDT, you make it as a plugin and do something like this where

1 You first make a project:

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject(projectName);
project.create(null);
project.open(null);
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] { JavaCore.NATURE_ID });
project.setDescription(description, null);
IJavaProject javaProject = JavaCore.create(project);

2 then set the project to your ASTParser:

ASTParser parser = ASTParser.newParser(AST.JLS9);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setProject(javaProject);

Is there a way I can achieve what the above is doing without having to open Eclipse and run as a plugin? There has to be a way to create the same data structures programmatically by recreating the same environment to be as if it is running on the Eclipse IDE. Particularly, I feel like the easiest would be creating a "IJavaProject" object somehow.

What are some ways to do this?

I know making it a headless RCP application is a possibility, but I want to know if I can make it as a standalone Java application by using the appropriate jars or even modifying the jars and creating my own.

itsmarziparzi
  • 699
  • 7
  • 21
  • The JDT core plugin can be used as a ordinary jar see for example [this question](https://stackoverflow.com/q/12534012/2670892) – greg-449 Jan 30 '18 at 08:04
  • But you still won't be able to run for example "IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();", right? As you said, I'm trying to use the jar, but my question is tailored to how to use the jar. – itsmarziparzi Jan 30 '18 at 08:42
  • You can't use any of the methods that use things like IWorkspaceRoot but there are other methods which just accept file path strings or the actual source code. Example [here](https://www.programcreek.com/2011/01/a-complete-standalone-example-of-astparser/) – greg-449 Jan 30 '18 at 08:47
  • I've seen those already, but those examples only parse one java file at a time. I need to be able to resolve binding across multiple files, and from what I've seen, the only way to do that is to do "parser.setProject(javaProject);" Hence, I believe I need a IJavaProject object. If you know a way to do this without a IJavaProject, please help me. – itsmarziparzi Jan 30 '18 at 08:51
  • Sounds like you are asking: "how can I use an Eclipse workspace without using a workspace":) Since IJavaProject depends on IResource etc. you'd need the dependencies that implement the resource model. A better question might be: what is the leanest way to create a headless RCP application? – Stephan Herrmann Feb 01 '18 at 17:25
  • Hi, did you happen to find a solution to your question? – Majeed Askari Sep 13 '22 at 19:24

0 Answers0