Lars Vogel's Eclipse Source Code gives very simple and effective way of installing eclipse source.
IResource resource = iCompilationUnit.getUnderlyingResource();
if (resource.getType() == IResource.FILE) {
IFile ifile = (IFile) resource;
String path = ifile.getRawLocation().toString();
}
Document document = new Document(iCompilationUnit.getSource()); ASTRewrite rewriter = ASTRewrite.create(compilationUnit); // ? check if the parameter object type is correct rewrite.rewriteAST().apply(document); String source = document.get(); File file = new File(DEST_FILE_PATH); FileUtils.writeStringToFile(file, source)
Click to enlarge
IWorkspace workspace = ResourcesPlugin.getWorkspace(); IWorkspaceRoot root = workspace.getRoot(); // Get all projects in the workspace IProject[] projects = root.getProjects();When you know the name of the project, you can find it.
IProject project = root.getProject("Hello");
Out of many project, you need only Java project.
IJavaProject javaProject = JavaCore.create(project);
IPackageFragment[] packages = javaProject.getPackageFragments();
for (IPackageFragment mypackage : packages) {
if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
IPackageFragment[] packages = JavaCore.create(project).getPackageFragments();
for (IPackageFragment mypackage : packages) {
if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
...
}
}
}
When you know the name of the type, you can get ICompilationUnit from it.
IType iType = javaProject.findType("PACKAGE_NAME.CLASS_NAME");
org.eclipse.jdt.core.ICompilationUnit iCompilationUnit = iType.getCompilationUnit();
final ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(iCompilationUnit); parser.setResolveBindings(true); // we need bindings later on CompilationUnit unit = (CompilationUnit) parser.createAST(null);
// … lwType is IType object org.eclipse.jdt.core.ICompilationUnit lwCompilationUnit = lwType.getCompilationUnit(); // create parser and set it up final ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(lwCompilationUnit); parser.setResolveBindings(true); // we need bindings later on // can get CompilationUnit from parser CompilationUnit unit = (CompilationUnit) parser.createAST(null /* IProgressMonitor */);