mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
replace onDemand static import with single imports (IDEA-175105)
This commit is contained in:
@@ -16,8 +16,8 @@
|
||||
package com.siyeh.ipp.imports;
|
||||
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiImportStatement;
|
||||
import com.intellij.psi.PsiImportStatementBase;
|
||||
import com.intellij.psi.PsiImportStaticStatement;
|
||||
import com.intellij.psi.PsiJavaFile;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
import com.siyeh.ipp.psiutils.ErrorUtil;
|
||||
@@ -27,14 +27,16 @@ class OnDemandImportPredicate implements PsiElementPredicate {
|
||||
|
||||
@Override
|
||||
public boolean satisfiedBy(@NotNull PsiElement element) {
|
||||
// doesn't work for import static yet.
|
||||
if (!(element instanceof PsiImportStatement)) {
|
||||
if (!(element instanceof PsiImportStatementBase)) {
|
||||
return false;
|
||||
}
|
||||
PsiImportStatementBase importStatement = (PsiImportStatementBase)element;
|
||||
if (!importStatement.isOnDemand() || ErrorUtil.containsError(element)) {
|
||||
return false;
|
||||
}
|
||||
if (importStatement instanceof PsiImportStaticStatement && ((PsiImportStaticStatement)importStatement).resolveTargetClass() == null) {
|
||||
return false;
|
||||
}
|
||||
return importStatement.getContainingFile() instanceof PsiJavaFile;
|
||||
}
|
||||
}
|
||||
+35
-15
@@ -17,15 +17,14 @@ package com.siyeh.ipp.imports;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.ClassUtil;
|
||||
import com.intellij.psi.util.ImportsUtil;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ipp.base.Intention;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ReplaceOnDemandImportIntention extends Intention {
|
||||
|
||||
@@ -38,9 +37,11 @@ public class ReplaceOnDemandImportIntention extends Intention {
|
||||
@Override
|
||||
protected void processIntention(@NotNull PsiElement element) {
|
||||
final PsiImportStatementBase importStatementBase = (PsiImportStatementBase)element;
|
||||
final PsiJavaFile javaFile = (PsiJavaFile)importStatementBase.getContainingFile();
|
||||
final PsiManager manager = importStatementBase.getManager();
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
|
||||
if (importStatementBase instanceof PsiImportStatement) {
|
||||
final PsiImportStatement importStatement = (PsiImportStatement)importStatementBase;
|
||||
final PsiJavaFile javaFile = (PsiJavaFile)importStatement.getContainingFile();
|
||||
final PsiClass[] classes = javaFile.getClasses();
|
||||
final String qualifiedName = importStatement.getQualifiedName();
|
||||
final ClassCollector visitor = new ClassCollector(qualifiedName);
|
||||
@@ -49,24 +50,43 @@ public class ReplaceOnDemandImportIntention extends Intention {
|
||||
}
|
||||
final PsiClass[] importedClasses = visitor.getImportedClasses();
|
||||
Arrays.sort(importedClasses, new PsiClassComparator());
|
||||
final PsiManager manager = importStatement.getManager();
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
|
||||
final PsiElement importList = importStatement.getParent();
|
||||
for (PsiClass importedClass : importedClasses) {
|
||||
final PsiImportStatement newImportStatement = factory.createImportStatement(importedClass);
|
||||
importList.add(newImportStatement);
|
||||
}
|
||||
new CommentTracker().deleteAndRestoreComments(importStatement);
|
||||
createImportStatements(importStatement, importedClasses, factory::createImportStatement);
|
||||
}
|
||||
else if (importStatementBase instanceof PsiImportStaticStatement) {
|
||||
// do something else
|
||||
PsiClass targetClass = ((PsiImportStaticStatement)importStatementBase).resolveTargetClass();
|
||||
if (targetClass != null) {
|
||||
String[] members = ImportsUtil.collectReferencesThrough(javaFile,
|
||||
importStatementBase.getImportReference(),
|
||||
(PsiImportStaticStatement)importStatementBase)
|
||||
.stream()
|
||||
.map(PsiReference::resolve)
|
||||
.filter(resolve -> resolve instanceof PsiMember)
|
||||
.map(member -> ((PsiMember)member).getName())
|
||||
.distinct()
|
||||
.filter(Objects::nonNull)
|
||||
.toArray(String[]::new);
|
||||
|
||||
createImportStatements(importStatementBase,
|
||||
members,
|
||||
member -> factory.createImportStaticStatement(targetClass, member));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> void createImportStatements(PsiImportStatementBase importStatement,
|
||||
T[] importedMembers,
|
||||
Function<T, PsiImportStatementBase> function) {
|
||||
final PsiElement importList = importStatement.getParent();
|
||||
for (T importedMember : importedMembers) {
|
||||
importList.add(function.apply(importedMember));
|
||||
}
|
||||
new CommentTracker().deleteAndRestoreComments(importStatement);
|
||||
}
|
||||
|
||||
private static class ClassCollector extends JavaRecursiveElementWalkingVisitor {
|
||||
|
||||
private final String importedPackageName;
|
||||
private final Set<PsiClass> importedClasses = new HashSet();
|
||||
private final Set<PsiClass> importedClasses = new HashSet<>();
|
||||
|
||||
ClassCollector(String importedPackageName) {
|
||||
this.importedPackageName = importedPackageName;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import static java.util.Arrays.*;
|
||||
class A {
|
||||
void f(String[] array){
|
||||
sort(array);
|
||||
System.out.println(asList(array));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Arrays.sort;
|
||||
|
||||
class A {
|
||||
void f(String[] array){
|
||||
sort(array);
|
||||
System.out.println(asList(array));
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.siyeh.ipp.imports;
|
||||
|
||||
import com.siyeh.IntentionPowerPackBundle;
|
||||
import com.siyeh.ipp.IPPTestCase;
|
||||
|
||||
public class ReplaceOnDemandImportIntentionTest extends IPPTestCase {
|
||||
|
||||
public void testStaticImport() { doTest(); }
|
||||
|
||||
@Override
|
||||
protected String getIntentionName() {
|
||||
return IntentionPowerPackBundle.message("replace.on.demand.import.intention.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRelativePath() {
|
||||
return "imports";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user