use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method processChangedMethodCall.
private static void processChangedMethodCall(PsiElement element, GrIntroduceParameterSettings settings) {
if (element.getParent() instanceof GrMethodCallExpression) {
GrMethodCallExpression methodCall = (GrMethodCallExpression) element.getParent();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
GrExpression expression = factory.createExpressionFromText(settings.getName(), null);
final GrArgumentList argList = methodCall.getArgumentList();
final PsiElement[] exprs = argList.getAllArguments();
if (exprs.length > 0) {
argList.addAfter(expression, exprs[exprs.length - 1]);
} else {
argList.add(expression);
}
removeParametersFromCall(methodCall, settings);
} else {
LOG.error(element.getParent());
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class ExpressionGenerator method visitNewExpression.
@Override
public void visitNewExpression(@NotNull GrNewExpression newExpression) {
boolean hasFieldInitialization = hasFieldInitialization(newExpression);
StringBuilder builder;
final PsiType type = newExpression.getType();
final String varName;
if (hasFieldInitialization) {
builder = new StringBuilder();
varName = GenerationUtil.suggestVarName(type, newExpression, context);
TypeWriter.writeType(builder, type, newExpression);
builder.append(' ').append(varName).append(" = ");
} else {
varName = null;
builder = this.builder;
}
final GrTypeElement typeElement = newExpression.getTypeElement();
final GrArrayDeclaration arrayDeclaration = newExpression.getArrayDeclaration();
final GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
builder.append("new ");
if (typeElement != null) {
final PsiType builtIn = typeElement.getType();
LOG.assertTrue(builtIn instanceof PsiPrimitiveType);
final PsiType boxed = TypesUtil.boxPrimitiveType(builtIn, newExpression.getManager(), newExpression.getResolveScope());
TypeWriter.writeTypeForNew(builder, boxed, newExpression);
} else if (referenceElement != null) {
GenerationUtil.writeCodeReferenceElement(builder, referenceElement);
}
final GrArgumentList argList = newExpression.getArgumentList();
if (argList != null) {
GrClosureSignature signature = null;
final GroovyResolveResult resolveResult = newExpression.advancedResolve();
final PsiElement constructor = resolveResult.getElement();
if (constructor instanceof PsiMethod) {
signature = GrClosureSignatureUtil.createSignature((PsiMethod) constructor, resolveResult.getSubstitutor());
} else if (referenceElement != null) {
final GroovyResolveResult clazzResult = referenceElement.advancedResolve();
final PsiElement clazz = clazzResult.getElement();
if (clazz instanceof PsiClass && ((PsiClass) clazz).getConstructors().length == 0) {
signature = GrClosureSignatureUtil.createSignature(PsiParameter.EMPTY_ARRAY, null);
}
}
final GrNamedArgument[] namedArgs = hasFieldInitialization ? GrNamedArgument.EMPTY_ARRAY : argList.getNamedArguments();
new ArgumentListGenerator(builder, context).generate(signature, argList.getExpressionArguments(), namedArgs, GrClosableBlock.EMPTY_ARRAY, newExpression);
}
final GrAnonymousClassDefinition anonymous = newExpression.getAnonymousClassDefinition();
if (anonymous != null) {
writeTypeBody(builder, anonymous);
}
if (arrayDeclaration != null) {
final GrExpression[] boundExpressions = arrayDeclaration.getBoundExpressions();
for (GrExpression boundExpression : boundExpressions) {
builder.append('[');
boundExpression.accept(this);
builder.append(']');
}
if (boundExpressions.length == 0) {
builder.append("[]");
}
}
if (hasFieldInitialization) {
builder.append(';');
context.myStatements.add(builder.toString());
final GrNamedArgument[] namedArguments = argList.getNamedArguments();
for (GrNamedArgument namedArgument : namedArguments) {
final String fieldName = namedArgument.getLabelName();
if (fieldName == null) {
//todo try to initialize field
final GrArgumentLabel label = namedArgument.getLabel();
LOG.info("cannot initialize field " + (label == null ? "<null>" : label.getText()));
} else {
final GroovyResolveResult resolveResult = referenceElement.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
LOG.assertTrue(resolved instanceof PsiClass);
initializeField(varName, type, ((PsiClass) resolved), resolveResult.getSubstitutor(), fieldName, namedArgument.getExpression());
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class ClassItemGeneratorImpl method writeEnumConstant.
@Override
public void writeEnumConstant(StringBuilder builder, GrEnumConstant constant) {
GenerationUtil.writeDocComment(builder, constant, false);
builder.append(constant.getName());
final GrArgumentList argumentList = constant.getArgumentList();
if (argumentList != null) {
final GroovyResolveResult resolveResult = constant.advancedResolve();
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(resolveResult);
new ArgumentListGenerator(builder, context.extend()).generate(signature, argumentList.getExpressionArguments(), argumentList.getNamedArguments(), GrClosableBlock.EMPTY_ARRAY, constant);
}
final GrEnumConstantInitializer anonymousBlock = constant.getInitializingClass();
if (anonymousBlock != null) {
builder.append("{\n");
new ClassGenerator(classNameProvider, this).writeMembers(builder, anonymousBlock);
builder.append("\n}");
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class ExpressionGenerator method visitIndexProperty.
@Override
public void visitIndexProperty(@NotNull GrIndexProperty expression) {
final GrExpression selectedExpression = expression.getInvokedExpression();
final PsiType thisType = selectedExpression.getType();
final GrArgumentList argList = expression.getArgumentList();
if (argList.getAllArguments().length == 0) {
// int[] or String[]
if (selectedExpression instanceof GrBuiltinTypeClassExpression) {
selectedExpression.accept(this);
return;
} else if (selectedExpression instanceof GrReferenceExpression) {
PsiElement resolved = ((GrReferenceExpression) selectedExpression).resolve();
if (resolved instanceof PsiClass) {
builder.append(((PsiClass) resolved).getQualifiedName());
builder.append("[].class");
return;
}
}
}
final PsiType[] argTypes = PsiUtil.getArgumentTypes(argList);
final GrExpression[] exprArgs = argList.getExpressionArguments();
final GrNamedArgument[] namedArgs = argList.getNamedArguments();
if (!PsiImplUtil.isSimpleArrayAccess(thisType, argTypes, expression, PsiUtil.isLValue(expression))) {
final GroovyResolveResult candidate = PsiImplUtil.extractUniqueResult(expression.multiResolve(false));
PsiElement element = candidate.getElement();
if (element != null || !PsiUtil.isLValue(expression)) {
//see the case of l-value in assignment expression
if (element instanceof GrGdkMethod && ((GrGdkMethod) element).getStaticMethod().getParameterList().getParameters()[0].getType().equalsToText("java.util.Map<K,V>")) {
PsiClass map = JavaPsiFacade.getInstance(context.project).findClass(CommonClassNames.JAVA_UTIL_MAP, expression.getResolveScope());
if (map != null) {
PsiMethod[] gets = map.findMethodsByName("get", false);
invokeMethodOn(gets[0], selectedExpression, exprArgs, namedArgs, GrClosableBlock.EMPTY_ARRAY, PsiSubstitutor.EMPTY, expression);
return;
}
} else if (element instanceof GrGdkMethod && ((GrGdkMethod) element).getStaticMethod().getParameterList().getParameters()[0].getType().equalsToText("java.util.List<T>")) {
PsiClass list = JavaPsiFacade.getInstance(context.project).findClass(CommonClassNames.JAVA_UTIL_LIST, expression.getResolveScope());
if (list != null) {
PsiMethod[] gets = list.findMethodsByName("get", false);
invokeMethodOn(gets[0], selectedExpression, exprArgs, namedArgs, GrClosableBlock.EMPTY_ARRAY, PsiSubstitutor.EMPTY, expression);
return;
}
}
GenerationUtil.invokeMethodByResolveResult(selectedExpression, candidate, "getAt", exprArgs, namedArgs, GrClosableBlock.EMPTY_ARRAY, this, expression);
return;
}
}
selectedExpression.accept(this);
builder.append('[');
final GrExpression arg = exprArgs[0];
arg.accept(this);
builder.append(']');
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method checkConstructorApplicability.
private <T extends GroovyPsiElement> boolean checkConstructorApplicability(@NotNull GroovyResolveResult constructorResolveResult, @NotNull CallInfo<T> info, boolean checkUnknownArgs) {
final PsiElement element = constructorResolveResult.getElement();
LOG.assertTrue(element instanceof PsiMethod && ((PsiMethod) element).isConstructor(), element);
final PsiMethod constructor = (PsiMethod) element;
final GrArgumentList argList = info.getArgumentList();
if (argList != null) {
final GrExpression[] exprArgs = argList.getExpressionArguments();
if (exprArgs.length == 0 && !PsiUtil.isConstructorHasRequiredParameters(constructor))
return true;
}
PsiType[] types = info.getArgumentTypes();
PsiClass containingClass = constructor.getContainingClass();
if (types != null && containingClass != null) {
final PsiType[] newTypes = GrInnerClassConstructorUtil.addEnclosingArgIfNeeded(types, info.getCall(), containingClass);
if (newTypes.length != types.length) {
return checkMethodApplicability(constructorResolveResult, checkUnknownArgs, new DelegatingCallInfo<T>(info) {
@Nullable
@Override
public PsiType[] getArgumentTypes() {
return newTypes;
}
});
}
}
return checkMethodApplicability(constructorResolveResult, checkUnknownArgs, info);
}
Aggregations