mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java inspection: convert the intention for "Infer lambda parameter type" into an INFORMATION-level inspection
This commit is contained in:
@@ -2539,6 +2539,10 @@
|
||||
key="expand.one.line.lambda2.code.block.name" groupBundle="messages.InspectionsBundle"
|
||||
groupKey="group.names.code.style.issues" enabledByDefault="true" level="INFORMATION" cleanupTool="true"
|
||||
implementationClass="com.siyeh.ig.style.ExpandOneLineLambda2CodeBlockInspection"/>
|
||||
<localInspection groupPath="Java" language="JAVA" shortName="InferLambdaParameterType" bundle="com.siyeh.InspectionGadgetsBundle"
|
||||
key="infer.lambda.parameter.type.name" groupBundle="messages.InspectionsBundle"
|
||||
groupKey="group.names.code.style.issues" enabledByDefault="true" level="INFORMATION"
|
||||
implementationClass="com.siyeh.ig.style.InferLambdaParameterTypeInspection"/>
|
||||
|
||||
<!--group.names.threading.issues-->
|
||||
<localInspection groupPath="Java" language="JAVA" suppressId="AccessToNonThreadSafeStaticField" shortName="AccessToNonThreadSafeStaticFieldFromInstance"
|
||||
|
||||
+3
-1
@@ -2178,4 +2178,6 @@ object.instantiation.inside.equals.or.hashcode.display.name=Object instantiation
|
||||
object.instantiation.inside.equals.or.hashcode.problem.descriptor=Object instantiation inside ''{0}()'' #loc
|
||||
expand.one.line.lambda2.code.block.name=Expand lambda expression body to code block
|
||||
expand.one.line.lambda2.code.block.descriptor=Expand lambda expression body to {...}
|
||||
expand.one.line.lambda2.code.block.quickfix=Expand lambda expression body to {...}
|
||||
expand.one.line.lambda2.code.block.quickfix=Expand lambda expression body to {...}
|
||||
infer.lambda.parameter.type.name=Infer lambda parameter type
|
||||
infer.lambda.parameter.type.descriptor=Expand lambda to {0} -> '{'...'}'
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Copyright 2011 Bas Leijdekkers
|
||||
*
|
||||
* 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.siyeh.ig.style;
|
||||
|
||||
import com.intellij.codeInspection.CleanupLocalInspectionTool;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class InferLambdaParameterTypeInspection extends BaseInspection implements CleanupLocalInspectionTool {
|
||||
private static final Logger LOG = Logger.getInstance("#" + InferLambdaParameterTypeInspection.class.getName());
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message("infer.lambda.parameter.type.name");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String buildErrorString(Object... infos) {
|
||||
return InspectionGadgetsBundle.message("infer.lambda.parameter.type.descriptor", infos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseInspectionVisitor buildVisitor() {
|
||||
return new InferLambdaParameterTypeVisitor();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new InferLambdaParameterTypeFix(infos);
|
||||
}
|
||||
|
||||
private static void doFix(@NotNull Project project, @NotNull PsiLambdaExpression lambdaExpression) {
|
||||
final PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
|
||||
final String buf = getInferredTypes(functionalInterfaceType, lambdaExpression, true);
|
||||
final PsiMethod methodFromText = JavaPsiFacade.getElementFactory(project).createMethodFromText("void foo" + buf, lambdaExpression);
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(lambdaExpression.getParameterList().replace(methodFromText.getParameterList()));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getInferredTypes(PsiType functionalInterfaceType, final PsiLambdaExpression lambdaExpression, boolean useFQN) {
|
||||
final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
buf.append("(");
|
||||
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
|
||||
LOG.assertTrue(interfaceMethod != null);
|
||||
final PsiParameter[] parameters = interfaceMethod.getParameterList().getParameters();
|
||||
final PsiParameter[] lambdaParameters = lambdaExpression.getParameterList().getParameters();
|
||||
if (parameters.length != lambdaParameters.length) return null;
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
PsiParameter parameter = parameters[i];
|
||||
final PsiType psiType = LambdaUtil.getSubstitutor(interfaceMethod, resolveResult).substitute(parameter.getType());
|
||||
if (!PsiTypesUtil.isDenotableType(psiType)) return null;
|
||||
if (psiType != null) {
|
||||
buf.append(useFQN ? psiType.getCanonicalText() : psiType.getPresentableText()).append(" ").append(lambdaParameters[i].getName());
|
||||
}
|
||||
else {
|
||||
buf.append(lambdaParameters[i].getName());
|
||||
}
|
||||
if (i < parameters.length - 1) {
|
||||
buf.append(", ");
|
||||
}
|
||||
}
|
||||
buf.append(")");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
private static class InferLambdaParameterTypeVisitor extends BaseInspectionVisitor {
|
||||
@Override
|
||||
public void visitLambdaExpression(PsiLambdaExpression lambdaExpression) {
|
||||
super.visitLambdaExpression(lambdaExpression);
|
||||
final PsiParameter[] parameters = lambdaExpression.getParameterList().getParameters();
|
||||
if (parameters.length == 0) return;
|
||||
for (PsiParameter parameter : parameters) {
|
||||
if (parameter.getTypeElement() != null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
final PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
|
||||
if (functionalInterfaceType != null &&
|
||||
LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType) != null &&
|
||||
LambdaUtil.isLambdaFullyInferred(lambdaExpression, functionalInterfaceType)) {
|
||||
final String inferredTypesText = getInferredTypes(functionalInterfaceType, lambdaExpression, false);
|
||||
if (inferredTypesText != null) {
|
||||
registerError(lambdaExpression, inferredTypesText);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class InferLambdaParameterTypeFix extends InspectionGadgetsFix {
|
||||
private final Object[] myInfos;
|
||||
|
||||
public InferLambdaParameterTypeFix(Object... infos) {
|
||||
myInfos = infos;
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return InspectionGadgetsBundle.message("infer.lambda.parameter.type.descriptor", myInfos);
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return InspectionGadgetsBundle.message("infer.lambda.parameter.type.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor) {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
if (element instanceof PsiLambdaExpression) {
|
||||
InferLambdaParameterTypeInspection.doFix(project, (PsiLambdaExpression)element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
-15
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 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.
|
||||
@@ -13,12 +13,18 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.siyeh.ipp.types;
|
||||
package com.siyeh.ig.fixes.style;
|
||||
|
||||
import com.siyeh.ipp.IPPTestCase;
|
||||
import com.siyeh.ig.IGQuickFixesTestCase;
|
||||
import com.siyeh.ig.style.InferLambdaParameterTypeInspection;
|
||||
|
||||
public class InferLambdaParameterTypeFixTest extends IGQuickFixesTestCase {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
myFixture.enableInspections(new InferLambdaParameterTypeInspection());
|
||||
}
|
||||
|
||||
public class InferLambdaParameterTypeIntentionTest extends IPPTestCase {
|
||||
|
||||
public void testSimple() {
|
||||
doTest("Expand lambda to (String o) -> {...}");
|
||||
}
|
||||
@@ -36,28 +42,23 @@ public class InferLambdaParameterTypeIntentionTest extends IPPTestCase {
|
||||
}
|
||||
|
||||
public void testAlreadyExist() throws Exception {
|
||||
assertIntentionNotAvailable("Expand lambda to (String o) -> {...}");
|
||||
assertQuickfixNotAvailable("Expand lambda to (String o) -> {...}");
|
||||
}
|
||||
|
||||
public void testCyclicInference() throws Exception {
|
||||
assertIntentionNotAvailable("Expand lambda to (Object x) -> {...}");
|
||||
assertQuickfixNotAvailable("Expand lambda to (Object x) -> {...}");
|
||||
}
|
||||
|
||||
public void testNoParams() throws Exception {
|
||||
assertIntentionNotAvailable("Expand lambda to () -> {...}");
|
||||
assertQuickfixNotAvailable("Expand lambda to () -> {...}");
|
||||
}
|
||||
|
||||
public void testCapturedWildcardParams() throws Exception {
|
||||
assertIntentionNotAvailable("Expand lambda to (capture of ?) -> {...}");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getIntentionName() {
|
||||
return "";
|
||||
assertQuickfixNotAvailable("Expand lambda to (capture of ?) -> {...}");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRelativePath() {
|
||||
return "types/inferLambdaParameterType";
|
||||
return "style/inferLambdaParameterType";
|
||||
}
|
||||
}
|
||||
@@ -274,11 +274,6 @@
|
||||
<bundleName>com.siyeh.IntentionPowerPackBundle</bundleName>
|
||||
<categoryKey>intention.category.declaration</categoryKey>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<className>com.siyeh.ipp.types.InferLambdaParameterTypeIntention</className>
|
||||
<bundleName>com.siyeh.IntentionPowerPackBundle</bundleName>
|
||||
<categoryKey>intention.category.declaration</categoryKey>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>com.siyeh.ipp.decls.ChangeVariableTypeToRhsTypeIntention</className>
|
||||
|
||||
-110
@@ -1,110 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 Bas Leijdekkers
|
||||
*
|
||||
* 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.siyeh.ipp.types;
|
||||
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ipp.base.Intention;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class InferLambdaParameterTypeIntention extends Intention {
|
||||
private static final Logger LOG = Logger.getInstance("#" + InferLambdaParameterTypeIntention.class.getName());
|
||||
private String myInferredTypesText;
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected PsiElementPredicate getElementPredicate() {
|
||||
return new LambdaParametersPredicate();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return "Expand lambda to " + myInferredTypesText + " -> {...}";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
|
||||
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
|
||||
LOG.assertTrue(lambdaExpression != null);
|
||||
final PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
|
||||
final String buf = getInferredTypes(functionalInterfaceType, lambdaExpression, true);
|
||||
final Project project = element.getProject();
|
||||
final PsiMethod methodFromText = JavaPsiFacade.getElementFactory(project).createMethodFromText("void foo" + buf, element);
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(lambdaExpression.getParameterList().replace(methodFromText.getParameterList()));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getInferredTypes(PsiType functionalInterfaceType, final PsiLambdaExpression lambdaExpression, boolean useFQN) {
|
||||
final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
buf.append("(");
|
||||
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
|
||||
LOG.assertTrue(interfaceMethod != null);
|
||||
final PsiParameter[] parameters = interfaceMethod.getParameterList().getParameters();
|
||||
final PsiParameter[] lambdaParameters = lambdaExpression.getParameterList().getParameters();
|
||||
if (parameters.length != lambdaParameters.length) return null;
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
PsiParameter parameter = parameters[i];
|
||||
final PsiType psiType = LambdaUtil.getSubstitutor(interfaceMethod, resolveResult).substitute(parameter.getType());
|
||||
if (!PsiTypesUtil.isDenotableType(psiType)) return null;
|
||||
if (psiType != null) {
|
||||
buf.append(useFQN ? psiType.getCanonicalText() : psiType.getPresentableText()).append(" ").append(lambdaParameters[i].getName());
|
||||
}
|
||||
else {
|
||||
buf.append(lambdaParameters[i].getName());
|
||||
}
|
||||
if (i < parameters.length - 1) {
|
||||
buf.append(", ");
|
||||
}
|
||||
}
|
||||
buf.append(")");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
|
||||
private class LambdaParametersPredicate implements PsiElementPredicate {
|
||||
@Override
|
||||
public boolean satisfiedBy(PsiElement element) {
|
||||
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
|
||||
if (lambdaExpression != null) {
|
||||
PsiParameter[] parameters = lambdaExpression.getParameterList().getParameters();
|
||||
if (parameters.length == 0) return false;
|
||||
for (PsiParameter parameter : parameters) {
|
||||
if (parameter.getTypeElement() != null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (PsiTreeUtil.isAncestor(lambdaExpression.getParameterList(), element, false)) {
|
||||
final PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
|
||||
if (functionalInterfaceType != null && LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType) != null && LambdaUtil.isLambdaFullyInferred(lambdaExpression, functionalInterfaceType)) {
|
||||
myInferredTypesText = getInferredTypes(functionalInterfaceType, lambdaExpression, false);
|
||||
return myInferredTypesText != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
public class X {
|
||||
{
|
||||
Comparable c = o -> {return 42;};
|
||||
}
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
public class X {
|
||||
{
|
||||
Comparable c = o -> <spot>42</spot>;
|
||||
}
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention converts lambda's expression body to code block
|
||||
</body>
|
||||
</html>
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
class X {
|
||||
{
|
||||
Comparable<String> c = (String o) -> 1;
|
||||
}
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
class X {
|
||||
{
|
||||
Comparable<String> c = <spot>o</spot> -> 1;
|
||||
}
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention extends lambda parameters to contain formal types
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user