use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class TypeProvider method inferMethodParameters.
@NotNull
private PsiType[] inferMethodParameters(@NotNull GrMethod method) {
PsiType[] psiTypes = inferredTypes.get(method);
if (psiTypes != null)
return psiTypes;
final GrParameter[] parameters = method.getParameters();
final TIntArrayList paramInds = new TIntArrayList(parameters.length);
final PsiType[] types = PsiType.createArray(parameters.length);
for (int i = 0; i < parameters.length; i++) {
if (parameters[i].getTypeElementGroovy() == null) {
paramInds.add(i);
} else {
types[i] = parameters[i].getType();
}
}
if (!paramInds.isEmpty()) {
final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, PsiSubstitutor.EMPTY);
MethodReferencesSearch.search(method, true).forEach(psiReference -> {
final PsiElement element = psiReference.getElement();
final PsiManager manager = element.getManager();
final GlobalSearchScope resolveScope = element.getResolveScope();
if (element instanceof GrReferenceExpression) {
final GrCall call = (GrCall) element.getParent();
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, call);
if (argInfos == null)
return true;
paramInds.forEach(new TIntProcedure() {
@Override
public boolean execute(int i) {
PsiType type = GrClosureSignatureUtil.getTypeByArg(argInfos[i], manager, resolveScope);
types[i] = TypesUtil.getLeastUpperBoundNullable(type, types[i], manager);
return true;
}
});
}
return true;
});
}
paramInds.forEach(new TIntProcedure() {
@Override
public boolean execute(int i) {
if (types[i] == null || types[i] == PsiType.NULL) {
types[i] = parameters[i].getType();
}
return true;
}
});
inferredTypes.put(method, types);
return types;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GroovyReferenceCopyPasteProcessor method findReferencesToRestore.
@NotNull
@Override
protected GrReferenceElement[] findReferencesToRestore(PsiFile file, RangeMarker bounds, ReferenceData[] referenceData) {
PsiManager manager = file.getManager();
final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
PsiResolveHelper helper = facade.getResolveHelper();
GrReferenceElement[] refs = new GrReferenceElement[referenceData.length];
for (int i = 0; i < referenceData.length; i++) {
ReferenceData data = referenceData[i];
PsiClass refClass = facade.findClass(data.qClassName, file.getResolveScope());
if (refClass == null)
continue;
int startOffset = data.startOffset + bounds.getStartOffset();
int endOffset = data.endOffset + bounds.getStartOffset();
PsiElement element = file.findElementAt(startOffset);
if (element != null && element.getParent() instanceof GrReferenceElement && !PsiUtil.isThisOrSuperRef(element.getParent())) {
GrReferenceElement reference = (GrReferenceElement) element.getParent();
TextRange range = reference.getTextRange();
if (range.getStartOffset() == startOffset && range.getEndOffset() == endOffset) {
if (data.staticMemberName == null) {
PsiClass refClass1 = helper.resolveReferencedClass(reference.getText(), reference);
if (refClass1 == null || !manager.areElementsEquivalent(refClass, refClass1)) {
refs[i] = reference;
}
} else {
if (reference instanceof GrReferenceExpression) {
PsiElement referent = resolveReferenceIgnoreOverriding(reference);
if (!(referent instanceof PsiNamedElement) || !data.staticMemberName.equals(((PsiNamedElement) referent).getName()) || !(referent instanceof PsiMember) || ((PsiMember) referent).getContainingClass() == null || !data.qClassName.equals(((PsiMember) referent).getContainingClass().getQualifiedName())) {
refs[i] = reference;
}
}
}
}
}
}
return refs;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GroovyStaticImportMethodFix method isAvailable.
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
myCandidates = null;
if (!file.getManager().isInProject(file))
return false;
GrReferenceExpression invokedExpression = getMethodExpression();
if (invokedExpression == null || invokedExpression.getQualifierExpression() != null)
return false;
return !getCandidates().isEmpty();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GroovyStaticImportMethodFix method getMethodsToImport.
@NotNull
private List<PsiMethod> getMethodsToImport() {
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(myMethodCall.getProject());
GrMethodCall element = myMethodCall.getElement();
LOG.assertTrue(element != null);
GrReferenceExpression reference = getMethodExpression(element);
LOG.assertTrue(reference != null);
GrArgumentList argumentList = element.getArgumentList();
String name = reference.getReferenceName();
ArrayList<PsiMethod> list = new ArrayList<>();
if (name == null)
return list;
GlobalSearchScope scope = element.getResolveScope();
PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(name, scope, 20);
List<PsiMethod> applicableList = new ArrayList<>();
for (PsiMethod method : methods) {
ProgressManager.checkCanceled();
if (JavaCompletionUtil.isInExcludedPackage(method, false))
continue;
if (!method.hasModifierProperty(PsiModifier.STATIC))
continue;
PsiFile file = method.getContainingFile();
if (file instanceof PsiClassOwner && //do not show methods from default package
!((PsiClassOwner) file).getPackageName().isEmpty() && PsiUtil.isAccessible(element, method)) {
list.add(method);
if (PsiUtil.isApplicable(PsiUtil.getArgumentTypes(element, true), method, PsiSubstitutor.EMPTY, element, false)) {
applicableList.add(method);
}
}
}
List<PsiMethod> result = applicableList.isEmpty() ? list : applicableList;
Collections.sort(result, new PsiProximityComparator(argumentList));
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GrVariableJoinLinesHandler method tryJoinStatements.
@Override
public int tryJoinStatements(@NotNull GrStatement first, @NotNull GrStatement second) {
if (first instanceof GrVariableDeclaration && !((GrVariableDeclaration) first).isTuple() && second instanceof GrAssignmentExpression) {
final GrExpression lvalue = ((GrAssignmentExpression) second).getLValue();
final GrExpression rValue = ((GrAssignmentExpression) second).getRValue();
if (lvalue instanceof GrReferenceExpression && rValue != null) {
final PsiElement resolved = ((GrReferenceExpression) lvalue).resolve();
if (ArrayUtil.contains(resolved, ((GrVariableDeclaration) first).getVariables())) {
assert resolved instanceof GrVariable;
if (((GrVariable) resolved).getInitializerGroovy() == null) {
((GrVariable) resolved).setInitializerGroovy(rValue);
second.delete();
GrExpression newInitializer = ((GrVariable) resolved).getInitializerGroovy();
assert newInitializer != null;
return newInitializer.getTextRange().getEndOffset();
}
}
}
}
return CANNOT_JOIN;
}
Aggregations