summaryrefslogtreecommitdiff
path: root/plugins/structuralsearch/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/predicates/ExprTypePredicate.java
blob: d66db76c272dc9dc95acfd111c8fc330a229b36a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.intellij.structuralsearch.impl.matcher.predicates;

import com.intellij.psi.*;
import com.intellij.structuralsearch.impl.matcher.MatchContext;
import com.intellij.structuralsearch.impl.matcher.handlers.MatchPredicate;
import com.intellij.structuralsearch.impl.matcher.iterators.HierarchyNodeIterator;
import com.intellij.dupLocator.iterators.NodeIterator;

/**
 * Created by IntelliJ IDEA.
 * User: Maxim.Mossienko
 * Date: Mar 23, 2004
 * Time: 6:37:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class ExprTypePredicate extends MatchPredicate {
  private final RegExpPredicate delegate;
  private final boolean withinHierarchy;

  public ExprTypePredicate(String type, String baseName, boolean _withinHierarchy, boolean caseSensitiveMatch,boolean target) {
    delegate = new RegExpPredicate(type,caseSensitiveMatch,baseName,false,target);
    withinHierarchy = _withinHierarchy;
  }

  public boolean match(PsiElement patternNode, PsiElement matchedNode, MatchContext context) {
    return match(patternNode, matchedNode, 0, -1, context);
  }

  public boolean match(PsiElement node, PsiElement match, int start, int end, MatchContext context) {
    if (match instanceof PsiIdentifier) {
      // since we pickup tokens
      match = match.getParent();
    }

    if (match instanceof PsiExpression) {
      final PsiType type = evalType((PsiExpression)match,context);
      if (type==null) return false;

      return doMatchWithTheType(type, context, match);
    } else {
      return false;
    }
  }

  protected PsiType evalType(PsiExpression match, MatchContext context) {
    PsiType type = null;

    if (match instanceof PsiReferenceExpression &&
        match.getParent() instanceof PsiMethodCallExpression) {
      PsiMethod method = ((PsiMethodCallExpression)match.getParent()).resolveMethod();
      if (method!=null) type = method.getReturnType();
    }

    if (type==null) type = match.getType();
    return type;
  }

  private boolean doMatchWithTheType(final PsiType type, MatchContext context, PsiElement matchedNode) {
    if (type instanceof PsiClassType) {
      PsiClass clazz = ((PsiClassType)type).resolve();

      if (clazz!=null) return checkClass(clazz, context);
    }

    if (type!=null) {
      final String presentableText = type.getPresentableText();
      boolean result = delegate.doMatch(presentableText,context, matchedNode);

      if (!result && type instanceof PsiArrayType && ((PsiArrayType)type).getComponentType() instanceof PsiClassType) {
        PsiClass clazz = ((PsiClassType)((PsiArrayType)type).getComponentType()).resolve();

        if (clazz!=null) { // presentable text for array is not qualified!
          result = delegate.doMatch(clazz.getQualifiedName()+"[]",context, matchedNode);
        }
      }
      return result;
    } else {
      return false;
    }
  }

  public boolean checkClass(PsiClass clazz, MatchContext context) {
    if (withinHierarchy) {
      final NodeIterator parents = new HierarchyNodeIterator(clazz,true,true);

      while(parents.hasNext() && !delegate.match(null,parents.current(),context)) {
        parents.advance();
      }

      return parents.hasNext();
    } else {
      return delegate.match(null,clazz,context);
    }
  }
}