summaryrefslogtreecommitdiff
path: root/plugins/devkit/src/inspections/DevKitImplicitUsageProvider.java
blob: 7aeb7a0470464033f8813efb36c9946aa481309f (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.idea.devkit.inspections;

import com.intellij.codeInsight.daemon.ImplicitUsageProvider;
import com.intellij.psi.*;
import com.intellij.psi.util.InheritanceUtil;
import org.jetbrains.annotations.Nullable;

/**
 * @author anna
 */
public class DevKitImplicitUsageProvider implements ImplicitUsageProvider {

  @Override
  public boolean isImplicitUsage(PsiElement element) {
    if (element instanceof PsiClass) {
      final PsiClass psiClass = (PsiClass)element;
      return isDomElementClass(psiClass);
    }

    if (element instanceof PsiMethod) {
      PsiMethod psiMethod = (PsiMethod)element;
      return isDomElementMethod(psiMethod);
    }

    return false;
  }

  @Override
  public boolean isImplicitRead(PsiElement element) {
    return false;
  }

  @Override
  public boolean isImplicitWrite(PsiElement element) {
    return false;
  }

  static boolean isDomElementClass(PsiClass psiClass) {
    if (psiClass.isEnum() ||
        psiClass.isAnnotationType() ||
        psiClass.hasModifierProperty(PsiModifier.PRIVATE)) {
      return false;
    }

    return isDomElementInheritor(psiClass);
  }

  private static boolean isDomElementMethod(PsiMethod psiMethod) {
    if (!psiMethod.hasModifierProperty(PsiModifier.PUBLIC) ||
        psiMethod.hasModifierProperty(PsiModifier.STATIC) ||
        psiMethod.isConstructor() ||
        psiMethod.getParameterList().getParametersCount() > 1) {
      return false;
    }

    final PsiClass containingClass = psiMethod.getContainingClass();
    if (containingClass == null) {
      return false;
    }

    if (!isDomElementClass(containingClass)) {
      return isDomElementVisitorMethod(psiMethod, containingClass);
    }

    final PsiType returnType = psiMethod.getReturnType();
    if (!(returnType instanceof PsiClassType)) {
      return false;
    }

    PsiClassType returnClassType = (PsiClassType)returnType;

    // Dom getDom(), GenericAttributeValue<X> getAttr(), ...
    final PsiClass returnResolved = returnClassType.resolve();
    if (isDomElementInheritor(returnResolved)) {
      return true;
    }

    // List<Dom> getDoms()
    if (returnClassType.getParameterCount() == 1 &&
        InheritanceUtil.isInheritor(returnResolved, CommonClassNames.JAVA_UTIL_LIST)) {
      final PsiType listType = returnClassType.getParameters()[0];
      return isDomElementInheritor(listType);
    }

    return false;
  }

  private static boolean isDomElementVisitorMethod(PsiMethod method,
                                                   PsiClass containingClass) {
    if (method.getReturnType() != PsiType.VOID ||
        !method.getName().startsWith("visit") ||
        method.getParameterList().getParametersCount() != 1 ||
        !InheritanceUtil.isInheritor(containingClass, "com.intellij.util.xml.DomElementVisitor")) {
      return false;
    }

    final PsiType psiType = method.getParameterList().getParameters()[0].getType();
    return isDomElementInheritor(psiType);
  }

  private static boolean isDomElementInheritor(@Nullable PsiType psiType) {
    return InheritanceUtil.isInheritor(psiType, "com.intellij.util.xml.DomElement");
  }

  private static boolean isDomElementInheritor(@Nullable PsiClass psiClass) {
    return InheritanceUtil.isInheritor(psiClass, "com.intellij.util.xml.DomElement");
  }
}