use of com.intellij.structuralsearch.impl.matcher.filters.LexicalNodesFilter in project intellij-community by JetBrains.
the class JavaMatchingVisitor method visitNewExpression.
@Override
public void visitNewExpression(final PsiNewExpression new1) {
final PsiElement other = myMatchingVisitor.getElement();
final PsiJavaCodeReferenceElement classReference = new1.getClassReference();
if (other instanceof PsiArrayInitializerExpression && other.getParent() instanceof PsiVariable && new1.getArrayDimensions().length == 0 && new1.getArrayInitializer() != null) {
final MatchContext matchContext = myMatchingVisitor.getMatchContext();
final MatchingHandler handler = matchContext.getPattern().getHandler(classReference);
final boolean looseMatching = myMatchingVisitor.getMatchContext().getOptions().isLooseMatching();
if ((handler instanceof SubstitutionHandler && ((SubstitutionHandler) handler).getMinOccurs() != 0) || !looseMatching) {
myMatchingVisitor.setResult(false);
return;
}
final PsiType otherType = ((PsiArrayInitializerExpression) other).getType();
if (handler instanceof SubstitutionHandler && otherType != null) {
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(other.getProject());
final PsiTypeElement otherTypeElement = factory.createTypeElement(otherType.getDeepComponentType());
final SubstitutionHandler substitutionHandler = (SubstitutionHandler) handler;
final MatchPredicate predicate = substitutionHandler.getPredicate();
myMatchingVisitor.setResult(predicate == null || predicate.match(null, otherTypeElement, matchContext));
} else {
final PsiType type = new1.getType();
myMatchingVisitor.setResult(type != null && type.equals(otherType));
}
if (myMatchingVisitor.getResult()) {
myMatchingVisitor.matchSons(new1.getArrayInitializer(), other);
}
return;
}
if (!(other instanceof PsiNewExpression)) {
myMatchingVisitor.setResult(false);
return;
}
final PsiNewExpression new2 = (PsiNewExpression) other;
if (classReference != null) {
if (new2.getClassReference() != null) {
myMatchingVisitor.setResult(myMatchingVisitor.match(classReference, new2.getClassReference()) && myMatchingVisitor.matchSons(new1.getArrayInitializer(), new2.getArrayInitializer()));
if (myMatchingVisitor.getResult()) {
// matching dims
matchArrayDims(new1, new2);
}
return;
} else {
// match array of primitive by new 'T();
final PsiKeyword newKeyword = PsiTreeUtil.getChildOfType(new2, PsiKeyword.class);
final PsiElement element = PsiTreeUtil.getNextSiblingOfType(newKeyword, PsiWhiteSpace.class);
if (element != null && element.getNextSibling() instanceof PsiKeyword) {
((LexicalNodesFilter) LexicalNodesFilter.getInstance()).setCareKeyWords(true);
myMatchingVisitor.setResult(myMatchingVisitor.match(classReference, element.getNextSibling()) && myMatchingVisitor.matchSons(new1.getArrayInitializer(), new2.getArrayInitializer()));
((LexicalNodesFilter) LexicalNodesFilter.getInstance()).setCareKeyWords(false);
if (myMatchingVisitor.getResult()) {
// matching dims
matchArrayDims(new1, new2);
}
return;
}
}
}
if (classReference == new2.getClassReference()) {
// probably anonymous class or array of primitive type
((LexicalNodesFilter) LexicalNodesFilter.getInstance()).setCareKeyWords(true);
myMatchingVisitor.setResult(myMatchingVisitor.matchSons(new1, new2));
((LexicalNodesFilter) LexicalNodesFilter.getInstance()).setCareKeyWords(false);
} else if (new1.getAnonymousClass() == null && classReference != null && new2.getAnonymousClass() != null) {
// allow matching anonymous class without pattern
myMatchingVisitor.setResult(myMatchingVisitor.match(classReference, new2.getAnonymousClass().getBaseClassReference()) && myMatchingVisitor.matchSons(new1.getArgumentList(), new2.getArgumentList()));
} else {
myMatchingVisitor.setResult(false);
}
}
Aggregations