Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Intellij Community
Commits
412d071d
Commit
412d071d
authored
8 years ago
by
Anna.Kozlova
Browse files
Options
Download
Email Patches
Plain Diff
add fixes to change expected type when inference fails due to it (IDEA-162846)
parent
f3259b84
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java
+32
-0
...codeInsight/daemon/impl/analysis/HighlightMethodUtil.java
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/afterFailedInferenceDueToWrongExpectedType.java
+10
-0
...iableType/afterFailedInferenceDueToWrongExpectedType.java
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/afterFailedInferenceDueToWrongExpectedTypeAssignment.java
+11
-0
...afterFailedInferenceDueToWrongExpectedTypeAssignment.java
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/afterFailedInferenceDueToWrongExpectedTypeNotApplicable.java
+15
-0
...erFailedInferenceDueToWrongExpectedTypeNotApplicable.java
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/beforeFailedInferenceDueToWrongExpectedType.java
+10
-0
...ableType/beforeFailedInferenceDueToWrongExpectedType.java
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/beforeFailedInferenceDueToWrongExpectedTypeAssignment.java
+11
-0
...eforeFailedInferenceDueToWrongExpectedTypeAssignment.java
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/beforeFailedInferenceDueToWrongExpectedTypeNotApplicable.java
+15
-0
...reFailedInferenceDueToWrongExpectedTypeNotApplicable.java
with
104 additions
and
0 deletions
+104
-0
java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java
+
32
-
0
View file @
412d071d
...
...
@@ -390,6 +390,7 @@ public class HighlightMethodUtil {
if
(
highlightInfo
!=
null
)
{
registerMethodCallIntentions
(
highlightInfo
,
methodCall
,
list
,
resolveHelper
);
registerMethodReturnFixAction
(
highlightInfo
,
(
MethodCandidateInfo
)
resolveResult
,
methodCall
);
registerTargetTypeFixesBasedOnApplicabilityInference
(
methodCall
,
(
MethodCandidateInfo
)
resolveResult
,
(
PsiMethod
)
resolved
,
highlightInfo
);
}
}
}
...
...
@@ -430,6 +431,7 @@ public class HighlightMethodUtil {
if
(
highlightInfo
!=
null
)
{
registerMethodCallIntentions
(
highlightInfo
,
methodCall
,
list
,
resolveHelper
);
registerMethodReturnFixAction
(
highlightInfo
,
candidateInfo
,
methodCall
);
registerTargetTypeFixesBasedOnApplicabilityInference
(
methodCall
,
candidateInfo
,
resolvedMethod
,
highlightInfo
);
}
}
else
{
...
...
@@ -472,6 +474,36 @@ public class HighlightMethodUtil {
return
highlightInfo
;
}
private
static
void
registerTargetTypeFixesBasedOnApplicabilityInference
(
@NotNull
PsiMethodCallExpression
methodCall
,
MethodCandidateInfo
resolveResult
,
PsiMethod
resolved
,
HighlightInfo
highlightInfo
)
{
PsiElement
parent
=
PsiUtil
.
skipParenthesizedExprUp
(
methodCall
.
getParent
());
PsiVariable
variable
=
null
;
if
(
parent
instanceof
PsiVariable
)
{
variable
=
(
PsiVariable
)
parent
;
}
else
if
(
parent
instanceof
PsiAssignmentExpression
)
{
PsiExpression
lExpression
=
((
PsiAssignmentExpression
)
parent
).
getLExpression
();
if
(
lExpression
instanceof
PsiReferenceExpression
)
{
PsiElement
resolve
=
((
PsiReferenceExpression
)
lExpression
).
resolve
();
if
(
resolve
instanceof
PsiVariable
)
{
variable
=
(
PsiVariable
)
resolve
;
}
}
}
if
(
variable
!=
null
)
{
PsiType
rType
=
methodCall
.
getType
();
if
(
rType
!=
null
&&
!
variable
.
getType
().
isAssignableFrom
(
rType
))
{
PsiType
expectedTypeByApplicabilityConstraints
=
resolveResult
.
getSubstitutor
(
false
).
substitute
(
resolved
.
getReturnType
());
if
(
expectedTypeByApplicabilityConstraints
!=
null
&&
!
expectedTypeByApplicabilityConstraints
.
equals
(
rType
))
{
HighlightUtil
.
registerChangeVariableTypeFixes
(
variable
,
expectedTypeByApplicabilityConstraints
,
methodCall
,
highlightInfo
);
}
}
}
}
/* see also PsiReferenceExpressionImpl.hasValidQualifier() */
@Nullable
private
static
String
checkStaticInterfaceMethodCallQualifier
(
PsiReferenceExpression
ref
,
PsiElement
scope
,
PsiClass
containingClass
)
{
...
...
This diff is collapsed.
Click to expand it.
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/afterFailedInferenceDueToWrongExpectedType.java
0 → 100644
+
10
-
0
View file @
412d071d
// "Change variable 'foo' type to 'java.util.List<java.lang.String>'" "true"
import
java.util.Arrays
;
import
java.util.List
;
class
MyClass
{
void
bar
()
{
List
<
String
>
foo
=
Arrays
.
asList
(
"a"
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/afterFailedInferenceDueToWrongExpectedTypeAssignment.java
0 → 100644
+
11
-
0
View file @
412d071d
// "Change variable 'foo' type to 'java.util.List<java.lang.String>'" "true"
import
java.util.Arrays
;
import
java.util.List
;
class
MyClass
{
void
bar
()
{
List
<
String
>
foo
;
foo
=
(
Arrays
.
asList
(
"a"
));
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/afterFailedInferenceDueToWrongExpectedTypeNotApplicable.java
0 → 100644
+
15
-
0
View file @
412d071d
// "Change variable 'foo' type to 'java.lang.String'" "true"
import
java.util.List
;
import
java.util.Set
;
class
MyClass
{
public
static
void
sort
(
final
List
<
String
>
list
)
{
String
foo
=
(
findStart
(
list
));
}
private
static
<
V
>
V
findStart
(
List
<
V
>
result
)
{
return
result
.
get
(
0
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/beforeFailedInferenceDueToWrongExpectedType.java
0 → 100644
+
10
-
0
View file @
412d071d
// "Change variable 'foo' type to 'java.util.List<java.lang.String>'" "true"
import
java.util.Arrays
;
import
java.util.List
;
class
MyClass
{
void
bar
()
{
String
[]
foo
=
Arrays
.
asList
(<
caret
>
"a"
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/beforeFailedInferenceDueToWrongExpectedTypeAssignment.java
0 → 100644
+
11
-
0
View file @
412d071d
// "Change variable 'foo' type to 'java.util.List<java.lang.String>'" "true"
import
java.util.Arrays
;
import
java.util.List
;
class
MyClass
{
void
bar
()
{
String
[]
foo
;
foo
=
(
Arrays
.
asList
(<
caret
>
"a"
));
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/variableType/beforeFailedInferenceDueToWrongExpectedTypeNotApplicable.java
0 → 100644
+
15
-
0
View file @
412d071d
// "Change variable 'foo' type to 'java.lang.String'" "true"
import
java.util.List
;
import
java.util.Set
;
class
MyClass
{
public
static
void
sort
(
final
List
<
String
>
list
)
{
Set
<
String
>
foo
=
(
findStart
(<
caret
>
list
));
}
private
static
<
V
>
V
findStart
(
List
<
V
>
result
)
{
return
result
.
get
(
0
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment