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

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

源代码1 项目: api-mining   文件: TypenameScopeExtractor.java
private Multimap<Scope, String> getClassnames(final ASTNode node) {
	final ClassnameFinder cf = new ClassnameFinder(methodsAsRoots);
	node.accept(cf);

	final Multimap<Scope, String> classnames = TreeMultimap.create();

	for (final Entry<ASTNode, String> classname : cf.types.entries()) {
		final ASTNode parentNode = classname.getKey();
		final Scope sc = new Scope(
				classname.getKey().toString(),
				parentNode.getNodeType() == ASTNode.METHOD_DECLARATION ? ScopeType.SCOPE_METHOD
						: ScopeType.SCOPE_CLASS, TYPENAME,
				parentNode.getNodeType(), -1);
		classnames.put(sc, classname.getValue());
	}
	return classnames;
}
 
@Override
public Set<Set<ASTNode>> getNameBindings(final ASTNode node) {
	final ClassnameFinder finder = new ClassnameFinder();
	node.accept(finder);

	final Set<Set<ASTNode>> nameBindings = Sets.newHashSet();
	for (final String typeName : finder.classNamePostions.keySet()) {
		for (final ASTNode nameNode : finder.classNamePostions
				.get(typeName)) {
			final Set<ASTNode> boundNodes = Sets.newIdentityHashSet();
			boundNodes.add(nameNode);
			nameBindings.add(boundNodes);
		}
	}
	return nameBindings;
}
 
private ASTNode findField(ASTNode astRoot, final String name) {

		class STOP_VISITING extends RuntimeException {
			private static final long serialVersionUID= 1L;
		}

		final ASTNode[] result= new ASTNode[1];

		try {
			astRoot.accept(new ASTVisitor() {

				@Override
				public boolean visit(VariableDeclarationFragment node) {
					if (name.equals(node.getName().getFullyQualifiedName())) {
						result[0]= node.getParent();
						throw new STOP_VISITING();
					}
					return true;
				}
			});
		} catch (STOP_VISITING ex) {
			// stop visiting AST
		}

		return result[0];
	}
 
源代码4 项目: spotbugs   文件: BugResolution.java
@Nonnull
private String findLabelReplacement(ASTVisitor labelFixingVisitor) {
    IMarker marker = getMarker();
    try {
        ASTNode node = getNodeForMarker(marker);
        if (node != null) {
            node.accept(labelFixingVisitor);
            String retVal = ((CustomLabelVisitor) labelFixingVisitor).getLabelReplacement();
            return retVal == null ? DEFAULT_REPLACEMENT : retVal;
        }
        // Catch all exceptions (explicit) so that the label creation won't fail
        // FindBugs prefers this being explicit instead of just catching Exception
    } catch (JavaModelException | ASTNodeNotFoundException | RuntimeException e) {
        FindbugsPlugin.getDefault().logException(e, e.getLocalizedMessage());
        return DEFAULT_REPLACEMENT;
    }
    return DEFAULT_REPLACEMENT;
}
 
源代码5 项目: tassal   文件: MethodScopeExtractor.java
public static Multimap<Scope, String> getScopeSnippets(final ASTNode node,
		final boolean methodAsRoots) {
	final ScopeFinder scopeFinder = new ScopeFinder(methodAsRoots);
	node.accept(scopeFinder);

	final Multimap<Scope, String> scopes = TreeMultimap.create();
	for (final Entry<ASTNode, Method> method : scopeFinder.methods
			.entries()) {
		scopes.put(new Scope(method.getKey().toString(),
				method.getValue().type, METHOD_CALL, 0, 0), method
				.getValue().name);
	}

	return scopes;

}
 
/**
 * Creates the necessary changes for the renaming of the type parameter.
 *
 * @param monitor
 *            the progress monitor to display progress
 * @return the status of the operation
 * @throws CoreException
 *             if the change could not be generated
 */
private RefactoringStatus createRenameChanges(IProgressMonitor monitor) throws CoreException {
	Assert.isNotNull(monitor);
	RefactoringStatus status= new RefactoringStatus();
	try {
		monitor.beginTask(RefactoringCoreMessages.RenameTypeParameterRefactoring_searching, 2);
		ICompilationUnit cu= fTypeParameter.getDeclaringMember().getCompilationUnit();
		CompilationUnit root= RefactoringASTParser.parseWithASTProvider(cu, true, null);
		CompilationUnitRewrite rewrite= new CompilationUnitRewrite(cu, root);
		IMember member= fTypeParameter.getDeclaringMember();
		ASTNode declaration= null;
		if (member instanceof IMethod) {
			declaration= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, root);
		} else if (member instanceof IType) {
			declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, root);
		} else {
			JavaPlugin.logErrorMessage("Unexpected sub-type of IMember: " + member.getClass().getName()); //$NON-NLS-1$
			Assert.isTrue(false);
		}
		monitor.worked(1);
		RenameTypeParameterVisitor visitor= new RenameTypeParameterVisitor(rewrite, fTypeParameter.getNameRange(), status);
		if (declaration != null)
			declaration.accept(visitor);
		fChange= visitor.getResult();
	} finally {
		monitor.done();
	}
	return status;
}
 
