List of usage examples for org.eclipse.jdt.core.dom ASTMatcher ASTMatcher
public ASTMatcher()
From source file:changenodes.matching.BestLeafTreeMatcher.java
License:Apache License
private void matchIdenticalMethods(ASTNode left, ASTNode right) { //fix in which some methods got pretty bad matches //feels pretty dirty though (but so does this whole file) for (Iterator<ASTNode> iterator = new BreadthFirstNodeIterator(left); iterator.hasNext();) { ASTNode n = iterator.next();/*from ww w . j a v a2s . c o m*/ ASTNode bestMatch = null; if (n.getNodeType() == ASTNode.METHOD_DECLARATION) { MethodDeclaration x = (MethodDeclaration) n; if (!leftMatching.containsKey(x)) { for (Iterator<ASTNode> rightIterator = new BreadthFirstNodeIterator(right); rightIterator .hasNext();) { ASTNode y = rightIterator.next(); if (y.getNodeType() == x.getNodeType() && x.subtreeMatch(new ASTMatcher(), y)) { bestMatch = y; break; } } } //we have found the best node, lets now match them together //afaik this is also not in the original paper, but we sometimes got some weird matches if (bestMatch != null) { markMatchedNode(x, bestMatch); } } } }
From source file:changenodes.matching.calculators.ChawatheCalculator.java
License:Apache License
private boolean isDescendant(ASTNode parent, ASTNode child) { if (child == null) { return false; }/* w w w .ja v a 2 s . c om*/ if (parent == null) { return false; } ASTNode childParent = child.getParent(); return parent.subtreeMatch(new ASTMatcher(), childParent) || isDescendant(parent, childParent); }
From source file:com.google.gdt.eclipse.core.JavaASTUtils.java
License:Open Source License
/** * Calculates the edit distance between two argument lists. *///from w ww .ja v a 2 s . c o m 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()]; }
From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java
License:Open Source License
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; }/*from ww w. ja v a 2s . c o m*/ 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)) + '\''; }
From source file:edu.buffalo.cse.green.relationships.Relationship.java
License:Open Source License
/** * @see java.lang.Object#equals(java.lang.Object) *//* w w w . j a va 2s .c o m*/ public boolean equals(Object o) { ASTMatcher matcher = new ASTMatcher(); if (o instanceof Relationship) { Relationship relationship = (Relationship) o; Iterator<ASTNode> iter = relationship.getFeatures().iterator(); for (ASTNode node1 : getFeatures()) { if (!iter.hasNext()) { return false; } ASTNode node2 = iter.next(); if (node1 == null) { } else if (node1.getNodeType() == EXPRESSION_STATEMENT) { ExpressionStatement stmt = (ExpressionStatement) node1; if (stmt.getExpression().getNodeType() == CLASS_INSTANCE_CREATION) { return false; } if (!matcher.match((ExpressionStatement) node1, node2)) { return false; } } else if (node1.getNodeType() == METHOD_DECLARATION) { if (!matcher.match((MethodDeclaration) node1, node2)) { return false; } } else if (node1.getNodeType() == SIMPLE_TYPE) { if (!matcher.match((SimpleType) node1, node2)) { return false; } } else if (node1.getNodeType() == VARIABLE_DECLARATION_STATEMENT) { if (!matcher.match((VariableDeclarationStatement) node1, node2)) { return false; } MethodDeclaration md1, md2; while (!(node1.getNodeType() == METHOD_DECLARATION)) { node1 = node1.getParent(); } while (!(node2.getNodeType() == METHOD_DECLARATION)) { node2 = node2.getParent(); } md1 = (MethodDeclaration) node1; md2 = (MethodDeclaration) node2; Iterator<SingleVariableDeclaration> pi1 = ((AbstractList<SingleVariableDeclaration>) (List) md1 .parameters()).iterator(); Iterator pi2 = md2.parameters().iterator(); while (pi1.hasNext()) { if (!pi2.hasNext()) return false; SingleVariableDeclaration d1 = pi1.next(); Object d2 = pi2.next(); if (!matcher.match(d1, d2)) return false; } if (pi2.hasNext()) return false; } else if (node1.getNodeType() == PARAMETERIZED_TYPE) { if (!matcher.match((ParameterizedType) node1, node2)) { return false; } } else if (node1.getNodeType() == CLASS_INSTANCE_CREATION) { return false; } else if (node1 instanceof Statement) { return false; } else if (node1 instanceof Expression) { return false; } else if (node1.getNodeType() == SINGLE_VARIABLE_DECLARATION && node2.getNodeType() == SINGLE_VARIABLE_DECLARATION) { SimpleName nodeOneName = ((SingleVariableDeclaration) node1).getName(); SimpleName nodeTwoName = ((SingleVariableDeclaration) node2).getName(); return nodeOneName.getIdentifier() == nodeTwoName.getIdentifier(); } else { GreenException.illegalOperation(node1.getClass() + "=" + node2.getClass()); } } return !iter.hasNext(); } else { return false; } }
From source file:edu.buffalo.cse.green.relationships.RelationshipGenerator.java
License:Open Source License
/** * Adds the desired import, if possible. * //from w ww . jav a 2s .c o m * @param qualifiedName - The desired <code>QualifiedName</code>. * @return false if the element cannot be imported because the element's * simple name is already used; true otherwise. */ private boolean addImport(QualifiedName qualifiedName) { List<ImportDeclaration> imports = (AbstractList<ImportDeclaration>) getCompilationUnit().imports(); // ensure the import is necessary and non-conflicting for (ImportDeclaration declaration : imports) { // do not contend with on-demand imports if (declaration.isOnDemand()) { continue; } // see if the import has already been added if (new ASTMatcher().match((QualifiedName) declaration.getName(), qualifiedName)) { return true; } /* see if the element referred to by the declaration has the same * simple name as the import we are trying to create (problem) */ if (declaration.resolveBinding() != null) { // not recently added String elementName = declaration.resolveBinding().getJavaElement().getElementName(); if (elementName.equals(qualifiedName.getName().toString())) { return false; } } } // create the import declaration ImportDeclaration i = getAST().newImportDeclaration(); i.setName(qualifiedName); // add the import declaration imports.add(i); return true; }
From source file:edu.buffalo.cse.green.relationships.RelationshipRemover.java
License:Open Source License
/** * Handles removal of add invocations involved in a relationship. * //ww w. ja v a2 s . co m * @param block - The block to process. */ protected void processAddInvocations(Block block) { List<Statement> stmts = (AbstractList<Statement>) (List) block.statements(); List<Statement> toRemove = new ArrayList<Statement>(); for (int x = 1; x < _relationship.getFeatures().size(); x++) { stmts.removeAll(toRemove); toRemove.clear(); for (Statement statement : stmts) { if (statement.getNodeType() == EXPRESSION_STATEMENT) { ExpressionStatement e = (ExpressionStatement) statement; if (new ASTMatcher().match(e, _relationship.getFeatures().get(x))) { toRemove.add(e); break; } } } } stmts.removeAll(toRemove); }
From source file:edu.buffalo.cse.green.relationships.RelationshipRemover.java
License:Open Source License
protected void processAddInvocations(MethodDeclaration method) { List<SingleVariableDeclaration> parameters = method.parameters(); List<SingleVariableDeclaration> toRemove = new ArrayList<SingleVariableDeclaration>(); for (int x = 1; x < _relationship.getFeatures().size(); x++) { parameters.removeAll(toRemove);//from w ww . j av a 2 s.c om toRemove.clear(); for (SingleVariableDeclaration param : parameters) { if (new ASTMatcher().match(param, _relationship.getFeatures().get(x))) ; { toRemove.add(param); break; } } } }
From source file:org.autorefactor.refactoring.rules.CollectionContainsRefactoring.java
License:Open Source License
private boolean matches(Expression e1, Expression e2) { return match(new ASTMatcher(), e1, e2); }
From source file:org.autorefactor.refactoring.rules.CollectionRefactoring.java
License:Open Source License
private boolean maybeReplaceSetContains(MethodInvocation miContains, Statement stmt, Expression toReplace, boolean negate) { if (isMethod(miContains, "java.util.Set", "contains", "java.lang.Object")) { Statement firstStmt = getAsList(stmt, 0); MethodInvocation miAdd = asExpression(firstStmt, MethodInvocation.class); final ASTMatcher astMatcher = new ASTMatcher(); if (isMethod(miAdd, "java.util.Set", "add", "java.lang.Object") && match(astMatcher, miContains.getExpression(), miAdd.getExpression()) && match(astMatcher, arg0(miContains), arg0(miAdd))) { ASTBuilder b = this.ctx.getASTBuilder(); Refactorings r = this.ctx.getRefactorings(); r.replace(toReplace, negate ? b.negate(miAdd, COPY.MOVE) : b.move(miAdd)); r.remove(firstStmt);/*from ww w .j a v a 2 s.co m*/ return DO_NOT_VISIT_SUBTREE; } } return VISIT_SUBTREE; }