I am writting an ordinary Java application and want to extract all ICompilationUnit of an input project (which is not necessary developed by Eclipse). As I am not developing an Eclipse plugin, I can't use the below code to extract ICompilationUnit:
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath path = Path.fromOSString(source.getAbsolutePath());
IFile file = workspace.getRoot().getFileForLocation(path);
ICompilationUnit compilationUnit = (ICompilationUnit) JavaCore.create(file);
Currently, I am using the below code to parse the input Java file. (str contains the source code of input java file)
ASTParser parser = ASTParser.newParser(AST.JLS12);
parser.setSource(str.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
However, the below code return null as it was not created from a Java element.
ICompilationUnit icu = (ICompilationUnit)compilationUnit.getJavaElement();
Question: Is there any way to extract ICompilationUnits in an ordinary Java Application?