源代码7 项目: xtext-xtend   文件: JavaASTFlattener.java
@Override
public boolean visit(final ArrayType node) {
  boolean _java8orHigher = this.java8orHigher();
  boolean _not = (!_java8orHigher);
  if (_not) {
    node.getComponentType().accept(this);
    this.appendToBuffer("[]");
  } else {
    ASTNode _genericChildProperty = this._aSTFlattenerUtils.genericChildProperty(node, "elementType");
    if (_genericChildProperty!=null) {
      _genericChildProperty.accept(this);
    }
    List<ASTNode> dimensions = this._aSTFlattenerUtils.genericChildListProperty(node, "dimensions");
    boolean _isNullOrEmpty = IterableExtensions.isNullOrEmpty(dimensions);
    boolean _not_1 = (!_isNullOrEmpty);
    if (_not_1) {
      final Consumer<ASTNode> _function = (ASTNode dim) -> {
        List<ASTNode> _genericChildListProperty = this._aSTFlattenerUtils.genericChildListProperty(dim, "annotations");
        if (_genericChildListProperty!=null) {
          this.visitAll(_genericChildListProperty);
        }
        this.appendToBuffer("[]");
      };
      dimensions.forEach(_function);
    }
  }
  return false;
}
 
源代码8 项目: xtext-xtend   文件: JavaASTFlattener.java
@Override
public boolean visit(final TagElement node) {
  boolean _isNested = node.isNested();
  if (_isNested) {
    this.appendToBuffer("{");
  } else {
    this.appendLineWrapToBuffer();
    this.appendToBuffer(" * ");
  }
  boolean previousRequiresWhiteSpace = false;
  String _tagName = node.getTagName();
  boolean _tripleNotEquals = (_tagName != null);
  if (_tripleNotEquals) {
    this.appendToBuffer(node.getTagName());
    previousRequiresWhiteSpace = true;
  }
  boolean previousRequiresNewLine = false;
  for (Iterator<? extends ASTNode> it = node.fragments().iterator(); it.hasNext();) {
    {
      ASTNode e = it.next();
      boolean currentIncludesWhiteSpace = (e instanceof TextElement);
      if ((previousRequiresNewLine && currentIncludesWhiteSpace)) {
        this.appendLineWrapToBuffer();
        this.appendToBuffer(" * ");
      }
      previousRequiresNewLine = currentIncludesWhiteSpace;
      if ((previousRequiresWhiteSpace && (!currentIncludesWhiteSpace))) {
        this.appendSpaceToBuffer();
      }
      e.accept(this);
      previousRequiresWhiteSpace = ((!currentIncludesWhiteSpace) && (!(e instanceof TagElement)));
    }
  }
  boolean _isNested_1 = node.isNested();
  if (_isNested_1) {
    this.appendToBuffer("}");
  }
  return false;
}
 
/**
 * Creates the necessary changes for the renaming of the type parameter.
 *
 * @param monitor
 *            the progress monitor to display progress
 * @return the status of the operation
 * @throws CoreException
 *             if the change could not be generated
 */
private RefactoringStatus createRenameChanges(IProgressMonitor monitor) throws CoreException {
	Assert.isNotNull(monitor);
	RefactoringStatus status= new RefactoringStatus();
	try {
		monitor.beginTask(RefactoringCoreMessages.RenameTypeParameterRefactoring_searching, 2);
		ICompilationUnit cu= fTypeParameter.getDeclaringMember().getCompilationUnit();
		CompilationUnit root= RefactoringASTParser.parseWithASTProvider(cu, true, null);
		CompilationUnitRewrite rewrite= new CompilationUnitRewrite(cu, root);
		IMember member= fTypeParameter.getDeclaringMember();
		ASTNode declaration= null;
		if (member instanceof IMethod) {
			declaration= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, root);
		} else if (member instanceof IType) {
			declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, root);
		} else {
			JavaLanguageServerPlugin.logError("Unexpected sub-type of IMember: " + member.getClass().getName()); //$NON-NLS-1$
			Assert.isTrue(false);
		}
		monitor.worked(1);
		RenameTypeParameterVisitor visitor= new RenameTypeParameterVisitor(rewrite, fTypeParameter.getNameRange(), status);
		if (declaration != null) {
			declaration.accept(visitor);
		}
		fChange= visitor.getResult();
	} finally {
		monitor.done();
	}
	return status;
}
 
