org.eclipse.jdt.core.dom.ASTMatcher Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.ASTMatcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StableNodeBasedRegionUpdater.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
public StableNodeBasedRegionUpdater(ReplaceEdit originalEdit,
    ReferenceUpdater referenceUpdater, N originalNode, ASTMatcher matcher)
    throws RefactoringException {
  super(originalEdit, referenceUpdater);

  this.originalNode = originalNode;
  this.matcher = matcher;

  // Look for a stable node
  StableBindingResolver stableBindingResolver = new StableBindingResolver(
      originalNode);
  if (stableBindingResolver.resolve()) {
    // Found one!
    isCompilationUnitTheStableNode = false;
    originalStableNode = stableBindingResolver.getStableNode();
    originalStableBindingKey = stableBindingResolver.getStableBinding().getKey();
  } else {
    // In some cases (like an import declaration), there isn't a stable
    // binding
    // node we can use, so instead we use the compilation unit
    isCompilationUnitTheStableNode = true;
    originalStableNode = originalNode.getRoot();
    originalStableBindingKey = null;
  }
}
 
Example #2
Source File: RegionUpdaterFactory.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a new region updater for the given text edit contained within the
 * given node.
 * 
 * @param originalEdit the text edit
 * @param node the most-specific node that contains the text edit
 * @param referenceUpdater an reference updater knowledgeable about the
 *          refactoring that is taking place
 * @param matcher an AST matcher knowledgeable about refactoring that is
 *          taking place
 * @return a region updater instance for the given text edit
 * @throws RefactoringException if there was an error creating a region
 *           updater
 */
public static RegionUpdater newRegionUpdater(ReplaceEdit originalEdit,
    ASTNode node, ReferenceUpdater referenceUpdater, ASTMatcher matcher)
    throws RefactoringException {

  if (node instanceof Name) {
    return new NameRegionUpdater(originalEdit, referenceUpdater, (Name) node,
        matcher);

  } else if (node instanceof TextElement) {
    return new TextElementRegionUpdater(originalEdit, referenceUpdater,
        (TextElement) node, matcher);
  }

  throw new RefactoringException("This AST node type is not supported");
}
 
Example #3
Source File: GenerateToStringOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Determines if given method exists in a given list
 * 
 * @param list list of method to search through
 * @param method method to find
 * @return declaration of method from the list that has the same name and parameter types, or
 *         null if not found
 */
protected BodyDeclaration findMethodToReplace(final List<BodyDeclaration> list, MethodDeclaration method) {
	for (final Iterator<BodyDeclaration> iterator= list.iterator(); iterator.hasNext();) {
		final BodyDeclaration bodyDecl= iterator.next();
		if (bodyDecl instanceof MethodDeclaration) {
			final MethodDeclaration method2= (MethodDeclaration)bodyDecl;
			if (method2.getName().getIdentifier().equals(method.getName().getIdentifier()) && method2.parameters().size() == method.parameters().size()) {
				Iterator<SingleVariableDeclaration> iterator1= method.parameters().iterator();
				Iterator<SingleVariableDeclaration> iterator2= method2.parameters().iterator();
				boolean ok= true;
				while (iterator1.hasNext()) {
					if (!iterator1.next().getType().subtreeMatch(new ASTMatcher(), iterator2.next().getType())) {
						ok= false;
						break;
					}
				}
				if (ok)
					return method2;
			}
		}
	}
	return null;
}
 
Example #4
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static String getArgumentName(List<Expression> arguments, int index) {
	String def= String.valueOf(index + 1);

	ASTNode expr= arguments.get(index);
	if (expr.getLength() > 18) {
		return def;
	}
	ASTMatcher matcher= new ASTMatcher();
	for (int i= 0; i < arguments.size(); i++) {
		if (i != index && matcher.safeSubtreeMatch(expr, arguments.get(i))) {
			return def;
		}
	}
	return '\'' + BasicElementLabels.getJavaElementName(ASTNodes.asString(expr)) + '\'';
}
 
Example #5
Source File: JavaASTUtils.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Calculates the edit distance between two argument lists.
 */
public static int editDistance(List<SingleVariableDeclaration> params,
    List<SingleVariableDeclaration> peerParams) {
  ASTMatcher matcher = new ASTMatcher();

  // Classic DP implementation
  int[][] dp = new int[params.size() + 1][peerParams.size() + 1];

  for (int i = 0; i <= params.size(); i++) {
    dp[i][0] = i;
  }

  for (int j = 0; j <= peerParams.size(); j++) {
    dp[0][j] = j;
  }

  for (int i = 1; i <= params.size(); i++) {
    for (int j = 1; j <= peerParams.size(); j++) {
      int distance = dp[i - 1][j - 1];

      if (!params.get(i - 1).subtreeMatch(matcher, peerParams.get(j - 1))) {
        distance += 1;
      }

      if (dp[i - 1][j] + 1 < distance) {
        distance = dp[i - 1][j] + 1;
      }

      if (dp[i][j - 1] + 1 < distance) {
        distance = dp[i][j - 1] + 1;
      }

      dp[i][j] = distance;
    }
  }

  return dp[params.size()][peerParams.size()];
}
 
