use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression 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.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class ReachingDefinitionsCollector method addClosureUsages.
private static void addClosureUsages(final Map<String, VariableInfo> imap, final Map<String, VariableInfo> omap, final GrStatement first, final GrStatement last, GrControlFlowOwner flowOwner) {
flowOwner.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
addUsagesInClosure(imap, omap, closure, first, last);
super.visitClosure(closure);
}
private void addUsagesInClosure(final Map<String, VariableInfo> imap, final Map<String, VariableInfo> omap, final GrClosableBlock closure, final GrStatement first, final GrStatement last) {
closure.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression refExpr) {
if (refExpr.isQualified()) {
return;
}
PsiElement resolved = refExpr.resolve();
if (!(resolved instanceof GrVariable)) {
return;
}
GrVariable variable = (GrVariable) resolved;
if (PsiTreeUtil.isAncestor(closure, variable, true)) {
return;
}
if (variable instanceof ClosureSyntheticParameter && PsiTreeUtil.isAncestor(closure, ((ClosureSyntheticParameter) variable).getClosure(), false)) {
return;
}
String name = variable.getName();
if (!(variable instanceof GrField)) {
if (!isInFragment(first, last, resolved)) {
if (isInFragment(first, last, closure)) {
addVariable(name, imap, variable.getManager(), variable.getType());
}
} else {
if (!isInFragment(first, last, closure)) {
addVariable(name, omap, variable.getManager(), variable.getType());
}
}
}
}
});
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GrReferenceExpressionImpl method bindWithQualifiedRef.
@Override
protected GrReferenceExpression bindWithQualifiedRef(@NotNull String qName) {
GrReferenceExpression qualifiedRef = GroovyPsiElementFactory.getInstance(getProject()).createReferenceExpressionFromText(qName);
final GrTypeArgumentList list = getTypeArgumentList();
if (list != null) {
qualifiedRef.getNode().addChild(list.copy().getNode());
}
getNode().getTreeParent().replaceChild(getNode(), qualifiedRef.getNode());
return qualifiedRef;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GrSuperReferenceResolver method resolveSuperExpression.
@Nullable("null if ref is not 'super' reference")
public static GroovyResolveResult[] resolveSuperExpression(@NotNull GrReferenceExpression ref) {
GrExpression qualifier = ref.getQualifier();
if (qualifier == null) {
final PsiElement parent = ref.getParent();
if (parent instanceof GrConstructorInvocation) {
return ((GrConstructorInvocation) parent).multiResolve(false);
}
PsiClass aClass = PsiUtil.getContextClass(ref);
if (aClass != null) {
return getSuperClass(aClass);
}
} else if (qualifier instanceof GrReferenceExpression) {
GroovyResolveResult result = ((GrReferenceExpression) qualifier).advancedResolve();
PsiElement resolved = result.getElement();
if (resolved instanceof PsiClass) {
PsiClass superClass = (PsiClass) resolved;
GrTypeDefinition scopeClass = PsiTreeUtil.getParentOfType(ref, GrTypeDefinition.class, true);
if (scopeClass != null && GrTraitUtil.isTrait(superClass) && scopeClass.isInheritor(superClass, false)) {
PsiSubstitutor superClassSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, scopeClass, PsiSubstitutor.EMPTY);
return new GroovyResolveResultImpl[] { new GroovyResolveResultImpl(superClass, null, null, superClassSubstitutor, true, true) };
}
if (PsiUtil.hasEnclosingInstanceInScope(superClass, ref, false)) {
return getSuperClass(superClass);
}
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class DefaultCallExpressionTypeCalculator method calculateReturnType.
@Override
public PsiType calculateReturnType(@NotNull GrMethodCall callExpression, GroovyResolveResult[] resolveResults) {
GrExpression invoked = callExpression.getInvokedExpression();
if (invoked instanceof GrReferenceExpression) {
GrReferenceExpression refExpr = (GrReferenceExpression) invoked;
PsiManager manager = callExpression.getManager();
PsiType result = null;
for (GroovyResolveResult resolveResult : resolveResults) {
PsiType returnType = calculateReturnTypeInner(callExpression, refExpr, resolveResult);
if (returnType == null)
return null;
PsiType nonVoid = PsiType.VOID.equals(returnType) && !isCompileStatic(callExpression) ? PsiType.NULL : returnType;
PsiType normalized = nonVoid instanceof GrLiteralClassType ? nonVoid : TypesUtil.substituteAndNormalizeType(nonVoid, resolveResult.getSubstitutor(), resolveResult.getSpreadState(), callExpression);
LOG.assertTrue(normalized != null, "return type: " + returnType + "; substitutor: " + resolveResult.getSubstitutor());
if (result == null || normalized.isAssignableFrom(result)) {
result = normalized;
} else if (!result.isAssignableFrom(normalized)) {
result = TypesUtil.getLeastUpperBound(result, normalized, manager);
}
}
return result;
} else {
return extractReturnTypeFromType(invoked.getType(), false, callExpression);
}
}
Aggregations