mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
small cleanup refactoring to stream inspection
This commit is contained in:
+8
-15
@@ -22,7 +22,6 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.impl.PsiDiamondTypeUtil;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.util.*;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
@@ -156,16 +155,17 @@ class PseudoLambdaReplaceTemplate {
|
||||
return null;
|
||||
}
|
||||
|
||||
final PsiMethod method = expression.resolveMethod();
|
||||
if (method == null) {
|
||||
final JavaResolveResult result = expression.getMethodExpression().advancedResolve(false);
|
||||
final PsiElement element = result.getElement();
|
||||
if (!(element instanceof PsiMethod)) {
|
||||
return null;
|
||||
}
|
||||
final PsiParameter[] expectedParameters = method.getParameterList().getParameters();
|
||||
final PsiParameter[] expectedParameters = ((PsiMethod)element).getParameterList().getParameters();
|
||||
|
||||
if (argumentTypes.length != expectedParameters.length) {
|
||||
return null;
|
||||
}
|
||||
final JavaResolveResult result = expression.getMethodExpression().advancedResolve(false);
|
||||
|
||||
final PsiSubstitutor methodSubstitutor = result.getSubstitutor();
|
||||
return validate(argumentTypes, methodReturnType, methodSubstitutor, expression);
|
||||
}
|
||||
@@ -357,8 +357,7 @@ class PseudoLambdaReplaceTemplate {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public PsiExpression convertToStream(final PsiMethodCallExpression expression, PsiMethod method, boolean force) {
|
||||
LOG.assertTrue(expression != null);
|
||||
public PsiExpression convertToStream(@NotNull final PsiMethodCallExpression expression, @Nullable PsiMethod method, boolean force) {
|
||||
if (method == null) {
|
||||
method = expression.resolveMethod();
|
||||
if (method == null) {
|
||||
@@ -556,15 +555,9 @@ class PseudoLambdaReplaceTemplate {
|
||||
|
||||
private static boolean isIterableOrArray(final PsiType type) {
|
||||
if (type instanceof PsiClassType) {
|
||||
final PsiClass resolvedClass = ((PsiClassType)type).resolve();
|
||||
if (resolvedClass != null) {
|
||||
return InheritanceUtil.isInheritor(resolvedClass, CommonClassNames.JAVA_LANG_ITERABLE);
|
||||
}
|
||||
return InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_ITERABLE);
|
||||
}
|
||||
else if (type instanceof PsiArrayType) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return type instanceof PsiArrayType;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+44
-41
@@ -61,47 +61,46 @@ public class StaticPseudoFunctionalStyleMethodInspection extends BaseJavaBatchLo
|
||||
if (!PsiUtil.isLanguageLevel8OrHigher(holder.getFile())) {
|
||||
return PsiElementVisitor.EMPTY_VISITOR;
|
||||
}
|
||||
return new PsiElementVisitor() {
|
||||
return new JavaElementVisitor() {
|
||||
@Override
|
||||
public void visitElement(PsiElement element) {
|
||||
if (element instanceof PsiMethodCallExpression) {
|
||||
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)element;
|
||||
String qName = methodCallExpression.getMethodExpression().getQualifiedName();
|
||||
if (qName == null) {
|
||||
return;
|
||||
}
|
||||
qName = StringUtil.getShortName(qName);
|
||||
final Collection<StaticPseudoFunctionalStyleMethodOptions.PipelineElement> handlerInfos = myOptions.findElementsByMethodName(qName);
|
||||
if (handlerInfos.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
final PsiMethod method = methodCallExpression.resolveMethod();
|
||||
if (method == null) {
|
||||
return;
|
||||
}
|
||||
final PsiClass aClass = method.getContainingClass();
|
||||
if (aClass == null) {
|
||||
return;
|
||||
}
|
||||
final String classQualifiedName = aClass.getQualifiedName();
|
||||
if (classQualifiedName == null) {
|
||||
return;
|
||||
}
|
||||
StaticPseudoFunctionalStyleMethodOptions.PipelineElement suitableHandler = null;
|
||||
for (StaticPseudoFunctionalStyleMethodOptions.PipelineElement h : handlerInfos) {
|
||||
if (h.getHandlerClass().equals(classQualifiedName)) {
|
||||
suitableHandler = h;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (suitableHandler == null) {
|
||||
return;
|
||||
}
|
||||
final PseudoLambdaReplaceTemplate.ValidationInfo validationInfo = suitableHandler.getTemplate().validate(methodCallExpression);
|
||||
if (validationInfo != null) {
|
||||
holder.registerProblem(methodCallExpression.getMethodExpression(), "Pseudo functional style code", new ReplacePseudoLambdaWithLambda(suitableHandler));
|
||||
public void visitMethodCallExpression(PsiMethodCallExpression methodCallExpression) {
|
||||
String qName = methodCallExpression.getMethodExpression().getQualifiedName();
|
||||
if (qName == null) {
|
||||
return;
|
||||
}
|
||||
qName = StringUtil.getShortName(qName);
|
||||
final Collection<StaticPseudoFunctionalStyleMethodOptions.PipelineElement> handlerInfos = myOptions.findElementsByMethodName(qName);
|
||||
if (handlerInfos.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
final PsiMethod method = methodCallExpression.resolveMethod();
|
||||
if (method == null) {
|
||||
return;
|
||||
}
|
||||
final PsiClass aClass = method.getContainingClass();
|
||||
if (aClass == null) {
|
||||
return;
|
||||
}
|
||||
final String classQualifiedName = aClass.getQualifiedName();
|
||||
if (classQualifiedName == null) {
|
||||
return;
|
||||
}
|
||||
StaticPseudoFunctionalStyleMethodOptions.PipelineElement suitableHandler = null;
|
||||
for (StaticPseudoFunctionalStyleMethodOptions.PipelineElement h : handlerInfos) {
|
||||
if (h.getHandlerClass().equals(classQualifiedName)) {
|
||||
suitableHandler = h;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (suitableHandler == null) {
|
||||
return;
|
||||
}
|
||||
final PseudoLambdaReplaceTemplate.ValidationInfo validationInfo = suitableHandler.getTemplate().validate(methodCallExpression);
|
||||
if (validationInfo != null) {
|
||||
holder.registerProblem(methodCallExpression.getMethodExpression(),
|
||||
"Pseudo functional style code",
|
||||
new ReplacePseudoLambdaWithLambda(suitableHandler));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -128,9 +127,13 @@ public class StaticPseudoFunctionalStyleMethodInspection extends BaseJavaBatchLo
|
||||
|
||||
@Override
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
PsiMethodCallExpression expression = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiMethodCallExpression.class);
|
||||
LOG.assertTrue(expression != null);
|
||||
myHandler.getTemplate().convertToStream(expression, null, false);
|
||||
PsiElement psiElement = descriptor.getPsiElement();
|
||||
if (psiElement instanceof PsiReferenceExpression) {
|
||||
PsiElement parent = psiElement.getParent();
|
||||
if (parent instanceof PsiMethodCallExpression) {
|
||||
myHandler.getTemplate().convertToStream((PsiMethodCallExpression)parent, null, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user