use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition in project intellij-community by JetBrains.
the class ControlFlowBuilder method visitTypeDefinition.
@Override
public void visitTypeDefinition(@NotNull final GrTypeDefinition typeDefinition) {
if (!(typeDefinition instanceof GrAnonymousClassDefinition))
return;
final Set<ReadWriteVariableInstruction> vars = collectUsedVariableWithoutInitialization(typeDefinition);
for (ReadWriteVariableInstruction var : vars) {
PsiElement element = var.getElement();
if (!(element instanceof GrReferenceExpression) || myPolicy.isReferenceAccepted((GrReferenceExpression) element)) {
addNodeAndCheckPending(new ReadWriteVariableInstruction(var.getVariableName(), typeDefinition, ReadWriteVariableInstruction.READ));
}
}
addNodeAndCheckPending(new InstructionImpl(typeDefinition));
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition in project intellij-community by JetBrains.
the class PsiUtil method getArgumentTypes.
@Nullable
public static PsiType[] getArgumentTypes(@Nullable PsiElement place, boolean nullAsBottom, @Nullable GrExpression stopAt) {
PsiElement parent = place instanceof GrEnumConstant ? place : place != null ? place.getParent() : null;
if (parent instanceof GrIndexProperty) {
GrIndexProperty index = (GrIndexProperty) parent;
PsiType[] argTypes = getArgumentTypes(index.getNamedArguments(), index.getExpressionArguments(), index.getClosureArguments(), nullAsBottom, stopAt);
if (isLValue(index) && argTypes != null) {
PsiType rawInitializer = TypeInferenceHelper.getInitializerTypeFor(index);
PsiType initializer = notNullizeType(rawInitializer, nullAsBottom, index);
return ArrayUtil.append(argTypes, initializer);
} else {
return argTypes;
}
}
if (parent instanceof GrCall) {
GrCall call = (GrCall) parent;
GrNamedArgument[] namedArgs = call.getNamedArguments();
GrExpression[] expressions = call.getExpressionArguments();
GrClosableBlock[] closures = call.getClosureArguments();
return getArgumentTypes(namedArgs, expressions, closures, nullAsBottom, stopAt);
} else if (parent instanceof GrAnonymousClassDefinition) {
final GrArgumentList argList = ((GrAnonymousClassDefinition) parent).getArgumentListGroovy();
if (argList == null) {
return getArgumentTypes(GrNamedArgument.EMPTY_ARRAY, GrExpression.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
} else {
return getArgumentTypes(argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
}
} else if (parent instanceof GrBinaryExpression) {
GrExpression right = ((GrBinaryExpression) parent).getRightOperand();
PsiType type = right != null ? right.getType() : null;
return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
} else if (parent instanceof GrAssignmentExpression) {
PsiType type = ((GrAssignmentExpression) parent).getType();
return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition in project intellij-community by JetBrains.
the class GroovyClassNameInsertHandler method findNewExpression.
@Nullable
private static GrNewExpression findNewExpression(@Nullable PsiElement position) {
if (position == null)
return null;
final PsiElement reference = position.getParent();
if (!(reference instanceof GrCodeReferenceElement))
return null;
PsiElement parent = reference.getParent();
while (parent instanceof GrCodeReferenceElement) parent = parent.getParent();
if (parent instanceof GrAnonymousClassDefinition)
parent = parent.getParent();
return parent instanceof GrNewExpression ? (GrNewExpression) parent : null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition in project intellij-community by JetBrains.
the class GroovyDirectInheritorsSearcher method getDerivingClassCandidates.
@NotNull
private static List<PsiClass> getDerivingClassCandidates(PsiClass clazz, GlobalSearchScope scope, boolean includeAnonymous) {
final String name = clazz.getName();
if (name == null)
return Collections.emptyList();
final ArrayList<PsiClass> inheritors = new ArrayList<>();
for (GrReferenceList list : StubIndex.getElements(GrDirectInheritorsIndex.KEY, name, clazz.getProject(), scope, GrReferenceList.class)) {
final PsiElement parent = list.getParent();
if (parent instanceof GrTypeDefinition) {
inheritors.add((PsiClass) parent);
}
}
if (includeAnonymous) {
final Collection<GrAnonymousClassDefinition> classes = StubIndex.getElements(GrAnonymousClassIndex.KEY, name, clazz.getProject(), scope, GrAnonymousClassDefinition.class);
for (GrAnonymousClassDefinition aClass : classes) {
inheritors.add(aClass);
}
}
return inheritors;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition in project intellij-community by JetBrains.
the class GroovyInlineLocalProcessor method collectRefs.
private static void collectRefs(final GrVariable variable, Instruction[] flow, final List<BitSet> writes, final int writeInstructionNumber, final ArrayList<UsageInfo> toInline) {
for (Instruction instruction : flow) {
final PsiElement element = instruction.getElement();
if (instruction instanceof ReadWriteVariableInstruction) {
if (((ReadWriteVariableInstruction) instruction).isWrite())
continue;
if (element instanceof GrVariable && element != variable)
continue;
if (!(element instanceof GrReferenceExpression))
continue;
final GrReferenceExpression ref = (GrReferenceExpression) element;
if (ref.isQualified() || ref.resolve() != variable)
continue;
final BitSet prev = writes.get(instruction.num());
if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber)) {
toInline.add(new UsageInfo(ref));
} else if (writeInstructionNumber == -1 && prev.cardinality() == 0) {
toInline.add(new ClosureUsage(ref));
}
} else if (element instanceof GrClosableBlock) {
final BitSet prev = writes.get(instruction.num());
if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber) || writeInstructionNumber == -1 && prev.cardinality() == 0) {
final Instruction[] closureFlow = ((GrClosableBlock) element).getControlFlow();
collectRefs(variable, closureFlow, ControlFlowUtils.inferWriteAccessMap(closureFlow, variable), -1, toInline);
}
} else if (element instanceof GrAnonymousClassDefinition) {
final BitSet prev = writes.get(instruction.num());
if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber) || writeInstructionNumber == -1 && prev.cardinality() == 0) {
((GrAnonymousClassDefinition) element).acceptChildren(new GroovyRecursiveElementVisitor() {
@Override
public void visitField(@NotNull GrField field) {
GrExpression initializer = field.getInitializerGroovy();
if (initializer != null) {
Instruction[] flow = new ControlFlowBuilder(field.getProject()).buildControlFlow(initializer);
collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
}
}
@Override
public void visitMethod(@NotNull GrMethod method) {
GrOpenBlock block = method.getBlock();
if (block != null) {
Instruction[] flow = block.getControlFlow();
collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
}
}
@Override
public void visitClassInitializer(@NotNull GrClassInitializer initializer) {
GrOpenBlock block = initializer.getBlock();
Instruction[] flow = block.getControlFlow();
collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
}
});
}
}
}
}
Aggregations