org.eclipse.jdt.core.dom.BodyDeclaration#accept ( )源码实例Demo

下面列出了org.eclipse.jdt.core.dom.BodyDeclaration#accept ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

public static ITypeBinding[] perform(BodyDeclaration enclosingNode, Selection selection) {
	ExceptionAnalyzer analyzer= new ExceptionAnalyzer(selection);
	enclosingNode.accept(analyzer);
	List<ITypeBinding> exceptions= analyzer.getCurrentExceptions();
	if (enclosingNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
		List<Type> thrownExceptions= ((MethodDeclaration) enclosingNode).thrownExceptionTypes();
		for (Iterator<Type> thrown= thrownExceptions.iterator(); thrown.hasNext();) {
			ITypeBinding thrownException= thrown.next().resolveBinding();
			if (thrownException != null) {
				for (Iterator<ITypeBinding> excep= exceptions.iterator(); excep.hasNext();) {
					ITypeBinding exception= excep.next();
					if (exception.isAssignmentCompatible(thrownException))
						excep.remove();
				}
			}
		}
	}
	Collections.sort(exceptions, new ExceptionComparator());
	return exceptions.toArray(new ITypeBinding[exceptions.size()]);
}
 
源代码2 项目: eclipse.jdt.ls   文件: LocalTypeAnalyzer.java
public static RefactoringStatus perform(BodyDeclaration declaration, Selection selection) {
	LocalTypeAnalyzer analyzer = new LocalTypeAnalyzer(selection);
	declaration.accept(analyzer);
	RefactoringStatus result = new RefactoringStatus();
	analyzer.check(result);
	return result;
}
 
public static RefactoringStatus perform(BodyDeclaration declaration, Selection selection) {
	LocalTypeAnalyzer analyzer= new LocalTypeAnalyzer(selection);
	declaration.accept(analyzer);
	RefactoringStatus result= new RefactoringStatus();
	analyzer.check(result);
	return result;
}
 
/**
 * @param scope not a TypeDeclaration
 * @return Set containing Strings representing simple names
 */
private Set<String> getLocallyDeclaredNames(BodyDeclaration scope) {
	Assert.isTrue(!(scope instanceof AbstractTypeDeclaration));

	final Set<String> result= new HashSet<String>();

	if (scope instanceof FieldDeclaration)
		return result;

	scope.accept(new HierarchicalASTVisitor() {

		@Override
		public boolean visit(AbstractTypeDeclaration node) {
			Assert.isTrue(node.getParent() instanceof TypeDeclarationStatement);

			result.add(node.getName().getIdentifier());
			return false;
		}

		@Override
		public boolean visit(AnonymousClassDeclaration anonDecl) {
			return false;
		}

		@Override
		public boolean visit(VariableDeclaration varDecl) {
			result.add(varDecl.getName().getIdentifier());
			return false;
		}
	});
	return result;
}
 
private boolean addLocalDeclarations(ASTNode node, int offset, int flags, IBindingRequestor requestor) {
	if (hasFlag(VARIABLES, flags) || hasFlag(TYPES, flags)) {
		BodyDeclaration declaration= ASTResolving.findParentBodyDeclaration(node);
		if (declaration instanceof MethodDeclaration || declaration instanceof Initializer || declaration instanceof FieldDeclaration) {
			ScopeAnalyzerVisitor visitor= new ScopeAnalyzerVisitor(offset, flags, requestor);
			declaration.accept(visitor);
			return visitor.fBreak;
		}
	}
	return false;
}
 
源代码6 项目: eclipse.jdt.ls   文件: LocalVariableIndex.java
private static int doPerform(BodyDeclaration node) {
	LocalVariableIndex counter = new LocalVariableIndex();
	node.accept(counter);
	return counter.fTopIndex;
}
 
源代码7 项目: eclipse.jdt.ls   文件: CodeScopeBuilder.java
public static Scope perform(BodyDeclaration node, IBinding ignore) {
	CodeScopeBuilder collector = new CodeScopeBuilder(node, ignore);
	node.accept(collector);
	return collector.fScope;
}
 
源代码8 项目: eclipse.jdt.ls   文件: CodeScopeBuilder.java
public static Scope perform(BodyDeclaration node, Selection ignore) {
	CodeScopeBuilder collector = new CodeScopeBuilder(node, ignore);
	node.accept(collector);
	return collector.fScope;
}
 
源代码9 项目: eclipse.jdt.ls   文件: InputFlowAnalyzer.java
public FlowInfo perform(BodyDeclaration node) {
	Assert.isTrue(!(node instanceof AbstractTypeDeclaration));
	node.accept(this);
	return getFlowInfo(node);
}
 
源代码10 项目: eclipse.jdt.ls   文件: ReturnTypeSubProcessor.java
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> 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, BindingLabelProviderCore.getBindingLabel(typeBinding, BindingLabelProviderCore.DEFAULT_TEXTFLAGS));
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, 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));
			}
		}
	}
}
 