Example #6
Source File: EquivalentNodeFinder.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public EquivalentNodeFinder(ASTNode node, ASTNode ancestorNode,
    ASTNode newAncestorNode, ASTMatcher matcher) {
  this.originalNode = node;
  this.ancestorNode = ancestorNode;
  this.newAncestorNode = newAncestorNode;
  this.matcher = matcher;
}
 
Example #7
Source File: EquivalentNodeFinder.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private boolean treesMatch(ASTNode node, ASTNode otherNode,
    ASTNode topmostAncestorNode, ASTNode topmostNewAncestorNode,
    ASTMatcher matcher) {

  while (true) {
    if ((node == topmostAncestorNode) != (otherNode == topmostNewAncestorNode)) {
      // One has reached the end and the other has not
      return false;
    }

    if (node == topmostAncestorNode) {
      // They have both reached an end, and everything went smoothly
      return true;
    }

    if (!node.subtreeMatch(matcher, otherNode)) {
      // Subtrees do not match
      return false;
    }

    if (!indexMatches(node, otherNode)) {
      // The index of each does not match
      return false;
    }

    node = node.getParent();
    otherNode = otherNode.getParent();
  }
}
 
Example #8
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static String getArgumentName(List<Expression> arguments, int index) {
	String def= String.valueOf(index + 1);

	ASTNode expr= arguments.get(index);
	if (expr.getLength() > 18) {
		return def;
	}
	ASTMatcher matcher= new ASTMatcher();
	for (int i= 0; i < arguments.size(); i++) {
		if (i != index && matcher.safeSubtreeMatch(expr, arguments.get(i))) {
			return def;
		}
	}
	return '\'' + BasicElementLabels.getJavaElementName(ASTNodes.asString(expr)) + '\'';
}
 
Example #9
Source File: ReplaceConditionalWithPolymorphism.java    From JDeodorant with MIT License 5 votes vote down vote up
private void replaceCastExpressionWithThisExpression(List<Expression> oldCastExpressions, List<Expression> newCastExpressions, TypeDeclaration subclassTypeDeclaration, AST subclassAST, ASTRewrite subclassRewriter) {
	int j = 0;
	for(Expression expression : oldCastExpressions) {
		CastExpression castExpression = (CastExpression)expression;
		if(castExpression.getType().resolveBinding().isEqualTo(subclassTypeDeclaration.resolveBinding())) {
			if(castExpression.getExpression() instanceof SimpleName) {
				SimpleName castSimpleName = (SimpleName)castExpression.getExpression();
				if(typeVariable != null && typeVariable.getName().resolveBinding().isEqualTo(castSimpleName.resolveBinding())) {
					subclassRewriter.replace(newCastExpressions.get(j), subclassAST.newThisExpression(), null);
				}
			}
			else if(castExpression.getExpression() instanceof MethodInvocation) {
				MethodInvocation castMethodInvocation = (MethodInvocation)castExpression.getExpression();
				if(typeMethodInvocation != null && typeMethodInvocation.subtreeMatch(new ASTMatcher(), castMethodInvocation)) {
					subclassRewriter.replace(newCastExpressions.get(j), subclassAST.newThisExpression(), null);
				}
			}
		}
		j++;
	}
}
 
Example #10
Source File: NameRegionUpdater.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
public NameRegionUpdater(ReplaceEdit originalEdit,
    ReferenceUpdater referenceUpdater, Name originalNode, ASTMatcher matcher)
    throws RefactoringException {
  super(originalEdit, referenceUpdater, originalNode, matcher);
}
 
Example #11
Source File: TextElementRegionUpdater.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
public TextElementRegionUpdater(ReplaceEdit originalEdit,
    ReferenceUpdater referenceUpdater, TextElement originalNode,
    ASTMatcher matcher) throws RefactoringException {
  super(originalEdit, referenceUpdater, originalNode, matcher);
}
 
Example #12
Source File: RegionUpdaterChangeWeavingVisitor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates the visitor.
 * 
 * @param astMatcher a relaxed matcher that is knowledgeable about the
 *          refactoring (for example, it matches two nodes even if the names
 *          don't match -- given the new node has the new name and the old
 *          node has the old name)
 * @param referenceUpdater a reference updater that can update references from
 *          the old matcher to the new
 */
public RegionUpdaterChangeWeavingVisitor(ASTMatcher astMatcher,
    ReferenceUpdater referenceUpdater) {
  this.astMatcher = astMatcher;
  this.referenceUpdater = referenceUpdater;
}