use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class MyPredicate method getParameterByArgument.
@Nullable
private static GrParameter getParameterByArgument(GrExpression arg) {
PsiElement parent = PsiUtil.skipParentheses(arg.getParent(), true);
if (!(parent instanceof GrArgumentList))
return null;
final GrArgumentList argList = (GrArgumentList) parent;
parent = parent.getParent();
if (!(parent instanceof GrMethodCall))
return null;
final GrMethodCall methodCall = (GrMethodCall) parent;
final GrExpression expression = methodCall.getInvokedExpression();
if (!(expression instanceof GrReferenceExpression))
return null;
final GroovyResolveResult resolveResult = ((GrReferenceExpression) expression).advancedResolve();
if (resolveResult == null)
return null;
GrClosableBlock[] closures = methodCall.getClosureArguments();
final Map<GrExpression, Pair<PsiParameter, PsiType>> mapToParams = GrClosureSignatureUtil.mapArgumentsToParameters(resolveResult, arg, false, false, argList.getNamedArguments(), argList.getExpressionArguments(), closures);
if (mapToParams == null)
return null;
final Pair<PsiParameter, PsiType> parameterPair = mapToParams.get(arg);
final PsiParameter parameter = parameterPair == null ? null : parameterPair.getFirst();
return parameter instanceof GrParameter ? ((GrParameter) parameter) : null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method replaceParametersWithArguments.
/**
* Inline method call's arguments as its parameters
*
* @param call method call
* @param method given method
*/
public static void replaceParametersWithArguments(GrCallExpression call, GrMethod method) throws IncorrectOperationException {
GrParameter[] parameters = method.getParameters();
if (parameters.length == 0)
return;
GrArgumentList argumentList = call.getArgumentList();
if (argumentList == null) {
setDefaultValuesToParameters(method, null, call);
return;
}
Project project = call.getProject();
final GroovyResolveResult resolveResult = call.advancedResolve();
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, resolveResult.getSubstitutor());
if (signature == null) {
return;
}
GrClosureSignatureUtil.ArgInfo<PsiElement>[] infos = GrClosureSignatureUtil.mapParametersToArguments(signature, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments(), call, true, false);
if (infos == null)
return;
for (int i = 0; i < infos.length; i++) {
GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = infos[i];
GrParameter parameter = parameters[i];
final GrExpression arg = inferArg(signature, parameters, parameter, argInfo, project);
if (arg != null) {
replaceAllOccurrencesWithExpression(method, call, arg, parameter);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter 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.params.GrParameter in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method getExpressionOccurrences.
public static PsiElement[] getExpressionOccurrences(@NotNull PsiElement expr, @NotNull PsiElement scope) {
ArrayList<PsiElement> occurrences = new ArrayList<>();
Comparator<PsiElement> comparator = (element1, element2) -> {
if (element1 != null && element1.equals(element2))
return 0;
if (element1 instanceof GrParameter && element2 instanceof GrParameter) {
final String name1 = ((GrParameter) element1).getName();
final String name2 = ((GrParameter) element2).getName();
return name1.compareTo(name2);
}
return 1;
};
if (scope instanceof GrLoopStatement) {
PsiElement son = expr;
while (son.getParent() != null && !(son.getParent() instanceof GrLoopStatement)) {
son = son.getParent();
}
assert scope.equals(son.getParent());
collectOccurrences(expr, son, occurrences, comparator, false);
} else {
collectOccurrences(expr, scope, occurrences, comparator, scope instanceof GrTypeDefinition || scope instanceof GroovyFileBase);
}
return PsiUtilCore.toPsiElementArray(occurrences);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GrChangeSignatureUsageProcessor method processPrimaryMethodInner.
private static boolean processPrimaryMethodInner(JavaChangeInfo changeInfo, GrMethod method, @Nullable PsiMethod baseMethod) {
if (changeInfo.isNameChanged()) {
String newName = baseMethod == null ? changeInfo.getNewName() : RefactoringUtil.suggestNewOverriderName(method.getName(), baseMethod.getName(), changeInfo.getNewName());
if (newName != null && !newName.equals(method.getName())) {
method.setName(changeInfo.getNewName());
}
}
final GrModifierList modifierList = method.getModifierList();
if (changeInfo.isVisibilityChanged()) {
modifierList.setModifierProperty(changeInfo.getNewVisibility(), true);
}
PsiSubstitutor substitutor = baseMethod != null ? calculateSubstitutor(method, baseMethod) : PsiSubstitutor.EMPTY;
final PsiMethod context = changeInfo.getMethod();
GrTypeElement oldReturnTypeElement = method.getReturnTypeElementGroovy();
if (changeInfo.isReturnTypeChanged()) {
CanonicalTypes.Type newReturnType = changeInfo.getNewReturnType();
if (newReturnType == null) {
if (oldReturnTypeElement != null) {
oldReturnTypeElement.delete();
if (modifierList.getModifiers().length == 0) {
modifierList.setModifierProperty(GrModifier.DEF, true);
}
}
} else {
PsiType type = newReturnType.getType(context, method.getManager());
GrReferenceAdjuster.shortenAllReferencesIn(method.setReturnType(substitutor.substitute(type)));
if (oldReturnTypeElement == null) {
modifierList.setModifierProperty(GrModifier.DEF, false);
}
}
}
JavaParameterInfo[] newParameters = changeInfo.getNewParameters();
final GrParameterList parameterList = method.getParameterList();
GrParameter[] oldParameters = parameterList.getParameters();
final PsiParameter[] oldBaseParams = baseMethod != null ? baseMethod.getParameterList().getParameters() : null;
Set<GrParameter> toRemove = new HashSet<>(oldParameters.length);
ContainerUtil.addAll(toRemove, oldParameters);
GrParameter anchor = null;
final GrDocComment docComment = method.getDocComment();
final GrDocTag[] tags = docComment == null ? null : docComment.getTags();
int newParamIndex = 0;
for (JavaParameterInfo newParameter : newParameters) {
//if old parameter name differs from base method parameter name we don't change it
final String newName;
final int oldIndex = newParameter.getOldIndex();
if (oldIndex >= 0 && oldBaseParams != null) {
final String oldName = oldParameters[oldIndex].getName();
if (oldName.equals(oldBaseParams[oldIndex].getName())) {
newName = newParameter.getName();
} else {
newName = oldName;
}
} else {
newName = newParameter.getName();
}
final GrParameter oldParameter = oldIndex >= 0 ? oldParameters[oldIndex] : null;
if (docComment != null && oldParameter != null) {
final String oldName = oldParameter.getName();
for (GrDocTag tag : tags) {
if ("@param".equals(tag.getName())) {
final GrDocParameterReference parameterReference = tag.getDocParameterReference();
if (parameterReference != null && oldName.equals(parameterReference.getText())) {
parameterReference.handleElementRename(newName);
}
}
}
}
GrParameter grParameter = createNewParameter(substitutor, context, parameterList, newParameter, newName);
if (oldParameter != null) {
grParameter.getModifierList().replace(oldParameter.getModifierList());
}
if ("def".equals(newParameter.getTypeText())) {
grParameter.getModifierList().setModifierProperty(GrModifier.DEF, true);
} else if (StringUtil.isEmpty(newParameter.getTypeText())) {
grParameter.getModifierList().setModifierProperty(GrModifier.DEF, false);
}
anchor = (GrParameter) parameterList.addAfter(grParameter, anchor);
if (newParamIndex < oldParameters.length) {
GrParameter oldParam = oldParameters[newParamIndex];
PsiElement prev = oldParam.getPrevSibling();
if (prev instanceof PsiWhiteSpace) {
parameterList.addBefore(prev, anchor);
}
}
newParamIndex++;
}
for (GrParameter oldParameter : toRemove) {
oldParameter.delete();
}
JavaCodeStyleManager.getInstance(parameterList.getProject()).shortenClassReferences(parameterList);
CodeStyleManager.getInstance(parameterList.getProject()).reformat(parameterList);
if (changeInfo.isExceptionSetOrOrderChanged()) {
final ThrownExceptionInfo[] infos = changeInfo.getNewExceptions();
PsiClassType[] exceptionTypes = new PsiClassType[infos.length];
for (int i = 0; i < infos.length; i++) {
ThrownExceptionInfo info = infos[i];
exceptionTypes[i] = (PsiClassType) info.createType(method, method.getManager());
}
PsiReferenceList thrownList = GroovyPsiElementFactory.getInstance(method.getProject()).createThrownList(exceptionTypes);
thrownList = (PsiReferenceList) method.getThrowsList().replace(thrownList);
JavaCodeStyleManager.getInstance(thrownList.getProject()).shortenClassReferences(thrownList);
CodeStyleManager.getInstance(method.getProject()).reformat(method.getThrowsList());
}
return true;
}
Aggregations