use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ExpressionGenerator method visitUnaryExpression.
@Override
public void visitUnaryExpression(@NotNull GrUnaryExpression expression) {
final boolean postfix = expression.isPostfix();
final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(expression.multiResolve(false));
final PsiElement resolved = resolveResult.getElement();
final GrExpression operand = expression.getOperand();
IElementType opType = expression.getOperationTokenType();
if (resolved instanceof PsiMethod) {
if (opType == GroovyTokenTypes.mINC || opType == GroovyTokenTypes.mDEC) {
if (!postfix || expression.getParent() instanceof GrStatementOwner || expression.getParent() instanceof GrControlStatement) {
if (generatePrefixIncDec((PsiMethod) resolved, operand, expression))
return;
}
}
if (operand != null && shouldNotReplaceOperatorWithMethod(operand.getType(), null, expression.getOperationTokenType())) {
writeSimpleUnary(operand, expression, this);
} else {
if (opType == GroovyTokenTypes.mLNOT) {
builder.append('!');
}
invokeMethodOn(((PsiMethod) resolved), operand, GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), expression);
}
} else if (operand != null) {
if (postfix) {
operand.accept(this);
builder.append(expression.getOperationToken().getText());
} else {
builder.append(expression.getOperationToken().getText());
operand.accept(this);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ExpressionGenerator method visitReferenceExpression.
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
final GrExpression qualifier = referenceExpression.getQualifier();
final GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
final String referenceName = referenceExpression.getReferenceName();
if (PsiUtil.isThisOrSuperRef(referenceExpression)) {
writeThisOrSuperRef(referenceExpression, qualifier, referenceName);
return;
}
if (ResolveUtil.isClassReference(referenceExpression)) {
// just delegate to qualifier
LOG.assertTrue(qualifier != null);
qualifier.accept(this);
return;
}
if (resolved instanceof PsiClass) {
builder.append(((PsiClass) resolved).getQualifiedName());
if (PsiUtil.isExpressionUsed(referenceExpression)) {
builder.append(".class");
}
return;
}
//don't try to resolve local vars that are provided my this generator (they are listed in myUsedVarNames)
if (resolved == null && qualifier == null && context.myUsedVarNames.contains(referenceName)) {
builder.append(referenceName);
return;
}
//all refs in script that are not resolved are saved in 'binding' of the script
if (qualifier == null && (resolved == null || resolved instanceof GrBindingVariable || resolved instanceof LightElement && !(resolved instanceof ClosureSyntheticParameter)) && (referenceExpression.getParent() instanceof GrIndexProperty || !(referenceExpression.getParent() instanceof GrCall)) && PsiUtil.getContextClass(referenceExpression) instanceof GroovyScriptClass) {
final GrExpression thisExpr = factory.createExpressionFromText("this", referenceExpression);
thisExpr.accept(this);
builder.append(".getBinding().getProperty(\"").append(referenceExpression.getReferenceName()).append("\")");
return;
}
final IElementType type = referenceExpression.getDotTokenType();
GrExpression qualifierToUse = qualifier;
if (type == GroovyTokenTypes.mMEMBER_POINTER) {
LOG.assertTrue(qualifier != null);
builder.append("new ").append(GroovyCommonClassNames.ORG_CODEHAUS_GROOVY_RUNTIME_METHOD_CLOSURE).append('(');
qualifier.accept(this);
builder.append(", \"").append(referenceName).append("\")");
return;
}
if (type == GroovyTokenTypes.mOPTIONAL_DOT) {
LOG.assertTrue(qualifier != null);
String qualifierName = createVarByInitializer(qualifier);
builder.append('(').append(qualifierName).append(" == null ? null : ");
qualifierToUse = factory.createReferenceExpressionFromText(qualifierName, referenceExpression);
}
if (resolveResult.isInvokedOnProperty()) {
//property-style access to accessor (e.g. qual.prop should be translated to qual.getProp())
LOG.assertTrue(resolved instanceof PsiMethod);
LOG.assertTrue(GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) resolved));
invokeMethodOn(((PsiMethod) resolved), qualifierToUse, GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), referenceExpression);
} else {
if (qualifierToUse != null) {
qualifierToUse.accept(this);
builder.append('.');
}
if (resolved instanceof PsiNamedElement && !(resolved instanceof GrBindingVariable)) {
final String refName = ((PsiNamedElement) resolved).getName();
if (resolved instanceof GrVariable && context.analyzedVars.toWrap((GrVariable) resolved)) {
//this var should be wrapped by groovy.lang.Reference. so we add .get() tail.
builder.append(context.analyzedVars.toVarName((GrVariable) resolved));
if (!PsiUtil.isAccessedForWriting(referenceExpression)) {
builder.append(".get()");
}
} else {
builder.append(refName);
}
} else {
//unresolved reference
if (referenceName != null) {
if (PsiUtil.isAccessedForWriting(referenceExpression)) {
builder.append(referenceName);
} else {
PsiType stringType = PsiType.getJavaLangString(referenceExpression.getManager(), referenceExpression.getResolveScope());
PsiType qualifierType = PsiImplUtil.getQualifierType(referenceExpression);
GroovyResolveResult[] candidates = qualifierType != null ? ResolveUtil.getMethodCandidates(qualifierType, "getProperty", referenceExpression, stringType) : GroovyResolveResult.EMPTY_ARRAY;
final PsiElement method = PsiImplUtil.extractUniqueElement(candidates);
if (method != null) {
builder.append("getProperty(\"").append(referenceName).append("\")");
} else {
builder.append(referenceName);
}
}
} else {
final PsiElement nameElement = referenceExpression.getReferenceNameElement();
if (nameElement instanceof GrExpression) {
((GrExpression) nameElement).accept(this);
} else if (nameElement != null) {
builder.append(nameElement.toString());
}
}
}
}
if (type == GroovyTokenTypes.mOPTIONAL_DOT) {
builder.append(')');
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ExpressionGenerator method visitSafeCastExpression.
@Override
public void visitSafeCastExpression(@NotNull GrSafeCastExpression typeCastExpression) {
final GrExpression operand = (GrExpression) PsiUtil.skipParenthesesIfSensibly(typeCastExpression.getOperand(), false);
final GrTypeElement typeElement = typeCastExpression.getCastTypeElement();
if (operand instanceof GrListOrMap && ((GrListOrMap) operand).isMap() && typeElement != null) {
AnonymousFromMapGenerator.writeAnonymousMap((GrListOrMap) operand, typeElement, builder, context);
return;
}
final PsiType type = typeElement.getType();
if (operand instanceof GrListOrMap && !((GrListOrMap) operand).isMap() && type instanceof PsiArrayType) {
builder.append("new ");
final GrExpression[] initializers = ((GrListOrMap) operand).getInitializers();
if (initializers.length == 0) {
TypeWriter.writeTypeForNew(builder, ((PsiArrayType) type).getComponentType(), typeCastExpression);
builder.append("[0]");
} else {
TypeWriter.writeTypeForNew(builder, type, typeCastExpression);
builder.append('{');
for (GrExpression initializer : initializers) {
initializer.accept(this);
builder.append(", ");
}
if (initializers.length > 0) {
builder.delete(builder.length() - 2, builder.length());
//builder.removeFromTheEnd(2);
}
builder.append('}');
}
return;
}
final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(typeCastExpression.multiResolve(false));
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod) {
final GrExpression typeParam;
try {
typeParam = factory.createExpressionFromText(typeElement.getText(), typeCastExpression);
} catch (IncorrectOperationException e) {
generateCast(typeElement, operand);
return;
}
invokeMethodOn(((PsiMethod) resolved), operand, new GrExpression[] { typeParam }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), typeCastExpression);
} else {
generateCast(typeElement, operand);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ExpressionGenerator method hasFieldInitialization.
private static boolean hasFieldInitialization(GrNewExpression newExpression) {
final GrArgumentList argumentList = newExpression.getArgumentList();
if (argumentList == null)
return false;
if (argumentList.getNamedArguments().length == 0)
return false;
final GrCodeReferenceElement refElement = newExpression.getReferenceElement();
if (refElement == null)
return false;
final GroovyResolveResult resolveResult = newExpression.advancedResolve();
final PsiElement constructor = resolveResult.getElement();
if (constructor instanceof PsiMethod) {
return ((PsiMethod) constructor).getParameterList().getParametersCount() == 0;
}
final PsiElement resolved = refElement.resolve();
return resolved instanceof PsiClass;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ExpressionGenerator method visitConditionalExpression.
@Override
public void visitConditionalExpression(@NotNull GrConditionalExpression expression) {
final GrExpression condition = expression.getCondition();
final GrExpression thenBranch = expression.getThenBranch();
final GrExpression elseBranch = expression.getElseBranch();
final boolean elvis = expression instanceof GrElvisExpression;
final String var;
if (elvis) {
var = createVarByInitializer(condition);
} else {
var = null;
}
final PsiType type = condition.getType();
if (type == null || PsiType.BOOLEAN.equals(TypesUtil.unboxPrimitiveTypeWrapper(type))) {
if (elvis) {
builder.append(var);
} else {
condition.accept(this);
}
} else {
final GroovyResolveResult[] results = ResolveUtil.getMethodCandidates(type, "asBoolean", expression, PsiType.EMPTY_ARRAY);
final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(results);
GenerationUtil.invokeMethodByResolveResult(elvis ? factory.createReferenceExpressionFromText(var, expression) : condition, result, "asBoolean", GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, this, expression);
}
builder.append('?');
if (thenBranch != null) {
if (elvis) {
builder.append(var);
} else {
thenBranch.accept(this);
}
}
builder.append(':');
if (elseBranch != null) {
elseBranch.accept(this);
}
}
Aggregations