mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
RefactoringUtil#createResourceVariable => PsiElementFactory (IDEA-CR-41591)
This commit is contained in:
+3
-1
@@ -31,6 +31,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -203,7 +204,8 @@ class VariableExtractor {
|
||||
if (anchor instanceof PsiResourceListElement) {
|
||||
PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)declaration;
|
||||
PsiLocalVariable localVariable = (PsiLocalVariable)declarationStatement.getDeclaredElements()[0];
|
||||
PsiResourceVariable resourceVariable = RefactoringUtil.createResourceVariable(anchor.getProject(), localVariable, initializer);
|
||||
PsiResourceVariable resourceVariable = JavaPsiFacade.getElementFactory(anchor.getProject())
|
||||
.createResourceVariable(Objects.requireNonNull(localVariable.getName()), localVariable.getType(), initializer, anchor);
|
||||
return anchor.replace(resourceVariable);
|
||||
}
|
||||
return anchor.getParent().addBefore(declaration, anchor);
|
||||
|
||||
@@ -1023,23 +1023,6 @@ public class RefactoringUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static PsiResourceVariable createResourceVariable(@NotNull Project project, PsiLocalVariable variable, PsiExpression initializer) {
|
||||
PsiTryStatement tryStatement = (PsiTryStatement)JavaPsiFacade.getElementFactory(project).createStatementFromText("try (X x = null){}", variable);
|
||||
PsiResourceList resourceList = tryStatement.getResourceList();
|
||||
assert resourceList != null;
|
||||
PsiResourceVariable resourceVariable = (PsiResourceVariable)resourceList.iterator().next();
|
||||
resourceVariable.getTypeElement().replace(variable.getTypeElement());
|
||||
PsiIdentifier nameIdentifier = resourceVariable.getNameIdentifier();
|
||||
assert nameIdentifier != null;
|
||||
PsiIdentifier oldIdentifier = variable.getNameIdentifier();
|
||||
assert oldIdentifier != null;
|
||||
nameIdentifier.replace(oldIdentifier);
|
||||
if (initializer != null) {
|
||||
resourceVariable.setInitializer(initializer);
|
||||
}
|
||||
return resourceVariable;
|
||||
}
|
||||
|
||||
public interface ImplicitConstructorUsageVisitor {
|
||||
void visitConstructor(PsiMethod constructor, PsiMethod baseConstructor);
|
||||
|
||||
|
||||
@@ -411,7 +411,7 @@ public interface PsiElementFactory extends PsiJavaParserFacade, JVMElementFactor
|
||||
* @param type the type of the variable to create.
|
||||
* @param initializer the initializer for the variable.
|
||||
* @param context the context for dummy holder
|
||||
* @return the variable instance.
|
||||
* @return a newly created declaration statement which contains a variable.
|
||||
* @throws IncorrectOperationException if {@code name} is not a valid identifier or
|
||||
* {@code type} is not a valid type.
|
||||
*/
|
||||
@@ -420,6 +420,21 @@ public interface PsiElementFactory extends PsiJavaParserFacade, JVMElementFactor
|
||||
@Nullable PsiExpression initializer, @Nullable PsiElement context)
|
||||
throws IncorrectOperationException;
|
||||
|
||||
/**
|
||||
* Creates a resource variable (which can be inserted into the resource list of try-with-resources statement)
|
||||
* with the specified name, type and initializer
|
||||
*
|
||||
* @param name the name of the variable to create.
|
||||
* @param type the type of the variable to create.
|
||||
* @param initializer the initializer for the variable.
|
||||
* @param context the context for dummy holder
|
||||
* @return the variable instance.
|
||||
*/
|
||||
PsiResourceVariable createResourceVariable(@NonNls @NotNull String name,
|
||||
@NotNull PsiType type,
|
||||
@Nullable PsiExpression initializer,
|
||||
@Nullable PsiElement context);
|
||||
|
||||
/**
|
||||
* Creates a PSI element for the "@param" JavaDoc tag.
|
||||
*
|
||||
|
||||
@@ -621,6 +621,25 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps
|
||||
return statement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiResourceVariable createResourceVariable(@NonNls @NotNull String name,
|
||||
@NotNull PsiType type,
|
||||
@Nullable PsiExpression initializer,
|
||||
@Nullable PsiElement context) {
|
||||
PsiTryStatement tryStatement = (PsiTryStatement)createStatementFromText("try (X x = null){}", context);
|
||||
PsiResourceList resourceList = tryStatement.getResourceList();
|
||||
assert resourceList != null;
|
||||
PsiResourceVariable resourceVariable = (PsiResourceVariable)resourceList.iterator().next();
|
||||
resourceVariable.getTypeElement().replace(createTypeElement(type));
|
||||
PsiIdentifier nameIdentifier = resourceVariable.getNameIdentifier();
|
||||
assert nameIdentifier != null;
|
||||
nameIdentifier.replace(createIdentifier(name));
|
||||
if (initializer != null) {
|
||||
resourceVariable.setInitializer(initializer);
|
||||
}
|
||||
return resourceVariable;
|
||||
}
|
||||
|
||||
private static void replace(@Nullable PsiElement original, @NotNull PsiElement replacement, @NotNull String message) {
|
||||
assert original != null : message;
|
||||
original.replace(replacement);
|
||||
|
||||
@@ -28,7 +28,6 @@ import com.intellij.psi.util.FileTypeUtils;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.util.SmartList;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
@@ -40,6 +39,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class TooBroadScopeInspection extends BaseInspection {
|
||||
|
||||
@@ -388,8 +388,11 @@ public class TooBroadScopeInspection extends BaseInspection {
|
||||
CommentTracker tracker = new CommentTracker();
|
||||
if (commonParent instanceof PsiTryStatement) {
|
||||
PsiElement resourceReference = referenceElement.getParent();
|
||||
PsiResourceVariable resourceVariable = RefactoringUtil
|
||||
.createResourceVariable(project, variable, initializer != null ? tracker.markUnchanged(initializer) : null);
|
||||
if (initializer != null) {
|
||||
tracker.markUnchanged(initializer);
|
||||
}
|
||||
PsiResourceVariable resourceVariable = JavaPsiFacade.getElementFactory(project).createResourceVariable(
|
||||
Objects.requireNonNull(variable.getName()), variable.getType(), initializer, variable);
|
||||
newDeclaration = resourceReference.getParent().addBefore(resourceVariable, resourceReference);
|
||||
resourceReference.delete();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user