use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature 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.signatures.GrClosureSignature in project intellij-community by JetBrains.
the class ExpressionGenerator method invokeMethodOn.
public void invokeMethodOn(@NotNull PsiMethod method, @Nullable GrExpression caller, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] closures, @NotNull PsiSubstitutor substitutor, @NotNull GroovyPsiElement context) {
if (method instanceof GrGdkMethod) {
if (CustomMethodInvocator.invokeMethodOn(this, (GrGdkMethod) method, caller, exprs, namedArgs, closures, substitutor, context))
return;
GrExpression[] newArgs = new GrExpression[exprs.length + 1];
System.arraycopy(exprs, 0, newArgs, 1, exprs.length);
if (method.hasModifierProperty(PsiModifier.STATIC)) {
newArgs[0] = factory.createExpressionFromText("null");
} else {
if (caller == null) {
caller = factory.createExpressionFromText("this", context);
}
newArgs[0] = caller;
}
invokeMethodOn(((GrGdkMethod) method).getStaticMethod(), null, newArgs, namedArgs, closures, substitutor, context);
return;
}
if (method.hasModifierProperty(PsiModifier.STATIC) && caller == null) {
final PsiClass containingClass = method.getContainingClass();
if (containingClass != null && !PsiTreeUtil.isAncestor(containingClass, context, true)) {
builder.append(containingClass.getQualifiedName()).append('.');
}
} else {
if (caller != null) {
final boolean castNeeded = GenerationUtil.isCastNeeded(caller, method, this.context);
if (castNeeded) {
writeCastForMethod(caller, method, context);
}
caller.accept(this);
if (castNeeded) {
builder.append(')');
}
builder.append('.');
}
}
builder.append(method.getName());
final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, substitutor);
new ArgumentListGenerator(builder, this.context).generate(signature, exprs, namedArgs, closures, context);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature 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}");
}
}
Aggregations