From 7de2f4a1eca73adc7b6cb52d3d2f2eddb8abc285 Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Thu, 19 Dec 2019 18:17:26 +0300 Subject: [PATCH] [groovy] clean up ControlFlowBuilder GitOrigin-RevId: 4f9375e75d77a11144c7575e6d2863cd191a6909 --- .../GrFinalVariableAccessInspection.java | 4 +-- .../utils/ControlFlowUtils.java | 2 +- .../controlFlow/impl/ControlFlowBuilder.java | 26 +++++-------------- .../psi/impl/GrExpressionLambdaBodyImpl.kt | 2 +- .../lang/psi/impl/GroovyFileBaseImpl.java | 4 +-- .../impl/statements/blocks/GrBlockImpl.java | 6 ++--- .../extract/GroovyExtractChooser.java | 18 ++----------- .../inline/GroovyInlineLocalProcessor.java | 18 ++----------- .../plugins/groovy/ReachingDefsTest.groovy | 18 ++----------- .../lang/controlFlow/ControlFlowTest.groovy | 2 +- 10 files changed, 23 insertions(+), 77 deletions(-) diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/control/finalVar/GrFinalVariableAccessInspection.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/control/finalVar/GrFinalVariableAccessInspection.java index 754d4d8a6f06..712e278f4c15 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/control/finalVar/GrFinalVariableAccessInspection.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/control/finalVar/GrFinalVariableAccessInspection.java @@ -387,7 +387,7 @@ public class GrFinalVariableAccessInspection extends BaseInspection { @NotNull private static Instruction[] buildFlowForField(@NotNull GrOpenBlock block) { - return new ControlFlowBuilder(block.getProject(), GrFieldControlFlowPolicy.getInstance()).buildControlFlow(block); + return new ControlFlowBuilder(GrFieldControlFlowPolicy.getInstance()).buildControlFlow(block); } @@ -416,7 +416,7 @@ public class GrFinalVariableAccessInspection extends BaseInspection { private static Instruction[] getFlow(@NotNull PsiElement element) { return element instanceof GrControlFlowOwner ? ((GrControlFlowOwner)element).getControlFlow() - : new ControlFlowBuilder(element.getProject()).buildControlFlow((GroovyPsiElement)element); + : new ControlFlowBuilder().buildControlFlow((GroovyPsiElement)element); } diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java index ec332dd5f977..0a66a36e5112 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java @@ -435,7 +435,7 @@ public class ControlFlowUtils { flow = ((GrControlFlowOwner)element).getControlFlow(); } else { - flow = new ControlFlowBuilder(element.getProject()).buildControlFlow((GroovyPsiElement)element); + flow = new ControlFlowBuilder().buildControlFlow((GroovyPsiElement)element); } return collectReturns(flow, allExitPoints); } diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/controlFlow/impl/ControlFlowBuilder.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/controlFlow/impl/ControlFlowBuilder.java index d785c33cefaa..26c303080f25 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/controlFlow/impl/ControlFlowBuilder.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/controlFlow/impl/ControlFlowBuilder.java @@ -4,7 +4,6 @@ package org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl; import com.intellij.openapi.diagnostic.Attachment; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProgressManager; -import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; @@ -84,11 +83,11 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { private int myInstructionNumber; private final GrControlFlowPolicy myPolicy; - public ControlFlowBuilder(Project project) { - this(project, GrResolverPolicy.getInstance()); + public ControlFlowBuilder() { + this(GrResolverPolicy.getInstance()); } - public ControlFlowBuilder(Project project, GrControlFlowPolicy policy) { + public ControlFlowBuilder(GrControlFlowPolicy policy) { myPolicy = policy; } @@ -98,7 +97,6 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { final PsiElement lbrace = block.getLBrace(); if (lbrace != null && parent instanceof GrMethod) { for (GrParameter parameter : ((GrMethod)parent).getParameters()) { - String parameterName = parameter.getName(); if (myPolicy.isVariableInitialized(parameter)) { addNode(new ReadWriteVariableInstruction(createDescriptor(parameter), parameter, ReadWriteVariableInstruction.WRITE)); } @@ -167,17 +165,7 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { checkPending(end); //collect return edges - return assertValidPsi(myInstructions.toArray(Instruction.EMPTY_ARRAY)); - } - - public static Instruction[] assertValidPsi(Instruction[] instructions) { - /*for (Instruction instruction : instructions) { - PsiElement element = instruction.getElement(); - if (element != null && !element.isValid()) { - throw new AssertionError("invalid element in dfa: " + element); - } - }*/ - return instructions; + return myInstructions.toArray(Instruction.EMPTY_ARRAY); } private void addControlFlowInstructions(final GrStatementOwner owner) { @@ -197,7 +185,6 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { private void addFunctionalExpressionParameters(GrFunctionalExpression expression) { for (GrParameter parameter : expression.getAllParameters()) { - String parameterName = parameter.getName(); if (myPolicy.isVariableInitialized(parameter)) { addNode(new ReadWriteVariableInstruction(createDescriptor(parameter), parameter, ReadWriteVariableInstruction.WRITE)); } @@ -1027,6 +1014,7 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { //infer 'may be return' position int i; + //noinspection StatementWithEmptyBody for (i = statements.length - 1; i >= 0 && statements[i] instanceof GrBreakStatement; i--) { } @@ -1164,7 +1152,7 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { VirtualFile virtualFile = PsiUtilCore.getVirtualFile(file); String path = virtualFile == null ? null : virtualFile.getPresentableUrl(); - LOG.error(descr+ myScope.getText(), new Attachment(path+"", fileText+"")); + LOG.error(descr + myScope.getText(), new Attachment(String.valueOf(path), String.valueOf(fileText))); } private AfterCallInstruction addCallNode(InstructionImpl finallyInstruction, GroovyPsiElement scopeWhenAdded, InstructionImpl src) { @@ -1241,7 +1229,7 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { public void visitField(@NotNull GrField field) { GrExpression initializer = field.getInitializerGroovy(); if (initializer != null) { - Instruction[] flow = new ControlFlowBuilder(field.getProject()).buildControlFlow(initializer); + Instruction[] flow = new ControlFlowBuilder().buildControlFlow(initializer); collectVars(flow); } } diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GrExpressionLambdaBodyImpl.kt b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GrExpressionLambdaBodyImpl.kt index 9eadb861a38a..5c16bdbd6f3b 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GrExpressionLambdaBodyImpl.kt +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GrExpressionLambdaBodyImpl.kt @@ -22,7 +22,7 @@ class GrExpressionLambdaBodyImpl(node: ASTNode) : GroovyPsiElementImpl(node), Gr override fun toString(): String = "Lambda body" - override fun getControlFlow(): Array = ControlFlowBuilder(project).buildControlFlow(this) + override fun getControlFlow(): Array = ControlFlowBuilder().buildControlFlow(this) override fun isTopControlFlowOwner(): Boolean = true } diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyFileBaseImpl.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyFileBaseImpl.java index 038ef668df69..77f2a58f2df5 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyFileBaseImpl.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyFileBaseImpl.java @@ -200,10 +200,10 @@ public abstract class GroovyFileBaseImpl extends PsiFileBase implements GroovyFi assert isValid(); Instruction[] result = SoftReference.dereference(myControlFlow); if (result == null) { - result = new ControlFlowBuilder(getProject()).buildControlFlow(this); + result = new ControlFlowBuilder().buildControlFlow(this); myControlFlow = new SoftReference<>(result); } - return ControlFlowBuilder.assertValidPsi(result); + return result; } @Override diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/blocks/GrBlockImpl.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/blocks/GrBlockImpl.java index 26bc412bc877..d14cb0ada3e9 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/blocks/GrBlockImpl.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/blocks/GrBlockImpl.java @@ -1,4 +1,4 @@ -// 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. +// Copyright 2000-2019 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 org.jetbrains.plugins.groovy.lang.psi.impl.statements.blocks; @@ -101,7 +101,7 @@ public abstract class GrBlockImpl extends LazyParseablePsiElement implements GrC controlFlow = CachedValuesManager.getManager(getProject()).createCachedValue(() -> { try { ResolveProfiler.start(); - final Instruction[] flow = new ControlFlowBuilder(getProject()).buildControlFlow(this); + final Instruction[] flow = new ControlFlowBuilder().buildControlFlow(this); return CachedValueProvider.Result.create(flow, getContainingFile(), PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); } finally { @@ -111,7 +111,7 @@ public abstract class GrBlockImpl extends LazyParseablePsiElement implements GrC }, false); controlFlow = putUserDataIfAbsent(CONTROL_FLOW, controlFlow); } - return ControlFlowBuilder.assertValidPsi(controlFlow.getValue()); + return controlFlow.getValue(); } @Override diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/extract/GroovyExtractChooser.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/extract/GroovyExtractChooser.java index a436d1559133..b5712e7f8989 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/extract/GroovyExtractChooser.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/extract/GroovyExtractChooser.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 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 org.jetbrains.plugins.groovy.refactoring.extract; import com.intellij.openapi.diagnostic.Logger; @@ -154,7 +140,7 @@ public class GroovyExtractChooser { Set allReturnStatements = new HashSet<>(); GrControlFlowOwner controlFlowOwner = ControlFlowUtils.findControlFlowOwner(statement0); LOG.assertTrue(controlFlowOwner != null); - final Instruction[] flow = new ControlFlowBuilder(project, GrAllVarsInitializedPolicy.getInstance()).buildControlFlow(controlFlowOwner); + final Instruction[] flow = new ControlFlowBuilder(GrAllVarsInitializedPolicy.getInstance()).buildControlFlow(controlFlowOwner); allReturnStatements.addAll(ControlFlowUtils.collectReturns(flow, true)); ArrayList returnStatements = new ArrayList<>(); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/inline/GroovyInlineLocalProcessor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/inline/GroovyInlineLocalProcessor.java index 4f827d1974e0..f030a5b32e63 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/inline/GroovyInlineLocalProcessor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/inline/GroovyInlineLocalProcessor.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 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 org.jetbrains.plugins.groovy.refactoring.inline; import com.intellij.openapi.diagnostic.Logger; @@ -165,7 +151,7 @@ public class GroovyInlineLocalProcessor extends BaseRefactoringProcessor { public void visitField(@NotNull GrField field) { GrExpression initializer = field.getInitializerGroovy(); if (initializer != null) { - Instruction[] flow = new ControlFlowBuilder(field.getProject()).buildControlFlow(initializer); + Instruction[] flow = new ControlFlowBuilder().buildControlFlow(initializer); collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline); } } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/ReachingDefsTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/ReachingDefsTest.groovy index e8aa534b28e5..200325198824 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/ReachingDefsTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/ReachingDefsTest.groovy @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 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 org.jetbrains.plugins.groovy import com.intellij.psi.PsiElement @@ -84,7 +70,7 @@ class ReachingDefsTest extends LightJavaCodeInsightFixtureTestCase { GrStatement lastStatement = getStatement(end, owner) final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(firstStatement) - final ControlFlowBuilder flowBuilder = new ControlFlowBuilder(firstStatement.getProject(), GrAllVarsInitializedPolicy.getInstance()) + final ControlFlowBuilder flowBuilder = new ControlFlowBuilder(GrAllVarsInitializedPolicy.getInstance()) final Instruction[] flow = flowBuilder.buildControlFlow(flowOwner) final FragmentVariableInfos fragmentVariableInfos = ReachingDefinitionsCollector.obtainVariableFlowInformation(firstStatement, lastStatement, flowOwner, flow) diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/controlFlow/ControlFlowTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/controlFlow/ControlFlowTest.groovy index 991804e5fb3d..7584edc786d9 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/controlFlow/ControlFlowTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/controlFlow/ControlFlowTest.groovy @@ -158,7 +158,7 @@ class ControlFlowTest extends LightJavaCodeInsightFixtureTestCase { final PsiElement start = file.findElementAt(model.hasSelection() ? model.selectionStart : 0) final PsiElement end = file.findElementAt(model.hasSelection() ? model.selectionEnd - 1 : file.textLength - 1) final GrControlFlowOwner owner = PsiTreeUtil.getParentOfType(PsiTreeUtil.findCommonParent(start, end), GrControlFlowOwner, false) - final Instruction[] instructions = new ControlFlowBuilder(project).buildControlFlow(owner) + final Instruction[] instructions = new ControlFlowBuilder().buildControlFlow(owner) final String cf = ControlFlowUtils.dumpControlFlow(instructions) myFixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, code + "\n-----\n" + cf.trim()) myFixture.checkResultByFile(path)