use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature in project intellij-community by JetBrains.
the class ConvertClosureToMethodIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrField field;
if (element.getParent() instanceof GrField) {
field = (GrField) element.getParent();
} else {
final PsiReference ref = element.getReference();
LOG.assertTrue(ref != null);
PsiElement resolved = ref.resolve();
if (resolved instanceof GrAccessorMethod) {
resolved = ((GrAccessorMethod) resolved).getProperty();
}
LOG.assertTrue(resolved instanceof GrField);
field = (GrField) resolved;
}
final HashSet<PsiReference> usages = new HashSet<>();
usages.addAll(ReferencesSearch.search(field).findAll());
final GrAccessorMethod[] getters = field.getGetters();
for (GrAccessorMethod getter : getters) {
usages.addAll(MethodReferencesSearch.search(getter).findAll());
}
final GrAccessorMethod setter = field.getSetter();
if (setter != null) {
usages.addAll(MethodReferencesSearch.search(setter).findAll());
}
final String fieldName = field.getName();
LOG.assertTrue(fieldName != null);
final Collection<PsiElement> fieldUsages = new HashSet<>();
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
for (PsiReference usage : usages) {
final PsiElement psiElement = usage.getElement();
if (PsiUtil.isMethodUsage(psiElement))
continue;
if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("closure.is.accessed.outside.of.groovy", fieldName));
} else {
if (psiElement instanceof GrReferenceExpression) {
fieldUsages.add(psiElement);
if (PsiUtil.isAccessedForWriting((GrExpression) psiElement)) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("write.access.to.closure.variable", fieldName));
}
} else if (psiElement instanceof GrArgumentLabel) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("field.is.used.in.argument.label", fieldName));
}
}
}
final PsiClass containingClass = field.getContainingClass();
final GrExpression initializer = field.getInitializerGroovy();
LOG.assertTrue(initializer != null);
final PsiType type = initializer.getType();
LOG.assertTrue(type instanceof GrClosureType);
final GrSignature signature = ((GrClosureType) type).getSignature();
final List<MethodSignature> signatures = GrClosureSignatureUtil.generateAllMethodSignaturesBySignature(fieldName, signature);
for (MethodSignature s : signatures) {
final PsiMethod method = MethodSignatureUtil.findMethodBySignature(containingClass, s, true);
if (method != null) {
conflicts.putValue(method, GroovyIntentionsBundle.message("method.with.signature.already.exists", GroovyPresentationUtil.getSignaturePresentation(s)));
}
}
if (!conflicts.isEmpty()) {
final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(field, fieldUsages));
conflictsDialog.show();
if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
}
execute(field, fieldUsages);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature 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.signatures.GrSignature in project intellij-community by JetBrains.
the class TypesUtil method getLeastUpperBound.
@Nullable
public static PsiType getLeastUpperBound(@NotNull PsiType type1, @NotNull PsiType type2, PsiManager manager) {
{
PsiType numericLUB = getNumericLUB(type1, type2);
if (numericLUB != null)
return numericLUB;
}
if (type1 instanceof GrTupleType && type2 instanceof GrTupleType) {
GrTupleType tuple1 = (GrTupleType) type1;
GrTupleType tuple2 = (GrTupleType) type2;
PsiType[] components1 = tuple1.getComponentTypes();
PsiType[] components2 = tuple2.getComponentTypes();
if (components1.length == 0)
return genNewListBy(type2, manager);
if (components2.length == 0)
return genNewListBy(type1, manager);
PsiType[] components3 = PsiType.createArray(Math.min(components1.length, components2.length));
for (int i = 0; i < components3.length; i++) {
PsiType c1 = components1[i];
PsiType c2 = components2[i];
if (c1 == null || c2 == null) {
components3[i] = null;
} else {
components3[i] = getLeastUpperBound(c1, c2, manager);
}
}
return new GrImmediateTupleType(components3, JavaPsiFacade.getInstance(manager.getProject()), tuple1.getScope().intersectWith(tuple2.getResolveScope()));
} else if (checkEmptyListAndList(type1, type2)) {
return genNewListBy(type2, manager);
} else if (checkEmptyListAndList(type2, type1)) {
return genNewListBy(type1, manager);
} else if (type1 instanceof GrMapType && type2 instanceof GrMapType) {
return GrMapType.merge(((GrMapType) type1), ((GrMapType) type2));
} else if (checkEmptyMapAndMap(type1, type2)) {
return genNewMapBy(type2, manager);
} else if (checkEmptyMapAndMap(type2, type1)) {
return genNewMapBy(type1, manager);
} else if (type1 instanceof GrClosureType && type2 instanceof GrClosureType) {
GrClosureType clType1 = (GrClosureType) type1;
GrClosureType clType2 = (GrClosureType) type2;
GrSignature signature1 = clType1.getSignature();
GrSignature signature2 = clType2.getSignature();
if (signature1 instanceof GrClosureSignature && signature2 instanceof GrClosureSignature) {
if (((GrClosureSignature) signature1).getParameterCount() == ((GrClosureSignature) signature2).getParameterCount()) {
final GrClosureSignature signature = GrImmediateClosureSignatureImpl.getLeastUpperBound(((GrClosureSignature) signature1), ((GrClosureSignature) signature2), manager);
if (signature != null) {
GlobalSearchScope scope = clType1.getResolveScope().intersectWith(clType2.getResolveScope());
final LanguageLevel languageLevel = ComparatorUtil.max(clType1.getLanguageLevel(), clType2.getLanguageLevel());
return GrClosureType.create(signature, scope, JavaPsiFacade.getInstance(manager.getProject()), languageLevel, true);
}
}
}
} else if (GroovyCommonClassNames.GROOVY_LANG_GSTRING.equals(getQualifiedName(type1)) && CommonClassNames.JAVA_LANG_STRING.equals(getQualifiedName(type2))) {
return type2;
} else if (GroovyCommonClassNames.GROOVY_LANG_GSTRING.equals(getQualifiedName(type2)) && CommonClassNames.JAVA_LANG_STRING.equals(getQualifiedName(type1))) {
return type1;
}
return GenericsUtil.getLeastUpperBound(type1, type2, manager);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature in project intellij-community by JetBrains.
the class PsiUtil method isRawClosureCall.
private static boolean isRawClosureCall(GrMethodCallExpression call, GroovyResolveResult result, GrClosureType returnType) {
final GrSignature signature = returnType.getSignature();
GrClosureSignature _signature;
if (signature instanceof GrClosureSignature) {
_signature = (GrClosureSignature) signature;
} else {
final PsiType[] types = getArgumentTypes(call.getInvokedExpression(), true);
final Trinity<GrClosureSignature, GrClosureSignatureUtil.ArgInfo<PsiType>[], GrClosureSignatureUtil.ApplicabilityResult> resultTrinity = types != null ? GrClosureSignatureUtil.getApplicableSignature(signature, types, call) : null;
_signature = resultTrinity != null ? resultTrinity.first : null;
}
if (_signature != null) {
return isRawType(_signature.getReturnType(), TypesUtil.composeSubstitutors(_signature.getSubstitutor(), result.getSubstitutor()));
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature in project intellij-community by JetBrains.
the class ClosureToSamConverter method isConvertible.
@Override
public Boolean isConvertible(@NotNull PsiType ltype, @NotNull PsiType rtype, @NotNull final GroovyPsiElement context) {
if (rtype instanceof GrClosureType && ltype instanceof PsiClassType && isSamConversionAllowed(context) && !TypesUtil.isClassType(ltype, GroovyCommonClassNames.GROOVY_LANG_CLOSURE)) {
MethodSignature signature = findSAMSignature(ltype);
if (signature != null) {
final PsiType[] samParameterTypes = signature.getParameterTypes();
GrSignature closureSignature = ((GrClosureType) rtype).getSignature();
boolean raw = ((PsiClassType) ltype).isRaw();
if (raw)
return true;
if (GrClosureSignatureUtil.isSignatureApplicable(closureSignature, samParameterTypes, context)) {
return true;
}
}
}
return null;
}
Aggregations