mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
after review IDEA-CR-5305 changes
This commit is contained in:
+30
-28
@@ -16,7 +16,8 @@
|
||||
package com.intellij.refactoring.typeMigration.inspections;
|
||||
|
||||
import com.intellij.codeInsight.daemon.impl.quickfix.VariableTypeFix;
|
||||
import com.intellij.codeInspection.*;
|
||||
import com.intellij.codeInspection.BaseJavaLocalInspectionTool;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.AtomicNotNullLazyValue;
|
||||
import com.intellij.psi.*;
|
||||
@@ -28,7 +29,8 @@ import com.intellij.refactoring.typeMigration.rules.guava.BaseGuavaTypeConversio
|
||||
import com.intellij.util.containers.hash.HashMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Dmitry Batkovich
|
||||
@@ -44,21 +46,27 @@ public class GuavaInspection extends BaseJavaLocalInspectionTool {
|
||||
return PsiElementVisitor.EMPTY_VISITOR;
|
||||
}
|
||||
return new JavaElementVisitor() {
|
||||
private final AtomicNotNullLazyValue<Map<String, String>> myGuavaClassConversions = new AtomicNotNullLazyValue<Map<String, String>>() {
|
||||
@NotNull
|
||||
@Override
|
||||
protected Map<String, String> compute() {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
for (TypeConversionRule rule : TypeConversionRule.EP_NAME.getExtensions()) {
|
||||
if (rule instanceof BaseGuavaTypeConversionRule) {
|
||||
final String fromClass = ((BaseGuavaTypeConversionRule)rule).ruleFromClass();
|
||||
final String toClass = ((BaseGuavaTypeConversionRule)rule).ruleToClass();
|
||||
map.put(fromClass, toClass);
|
||||
private final AtomicNotNullLazyValue<Map<String, PsiClass>> myGuavaClassConversions =
|
||||
new AtomicNotNullLazyValue<Map<String, PsiClass>>() {
|
||||
@NotNull
|
||||
@Override
|
||||
protected Map<String, PsiClass> compute() {
|
||||
Map<String, PsiClass> map = new HashMap<String, PsiClass>();
|
||||
for (TypeConversionRule rule : TypeConversionRule.EP_NAME.getExtensions()) {
|
||||
if (rule instanceof BaseGuavaTypeConversionRule) {
|
||||
final String fromClass = ((BaseGuavaTypeConversionRule)rule).ruleFromClass();
|
||||
final String toClass = ((BaseGuavaTypeConversionRule)rule).ruleToClass();
|
||||
|
||||
final Project project = holder.getProject();
|
||||
final JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
|
||||
final PsiClass targetClass = javaPsiFacade.findClass(toClass, GlobalSearchScope.allScope(project));
|
||||
|
||||
map.put(fromClass, targetClass);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@Override
|
||||
public void visitVariable(PsiVariable variable) {
|
||||
@@ -68,19 +76,13 @@ public class GuavaInspection extends BaseJavaLocalInspectionTool {
|
||||
final PsiClass psiClass = resolveResult.getElement();
|
||||
if (psiClass != null) {
|
||||
final String qName = psiClass.getQualifiedName();
|
||||
final String toQName = myGuavaClassConversions.getValue().get(qName);
|
||||
if (toQName != null) {
|
||||
|
||||
final Project project = holder.getProject();
|
||||
final JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
|
||||
final PsiClass targetClass = javaPsiFacade.findClass(toQName, GlobalSearchScope.allScope(project));
|
||||
if (targetClass != null) {
|
||||
final Collection<PsiType> typeParameters = resolveResult.getSubstitutor().getSubstitutionMap().values();
|
||||
final PsiClassType targetType =
|
||||
javaPsiFacade.getElementFactory().createType(targetClass, typeParameters.toArray(new PsiType[typeParameters.size()]));
|
||||
final VariableTypeFix fix = TypeMigrationVariableTypeFixProvider.createTypeMigrationFix(variable, targetType);
|
||||
holder.registerProblem(variable, PROBLEM_DESCRIPTION, fix);
|
||||
}
|
||||
final PsiClass targetClass = myGuavaClassConversions.getValue().get(qName);
|
||||
if (targetClass != null) {
|
||||
final Collection<PsiType> typeParameters = resolveResult.getSubstitutor().getSubstitutionMap().values();
|
||||
final PsiClassType targetType =
|
||||
JavaPsiFacade.getElementFactory(holder.getProject()).createType(targetClass, typeParameters.toArray(new PsiType[typeParameters.size()]));
|
||||
final VariableTypeFix fix = TypeMigrationVariableTypeFixProvider.createTypeMigrationFix(variable, targetType);
|
||||
holder.registerProblem(variable, PROBLEM_DESCRIPTION, fix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -80,7 +80,7 @@ public abstract class BaseGuavaTypeConversionRule extends TypeConversionRule {
|
||||
return findConversionForMethod(from, to, method, methodName, context, labeler);
|
||||
} else if (context instanceof PsiNewExpression) {
|
||||
final PsiAnonymousClass anonymousClass = ((PsiNewExpression)context).getAnonymousClass();
|
||||
if (AnonymousCanBeLambdaInspection.canBeConvertedToLambda(anonymousClass, false)) {
|
||||
if (anonymousClass != null && AnonymousCanBeLambdaInspection.canBeConvertedToLambda(anonymousClass, false)) {
|
||||
return new TypeConversionDescriptorBase() {
|
||||
@Override
|
||||
public PsiExpression replace(PsiExpression expression) throws IncorrectOperationException {
|
||||
|
||||
+11
-16
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.refactoring.typeMigration.rules.guava;
|
||||
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.intellij.refactoring.typeMigration.TypeConversionDescriptor;
|
||||
@@ -29,6 +30,8 @@ import java.util.Map;
|
||||
* @author Dmitry Batkovich
|
||||
*/
|
||||
public class GuavaOptionalConversionRule extends BaseGuavaTypeConversionRule {
|
||||
private final static Logger LOG = Logger.getInstance(GuavaOptionalConversionRule.class);
|
||||
|
||||
public final static String GUAVA_OPTIONAL = "com.google.common.base.Optional";
|
||||
|
||||
@Nullable
|
||||
@@ -49,15 +52,11 @@ public class GuavaOptionalConversionRule extends BaseGuavaTypeConversionRule {
|
||||
final PsiClass aClass = PsiTypesUtil.getPsiClass(parameters[0].getType());
|
||||
if (aClass != null) {
|
||||
final String qName = aClass.getQualifiedName();
|
||||
if (GUAVA_OPTIONAL.equals(qName)) {
|
||||
return new TypeConversionDescriptor("$expr$", "java.util.Optional.ofNullable($expr$.get())");
|
||||
}
|
||||
else if ("com.google.common.bas.Supplier".equals(qName)) {
|
||||
return new TypeConversionDescriptor("$expr$", "java.util.Optional.ofNullable($expr$)");
|
||||
}
|
||||
else {
|
||||
return new TypeConversionDescriptor("$expr$", "java.util.Optional.ofNullable($expr$)");
|
||||
}
|
||||
String pattern =
|
||||
GUAVA_OPTIONAL.equals(qName) ? "java.util.Optional.ofNullable($expr$.get())" : "com.google.common.bas.Supplier".equals(qName)
|
||||
? "java.util.Optional.ofNullable($expr$)"
|
||||
: "java.util.Optional.ofNullable($expr$)";
|
||||
return new TypeConversionDescriptor("$expr$", pattern);
|
||||
}
|
||||
return null;
|
||||
} else if (context.getParent() instanceof PsiMethodCallExpression) {
|
||||
@@ -76,12 +75,8 @@ public class GuavaOptionalConversionRule extends BaseGuavaTypeConversionRule {
|
||||
if (GUAVA_OPTIONAL.equals(qName)) {
|
||||
return new TypeConversionDescriptor("$val$.or($other$)", "java.util.Optional.ofNullable($val$.orElseGet($other$::get))", to);
|
||||
}
|
||||
else if ("com.google.common.bas.Supplier".equals(qName)) {
|
||||
return new TypeConversionDescriptor("$val$.or($other$)", "$val$.orElseGet($other$::get)");
|
||||
}
|
||||
else {
|
||||
return new TypeConversionDescriptor("$val$.or($other$)", "$val$.orElse($other$)");
|
||||
}
|
||||
String pattern = "com.google.common.bas.Supplier".equals(qName) ? "$val$.orElseGet($other$::get)" : "$val$.orElse($other$)";
|
||||
return new TypeConversionDescriptor("$val$.or($other$)", pattern);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -93,7 +88,7 @@ public class GuavaOptionalConversionRule extends BaseGuavaTypeConversionRule {
|
||||
descriptorsMap.put("absent", new TypeConversionDescriptor("Optional.absent()", "java.util.Optional.empty()") {
|
||||
@Override
|
||||
public PsiExpression replace(PsiExpression expression) {
|
||||
assert expression instanceof PsiMethodCallExpression;
|
||||
LOG.assertTrue(expression instanceof PsiMethodCallExpression);
|
||||
|
||||
final PsiReferenceParameterList typeArguments = ((PsiMethodCallExpression)expression).getTypeArgumentList();
|
||||
PsiReferenceParameterList typeArgumentsCopy =
|
||||
|
||||
Reference in New Issue
Block a user