use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class GrReassignedLocalVarsChecker method getUsedVarsInsideBlock.
@NotNull
private static Set<String> getUsedVarsInsideBlock(@NotNull final GrCodeBlock block) {
return CachedValuesManager.getCachedValue(block, () -> {
final Set<String> result = ContainerUtil.newHashSet();
block.acceptChildren(new GroovyRecursiveElementVisitor() {
@Override
public void visitOpenBlock(@NotNull GrOpenBlock openBlock) {
result.addAll(getUsedVarsInsideBlock(openBlock));
}
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
result.addAll(getUsedVarsInsideBlock(closure));
}
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
if (referenceExpression.getQualifier() == null && referenceExpression.getReferenceName() != null) {
result.add(referenceExpression.getReferenceName());
}
}
});
return CachedValueProvider.Result.create(result, block);
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class PsiImplUtil method inferReturnType.
@Nullable
public static PsiType inferReturnType(PsiElement position) {
final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(position);
if (flowOwner == null)
return null;
final PsiElement parent = flowOwner.getContext();
if (flowOwner instanceof GrOpenBlock && parent instanceof GrMethod) {
final GrMethod method = (GrMethod) parent;
if (method.isConstructor())
return null;
return method.getReturnType();
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class ControlFlowBuilder method visitTryStatement.
@Override
public void visitTryStatement(@NotNull GrTryCatchStatement tryCatchStatement) {
final GrOpenBlock tryBlock = tryCatchStatement.getTryBlock();
final GrCatchClause[] catchClauses = tryCatchStatement.getCatchClauses();
final GrFinallyClause finallyClause = tryCatchStatement.getFinallyClause();
for (int i = catchClauses.length - 1; i >= 0; i--) {
myCaughtExceptionInfos.push(new ExceptionInfo(catchClauses[i]));
}
if (finallyClause != null)
myFinallyCount++;
List<Pair<InstructionImpl, GroovyPsiElement>> oldPending = null;
if (finallyClause != null) {
oldPending = myPending;
myPending = new ArrayList<>();
}
InstructionImpl tryBegin = startNode(tryBlock);
tryBlock.accept(this);
InstructionImpl tryEnd = myHead;
finishNode(tryBegin);
Set<Pair<InstructionImpl, GroovyPsiElement>> pendingAfterTry = new LinkedHashSet<>(myPending);
@SuppressWarnings("unchecked") List<InstructionImpl>[] throwers = new List[catchClauses.length];
for (int i = 0; i < catchClauses.length; i++) {
throwers[i] = myCaughtExceptionInfos.pop().myThrowers;
}
InstructionImpl[] catches = new InstructionImpl[catchClauses.length];
for (int i = 0; i < catchClauses.length; i++) {
interruptFlow();
final InstructionImpl catchBeg = startNode(catchClauses[i]);
for (InstructionImpl thrower : throwers[i]) {
addEdge(thrower, catchBeg);
}
final GrParameter parameter = catchClauses[i].getParameter();
if (parameter != null && myPolicy.isVariableInitialized(parameter)) {
addNode(new ReadWriteVariableInstruction(parameter.getName(), parameter, ReadWriteVariableInstruction.WRITE));
}
catchClauses[i].accept(this);
catches[i] = myHead;
finishNode(catchBeg);
}
pendingAfterTry.addAll(myPending);
myPending = new ArrayList<>(pendingAfterTry);
if (finallyClause != null) {
myFinallyCount--;
interruptFlow();
final InstructionImpl finallyInstruction = startNode(finallyClause, false);
Set<AfterCallInstruction> postCalls = new LinkedHashSet<>();
final List<Pair<InstructionImpl, GroovyPsiElement>> copy = myPending;
myPending = new ArrayList<>();
for (Pair<InstructionImpl, GroovyPsiElement> pair : copy) {
postCalls.add(addCallNode(finallyInstruction, pair.getSecond(), pair.getFirst()));
}
if (tryEnd != null) {
postCalls.add(addCallNode(finallyInstruction, tryCatchStatement, tryEnd));
}
for (InstructionImpl catchEnd : catches) {
if (catchEnd != null) {
postCalls.add(addCallNode(finallyInstruction, tryCatchStatement, catchEnd));
}
}
//save added postcalls into separate list because we don't want returnInstruction grabbed their pending edges
List<Pair<InstructionImpl, GroovyPsiElement>> pendingPostCalls = myPending;
myPending = new ArrayList<>();
myHead = finallyInstruction;
finallyClause.accept(this);
final ReturnInstruction returnInstruction = new ReturnInstruction(finallyClause);
for (AfterCallInstruction postCall : postCalls) {
postCall.setReturnInstruction(returnInstruction);
addEdge(returnInstruction, postCall);
}
addNodeAndCheckPending(returnInstruction);
interruptFlow();
finishNode(finallyInstruction);
if (oldPending == null) {
error();
}
oldPending.addAll(pendingPostCalls);
myPending = oldPending;
} else {
if (tryEnd != null) {
addPendingEdge(tryCatchStatement, tryEnd);
}
for (InstructionImpl catchEnd : catches) {
addPendingEdge(tryBlock, catchEnd);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class ControlFlowBuilder method collectUsedVariableWithoutInitialization.
private static Set<ReadWriteVariableInstruction> collectUsedVariableWithoutInitialization(GrTypeDefinition typeDefinition) {
final Set<ReadWriteVariableInstruction> vars = ContainerUtil.newLinkedHashSet();
typeDefinition.acceptChildren(new GroovyRecursiveElementVisitor() {
private void collectVars(Instruction[] flow) {
ReadWriteVariableInstruction[] reads = ControlFlowBuilderUtil.getReadsWithoutPriorWrites(flow, false);
Collections.addAll(vars, reads);
}
@Override
public void visitField(@NotNull GrField field) {
GrExpression initializer = field.getInitializerGroovy();
if (initializer != null) {
Instruction[] flow = new ControlFlowBuilder(field.getProject()).buildControlFlow(initializer);
collectVars(flow);
}
}
@Override
public void visitMethod(@NotNull GrMethod method) {
GrOpenBlock block = method.getBlock();
if (block != null) {
collectVars(block.getControlFlow());
}
}
@Override
public void visitClassInitializer(@NotNull GrClassInitializer initializer) {
GrOpenBlock block = initializer.getBlock();
collectVars(block.getControlFlow());
}
});
return vars;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class PsiUtil method getConstructorInvocation.
@Nullable
public static GrConstructorInvocation getConstructorInvocation(@NotNull GrMethod constructor) {
assert constructor.isConstructor();
final GrOpenBlock body = constructor.getBlock();
if (body == null)
return null;
final GrStatement[] statements = body.getStatements();
if (statements.length > 0 && statements[0] instanceof GrConstructorInvocation) {
return ((GrConstructorInvocation) statements[0]);
} else {
return null;
}
}
Aggregations