當前位置: 首頁>>代碼示例>>Java>>正文


Java IMethod.getCompilationUnit方法代碼示例

本文整理匯總了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;
}
 
開發者ID:VisuFlow,項目名稱:visuflow-plugin,代碼行數:74,代碼來源:BreakpointLocator.java


注:本文中的org.eclipse.jdt.core.IMethod.getCompilationUnit方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。