Created
April 20, 2014 00:15
-
-
Save samueltcsantos/11101434 to your computer and use it in GitHub Desktop.
Como utilizar ASTparser e MethodVisitor para localizar os métodos do projeto.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jdt.info.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.jdt.core.dom.AST; | |
import org.eclipse.jdt.core.dom.ASTParser; | |
import org.eclipse.jdt.core.dom.CompilationUnit; | |
import org.eclipse.jdt.core.dom.MethodDeclaration; | |
/** | |
* Our sample handler extends AbstractHandler, an IHandler base class. | |
* | |
* @author Samuel T. C. Santos | |
* | |
* @see org.eclipse.core.commands.IHandler | |
* @see org.eclipse.core.commands.AbstractHandler | |
* | |
* @see {https://github.com/samueltcsantos/eclipse-jdt} | |
* | |
*/ | |
public class SampleHandler extends AbstractHandler { | |
private IWorkspace workspace; | |
private IWorkspaceRoot root; | |
private final static String NATURE_ID = "org.eclipse.jdt.core.javanature"; | |
/** | |
* The constructor. | |
* | |
* @see {https://github.com/samueltcsantos/eclipse-jdt} | |
*/ | |
public SampleHandler() { | |
} | |
/** | |
* the command has been executed, so extract extract the needed information | |
* from the application context. | |
* | |
* @see {https://github.com/samueltcsantos/eclipse-jdt} | |
* | |
*/ | |
public Object execute(ExecutionEvent event) throws ExecutionException { | |
workspace = ResourcesPlugin.getWorkspace(); | |
root = workspace.getRoot(); | |
IProject[] projects = root.getProjects(); | |
for (IProject project : projects){ | |
try { | |
analyseMethods(project); | |
} catch (CoreException e) { | |
e.printStackTrace(); | |
} | |
} | |
return null; | |
} | |
/** | |
* Show the project name and create a IJavaProject object. | |
* | |
* @see {https://github.com/samueltcsantos/eclipse-jdt} | |
* | |
* @param project | |
* @throws CoreException | |
*/ | |
public IJavaProject createIJavaProject(IProject project) throws CoreException { | |
if (project.isNatureEnabled("org.eclipse.jdt.core.javanature")){ | |
IJavaProject javaProject = JavaCore.create(project); | |
return javaProject; | |
} | |
else{ | |
throw new CoreException(null); | |
} | |
} | |
/** | |
* Analyse the packages in the project. | |
* | |
* @param project | |
* @throws CoreException | |
*/ | |
private void analyseMethods(IProject project) throws CoreException { | |
IPackageFragment[] packages = createIJavaProject(project).getPackageFragments(); | |
for (IPackageFragment mypackage : packages) { | |
if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) { | |
createAST(mypackage); | |
} | |
} | |
} | |
/** | |
* Using the visitor to list all methods. | |
* | |
* @param mypackage | |
* @throws JavaModelException | |
*/ | |
private void createAST(IPackageFragment mypackage) throws JavaModelException { | |
for (ICompilationUnit unit : mypackage.getCompilationUnits()) { | |
// now create the AST for the ICompilationUnits | |
CompilationUnit parse = createParse(unit); | |
MethodVisitor visitor = new MethodVisitor(); | |
parse.accept(visitor); | |
for (MethodDeclaration method : visitor.getMethods()) { | |
System.out.print("\nMethod name: " + method.getName()); | |
} | |
} | |
} | |
/** | |
* Reads a ICompilationUnit and creates the AST DOM for manipulating the | |
* Java source file | |
* | |
* @see {https://github.com/samueltcsantos/eclipse-jdt} | |
* | |
* @param unit | |
* @return | |
*/ | |
public static CompilationUnit createParse(ICompilationUnit unit) { | |
ASTParser parser = ASTParser.newParser(AST.JLS3); | |
parser.setKind(ASTParser.K_COMPILATION_UNIT); | |
parser.setSource(unit); | |
parser.setResolveBindings(true); | |
return (CompilationUnit) parser.createAST(null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment