add explicit type arguments intention (IDEA-173960)

This commit is contained in:
Anna Kozlova
2017-06-09 21:15:37 +03:00
parent d55d21f31c
commit fcd84a0d35
7 changed files with 173 additions and 0 deletions
@@ -0,0 +1,65 @@
/*
* Copyright 2000-2017 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.codeInsight.intention.impl;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.daemon.impl.quickfix.AddTypeArgumentsFix;
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.infos.MethodCandidateInfo;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
public class AddExplicitTypeArgumentsIntention extends BaseElementAtCaretIntentionAction {
@Override
@NotNull
public String getFamilyName() {
return CodeInsightBundle.message("intention.add.explicit.type.arguments.family");
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
PsiIdentifier identifier = ObjectUtils.tryCast(element, PsiIdentifier.class);
if (identifier == null) return false;
PsiReferenceExpression methodExpression = ObjectUtils.tryCast(identifier.getParent(), PsiReferenceExpression.class);
if (methodExpression == null) return false;
PsiElement parent = methodExpression.getParent();
if (parent instanceof PsiMethodCallExpression && ((PsiMethodCallExpression)parent).getTypeArguments().length == 0) {
JavaResolveResult result = ((PsiMethodCallExpression)parent).resolveMethodGenerics();
if (result instanceof MethodCandidateInfo && ((MethodCandidateInfo)result).isApplicable()) {
PsiElement method = result.getElement();
setText(getFamilyName());
return method instanceof PsiMethod && !((PsiMethod)method).isConstructor() && ((PsiMethod)method).hasTypeParameters();
}
}
return false;
}
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
PsiMethodCallExpression callExpression = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class);
assert callExpression != null;
PsiExpression withArgs = AddTypeArgumentsFix.addTypeArguments(callExpression, null);
if (withArgs != null) {
CodeStyleManager.getInstance(project).reformat(callExpression.replace(withArgs));
}
}
}
@@ -0,0 +1,86 @@
/*
* Copyright 2000-2017 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.java.codeInsight.intention;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
public class AddExplicitTypeArgumentsIntentionTest extends JavaCodeInsightFixtureTestCase {
public void testNoStaticQualifier() {
doTest("class Test {\n" +
" static {\n" +
" String s = fo<caret>o(\"\");\n" +
" }\n" +
" static <T> T foo(T t) {\n" +
" return t;\n" +
" }\n" +
"}",
"class Test {\n" +
" static {\n" +
" String s = Test.<String>foo(\"\");\n" +
" }\n" +
" static <T> T foo(T t) {\n" +
" return t;\n" +
" }\n" +
"}");
}
public void testNoThisQualifier() {
doTest("class Test {\n" +
" {\n" +
" String s = fo<caret>o(\"\");\n" +
" }\n" +
" <T> T foo(T t) {\n" +
" return t;\n" +
" }\n" +
"}",
"class Test {\n" +
" {\n" +
" String s = this.<String>foo(\"\");\n" +
" }\n" +
" <T> T foo(T t) {\n" +
" return t;\n" +
" }\n" +
"}");
}
public void testInferredCapturedWildcard() {
doTest("class Test {\n" +
" static void m(java.util.List<? extends String> l) {\n" +
" fo<caret>o(l.get(0));\n" +
" }\n" +
" static <T> void foo(T t) {\n" +
" }\n" +
"}",
"class Test {\n" +
" static void m(java.util.List<? extends String> l) {\n" +
" Test.<String>foo(l.get(0));\n" +
" }\n" +
" static <T> void foo(T t) {\n" +
" }\n" +
"}");
}
private void doTest(String beforeText, String afterText) {
myFixture.configureByText("a.java", beforeText);
final IntentionAction intentionAction = myFixture.findSingleIntention(CodeInsightBundle.message("intention.add.explicit.type.arguments.family"));
assertNotNull(intentionAction);
myFixture.launchAction(intentionAction);
myFixture.checkResult(afterText);
}
}
@@ -192,6 +192,7 @@ intention.add.on.demand.static.import.text=Add on demand static import for ''{0}
intention.add.single.member.static.import.family=Add Single-Member Static Import
intention.add.single.member.static.import.text=Add static import for ''{0}''
intention.add.single.member.import.text=Add import for ''{0}''
intention.add.explicit.type.arguments.family=Add explicit type arguments
intention.replace.concatenation.with.formatted.output.family=Replace Concatenation with Formatted Output
intention.replace.concatenation.with.formatted.output.text=Replace '+' with 'java.text.MessageFormat.format()'
intention.color.chooser.dialog=Choose Color
@@ -0,0 +1,6 @@
import java.util.*;
class Computer {
void f() {
List<String> l = Arrays.<String>asList("a", "b", "c");
}
}
@@ -0,0 +1,6 @@
import java.util.*;
class Computer {
void f() {
List<String> l = <spot>Arrays.asList("a", "b", "c")</spot>;
}
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention add explicit type arguments to method calls
</body>
</html>
+4
View File
@@ -1167,6 +1167,10 @@
<className>com.intellij.codeInsight.intention.impl.AddSingleMemberStaticImportAction</className>
<category>Java/Imports</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.AddExplicitTypeArgumentsIntention</className>
<category>Java/Declaration</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.ExpandStaticImportAction</className>
<category>Java/Imports</category>