mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java] cleanup: use reversed iteration
GitOrigin-RevId: d06c8b96458e1f73308825669af589888ed67aef
This commit is contained in:
committed by
intellij-monorepo-bot
parent
53489602f1
commit
865726ee14
+2
-4
@@ -15,6 +15,7 @@ import com.intellij.refactoring.util.RefactoringChangeUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.ArrayUtilRt;
|
||||
import com.intellij.util.JavaPsiConstructorUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -238,10 +239,7 @@ final class StatementChecker {
|
||||
}
|
||||
|
||||
private static boolean checkMultipleTypes(@NotNull PsiClass catchClass, @NotNull List<? extends PsiType> upperCatchTypes) {
|
||||
for (int i = upperCatchTypes.size() - 1; i >= 0; i--) {
|
||||
if (checkSingleType(catchClass, upperCatchTypes.get(i))) return true;
|
||||
}
|
||||
return false;
|
||||
return ContainerUtil.exists(upperCatchTypes.reversed(), type -> checkSingleType(catchClass, type));
|
||||
}
|
||||
|
||||
private static boolean checkSingleType(@NotNull PsiClass catchClass, @Nullable PsiType upperCatchType) {
|
||||
|
||||
+2
-2
@@ -97,8 +97,8 @@ public final class DefaultSourcePositionProvider extends SourcePositionProvider
|
||||
// important: use the last location to be sure the position will be within the anonymous class
|
||||
// and do not use type.allLineLocations as it fetches line tables for all methods
|
||||
List<Method> methods = type.methods();
|
||||
for (int i = methods.size() - 1; i >= 0; i--) {
|
||||
List<Location> locations = methods.get(i).allLineLocations();
|
||||
for (Method m : methods.reversed()) {
|
||||
List<Location> locations = m.allLineLocations();
|
||||
if (!locations.isEmpty()) {
|
||||
aClass = JVMNameUtil.getClassAt(debugProcess.getPositionManager().getSourcePosition(ContainerUtil.getLastItem(locations)));
|
||||
break;
|
||||
|
||||
@@ -590,11 +590,7 @@ public final class JarRepositoryManager {
|
||||
@NotNull ArtifactRepositoryManager manager) throws Exception {
|
||||
try {
|
||||
List<Version> versions = new ArrayList<>(manager.getAvailableVersions(groupId, artifactId, "[0,)", ArtifactKind.ARTIFACT));
|
||||
ArrayList<String> strings = new ArrayList<>(versions.size());
|
||||
for (int i = versions.size() - 1; i >= 0; i--) {
|
||||
strings.add(versions.get(i).toString());
|
||||
}
|
||||
return strings;
|
||||
return ContainerUtil.map(versions.reversed(), Version::toString);
|
||||
}
|
||||
catch (TransferCancelledException e) {
|
||||
throw new ProcessCanceledException(e);
|
||||
|
||||
+4
-8
@@ -13,6 +13,7 @@ import com.intellij.psi.impl.source.tree.java.PsiSwitchLabeledRuleStatementImpl;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.tree.TokenSet;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import one.util.streamex.StreamEx;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -259,8 +260,7 @@ class MethodReturnInferenceVisitor {
|
||||
}
|
||||
if (tokenType == DECLARATION_STATEMENT) {
|
||||
List<LighterASTNode> declaredElements = tree.getChildren(statement);
|
||||
for (int i = declaredElements.size() - 1; i >= 0; i--) {
|
||||
LighterASTNode declared = declaredElements.get(i);
|
||||
for (LighterASTNode declared : declaredElements.reversed()) {
|
||||
if (declared.getTokenType() != LOCAL_VARIABLE) continue;
|
||||
LighterASTNode initializer = findExpressionChild(tree, declared);
|
||||
if (declared.equals(target)) {
|
||||
@@ -276,12 +276,8 @@ class MethodReturnInferenceVisitor {
|
||||
LighterASTNode block = firstChildOfType(tree, statement, CODE_BLOCK);
|
||||
if (block != null) {
|
||||
List<LighterASTNode> children = getChildrenOfType(tree, block, ElementType.JAVA_STATEMENT_BIT_SET);
|
||||
for (int i = children.size() - 1; i >= 0; i--) {
|
||||
ReturnValue value = findValueInStatement(children.get(i), target);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return StreamEx.ofReversed(children).map(node -> findValueInStatement(node, target))
|
||||
.nonNull().findFirst().orElse(null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+1
-2
@@ -109,8 +109,7 @@ public class ParametersFolder {
|
||||
|
||||
int currentRank = 0;
|
||||
PsiExpression mostRanked = null;
|
||||
for (int i = mentionedInExpressions.size() - 1; i >= 0; i--) {
|
||||
PsiExpression expression = mentionedInExpressions.get(i);
|
||||
for (PsiExpression expression : mentionedInExpressions.reversed()) {
|
||||
final int r = findUsedVariables(data, inputVariables, expression).size();
|
||||
if (currentRank < r || expression instanceof PsiArrayAccessExpression && myFoldingSelectedByDefault && currentRank == r) {
|
||||
currentRank = r;
|
||||
|
||||
+2
-3
@@ -121,15 +121,14 @@ public final class TryFinallyCanBeTryWithResourcesInspection extends BaseInspect
|
||||
.toList();
|
||||
|
||||
if (locals.size() == 1) {
|
||||
PsiLocalVariable variable = locals.get(0);
|
||||
PsiLocalVariable variable = locals.getFirst();
|
||||
PsiStatement declaration = PsiTreeUtil.getParentOfType(variable, PsiStatement.class);
|
||||
if (declaration != null) {
|
||||
if (declaration.getParent() == tryStatement.getParent()) {
|
||||
List<PsiStatement> statements = collectStatementsBetween(declaration, tryStatement);
|
||||
PsiJavaToken lBrace = tryBlock.getLBrace();
|
||||
if (lBrace != null) {
|
||||
for (int i = statements.size() - 1; i >= 0; i--) {
|
||||
PsiStatement statement = statements.get(i);
|
||||
for (PsiStatement statement : statements.reversed()) {
|
||||
tryBlock.addAfter(statement, lBrace);
|
||||
if (statement.isValid()) {
|
||||
statement.delete();
|
||||
|
||||
+1
-3
@@ -44,9 +44,7 @@ public final class DocTagSelectioner extends WordSelectioner {
|
||||
|
||||
List<ASTNode> children = BasicJavaAstTreeUtil.getChildren(node);
|
||||
|
||||
for (int i = children.size() - 1; i >= 0; i--) {
|
||||
ASTNode child = children.get(i);
|
||||
|
||||
for (ASTNode child : children.reversed()) {
|
||||
int childStartOffset = child.getTextRange().getStartOffset();
|
||||
|
||||
if (childStartOffset <= minOffset) {
|
||||
|
||||
+1
-4
@@ -16,7 +16,6 @@ import com.intellij.psi.util.PsiLiteralUtil;
|
||||
import com.siyeh.ig.PsiReplacementUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
@@ -177,9 +176,7 @@ public final class TrailingWhitespacesInTextBlockInspection extends AbstractBase
|
||||
}
|
||||
else if (element instanceof PsiFragment fragment) {
|
||||
if (fragment.isTextBlock() && fragment.getParent() instanceof PsiTemplate template) {
|
||||
@NotNull List<@NotNull PsiFragment> fragments = template.getFragments();
|
||||
for (int i = fragments.size() - 1; i >= 0; i--) {
|
||||
PsiFragment current = fragments.get(i);
|
||||
for (PsiFragment current : template.getFragments().reversed()) {
|
||||
String suffix = fragment.getTokenType() == JavaTokenType.TEXT_BLOCK_TEMPLATE_END ? "\"\"\"" : "\\{";
|
||||
String text = buildReplacementText(current, suffix, myTransformation);
|
||||
if (text == null) return;
|
||||
|
||||
+1
-2
@@ -707,8 +707,7 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
private static void replaceWithAccessors(PsiReferenceExpression ref, PsiCodeBlock block) {
|
||||
List<PsiReferenceExpression> list = SyntaxTraverser.psiTraverser(block).filter(PsiReferenceExpression.class).toList();
|
||||
// Iterate in opposite order, so in case of nested accessors, we first replace method arguments, then methods itself
|
||||
for (int i = list.size() - 1; i >= 0; i--) {
|
||||
PsiReferenceExpression r = list.get(i);
|
||||
for (PsiReferenceExpression r: list.reversed()) {
|
||||
if (!r.isValid()) continue;
|
||||
FieldAccessFixer fixer = FieldAccessFixer.create(r, r.resolve(), ref);
|
||||
// Name-based is too risky for inline
|
||||
|
||||
@@ -236,9 +236,8 @@ public final class ExpectedTypesProvider {
|
||||
|
||||
List<PsiPrimitiveType> primitiveTypes = PsiTypes.primitiveTypes();
|
||||
Stack<PsiType> stack = new Stack<>();
|
||||
for (int i = primitiveTypes.size() - 1; i >= 0; i--) {
|
||||
PsiPrimitiveType primitiveType = primitiveTypes.get(i);
|
||||
if (primitiveTypes.get(i).equals(type)) break;
|
||||
for (PsiPrimitiveType primitiveType : primitiveTypes.reversed()) {
|
||||
if (primitiveType.equals(type)) break;
|
||||
//it is already processed before
|
||||
if (primitiveType.equals(PsiTypes.booleanType())) continue;
|
||||
stack.push(primitiveType);
|
||||
|
||||
+4
-7
@@ -357,14 +357,11 @@ public final class MagicConstantInspection extends AbstractBaseJavaLocalInspecti
|
||||
if (remainingFlags == 0) {
|
||||
// found flags to combine with OR, suggest the fix
|
||||
if (flags.size() > 1) {
|
||||
for (int i = flags.size() - 1; i >= 0; i--) {
|
||||
PsiAnnotationMemberValue flag = flags.get(i);
|
||||
flags.removeIf(flag -> {
|
||||
Long flagValue = MagicConstantUtils.evaluateLongConstant((PsiExpression)flag);
|
||||
if (flagValue != null && flagValue == 0) {
|
||||
// no sense in ORing with '0'
|
||||
flags.remove(i);
|
||||
}
|
||||
}
|
||||
// no sense in ORing with '0'
|
||||
return flagValue != null && flagValue == 0;
|
||||
});
|
||||
}
|
||||
if (!flags.isEmpty()) {
|
||||
return LocalQuickFix.from(new ReplaceWithMagicConstantFix(argument, flags.toArray(PsiAnnotationMemberValue.EMPTY_ARRAY)));
|
||||
|
||||
+4
-7
@@ -174,14 +174,11 @@ public final class MagicConstantUtils {
|
||||
if (remainingFlags == 0) {
|
||||
// found flags to combine with OR, suggest the fix
|
||||
if (flags.size() > 1) {
|
||||
for (int i = flags.size() - 1; i >= 0; i--) {
|
||||
PsiAnnotationMemberValue flag = flags.get(i);
|
||||
flags.removeIf(flag -> {
|
||||
Long flagValue = evaluateLongConstant((PsiExpression)flag);
|
||||
if (flagValue != null && flagValue == 0) {
|
||||
// no sense in ORing with '0'
|
||||
flags.remove(i);
|
||||
}
|
||||
}
|
||||
// no sense in ORing with '0'
|
||||
return flagValue != null && flagValue == 0;
|
||||
});
|
||||
}
|
||||
if (!flags.isEmpty()) {
|
||||
return StreamEx.of(flags)
|
||||
|
||||
@@ -142,9 +142,8 @@ public final class TailRecursionInspection extends BaseInspection implements Cle
|
||||
for (PsiStatement statement : emptyElseBranches) {
|
||||
final List<PsiComment> comments = new ArrayList<>(PsiTreeUtil.collectElementsOfType(statement, PsiComment.class));
|
||||
final PsiParserFacade parserFacade = PsiParserFacade.getInstance(statement.getProject());
|
||||
for (int i = comments.size() - 1; i >= 0; i--) {
|
||||
final PsiElement parent = statement.getParent();
|
||||
final PsiComment comment = comments.get(i);
|
||||
final PsiElement parent = statement.getParent();
|
||||
for (PsiComment comment : comments.reversed()) {
|
||||
parent.addAfter(comment, statement);
|
||||
// newline followed by space convinces formatter to indent line
|
||||
parent.addAfter(parserFacade.createWhiteSpaceFromText(isAtStartOfLine(comment) ? "\n" : "\n "), statement);
|
||||
|
||||
@@ -64,8 +64,8 @@ public final class ReplaceArmWithTryFinallyIntention extends MCIntention {
|
||||
final PsiElement child = children[i];
|
||||
newTryStatement.append(child.getText());
|
||||
}
|
||||
for (int i = resources.size() - 1; i >= 0; i--) {
|
||||
newTryStatement.append("} finally {\n").append(resources.get(i)).append(".close();\n}");
|
||||
for (String res : resources.reversed()) {
|
||||
newTryStatement.append("} finally {\n").append(res).append(".close();\n}");
|
||||
}
|
||||
newTryStatement.append('}');
|
||||
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(element.getProject());
|
||||
|
||||
Reference in New Issue
Block a user