本文整理汇总了Java中org.eclipse.jdt.core.IMethod.getCompilationUnit方法的典型用法代码示例。如果您正苦于以下问题:Java IMethod.getCompilationUnit方法的具体用法?Java IMethod.getCompilationUnit怎么用?Java IMethod.getCompilationUnit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.IMethod
的用法示例。
在下文中一共展示了IMethod.getCompilationUnit方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findFlowFunctions
import org.eclipse.jdt.core.IMethod; //导入方法依赖的package包/类
/**
* Searches the current analysis project for known flow functions and returns
* a list of BreakpointLocations, which contain all information needed to set
* a Java line breakpoint.
* @return a list of {@link BreakpointLocation}s
* @throws JavaModelException
*/
public List<BreakpointLocation> findFlowFunctions() throws JavaModelException {
List<BreakpointLocation> locations = new ArrayList<>();
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot workspaceRoot = workspace.getRoot();
IJavaModel model = JavaCore.create(workspaceRoot);
String analysisProjectName = GlobalSettings.get("AnalysisProject");
IJavaElement analysisProject = getJavaProject(model, analysisProjectName);
List<IJavaElement> sourceFolders = getSourceFolders(analysisProject);
List<IJavaElement> flowFunctions = new ArrayList<>();
for (IJavaElement packageFragment : sourceFolders) {
for (String functionName : flowFunctionNames) {
findRecursive(flowFunctions, packageFragment, functionName);
}
}
for (IJavaElement flowFunction : flowFunctions) {
IMethod method = (IMethod) flowFunction;
ICompilationUnit cu = method.getCompilationUnit();
String sourceCode = cu.getSource();
ASTParser parser = ASTParser.newParser(AST.JLS8);
// Parse the class as a compilation unit.
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(sourceCode.toCharArray());
parser.setResolveBindings(true);
// Return the compiled class as a compilation unit
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
compilationUnit.accept(new ASTVisitor() {
@Override
public boolean visit(MethodDeclaration node) {
int lineNumber = compilationUnit.getLineNumber(node.getName().getStartPosition());
if (node.getName().toString().equals(method.getElementName())) {
IResource res = cu.getResource();
try {
BreakpointLocation location = new BreakpointLocation();
location.method = method;
location.resource = res;
location.className = method.getDeclaringType().getFullyQualifiedName();
location.methodName = method.getElementName();
location.methodSignature = resolveMethodSignature(method);
location.lineNumber = lineNumber;
location.offset = node.getName().getStartPosition();
location.length = node.getName().getLength();
locations.add(location);
} catch (CoreException e) {
exception = e;
return false;
}
}
return true;
}
});
if(exception != null) {
throw new JavaModelException(exception);
}
}
return locations;
}