RSS

Search Engine

Saturday, July 10, 2010

Accessing your Java projects with the JDT Java model

The following will create a command which will read the project of the workspace, get all package and Java source files for these projects and read all methods for them by using the JDT Java Model API.

Create the plugin project "de.vogella.jdt.infos". Choose the "Hello World, command" as a template.

Add the following dependencies to your plugin "org.eclipse.core.resources", "org.eclipse.jdt", org.eclipse.jdt.core", "org.eclipse.core.runtime", "org.eclipse.jdt.ui", "org.eclipse.jface.text"

Change the handler to the following.

   
package de.vogella.jdt.infos.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.text.Document;

public class SampleHandler extends AbstractHandler {

public Object execute(ExecutionEvent event) throws ExecutionException {
// Get the root of the workspace
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
// Get all projects in the workspace
IProject[] projects = root.getProjects();
// Loop over all projects
for (IProject project : projects) {
try {
printProjectInfo(project);
} catch (CoreException e) {
e.printStackTrace();
}
}
return null;
}

private void printProjectInfo(IProject project) throws CoreException,
JavaModelException {
System.out.println("Working in project " + project.getName());
// Check if we have a Java project
if (project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
IJavaProject javaProject = JavaCore.create(project);
printPackageInfos(javaProject);
}
}

private void printPackageInfos(IJavaProject javaProject)
throws JavaModelException {
IPackageFragment[] packages = javaProject.getPackageFragments();
for (IPackageFragment mypackage : packages) {
// Package fragments include all packages in the
// classpath
// We will only look at the package from the source
// folder
// K_BINARY would include also included JARS, e.g.
// rt.jar
if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
System.out.println("Package " + mypackage.getElementName());
printICompilationUnitInfo(mypackage);

}

}
}

private void printICompilationUnitInfo(IPackageFragment mypackage)
throws JavaModelException {
for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
System.out.println("Source file " + unit.getElementName());
Document doc = new Document(unit.getSource());
System.out
.println("Has number of lines: " + doc.getNumberOfLines());
printIMethods(unit);

}
}

private void printIMethods(ICompilationUnit unit) throws JavaModelException {
IType[] allTypes = unit.getAllTypes();
for (IType type : allTypes) {
IMethod[] methods = type.getMethods();
for (IMethod method : methods) {

System.out.println("Method name " + method.getElementName());
System.out.println("Signature " + method.getSignature());
System.out.println("Return Type " + method.getReturnType());

}
}
}
}

Start your plugin. Create a few projects in your new workspace. Create a few packages for them and a few Java files. Press the menu entry which points to your sample command.

You should see the projects, package and source files listed in the console of the calling workbench.

0 comments:

Post a Comment