use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GrAnnotationNameValuePairImpl method multiResolveFromAlias.
private static GroovyResolveResult[] multiResolveFromAlias(@NotNull GrAnnotation alias, @NotNull String name, @NotNull PsiAnnotation annotationCollector) {
List<GroovyResolveResult> result = ContainerUtilRt.newArrayList();
List<GrAnnotation> annotations = ContainerUtilRt.newArrayList();
GrAnnotationCollector.collectAnnotations(annotations, alias, annotationCollector);
for (GrAnnotation annotation : annotations) {
final PsiElement clazz = annotation.getClassReference().resolve();
if (clazz instanceof PsiClass && ((PsiClass) clazz).isAnnotationType()) {
if (GroovyCommonClassNames.GROOVY_TRANSFORM_ANNOTATION_COLLECTOR.equals(((PsiClass) clazz).getQualifiedName()))
continue;
for (PsiMethod method : ((PsiClass) clazz).findMethodsByName(name, false)) {
result.add(new GroovyResolveResultImpl(method, true));
}
}
}
return result.toArray(new GroovyResolveResult[result.size()]);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ArgumentInstruction method inferMixinType.
@Override
@Nullable
public PsiType inferMixinType() {
PsiElement element = getElement();
assert element != null;
GrCall call = findCall(element);
GrExpression[] arguments = call.getExpressionArguments();
boolean hasNamed = PsiImplUtil.hasNamedArguments(call.getArgumentList());
int index = ArrayUtil.indexOf(arguments, element) + (hasNamed ? 1 : 0);
GroovyResolveResult[] variants = call.getCallVariants((GrReferenceExpression) element);
PsiType result = null;
for (GroovyResolveResult variant : variants) {
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(variant);
if (signature == null)
continue;
if (GrClosureSignatureUtil.mapParametersToArguments(signature, call) != null && !haveNullParameters(call)) {
return null;
}
GrClosureParameter[] parameters = signature.getParameters();
if (index >= parameters.length)
continue;
result = TypesUtil.getLeastUpperBoundNullable(result, parameters[index].getType(), element.getManager());
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GrClosureSignatureUtil method createSignature.
@Nullable
public static GrClosureSignature createSignature(GrCall call) {
if (call instanceof GrMethodCall) {
final GrExpression invokedExpression = ((GrMethodCall) call).getInvokedExpression();
final PsiType type = invokedExpression.getType();
if (type instanceof GrClosureType) {
final GrSignature signature = ((GrClosureType) type).getSignature();
final Trinity<GrClosureSignature, ArgInfo<PsiType>[], ApplicabilityResult> trinity = getApplicableSignature(signature, PsiUtil.getArgumentTypes(invokedExpression, true), call);
if (trinity != null) {
return trinity.first;
}
return null;
}
}
final GroovyResolveResult resolveResult = call.advancedResolve();
final PsiElement element = resolveResult.getElement();
if (element instanceof PsiMethod) {
return createSignature((PsiMethod) element, resolveResult.getSubstitutor());
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GrNewExpressionImpl method resolveImpl.
private GroovyResolveResult[] resolveImpl(boolean incompleteCode) {
GrCodeReferenceElement ref = getReferenceElement();
if (ref == null)
return GroovyResolveResult.EMPTY_ARRAY;
GroovyResolveResult classCandidate = inferClassCandidate(ref);
if (classCandidate == null)
return GroovyResolveResult.EMPTY_ARRAY;
assert classCandidate.getElement() instanceof PsiClass;
if (incompleteCode) {
return PsiUtil.getConstructorCandidates(ref, classCandidate, null);
}
final GrArgumentList argumentList = getArgumentList();
if (argumentList == null)
return GroovyResolveResult.EMPTY_ARRAY;
if (argumentList.getNamedArguments().length > 0 && argumentList.getExpressionArguments().length == 0) {
PsiType mapType = GrMapType.createFromNamedArgs(argumentList, getNamedArguments());
//one Map parameter, actually
GroovyResolveResult[] constructorResults = PsiUtil.getConstructorCandidates(ref, classCandidate, new PsiType[] { mapType });
for (GroovyResolveResult result : constructorResults) {
final PsiElement resolved = result.getElement();
if (resolved instanceof PsiMethod) {
PsiMethod constructor = (PsiMethod) resolved;
final PsiParameter[] parameters = constructor.getParameterList().getParameters();
if (parameters.length == 1 && InheritanceUtil.isInheritor(parameters[0].getType(), CommonClassNames.JAVA_UTIL_MAP)) {
return constructorResults;
}
}
}
final GroovyResolveResult[] emptyConstructors = PsiUtil.getConstructorCandidates(ref, classCandidate, PsiType.EMPTY_ARRAY);
if (emptyConstructors.length > 0) {
return emptyConstructors;
}
}
PsiType[] types = PsiUtil.getArgumentTypes(ref, true);
if (types != null) {
types = GrInnerClassConstructorUtil.addEnclosingArgIfNeeded(types, this, (PsiClass) classCandidate.getElement());
}
return PsiUtil.getConstructorCandidates(ref, classCandidate, types);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GrReferenceExpressionImpl method calculateType.
@Nullable
private static PsiType calculateType(@NotNull GrReferenceExpressionImpl refExpr, boolean forceRValue) {
final GroovyResolveResult[] results = refExpr.multiResolve(false, forceRValue);
final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(results);
final PsiElement resolved = result.getElement();
for (GrExpressionTypeCalculator calculator : GrExpressionTypeCalculator.EP_NAME.getExtensions()) {
PsiType type = calculator.calculateType(refExpr, resolved);
if (type != null)
return type;
}
if (ResolveUtil.isClassReference(refExpr)) {
GrExpression qualifier = refExpr.getQualifier();
LOG.assertTrue(qualifier != null);
return qualifier.getType();
}
if (PsiUtil.isCompileStatic(refExpr)) {
final PsiType type;
if (resolved instanceof GrField) {
type = ((GrField) resolved).getType();
} else if (resolved instanceof GrVariable) {
type = ((GrVariable) resolved).getDeclaredType();
} else if (resolved instanceof GrAccessorMethod) {
type = ((GrAccessorMethod) resolved).getProperty().getType();
} else {
type = null;
}
if (type != null) {
return result.getSubstitutor().substitute(type);
}
}
final PsiType nominal = refExpr.getNominalType(forceRValue);
Boolean reassigned = GrReassignedLocalVarsChecker.isReassignedVar(refExpr);
if (reassigned != null && reassigned.booleanValue()) {
return GrReassignedLocalVarsChecker.getReassignedVarType(refExpr, true);
}
final PsiType inferred = getInferredTypes(refExpr, resolved);
if (inferred == null) {
if (nominal == null) {
//inside nested closure we could still try to infer from variable initializer. Not sound, but makes sense
if (resolved instanceof GrVariable) {
LOG.assertTrue(resolved.isValid());
return ((GrVariable) resolved).getTypeGroovy();
}
}
return nominal;
}
if (nominal == null)
return inferred;
if (!TypeConversionUtil.isAssignable(TypeConversionUtil.erasure(nominal), inferred, false)) {
if (resolved instanceof GrVariable && ((GrVariable) resolved).getTypeElementGroovy() != null) {
return nominal;
}
}
return inferred;
}
Aggregations