当前位置: 首页>>代码示例>>Java>>正文


Java CompilationUnit.getAST方法代码示例

本文整理汇总了Java中org.eclipse.jdt.core.dom.CompilationUnit.getAST方法的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnit.getAST方法的具体用法?Java CompilationUnit.getAST怎么用?Java CompilationUnit.getAST使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.eclipse.jdt.core.dom.CompilationUnit的用法示例。


在下文中一共展示了CompilationUnit.getAST方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getRewrite

import org.eclipse.jdt.core.dom.CompilationUnit; //导入方法依赖的package包/类
@Override
protected ASTRewrite getRewrite() throws CoreException {
	CompilationUnit astRoot= fContext.getASTRoot();

	AST ast= astRoot.getAST();
	ASTRewrite rewrite= ASTRewrite.create(ast);

	AbstractTypeDeclaration decl= findTypeDeclaration(astRoot.types(), fOldName);
	if (decl != null) {
		ASTNode[] sameNodes= LinkedNodeFinder.findByNode(astRoot, decl.getName());
		for (int i= 0; i < sameNodes.length; i++) {
			rewrite.replace(sameNodes[i], ast.newSimpleName(fNewName), null);
		}
	}
	return rewrite;
}
 
开发者ID:eclipse,项目名称:eclipse.jdt.ls,代码行数:17,代码来源:CorrectMainTypeNameProposal.java

示例2: addBuilder

import org.eclipse.jdt.core.dom.CompilationUnit; //导入方法依赖的package包/类
private void addBuilder(ICompilationUnit iCompilationUnit, BuilderType builderType) {
    CompilationUnit compilationUnit = compilationUnitParser.parse(iCompilationUnit);
    AST ast = compilationUnit.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);

    CompilationUnitModificationDomain compilationUnitModificationDomain = builderOwnerClassFinder.provideBuilderOwnerClass(compilationUnit, ast, rewriter, iCompilationUnit);

    builderGenerators.stream()
            .filter(builderGenerator -> builderGenerator.canHandle(builderType))
            .findFirst()
            .orElseThrow(() -> new IllegalStateException("No builder generator can handle " + builderType))
            .generateBuilder(compilationUnitModificationDomain);

    compilationUnitSourceSetter.commitCodeChange(iCompilationUnit, rewriter);
}
 
开发者ID:helospark,项目名称:SparkBuilderGenerator,代码行数:16,代码来源:GenerateBuilderExecutorImpl.java

示例3: addMissingReturnTypeProposals

import org.eclipse.jdt.core.dom.CompilationUnit; //导入方法依赖的package包/类
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null) {
		return;
	}
	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methodDeclaration= (MethodDeclaration) decl;

		ReturnStatementCollector eval= new ReturnStatementCollector();
		decl.accept(eval);

		AST ast= astRoot.getAST();

		ITypeBinding typeBinding= eval.getTypeBinding(decl.getAST());
		typeBinding= Bindings.normalizeTypeBinding(typeBinding);
		if (typeBinding == null) {
			typeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
		}
		if (typeBinding.isWildcardType()) {
			typeBinding= ASTResolving.normalizeWildcardType(typeBinding, true, ast);
		}

		ASTRewrite rewrite= ASTRewrite.create(ast);

		String label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE);

		ImportRewrite imports= proposal.createImportRewrite(astRoot);
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);
		Type type= imports.addImport(typeBinding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);

		rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
		rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);

		Javadoc javadoc= methodDeclaration.getJavadoc();
		if (javadoc != null && typeBinding != null) {
			TagElement newTag= ast.newTagElement();
			newTag.setTagName(TagElement.TAG_RETURN);
			TextElement commentStart= ast.newTextElement();
			newTag.fragments().add(commentStart);

			JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
			proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start"); //$NON-NLS-1$
		}

		String key= "return_type"; //$NON-NLS-1$
		proposal.addLinkedPosition(rewrite.track(type), true, key);
		if (typeBinding != null) {
			ITypeBinding[] bindings= ASTResolving.getRelaxingTypes(ast, typeBinding);
			for (int i= 0; i < bindings.length; i++) {
				proposal.addLinkedPositionProposal(key, bindings[i]);
			}
		}

		proposals.add(proposal);

		// change to constructor
		ASTNode parentType= ASTResolving.findParentType(decl);
		if (parentType instanceof AbstractTypeDeclaration) {
			boolean isInterface= parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
			if (!isInterface) {
				String constructorName= ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
				ASTNode nameNode= methodDeclaration.getName();
				label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
				proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
			}
		}
	}
}
 
开发者ID:eclipse,项目名称:eclipse.jdt.ls,代码行数:74,代码来源:ReturnTypeSubProcessor.java

