Este ejemplo de código es para contar el número total de métodos en proyectos en el espacio de trabajo de Eclipse. Podemos usar Eclipse JDT – Java Model para hacer esto. Contar el número total de métodos en un determinado proyecto Java es una tarea muy sencilla.
El siguiente código solo funciona en el proyecto Eclipse Plug-in. Si no sabe cómo crear un complemento, puede leer Cómo crear un complemento.
private void processRootDirectory() throws JavaModelException, CoreException { IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); System.out.println("root" + root.getLocation().toOSString()); IProject[] projects = root.getProjects(); // process each project for (IProject project : projects) { System.out.println("project name: " + project.getName()); if (project.isNatureEnabled("org.eclipse.jdt.core.javanature")) { IJavaProject javaProject = JavaCore.create(project); IPackageFragment[] packages = javaProject.getPackageFragments(); // process each package for (IPackageFragment aPackage : packages) { // We will only look at the package from the source folder // K_BINARY would include also included JARS, e.g. rt.jar // only process the JAR files if (aPackage.getKind() == IPackageFragmentRoot.K_SOURCE) { for (ICompilationUnit unit : aPackage .getCompilationUnits()) { System.out.println("--class name: " + unit.getElementName()); IType[] allTypes = unit.getAllTypes(); for (IType type : allTypes) { IMethod[] methods = type.getMethods(); for (IMethod method : methods) { totalMethod++; System.out.println("--Method name: "+ method.getElementName()); System.out.println("Signature: "+ method.getSignature()); System.out.println("Return Type: "+ method.getReturnType()); System.out.println("source: "+ method.getSource()); System.out.println("to string: "+ method.toString()); System.out.println("new: "+ method.getPath().toString()); } } } } } } } } |
Referencia:
1. crear una aplicación de complemento de eclipse simple