源代码11 项目: xtext-xtend   文件: JavaASTFlattener.java
@Override
public boolean visit(final TypeDeclaration it) {
  boolean _isDummyType = this._aSTFlattenerUtils.isDummyType(it);
  if (_isDummyType) {
    this.visitAll(it.bodyDeclarations(), this.nl());
    return false;
  }
  boolean _isNotSupportedInnerType = this._aSTFlattenerUtils.isNotSupportedInnerType(it);
  if (_isNotSupportedInnerType) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/* FIXME Non-static inner classes are not supported.*/");
    this.appendToBuffer(_builder.toString());
    this.addProblem(it, "Non-static inner classes are not supported.");
  }
  Javadoc _javadoc = it.getJavadoc();
  boolean _tripleNotEquals = (_javadoc != null);
  if (_tripleNotEquals) {
    it.getJavadoc().accept(this);
  }
  this.appendModifiers(it, it.modifiers());
  boolean _isInterface = it.isInterface();
  if (_isInterface) {
    this.appendToBuffer("interface ");
  } else {
    boolean _isPackageVisibility = this._aSTFlattenerUtils.isPackageVisibility(Iterables.<Modifier>filter(it.modifiers(), Modifier.class));
    if (_isPackageVisibility) {
      this.appendToBuffer("package ");
    }
    this.appendToBuffer("class ");
  }
  it.getName().accept(this);
  boolean _isEmpty = it.typeParameters().isEmpty();
  boolean _not = (!_isEmpty);
  if (_not) {
    this.appendTypeParameters(it.typeParameters());
  }
  this.appendSpaceToBuffer();
  Type _superclassType = it.getSuperclassType();
  boolean _tripleNotEquals_1 = (_superclassType != null);
  if (_tripleNotEquals_1) {
    this.appendToBuffer("extends ");
    it.getSuperclassType().accept(this);
    this.appendSpaceToBuffer();
  }
  boolean _isEmpty_1 = it.superInterfaceTypes().isEmpty();
  boolean _not_1 = (!_isEmpty_1);
  if (_not_1) {
    boolean _isInterface_1 = it.isInterface();
    if (_isInterface_1) {
      this.appendToBuffer("extends ");
    } else {
      this.appendToBuffer("implements ");
    }
    this.visitAllSeparatedByComma(it.superInterfaceTypes());
  }
  this.appendToBuffer("{");
  this.increaseIndent();
  BodyDeclaration prev = null;
  List _bodyDeclarations = it.bodyDeclarations();
  for (final BodyDeclaration body : ((Iterable<BodyDeclaration>) _bodyDeclarations)) {
    {
      if ((prev instanceof EnumConstantDeclaration)) {
        if ((body instanceof EnumConstantDeclaration)) {
          this.appendToBuffer(", ");
        } else {
          this.appendToBuffer("; ");
        }
      }
      this.appendLineWrapToBuffer();
      body.accept(this);
      prev = body;
    }
  }
  ASTNode _root = it.getRoot();
  if ((_root instanceof CompilationUnit)) {
    ASTNode _root_1 = it.getRoot();
    final CompilationUnit cu = ((CompilationUnit) _root_1);
    final Consumer<Comment> _function = (Comment it_1) -> {
      it_1.accept(this);
      this.assignedComments.add(it_1);
    };
    this.unAssignedComments(cu).forEach(_function);
  }
  this.decreaseIndent();
  this.appendLineWrapToBuffer();
  this.appendToBuffer("}");
  return false;
}
 
public FlowInfo perform(BodyDeclaration node) {
	Assert.isTrue(!(node instanceof AbstractTypeDeclaration));
	node.accept(this);
	return getFlowInfo(node);
}
 
public static VariableDeclaration[] perform(BodyDeclaration parent, Selection selection) {
	LocalDeclarationAnalyzer analyzer= new LocalDeclarationAnalyzer(selection);
	parent.accept(analyzer);
	return analyzer.fAffectedLocals.toArray(new VariableDeclaration[analyzer.fAffectedLocals.size()]);
}
 
private static int doPerform(BodyDeclaration node) {
	LocalVariableIndex counter= new LocalVariableIndex();
	node.accept(counter);
	return counter.fTopIndex;
}
 
public static Scope perform(BodyDeclaration node, IBinding ignore) {
	CodeScopeBuilder collector= new CodeScopeBuilder(node, ignore);
	node.accept(collector);
	return collector.fScope;
}
 
public static Scope perform(BodyDeclaration node, Selection ignore) {
	CodeScopeBuilder collector= new CodeScopeBuilder(node, ignore);
	node.accept(collector);
	return collector.fScope;
}
 
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> 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));
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE, image);

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

		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));
			}
		}
	}
}