示例4: doAddParam

import org.eclipse.jdt.core.dom.CompilationUnit; //导入方法依赖的package包/类
private ASTRewrite doAddParam(CompilationUnit cu) {
	AST ast= cu.getAST();
	SimpleName node= fOriginalNode;

	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(node);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methodDeclaration= (MethodDeclaration) decl;

		ASTRewrite rewrite= ASTRewrite.create(ast);

		ImportRewrite imports= createImportRewrite((CompilationUnit) decl.getRoot());
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);

		SingleVariableDeclaration newDecl= ast.newSingleVariableDeclaration();
		newDecl.setType(evaluateVariableType(ast, imports, importRewriteContext, methodDeclaration.resolveBinding(), TypeLocation.PARAMETER));
		newDecl.setName(ast.newSimpleName(node.getIdentifier()));

		ListRewrite listRewriter= rewrite.getListRewrite(decl, MethodDeclaration.PARAMETERS_PROPERTY);
		listRewriter.insertLast(newDecl, null);

		// add javadoc tag
		Javadoc javadoc= methodDeclaration.getJavadoc();
		if (javadoc != null) {
			HashSet<String> leadingNames= new HashSet<>();
			for (Iterator<SingleVariableDeclaration> iter= methodDeclaration.parameters().iterator(); iter.hasNext();) {
				SingleVariableDeclaration curr= iter.next();
				leadingNames.add(curr.getName().getIdentifier());
			}
			SimpleName newTagRef= ast.newSimpleName(node.getIdentifier());

			TagElement newTagElement= ast.newTagElement();
			newTagElement.setTagName(TagElement.TAG_PARAM);
			newTagElement.fragments().add(newTagRef);
			TextElement commentStart= ast.newTextElement();
			newTagElement.fragments().add(commentStart);

			ListRewrite tagsRewriter= rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
			JavadocTagsSubProcessor.insertTag(tagsRewriter, newTagElement, leadingNames);
		}

		return rewrite;
	}
	return null;
}
 
开发者ID:eclipse,项目名称:eclipse.jdt.ls,代码行数:45,代码来源:NewVariableCorrectionProposal.java

示例5: addStaticImportFavoriteProposals

import org.eclipse.jdt.core.dom.CompilationUnit; //导入方法依赖的package包/类
private static void addStaticImportFavoriteProposals(IInvocationContext context, SimpleName node, boolean isMethod,
		Collection<CUCorrectionProposal> proposals) throws JavaModelException {
	IJavaProject project= context.getCompilationUnit().getJavaProject();
	if (JavaModelUtil.is50OrHigher(project)) {
		String pref = PreferenceManager.getPrefs(context.getCompilationUnit().getResource())
				.getFavoriteStaticMembers();
		String[] favourites= pref.split(";"); //$NON-NLS-1$
		if (favourites.length == 0) {
			return;
		}

		CompilationUnit root= context.getASTRoot();
		AST ast= root.getAST();

		String name= node.getIdentifier();
		String[] staticImports= SimilarElementsRequestor.getStaticImportFavorites(context.getCompilationUnit(), name, isMethod, favourites);
		for (int i= 0; i < staticImports.length; i++) {
			String curr= staticImports[i];

			ImportRewrite importRewrite= StubUtility.createImportRewrite(root, true);
			ASTRewrite astRewrite= ASTRewrite.create(ast);

			String label;
			String qualifiedTypeName= Signature.getQualifier(curr);
			String elementLabel= BasicElementLabels.getJavaElementName(JavaModelUtil.concatenateName(Signature.getSimpleName(qualifiedTypeName), name));

			String res= importRewrite.addStaticImport(qualifiedTypeName, name, isMethod, new ContextSensitiveImportRewriteContext(root, node.getStartPosition(), importRewrite));
			int dot= res.lastIndexOf('.');
			if (dot != -1) {
				String usedTypeName= importRewrite.addImport(qualifiedTypeName);
				Name newName= ast.newQualifiedName(ast.newName(usedTypeName), ast.newSimpleName(name));
				astRewrite.replace(node, newName, null);
				label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_change_to_static_import_description, elementLabel);
			} else {
				label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_add_static_import_description, elementLabel);
			}

			ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label,
					context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_STATIC_IMPORT);
			proposal.setImportRewrite(importRewrite);
			proposals.add(proposal);
		}
	}
}
 
开发者ID:eclipse,项目名称:eclipse.jdt.ls,代码行数:45,代码来源:UnresolvedElementsSubProcessor.java


注:本文中的org.eclipse.jdt.core.dom.CompilationUnit.getAST方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。