@Override
public Set<Set<ASTNode>> getNameBindings(final ASTNode node) {
	final VariableBindingFinder bindingFinder = new VariableBindingFinder();
	node.accept(bindingFinder);

	final Set<Set<ASTNode>> nameBindings = Sets.newHashSet();
	for (final Entry<Integer, List<ASTNode>> variableBindings : bindingFinder.variableBinding
			.entrySet()) {
		final Set<ASTNode> boundNodes = Sets.newIdentityHashSet();
		boundNodes.addAll(variableBindings.getValue());
		nameBindings.add(boundNodes);
	}
	return nameBindings;
}
 
@Override
public Set<Set<ASTNode>> getNameBindings(final ASTNode node) {
	final MethodBindings mb = new MethodBindings();
	node.accept(mb);

	final Set<Set<ASTNode>> nameBindings = Sets.newHashSet();
	for (final Entry<String, ASTNode> entry : mb.methodNamePostions
			.entries()) {
		final Set<ASTNode> boundNodes = Sets.newIdentityHashSet();
		boundNodes.add(entry.getValue());
		nameBindings.add(boundNodes);
	}
	return nameBindings;
}
 
@Override
public Set<Set<ASTNode>> getNameBindings(final ASTNode node) {
	final VariableBindingFinder bindingFinder = new VariableBindingFinder();
	node.accept(bindingFinder);

	final Set<Set<ASTNode>> nameBindings = Sets.newHashSet();
	for (final Entry<Integer, List<ASTNode>> variableBindings : bindingFinder.variableBinding
			.entrySet()) {
		final Set<ASTNode> boundNodes = Sets.newIdentityHashSet();
		boundNodes.addAll(variableBindings.getValue());
		nameBindings.add(boundNodes);
	}
	return nameBindings;
}
 
源代码13 项目: codemining-core   文件: JavaASTExtractor.java
private final MethodDeclaration getFirstMethodDeclaration(final ASTNode node) {
	final TopMethodRetriever visitor = new TopMethodRetriever();
	node.accept(visitor);
	return visitor.topDcl;
}
 
源代码14 项目: api-mining   文件: JavaASTExtractor.java
private final MethodDeclaration getFirstMethodDeclaration(final ASTNode node) {
	final TopMethodRetriever visitor = new TopMethodRetriever();
	node.accept(visitor);
	return visitor.topDcl;
}
 
public CompareInBitWiseOpFinder(ASTNode selectedNode) {
	fSelectedNode= selectedNode;
	selectedNode.accept(this);
}
 
源代码16 项目: xtext-xtend   文件: JavaASTFlattener.java
public void handleAssignment(final Assignment node, final ASTNode leftSide, final Type type) {
  leftSide.accept(this);
  this.appendToBuffer(node.getOperator().toString());
  this.handleRightHandSide(node, type);
}
 
public static List<IBinding> perform(ASTNode root) {
	TypeReferenceFinder visitor= new TypeReferenceFinder();
	root.accept(visitor);
	return visitor.fResult;
}
 
源代码18 项目: lapse-plus   文件: NodeFinder.java
/**
 * A visitor that maps a selection to a given ASTNode. The result node is
 * determined as follows:
 * <ul>
 *   <li>first the visitor tries to find a node with the exact start and length</li>
 *   <li>if no such node exists than the node that encloses the range defined by
 *       start and end is returned.</li>
 *   <li>if the length is zero than also nodes are considered where the node's
 *       start or end position matches <code>start</code>.</li>
 *   <li>otherwise <code>null</code> is returned.</li>
 * </ul>
 */
public static ASTNode perform(ASTNode root, int start, int length) {
  NodeFinder finder= new NodeFinder(start, length);
  root.accept(finder);
  ASTNode result= finder.getCoveredNode();
  if (result == null || result.getStartPosition() != start || result.getLength() != length) {
    return finder.getCoveringNode();
  }
  return result;
}
 
源代码19 项目: tassal   文件: VariableScopeExtractor.java
/**
 * Return the variable scopes of the given node.
 * 
 * @param node
 * @return
 */
public Multimap<ASTNode, Variable> getVariableScopes(final ASTNode node) {
	variableScopes.clear();
	node.accept(this);
	return ImmutableMultimap.copyOf(variableScopes);
}
 
源代码20 项目: codemining-core   文件: VariableScopeExtractor.java
/**
 * Return the variable scopes of the given node.
 * 
 * @param node
 * @return
 */
public Multimap<ASTNode, Variable> getVariableScopes(final ASTNode node) {
	variableScopes.clear();
	node.accept(this);
	return ImmutableMultimap.copyOf(variableScopes);
}