use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GenerationUtil method resolveMethod.
@NotNull
public static GroovyResolveResult resolveMethod(@Nullable GrExpression caller, @NotNull String methodName, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] closureArgs, @NotNull GroovyPsiElement psiContext) {
GroovyResolveResult call = GroovyResolveResult.EMPTY_RESULT;
final PsiType type;
if (caller == null) {
type = GroovyPsiElementFactory.getInstance(psiContext.getProject()).createExpressionFromText("this", psiContext).getType();
} else {
type = caller.getType();
}
if (type != null) {
final PsiType[] argumentTypes = PsiUtil.getArgumentTypes(namedArgs, exprs, closureArgs, false, null);
final GroovyResolveResult[] candidates = ResolveUtil.getMethodCandidates(type, methodName, psiContext, argumentTypes);
call = PsiImplUtil.extractUniqueResult(candidates);
}
return call;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class CodeBlockGenerator method writeTupleDeclaration.
private void writeTupleDeclaration(GrVariableDeclaration variableDeclaration, StringBuilder builder, ExpressionContext expressionContext) {
GrVariable[] variables = variableDeclaration.getVariables();
final GrExpression tupleInitializer = variableDeclaration.getTupleInitializer();
if (tupleInitializer instanceof GrListOrMap) {
for (GrVariable variable : variables) {
GenerationUtil.writeVariableSeparately(variable, builder, expressionContext);
builder.append(";\n");
}
} else if (tupleInitializer != null) {
GroovyResolveResult iteratorMethodResult = GenerationUtil.resolveMethod(tupleInitializer, "iterator", GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, variableDeclaration);
final PsiType iteratorType = inferIteratorType(iteratorMethodResult, tupleInitializer);
final String iteratorName = genIteratorVar(variableDeclaration, builder, expressionContext, tupleInitializer, iteratorType, iteratorMethodResult);
final GrModifierList modifierList = variableDeclaration.getModifierList();
PsiType iterableTypeParameter = PsiUtil.extractIterableTypeParameter(iteratorType, false);
for (final GrVariable v : variables) {
ModifierListGenerator.writeModifiers(builder, modifierList);
final PsiType type = context.typeProvider.getVarType(v);
TypeWriter.writeType(builder, type, variableDeclaration);
builder.append(' ').append(v.getName());
builder.append(" = ");
GenerationUtil.wrapInCastIfNeeded(builder, type, iterableTypeParameter, tupleInitializer, expressionContext, new StatementWriter() {
@Override
public void writeStatement(StringBuilder builder, ExpressionContext context) {
builder.append(iteratorName).append(".hasNext() ? ").append(iteratorName).append(".next() : null");
}
});
builder.append(";\n");
}
} else {
GenerationUtil.writeSimpleVarDeclaration(variableDeclaration, builder, expressionContext);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ExpressionGenerator method generateMethodCall.
private void generateMethodCall(GrMethodCall methodCallExpression) {
final GrExpression invoked = methodCallExpression.getInvokedExpression();
final GrExpression[] exprs = methodCallExpression.getExpressionArguments();
final GrNamedArgument[] namedArgs = methodCallExpression.getNamedArguments();
final GrClosableBlock[] clArgs = methodCallExpression.getClosureArguments();
if (invoked instanceof GrReferenceExpression) {
final GroovyResolveResult resolveResult = ((GrReferenceExpression) invoked).advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod) {
//todo replace null-qualifier with this-reference
final GrExpression qualifier = ((GrReferenceExpression) invoked).getQualifier();
invokeMethodOn(((PsiMethod) resolved), qualifier, exprs, namedArgs, clArgs, resolveResult.getSubstitutor(), methodCallExpression);
return;
} else if (resolved == null) {
final GrExpression qualifier = ((GrReferenceExpression) invoked).getQualifier();
final GrExpression[] args = generateArgsForInvokeMethod(((GrReferenceExpression) invoked).getReferenceName(), exprs, namedArgs, clArgs, methodCallExpression);
GenerationUtil.invokeMethodByName(qualifier, "invokeMethod", args, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, this, methodCallExpression);
return;
}
}
GenerationUtil.invokeMethodByName(invoked, "call", exprs, namedArgs, clArgs, this, methodCallExpression);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ExpressionGenerator method visitAssignmentExpression.
/**
* x= expr ->
* x = expr
* x.set(expr)[: x.get()]
* x+= expr ->
* x+=expr
* x= plus(x, expr)
* x.set(plus(x, expr))[expr]
* x[a] = 4 ->
* x[a] = 4
* x.putAt(a, 4) [4]
*/
@Override
public void visitAssignmentExpression(@NotNull final GrAssignmentExpression expression) {
final GrExpression lValue = expression.getLValue();
final GrExpression rValue = expression.getRValue();
final IElementType token = expression.getOperationTokenType();
PsiElement realLValue = PsiUtil.skipParentheses(lValue, false);
if (realLValue instanceof GrReferenceExpression && rValue != null) {
GroovyResolveResult resolveResult = ((GrReferenceExpression) realLValue).advancedResolve();
PsiElement resolved = resolveResult.getElement();
if (resolved instanceof GrVariable && context.analyzedVars.toWrap((GrVariable) resolved)) {
//write assignment to wrapped local var
writeAssignmentWithRefSetter((GrExpression) realLValue, expression);
return;
} else if (resolved instanceof PsiMethod && resolveResult.isInvokedOnProperty()) {
//write assignment via setter
writeAssignmentWithSetter(((GrReferenceExpression) realLValue).getQualifier(), (PsiMethod) resolved, expression);
return;
} else if (resolved == null || resolved instanceof GrBindingVariable) {
//write unresolved reference assignment via setter GroovyObject.setProperty(String name, Object value)
final GrExpression qualifier = ((GrReferenceExpression) realLValue).getQualifier();
final PsiType type = PsiImplUtil.getQualifierType((GrReferenceExpression) realLValue);
final GrExpression[] args = { factory.createExpressionFromText("\"" + ((GrReferenceExpression) realLValue).getReferenceName() + "\""), getRValue(expression) };
GroovyResolveResult[] candidates = type != null ? ResolveUtil.getMethodCandidates(type, "setProperty", expression, args[0].getType(), args[1].getType()) : GroovyResolveResult.EMPTY_ARRAY;
final PsiMethod method = PsiImplUtil.extractUniqueElement(candidates);
if (method != null) {
writeAssignmentWithSetter(qualifier, method, args, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, PsiSubstitutor.EMPTY, expression);
return;
}
}
} else if (realLValue instanceof GrIndexProperty) {
//qualifier[args] = rValue
//write assignment via qualifier.putAt(Args, Value) method
final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(((GrIndexProperty) realLValue).multiResolve(false));
final PsiElement resolved = result.getElement();
if (resolved instanceof PsiMethod) {
final GrExpression[] args = ((GrIndexProperty) realLValue).getArgumentList().getExpressionArguments();
writeAssignmentWithSetter(((GrIndexProperty) realLValue).getInvokedExpression(), (PsiMethod) resolved, ArrayUtil.append(args, getRValue(expression)), GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, result.getSubstitutor(), expression);
return;
}
}
final PsiType lType = GenerationUtil.getDeclaredType(lValue, context);
if (token == GroovyTokenTypes.mASSIGN) {
//write simple assignment
lValue.accept(this);
builder.append(" = ");
if (rValue != null) {
final PsiType rType = GenerationUtil.getDeclaredType(rValue, context);
GenerationUtil.wrapInCastIfNeeded(builder, GenerationUtil.getNotNullType(expression, lType), rType, expression, context, new StatementWriter() {
@Override
public void writeStatement(StringBuilder builder, ExpressionContext context) {
rValue.accept(ExpressionGenerator.this);
}
});
}
} else {
//write assignment such as +=, -=, etc
final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(expression.multiResolve(false));
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod && !shouldNotReplaceOperatorWithMethod(lValue.getType(), rValue, expression.getOperationTokenType())) {
lValue.accept(this);
builder.append(" = ");
final PsiType rType = GenerationUtil.getDeclaredType((PsiMethod) resolved, resolveResult.getSubstitutor(), context);
GenerationUtil.wrapInCastIfNeeded(builder, GenerationUtil.getNotNullType(expression, lType), rType, expression, context, new StatementWriter() {
@Override
public void writeStatement(StringBuilder builder, ExpressionContext context) {
invokeMethodOn(((PsiMethod) resolved), (GrExpression) lValue.copy(), rValue == null ? GrExpression.EMPTY_ARRAY : new GrExpression[] { rValue }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), expression);
}
});
} else {
writeSimpleBinaryExpression(expression.getOperationToken(), lValue, rValue);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ExpressionGenerator method generatePrefixIncDec.
private boolean generatePrefixIncDec(PsiMethod method, GrExpression operand, GrUnaryExpression unary) {
if (!(operand instanceof GrReferenceExpression))
return false;
final GrExpression qualifier = ((GrReferenceExpression) operand).getQualifier();
final GroovyResolveResult resolveResult = ((GrReferenceExpression) operand).advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) resolved)) {
final PsiMethod getter = (PsiMethod) resolved;
final String propertyName = GroovyPropertyUtils.getPropertyNameByGetter(getter);
final PsiType type;
if (qualifier == null) {
type = null;
} else {
type = qualifier.getType();
if (type == null)
return false;
}
final PsiMethod setter = GroovyPropertyUtils.findPropertySetter(type, propertyName, unary);
if (setter == null)
return false;
final ExpressionGenerator generator = new ExpressionGenerator(new StringBuilder(), context);
if (shouldNotReplaceOperatorWithMethod(operand.getType(), null, unary.getOperationTokenType())) {
writeSimpleUnary(operand, unary, generator);
} else {
generator.invokeMethodOn(method, operand, GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), unary);
}
final GrExpression fromText = factory.createExpressionFromText(generator.toString(), unary);
invokeMethodOn(setter, qualifier, new GrExpression[] { fromText }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), unary);
} else if (resolved instanceof PsiVariable) {
boolean wrap = context.analyzedVars.toWrap((PsiVariable) resolved);
boolean doNeedExpression = PsiUtil.isExpressionUsed(unary);
StringBuilder curBuilder;
ExpressionGenerator curGenerator;
if (doNeedExpression && wrap) {
curBuilder = new StringBuilder();
curGenerator = new ExpressionGenerator(curBuilder, context);
} else {
curBuilder = builder;
curGenerator = this;
}
boolean shouldInsertParentheses = !wrap && doNeedExpression;
if (shouldInsertParentheses) {
curBuilder.append('(');
}
operand.accept(curGenerator);
if (wrap) {
curBuilder.append(".set(");
} else {
curBuilder.append(" = ");
}
if (shouldNotReplaceOperatorWithMethod(operand.getType(), null, unary.getOperationTokenType())) {
writeSimpleUnary((GrExpression) operand.copy(), unary, curGenerator);
} else {
curGenerator.invokeMethodOn(method, (GrExpression) operand.copy(), GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), unary);
}
if (shouldInsertParentheses) {
curBuilder.append(')');
}
if (wrap) {
curBuilder.append(')');
if (doNeedExpression) {
curBuilder.append(';');
context.myStatements.add(curBuilder.toString());
operand.accept(this);
builder.append(".get()");
}
}
}
return true;
}
Aggregations