From bfc7dbf12362e899c3a17d187f0d3b0cda0334a0 Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Thu, 14 Jun 2018 16:07:12 +0300 Subject: [PATCH] [groovy] make get GrTryCatchStatement#getTryBlock @Nullable (EA-122538) After parser update 'try' statement is pinned on the keyword, meaning there could be no block, so usages are updated accordingly. --- .../api/statements/GrTryCatchStatement.java | 18 ++----------- .../controlFlow/impl/ControlFlowBuilder.java | 4 ++- .../statements/GrTryCatchStatementImpl.java | 4 +-- .../TryCatchFinallySurrounder.java | 21 ++++------------ .../lang/surroundWith/TryCatchSurrounder.java | 23 +++++------------ .../surroundWith/TryFinallySurrounder.java | 21 ++++------------ .../GrChangeSignatureUsageProcessor.java | 4 ++- .../convertToJava/CodeBlockGenerator.java | 25 ++++++------------- .../lang/controlFlow/ControlFlowTest.groovy | 2 ++ .../testdata/groovy/controlFlow/try11.test | 8 ++++++ 10 files changed, 44 insertions(+), 86 deletions(-) create mode 100644 plugins/groovy/testdata/groovy/controlFlow/try11.test diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrTryCatchStatement.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrTryCatchStatement.java index 8b66d9ed4c30..94205a9ad040 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrTryCatchStatement.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrTryCatchStatement.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2014 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-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 org.jetbrains.plugins.groovy.lang.psi.api.statements; @@ -26,7 +12,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock; */ public interface GrTryCatchStatement extends GroovyPsiElement, GrStatement { - @NotNull + @Nullable GrOpenBlock getTryBlock(); @NotNull 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 190625583c0d..a2affab2823b 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 @@ -1020,7 +1020,9 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { } InstructionImpl tryBegin = startNode(tryBlock); - tryBlock.accept(this); + if (tryBlock != null) { + tryBlock.accept(this); + } InstructionImpl tryEnd = myHead; finishNode(tryBegin); diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrTryCatchStatementImpl.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrTryCatchStatementImpl.java index 98ec3aa00cc9..82fb947454bf 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrTryCatchStatementImpl.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrTryCatchStatementImpl.java @@ -34,9 +34,9 @@ public class GrTryCatchStatementImpl extends GroovyPsiElementImpl implements GrT } @Override - @NotNull + @Nullable public GrOpenBlock getTryBlock() { - return findNotNullChildByClass(GrOpenBlock.class); + return findChildByClass(GrOpenBlock.class); } @Override diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryCatchFinallySurrounder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryCatchFinallySurrounder.java index e0fce823e920..ff7ce615a2d1 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryCatchFinallySurrounder.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryCatchFinallySurrounder.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2014 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-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 org.jetbrains.plugins.groovy.lang.surroundWith; import com.intellij.psi.PsiElement; @@ -20,6 +6,7 @@ import com.intellij.util.IncorrectOperationException; import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement; import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTryCatchStatement; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock; public class TryCatchFinallySurrounder extends TryCatchSurrounder { @@ -32,7 +19,9 @@ public class TryCatchFinallySurrounder extends TryCatchSurrounder { protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException { GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject()); GrTryCatchStatement tryStatement = (GrTryCatchStatement) factory.createStatementFromText("try {\n} catch(exception e){\n} finally{\n}", context); - addStatements(tryStatement.getTryBlock(), elements); + GrOpenBlock block = tryStatement.getTryBlock(); + assert block != null; + addStatements(block, elements); return tryStatement; } } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryCatchSurrounder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryCatchSurrounder.java index 9f2f5fd821ee..60bafc02afb5 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryCatchSurrounder.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryCatchSurrounder.java @@ -1,25 +1,12 @@ -/* - * Copyright 2000-2014 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-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 org.jetbrains.plugins.groovy.lang.surroundWith; import com.intellij.psi.PsiElement; import com.intellij.util.IncorrectOperationException; import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement; import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory; -import org.jetbrains.plugins.groovy.lang.psi.api.statements.*; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTryCatchStatement; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock; public class TryCatchSurrounder extends TrySurrounder { @Override @@ -31,7 +18,9 @@ public class TryCatchSurrounder extends TrySurrounder { protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException { GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject()); GrTryCatchStatement tryStatement = (GrTryCatchStatement) factory.createStatementFromText("try {\n} catch(exception){\n}", context); - addStatements(tryStatement.getTryBlock(), elements); + GrOpenBlock block = tryStatement.getTryBlock(); + assert block != null; + addStatements(block, elements); return tryStatement; } } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryFinallySurrounder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryFinallySurrounder.java index c918bf8c1e45..a9ab1f26ac3a 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryFinallySurrounder.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/TryFinallySurrounder.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2014 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-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 org.jetbrains.plugins.groovy.lang.surroundWith; import com.intellij.psi.PsiElement; @@ -20,13 +6,16 @@ import com.intellij.util.IncorrectOperationException; import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement; import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTryCatchStatement; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock; public class TryFinallySurrounder extends TrySurrounder { @Override protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException { GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject()); GrTryCatchStatement tryStatement = (GrTryCatchStatement) factory.createStatementFromText("try {\n} finally{\n}", context); - addStatements(tryStatement.getTryBlock(), elements); + GrOpenBlock block = tryStatement.getTryBlock(); + assert block != null; + addStatements(block, elements); return tryStatement; } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/changeSignature/GrChangeSignatureUsageProcessor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/changeSignature/GrChangeSignatureUsageProcessor.java index 120bf84b73b7..472965ac936f 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/changeSignature/GrChangeSignatureUsageProcessor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/changeSignature/GrChangeSignatureUsageProcessor.java @@ -739,7 +739,9 @@ public class GrChangeSignatureUsageProcessor implements ChangeSignatureUsageProc (GrTryCatchStatement)GroovyPsiElementFactory.getInstance(element.getProject()).createStatementFromText("try{} catch (Exception e){}"); final GrStatement statement = PsiTreeUtil.getParentOfType(element, GrStatement.class); assert statement != null; - tryCatch.getTryBlock().addStatementBefore(statement, null); + final GrOpenBlock block = tryCatch.getTryBlock(); + assert block != null; + block.addStatementBefore(statement, null); tryCatch = (GrTryCatchStatement)statement.replace(tryCatch); tryCatch.getCatchClauses()[0].delete(); fixCatchBlock(tryCatch, exceptions); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/convertToJava/CodeBlockGenerator.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/convertToJava/CodeBlockGenerator.java index 242c721a5143..baa89d62cd9a 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/convertToJava/CodeBlockGenerator.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/convertToJava/CodeBlockGenerator.java @@ -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-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 org.jetbrains.plugins.groovy.refactoring.convertToJava; import com.intellij.openapi.application.ApplicationManager; @@ -514,11 +500,16 @@ public class CodeBlockGenerator extends Generator { @Override public void visitTryStatement(@NotNull GrTryCatchStatement tryCatchStatement) { + builder.append("try"); final GrOpenBlock tryBlock = tryCatchStatement.getTryBlock(); + if (tryBlock == null) { + builder.append("{}"); + } + else { + tryBlock.accept(this); + } final GrCatchClause[] catchClauses = tryCatchStatement.getCatchClauses(); final GrFinallyClause finallyClause = tryCatchStatement.getFinallyClause(); - builder.append("try"); - tryBlock.accept(this); for (GrCatchClause catchClause : catchClauses) { catchClause.accept(this); } 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 0961c848fc9d..0e3e8f426ea5 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 @@ -86,6 +86,8 @@ class ControlFlowTest extends LightCodeInsightFixtureTestCase { void testTry10() { doTest() } + void testTry11() { doTest() } + void testWhile1() { doTest() } void testWhile2() { doTest() } diff --git a/plugins/groovy/testdata/groovy/controlFlow/try11.test b/plugins/groovy/testdata/groovy/controlFlow/try11.test new file mode 100644 index 000000000000..2dd46006207d --- /dev/null +++ b/plugins/groovy/testdata/groovy/controlFlow/try11.test @@ -0,0 +1,8 @@ +try +println 42 +----- +0(1) element: null +1(2) element: null +2(3) READ println +3(4) element: Call expression MAYBE_RETURN +4() element: null \ No newline at end of file