1
1
package com .bluelinelabs .conductor .lint ;
2
2
3
3
import com .android .SdkConstants ;
4
- import com .android .annotations .NonNull ;
5
- import com .android .tools .lint .client .api .JavaParser .ResolvedClass ;
4
+ import com .android .tools .lint .client .api .JavaEvaluator ;
6
5
import com .android .tools .lint .detector .api .Category ;
7
6
import com .android .tools .lint .detector .api .Detector ;
8
7
import com .android .tools .lint .detector .api .Implementation ;
9
8
import com .android .tools .lint .detector .api .Issue ;
10
9
import com .android .tools .lint .detector .api .JavaContext ;
11
10
import com .android .tools .lint .detector .api .Scope ;
12
11
import com .android .tools .lint .detector .api .Severity ;
13
- import com .android .tools .lint .detector .api .Speed ;
12
+ import com .intellij .psi .PsiClass ;
13
+ import com .intellij .psi .PsiMethod ;
14
+ import com .intellij .psi .PsiParameter ;
14
15
15
- import java .lang .reflect .Modifier ;
16
16
import java .util .Collections ;
17
17
import java .util .List ;
18
18
19
- import lombok .ast .ClassDeclaration ;
20
- import lombok .ast .ConstructorDeclaration ;
21
- import lombok .ast .Node ;
22
- import lombok .ast .NormalTypeBody ;
23
- import lombok .ast .StrictListAccessor ;
24
- import lombok .ast .TypeMember ;
25
- import lombok .ast .VariableDefinition ;
26
-
27
- public final class ControllerIssueDetector extends Detector implements Detector .JavaScanner , Detector .ClassScanner {
19
+ public final class ControllerIssueDetector extends Detector implements Detector .JavaPsiScanner {
28
20
29
21
public static final Issue ISSUE =
30
22
Issue .create ("ValidController" , "Controller not instantiatable" ,
@@ -35,74 +27,56 @@ public final class ControllerIssueDetector extends Detector implements Detector.
35
27
36
28
public ControllerIssueDetector () { }
37
29
38
- @ NonNull
39
- @ Override
40
- public Speed getSpeed () {
41
- return Speed .FAST ;
42
- }
43
-
44
30
@ Override
45
31
public List <String > applicableSuperClasses () {
46
32
return Collections .singletonList ("com.bluelinelabs.conductor.Controller" );
47
33
}
48
34
49
35
@ Override
50
- public void checkClass (@ NonNull JavaContext context , ClassDeclaration node ,
51
- @ NonNull Node declarationOrAnonymous , @ NonNull ResolvedClass cls ) {
52
-
53
- if (node == null ) {
36
+ public void checkClass (JavaContext context , PsiClass declaration ) {
37
+ final JavaEvaluator evaluator = context .getEvaluator ();
38
+ if (evaluator .isAbstract (declaration )) {
54
39
return ;
55
40
}
56
41
57
- final int flags = node .astModifiers ().getEffectiveModifierFlags ();
58
- if ((flags & Modifier .ABSTRACT ) != 0 ) {
42
+ if (!evaluator .isPublic (declaration )) {
43
+ String message = String .format ("This Controller class should be public (%1$s)" , declaration .getQualifiedName ());
44
+ context .report (ISSUE , declaration , context .getLocation (declaration ), message );
59
45
return ;
60
46
}
61
47
62
- if (( flags & Modifier . PUBLIC ) == 0 ) {
63
- String message = String .format ("This Controller class should be public (%1$s)" , cls . getName ());
64
- context .report (ISSUE , node , context .getLocation (node . astName () ), message );
48
+ if (declaration . getContainingClass () != null && ! evaluator . isStatic ( declaration ) ) {
49
+ String message = String .format ("This Controller inner class should be static (%1$s)" , declaration . getQualifiedName ());
50
+ context .report (ISSUE , declaration , context .getLocation (declaration ), message );
65
51
return ;
66
52
}
67
53
68
- if (cls .getContainingClass () != null && (flags & Modifier .STATIC ) == 0 ) {
69
- String message = String .format ("This Controller inner class should be static (%1$s)" , cls .getName ());
70
- context .report (ISSUE , node , context .getLocation (node .astName ()), message );
71
- return ;
72
- }
73
54
74
- boolean hasConstructor = false ;
75
55
boolean hasDefaultConstructor = false ;
76
56
boolean hasBundleConstructor = false ;
77
- NormalTypeBody body = node .astBody ();
78
- if (body != null ) {
79
- for (TypeMember member : body .astMembers ()) {
80
- if (member instanceof ConstructorDeclaration ) {
81
- hasConstructor = true ;
82
- ConstructorDeclaration constructor = (ConstructorDeclaration )member ;
83
-
84
- if (constructor .astModifiers ().isPublic ()) {
85
- StrictListAccessor <VariableDefinition , ConstructorDeclaration > params = constructor .astParameters ();
86
- if (params .isEmpty ()) {
87
- hasDefaultConstructor = true ;
88
- break ;
89
- } else if (params .size () == 1 &&
90
- (params .first ().astTypeReference ().getTypeName ().equals (SdkConstants .CLASS_BUNDLE )) ||
91
- params .first ().astTypeReference ().getTypeName ().equals ("Bundle" )) {
92
- hasBundleConstructor = true ;
93
- break ;
94
- }
95
- }
57
+ PsiMethod [] constructors = declaration .getConstructors ();
58
+ for (PsiMethod constructor : constructors ) {
59
+ if (evaluator .isPublic (constructor )) {
60
+ PsiParameter [] parameters = constructor .getParameterList ().getParameters ();
61
+
62
+ if (parameters .length == 0 ) {
63
+ hasDefaultConstructor = true ;
64
+ break ;
65
+ } else if (parameters .length == 1 &&
66
+ parameters [0 ].getType ().equalsToText (SdkConstants .CLASS_BUNDLE ) ||
67
+ parameters [0 ].getType ().equalsToText ("Bundle" )) {
68
+ hasBundleConstructor = true ;
69
+ break ;
96
70
}
97
71
}
98
72
}
99
73
100
- if (hasConstructor && !hasDefaultConstructor && !hasBundleConstructor ) {
74
+ if (constructors . length > 0 && !hasDefaultConstructor && !hasBundleConstructor ) {
101
75
String message = String .format (
102
76
"This Controller needs to have either a public default constructor or a" +
103
77
" public single-argument constructor that takes a Bundle. (`%1$s`)" ,
104
- cls . getName ());
105
- context .report (ISSUE , node , context .getLocation (node . astName () ), message );
78
+ declaration . getQualifiedName ());
79
+ context .report (ISSUE , declaration , context .getLocation (declaration ), message );
106
80
}
107
81
}
108
82
}
0 commit comments