use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class MissingReturnInspection method getExpectedClosureReturnType.
@Nullable
public static PsiType getExpectedClosureReturnType(GrClosableBlock closure) {
List<PsiType> expectedReturnTypes = new ArrayList<>();
PsiElement parent = closure.getParent();
if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall || parent instanceof GrMethodCall) {
GrMethodCall call = (GrMethodCall) (parent instanceof GrArgumentList ? parent.getParent() : parent);
GroovyResolveResult[] variants = call.getCallVariants(null);
for (GroovyResolveResult variant : variants) {
Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(variant, closure, true, true, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments());
if (map != null) {
Pair<PsiParameter, PsiType> pair = map.get(closure);
if (pair == null)
continue;
PsiParameter parameter = pair.getFirst();
PsiType type = parameter.getType();
if (TypesUtil.isPsiClassTypeToClosure(type)) {
PsiType[] parameters = ((PsiClassType) type).getParameters();
if (parameters.length == 1) {
expectedReturnTypes.add(parameters[0]);
}
}
}
}
} else {
for (PsiType expectedType : GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure)) {
if (TypesUtil.isPsiClassTypeToClosure(expectedType)) {
PsiType[] parameters = ((PsiClassType) expectedType).getParameters();
if (parameters.length == 1) {
expectedReturnTypes.add(parameters[0]);
}
}
}
}
for (PsiType type : expectedReturnTypes) {
if (PsiType.VOID.equals(type))
return PsiType.VOID;
}
return TypesUtil.getLeastUpperBoundNullable(expectedReturnTypes, closure.getManager());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class GrConcatenationInjector method findLanguageParams.
private static BaseInjection findLanguageParams(PsiElement place) {
PsiElement parent = place.getParent();
if (parent instanceof GrAssignmentExpression && ((GrAssignmentExpression) parent).getRValue() == place) {
final GrExpression lvalue = ((GrAssignmentExpression) parent).getLValue();
if (lvalue instanceof GrReferenceExpression) {
final PsiElement resolved = ((GrReferenceExpression) lvalue).resolve();
if (resolved instanceof PsiModifierListOwner) {
return getLanguageParams((PsiModifierListOwner) resolved);
}
}
} else if (parent instanceof GrVariable) {
return getLanguageParams((PsiModifierListOwner) parent);
} else if (parent instanceof GrArgumentList) {
final PsiElement pparent = parent.getParent();
if (pparent instanceof GrCall) {
final GrCall call = (GrCall) pparent;
final GroovyResolveResult result = call.advancedResolve();
if (result.getElement() != null) {
final Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(result, place, false, false, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments());
if (map != null) {
final Pair<PsiParameter, PsiType> pair = map.get(place);
return getLanguageParams(pair.first);
}
}
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class GrChangeSignatureUsageProcessor method processMethodUsage.
private static void processMethodUsage(PsiElement element, JavaChangeInfo changeInfo, boolean toChangeArguments, boolean toCatchExceptions, GrClosureSignatureUtil.ArgInfo<PsiElement>[] map, PsiSubstitutor substitutor) {
if (map == null)
return;
if (changeInfo.isNameChanged()) {
if (element instanceof GrReferenceElement) {
element = ((GrReferenceElement) element).handleElementRename(changeInfo.getNewName());
}
}
if (toChangeArguments) {
JavaParameterInfo[] parameters = changeInfo.getNewParameters();
GrArgumentList argumentList = PsiUtil.getArgumentsList(element);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
if (argumentList == null) {
if (element instanceof GrEnumConstant) {
argumentList = factory.createArgumentList();
argumentList = (GrArgumentList) element.add(argumentList);
} else {
return;
}
}
Set<PsiElement> argsToDelete = new HashSet<>(map.length * 2);
for (GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo : map) {
argsToDelete.addAll(argInfo.args);
}
GrExpression[] values = new GrExpression[parameters.length];
for (int i = 0; i < parameters.length; i++) {
JavaParameterInfo parameter = parameters[i];
int index = parameter.getOldIndex();
if (index >= 0) {
argsToDelete.removeAll(map[index].args);
} else {
values[i] = createDefaultValue(factory, changeInfo, parameter, argumentList, substitutor);
}
}
for (PsiElement arg : argsToDelete) {
arg.delete();
}
boolean skipOptionals = false;
//PsiTreeUtil.getChildOfAnyType(argumentList, GrExpression.class, GrNamedArgument.class);
PsiElement anchor = null;
for (int i = 0; i < parameters.length; i++) {
JavaParameterInfo parameter = parameters[i];
int index = parameter.getOldIndex();
if (index >= 0) {
GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = map[index];
List<PsiElement> arguments = argInfo.args;
if (argInfo.isMultiArg) {
//arguments for Map and varArg
if ((i != 0 || !(!arguments.isEmpty() && arguments.iterator().next() instanceof GrNamedArgument)) && (i != parameters.length - 1 || !parameter.isVarargType())) {
final PsiType type = parameter.createType(changeInfo.getMethod().getParameterList(), argumentList.getManager());
final GrExpression arg = GroovyRefactoringUtil.generateArgFromMultiArg(substitutor, arguments, type, element.getProject());
for (PsiElement argument : arguments) {
argument.delete();
}
anchor = argumentList.addAfter(arg, anchor);
JavaCodeStyleManager.getInstance(anchor.getProject()).shortenClassReferences(anchor);
}
} else {
//arguments for simple parameters
if (arguments.size() == 1) {
//arg exists
PsiElement arg = arguments.iterator().next();
if (i == parameters.length - 1 && parameter.isVarargType()) {
if (arg instanceof GrSafeCastExpression) {
PsiElement expr = ((GrSafeCastExpression) arg).getOperand();
if (expr instanceof GrListOrMap && !((GrListOrMap) expr).isMap()) {
final PsiElement copy = expr.copy();
PsiElement[] newVarargs = ((GrListOrMap) copy).getInitializers();
for (PsiElement vararg : newVarargs) {
anchor = argumentList.addAfter(vararg, anchor);
}
arg.delete();
continue;
}
}
}
PsiElement curArg = getNextOfType(argumentList, anchor, GrExpression.class);
if (curArg == arg) {
anchor = arg;
} else {
final PsiElement copy = arg.copy();
anchor = argumentList.addAfter(copy, anchor);
arg.delete();
}
} else {
//arg is skipped. Parameter is optional
skipOptionals = true;
}
}
} else {
if (skipOptionals && isParameterOptional(parameter))
continue;
if (forceOptional(parameter)) {
skipOptionals = true;
continue;
}
try {
final GrExpression value = values[i];
if (i > 0 && (value == null || anchor == null)) {
PsiElement comma = Factory.createSingleLeafElement(GroovyTokenTypes.mCOMMA, ",", 0, 1, SharedImplUtil.findCharTableByTree(argumentList.getNode()), argumentList.getManager()).getPsi();
if (anchor == null)
anchor = argumentList.getLeftParen();
anchor = argumentList.addAfter(comma, anchor);
}
if (value != null) {
anchor = argumentList.addAfter(value, anchor);
}
} catch (IncorrectOperationException e) {
LOG.error(e.getMessage());
}
}
}
for (PsiElement arg : argsToDelete) {
arg.delete();
}
GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
if (argumentList.getText().trim().isEmpty() && (call == null || !PsiImplUtil.hasClosureArguments(call))) {
argumentList = argumentList.replaceWithArgumentList(factory.createArgumentList());
}
CodeStyleManager.getInstance(argumentList.getProject()).reformat(argumentList);
}
if (toCatchExceptions) {
final ThrownExceptionInfo[] exceptionInfos = changeInfo.getNewExceptions();
PsiClassType[] exceptions = getExceptions(exceptionInfos, element, element.getManager());
fixExceptions(element, exceptions);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class ParameterCastFix method doFix.
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiElement element = descriptor.getPsiElement();
final GrArgumentList list = element instanceof GrArgumentList ? (GrArgumentList) element : PsiUtil.getArgumentsList(element);
if (list == null)
return;
GrCastFix.doCast(project, myType, myArgument);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class RecursionUtils method callExpressionDefinitelyRecurses.
private static boolean callExpressionDefinitelyRecurses(GrCallExpression exp, GrMethod method) {
final GrArgumentList argumentList = exp.getArgumentList();
if (argumentList != null) {
final GrExpression[] args = argumentList.getExpressionArguments();
for (final GrExpression arg : args) {
if (expressionDefinitelyRecurses(arg, method)) {
return true;
}
}
final GrNamedArgument[] namedArgs = argumentList.getNamedArguments();
for (final GrNamedArgument arg : namedArgs) {
if (expressionDefinitelyRecurses(arg.getExpression(), method)) {
return true;
}
}
}
return false;
}
Aggregations