[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.
This commit is contained in:
Daniil Ovchinnikov
2018-06-14 16:13:52 +03:00
parent 814447b465
commit bfc7dbf123
10 changed files with 44 additions and 86 deletions
@@ -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
@@ -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);
@@ -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
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
@@ -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);
@@ -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);
}
@@ -86,6 +86,8 @@ class ControlFlowTest extends LightCodeInsightFixtureTestCase {
void testTry10() { doTest() }
void testTry11() { doTest() }
void testWhile1() { doTest() }
void testWhile2() { doTest() }
+8
View File
@@ -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