mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PsiCodeBlock#getStatementCount/isEmpty()
This commit is contained in:
+1
-1
@@ -290,7 +290,7 @@ public class RefMethodImpl extends RefJavaElementImpl implements RefMethod {
|
||||
checkForSuperCall(method);
|
||||
setOnlyCallsSuper(refUtil.isMethodOnlyCallsSuper(method));
|
||||
|
||||
setBodyEmpty(isOnlyCallsSuper() || !isExternalOverride() && (body == null || body.getStatements().length == 0));
|
||||
setBodyEmpty(isOnlyCallsSuper() || !isExternalOverride() && (body == null || body.isEmpty()));
|
||||
|
||||
refUtil.addTypeReference(method, method.getReturnType(), getRefManager(), this);
|
||||
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ public class ConvertSwitchToIfIntention implements IntentionAction {
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
final PsiCodeBlock body = mySwitchExpression.getBody();
|
||||
return body != null && body.getStatements().length > 0;
|
||||
return body != null && !body.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+1
-1
@@ -177,7 +177,7 @@ public class CreateLocalFromUsageFix extends CreateVarFromUsageFix {
|
||||
}
|
||||
|
||||
final PsiCodeBlock block = PsiTreeUtil.getParentOfType(parent, PsiCodeBlock.class, false);
|
||||
LOG.assertTrue(block != null && block.getStatements().length > 0, "block: " + block +"; parent: " + parent);
|
||||
LOG.assertTrue(block != null && !block.isEmpty(), "block: " + block +"; parent: " + parent);
|
||||
PsiStatement[] statements = block.getStatements();
|
||||
for (int i = 1; i < statements.length; i++) {
|
||||
if (statements[i].getTextRange().getStartOffset() > minOffset) return statements[i-1];
|
||||
|
||||
@@ -37,7 +37,7 @@ public class JavaLambdaUnwrapper extends JavaUnwrapper {
|
||||
PsiElement from = JavaAnonymousUnwrapper.findElementToExtractFrom(element);
|
||||
PsiLambdaExpression lambdaExpression = (PsiLambdaExpression)element;
|
||||
PsiElement body = lambdaExpression.getBody();
|
||||
if (body instanceof PsiExpression || body instanceof PsiCodeBlock && ((PsiCodeBlock)body).getStatements().length == 1) {
|
||||
if (body instanceof PsiExpression || body instanceof PsiCodeBlock && ((PsiCodeBlock)body).getStatementCount() == 1) {
|
||||
List<PsiExpression> returnExpressions = LambdaUtil.getReturnExpressions(lambdaExpression);
|
||||
if (returnExpressions.size() == 1
|
||||
&& !PsiType.VOID.equals(returnExpressions.get(0).getType())
|
||||
|
||||
+1
-1
@@ -253,7 +253,7 @@ public class MoveFieldAssignmentToInitializerInspection extends AbstractBaseJava
|
||||
// Delete empty initializer left after fix
|
||||
if (owner instanceof PsiClassInitializer) {
|
||||
PsiCodeBlock body = ((PsiClassInitializer)owner).getBody();
|
||||
if(body.getStatements().length == 0 && Arrays.stream(body.getChildren()).noneMatch(PsiComment.class::isInstance)) {
|
||||
if(body.isEmpty() && Arrays.stream(body.getChildren()).noneMatch(PsiComment.class::isInstance)) {
|
||||
owner.delete();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -203,8 +203,7 @@ public class EmptyMethodInspection extends GlobalJavaBatchInspectionTool {
|
||||
@Override
|
||||
public boolean process(PsiMethod derivedMethod) {
|
||||
PsiCodeBlock body = derivedMethod.getBody();
|
||||
if (body == null) return true;
|
||||
if (body.getStatements().length == 0) return true;
|
||||
if (body == null || body.isEmpty()) return true;
|
||||
if (RefJavaUtil.getInstance().isMethodOnlyCallsSuper(derivedMethod)) return true;
|
||||
descriptionsProcessor.ignoreElement(refMethod);
|
||||
return false;
|
||||
|
||||
+2
-2
@@ -127,7 +127,7 @@ class InlineToAnonymousConstructorProcessor {
|
||||
int fieldCount = myClass.getFields().length;
|
||||
int processedFields = 0;
|
||||
PsiElement token = anonymousClass.getRBrace();
|
||||
if (initializerBlock.getBody().getStatements().length > 0 && fieldCount == 0) {
|
||||
if (!initializerBlock.getBody().isEmpty() && fieldCount == 0) {
|
||||
insertInitializerBefore(initializerBlock, anonymousClass, token);
|
||||
}
|
||||
|
||||
@@ -148,7 +148,7 @@ class InlineToAnonymousConstructorProcessor {
|
||||
field.setInitializer(initializer);
|
||||
}
|
||||
processedFields++;
|
||||
if (processedFields == fieldCount && initializerBlock.getBody().getStatements().length > 0) {
|
||||
if (processedFields == fieldCount && !initializerBlock.getBody().isEmpty()) {
|
||||
insertInitializerBefore(initializerBlock, anonymousClass, token);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -774,7 +774,7 @@ public class JavaSafeDeleteProcessor extends SafeDeleteProcessorDelegateBase {
|
||||
private static boolean isTheOnlyEmptyDefaultConstructor(final PsiMethod constructor) {
|
||||
if (!constructor.getParameterList().isEmpty()) return false;
|
||||
final PsiCodeBlock body = constructor.getBody();
|
||||
if (body != null && body.getStatements().length > 0) return false;
|
||||
if (body != null && !body.isEmpty()) return false;
|
||||
return constructor.getContainingClass().getConstructors().length == 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -67,4 +67,21 @@ public interface PsiCodeBlock extends PsiElement, PsiModifiableCodeBlock {
|
||||
*/
|
||||
@Nullable
|
||||
PsiJavaToken getRBrace();
|
||||
|
||||
/**
|
||||
* @return number of statements this code block contains.
|
||||
* @since 2018.1
|
||||
*/
|
||||
default int getStatementCount() {
|
||||
return getStatements().length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this code block contains no statements. Note that if code block contains
|
||||
* empty statements or empty code blocks inside, this method will return false.
|
||||
* @since 2018.1
|
||||
*/
|
||||
default boolean isEmpty() {
|
||||
return getStatementCount() == 0;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -92,7 +92,7 @@ public abstract class JavaFoldingBuilderBase extends CustomFoldingBuilder implem
|
||||
}
|
||||
|
||||
private static String getCodeBlockPlaceholder(PsiElement codeBlock) {
|
||||
return codeBlock instanceof PsiCodeBlock && ((PsiCodeBlock)codeBlock).getStatements().length == 0 ? "{}" : "{...}";
|
||||
return codeBlock instanceof PsiCodeBlock && ((PsiCodeBlock)codeBlock).isEmpty() ? "{}" : "{...}";
|
||||
}
|
||||
|
||||
private static boolean areOnAdjacentLines(@NotNull PsiElement e1, @NotNull PsiElement e2, @NotNull Document document) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.intellij.psi.impl.source.tree.java;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.Couple;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
@@ -43,6 +44,32 @@ public class PsiCodeBlockImpl extends LazyParseablePsiElement implements PsiCode
|
||||
return PsiImplUtil.getChildStatements(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStatementCount() {
|
||||
ApplicationManager.getApplication().assertReadAccessAllowed();
|
||||
// no lock is needed because all chameleons are expanded already
|
||||
int count = 0;
|
||||
for (ASTNode child = getFirstChildNode(); child != null; child = child.getTreeNext()) {
|
||||
if (child.getPsi() instanceof PsiStatement) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
ApplicationManager.getApplication().assertReadAccessAllowed();
|
||||
// no lock is needed because all chameleons are expanded already
|
||||
int count = 0;
|
||||
for (ASTNode child = getFirstChildNode(); child != null; child = child.getTreeNext()) {
|
||||
if (child.getPsi() instanceof PsiStatement) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement getFirstBodyElement() {
|
||||
final PsiJavaToken lBrace = getLBrace();
|
||||
|
||||
+1
-1
@@ -133,7 +133,7 @@ public class JavaStructuralSearchProfile extends StructuralSearchProfile {
|
||||
|
||||
@Override
|
||||
public PsiElement updateCurrentNode(PsiElement targetNode) {
|
||||
if (targetNode instanceof PsiCodeBlock && ((PsiCodeBlock)targetNode).getStatements().length == 1) {
|
||||
if (targetNode instanceof PsiCodeBlock && ((PsiCodeBlock)targetNode).getStatementCount() == 1) {
|
||||
PsiElement targetNodeParent = targetNode.getParent();
|
||||
if (targetNodeParent instanceof PsiBlockStatement) {
|
||||
targetNodeParent = targetNodeParent.getParent();
|
||||
|
||||
+1
-1
@@ -522,7 +522,7 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
|
||||
if (myMatchingVisitor.getMatchContext().getOptions().isLooseMatching()) {
|
||||
if (matchElement instanceof PsiBlockStatement) {
|
||||
final PsiCodeBlock codeBlock = ((PsiBlockStatement)matchElement).getCodeBlock();
|
||||
if (patternElement instanceof PsiBlockStatement || codeBlock.getStatements().length == 1) {
|
||||
if (patternElement instanceof PsiBlockStatement || codeBlock.getStatementCount() == 1) {
|
||||
matchElement = codeBlock.getFirstChild();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -446,7 +446,7 @@ public class IfStatementWithIdenticalBranchesInspection extends BaseJavaBatchLoc
|
||||
private static boolean isMeaningful(@NotNull PsiStatement statement) {
|
||||
if (statement instanceof PsiEmptyStatement) return false;
|
||||
if (statement instanceof PsiBlockStatement) {
|
||||
return ((PsiBlockStatement)statement).getCodeBlock().getStatements().length != 0;
|
||||
return !((PsiBlockStatement)statement).getCodeBlock().isEmpty();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
+1
-1
@@ -150,7 +150,7 @@ public class EmptyFinallyBlockInspection extends BaseInspection {
|
||||
if (finallyBlock == null) {
|
||||
return;
|
||||
}
|
||||
if (finallyBlock.getStatements().length != 0) {
|
||||
if (!finallyBlock.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
final PsiElement[] children = statement.getChildren();
|
||||
|
||||
+2
-2
@@ -18,10 +18,10 @@ package com.siyeh.ig.errorhandling;
|
||||
import com.intellij.psi.PsiCodeBlock;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiTryStatement;
|
||||
import com.intellij.psi.util.FileTypeUtils;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.intellij.psi.util.FileTypeUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class EmptyTryBlockInspection extends BaseInspection {
|
||||
@@ -64,7 +64,7 @@ public class EmptyTryBlockInspection extends BaseInspection {
|
||||
if (finallyBlock == null) {
|
||||
return;
|
||||
}
|
||||
if (finallyBlock.getStatements().length != 0) {
|
||||
if (!finallyBlock.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
registerStatementError(statement);
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ public class ControlFlowUtils {
|
||||
public static boolean isElseIf(PsiIfStatement ifStatement) {
|
||||
PsiElement parent = ifStatement.getParent();
|
||||
if (parent instanceof PsiCodeBlock &&
|
||||
((PsiCodeBlock)parent).getStatements().length == 1 &&
|
||||
((PsiCodeBlock)parent).getStatementCount() == 1 &&
|
||||
parent.getParent() instanceof PsiBlockStatement) {
|
||||
parent = parent.getParent().getParent();
|
||||
}
|
||||
|
||||
+1
-1
@@ -120,7 +120,7 @@ public class UnnecessaryBlockStatementInspection extends BaseInspection implemen
|
||||
return;
|
||||
}
|
||||
final PsiCodeBlock parentBlock = (PsiCodeBlock)parent;
|
||||
if (parentBlock.getStatements().length > 1 &&
|
||||
if (parentBlock.getStatementCount() > 1 &&
|
||||
DeclarationSearchUtils.containsConflictingDeclarations(codeBlock, parentBlock)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,6 @@ import com.intellij.rt.coverage.data.ClassData;
|
||||
import com.intellij.rt.coverage.data.LineCoverage;
|
||||
import com.intellij.rt.coverage.data.LineData;
|
||||
import com.intellij.rt.coverage.data.ProjectData;
|
||||
import java.util.HashMap;
|
||||
import com.intellij.util.containers.SmartHashSet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -532,7 +531,7 @@ public class PackageAnnotator {
|
||||
PsiMethod[] constructors = aClass.getConstructors();
|
||||
if (privateEmpty && constructors.length == 1 && constructors[0].hasModifierProperty(PsiModifier.PRIVATE)) {
|
||||
PsiCodeBlock body = constructors[0].getBody();
|
||||
return body != null && body.getStatements().length == 0 &&
|
||||
return body != null && body.isEmpty() &&
|
||||
Arrays.stream(aClass.getMethods()).allMatch(method -> method.isConstructor() || method.hasModifierProperty(PsiModifier.STATIC));
|
||||
}
|
||||
return implicitConstructor && constructors.length == 0;
|
||||
|
||||
+1
-1
@@ -50,7 +50,7 @@ public class GeneratedCodeFoldingBuilder extends FoldingBuilderEx {
|
||||
|
||||
private static boolean isGeneratedUIInitializer(PsiClassInitializer initializer) {
|
||||
PsiCodeBlock body = initializer.getBody();
|
||||
if (body.getStatements().length != 1) return false;
|
||||
if (body.getStatementCount() != 1) return false;
|
||||
PsiStatement statement = body.getStatements()[0];
|
||||
if (!(statement instanceof PsiExpressionStatement) ||
|
||||
!(((PsiExpressionStatement)statement).getExpression() instanceof PsiMethodCallExpression)) {
|
||||
|
||||
Reference in New Issue
Block a user