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
|
/*
* Copyright 2000-2009 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 com.intellij.refactoring.typeCook.deductive.util;
import com.intellij.psi.*;
import com.intellij.refactoring.typeCook.Settings;
import com.intellij.refactoring.typeCook.Util;
import java.util.HashSet;
/**
* @author db
*/
public class VictimCollector extends Visitor {
final HashSet<PsiElement> myVictims = new HashSet<PsiElement>();
final PsiElement[] myElements;
final Settings mySettings;
public VictimCollector(final PsiElement[] elements, final Settings settings) {
myElements = elements;
mySettings = settings;
}
private void testNAdd(final PsiElement element, final PsiType t) {
if (Util.isRaw(t, mySettings)) {
if (element instanceof PsiNewExpression && t.getCanonicalText().equals(CommonClassNames.JAVA_LANG_OBJECT)){
return;
}
myVictims.add(element);
}
}
@Override public void visitLocalVariable(final PsiLocalVariable variable) {
testNAdd(variable, variable.getType());
super.visitLocalVariable(variable);
}
@Override public void visitForeachStatement(final PsiForeachStatement statement) {
super.visitForeachStatement(statement);
final PsiParameter parameter = statement.getIterationParameter();
testNAdd(parameter, parameter.getType());
}
@Override public void visitField(final PsiField field) {
testNAdd(field, field.getType());
super.visitField(field);
}
@Override public void visitMethod(final PsiMethod method) {
final PsiParameter[] parms = method.getParameterList().getParameters();
for (PsiParameter parm : parms) {
testNAdd(parm, parm.getType());
}
if (Util.isRaw(method.getReturnType(), mySettings)) {
myVictims.add(method);
}
final PsiCodeBlock body = method.getBody();
if (body != null) {
body.accept(this);
}
}
@Override public void visitNewExpression(final PsiNewExpression expression) {
if (expression.getClassReference() != null) {
testNAdd(expression, expression.getType());
}
super.visitNewExpression(expression);
}
@Override public void visitTypeCastExpression (final PsiTypeCastExpression cast){
final PsiTypeElement typeElement = cast.getCastType();
if (typeElement != null) {
testNAdd(cast, typeElement.getType());
}
super.visitTypeCastExpression(cast);
}
@Override public void visitReferenceExpression(final PsiReferenceExpression expression) {
}
@Override public void visitFile(PsiFile file) {
if (file instanceof PsiJavaFile) {
super.visitFile(file);
}
}
public HashSet<PsiElement> getVictims() {
for (PsiElement element : myElements) {
element.accept(this);
}
return myVictims;
}
}
|