use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class ArgumentListGenerator method generate.
public void generate(@Nullable GrClosureSignature signature, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] clArgs, @NotNull GroovyPsiElement context) {
GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = signature == null ? null : GrClosureSignatureUtil.mapParametersToArguments(signature, namedArgs, exprs, clArgs, context, false, false);
if (argInfos == null && signature != null) {
argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, namedArgs, exprs, clArgs, context, true, true);
}
final PsiSubstitutor substitutor = signature == null ? PsiSubstitutor.EMPTY : signature.getSubstitutor();
if (argInfos == null || NullUtils.hasNull(argInfos)) {
generateSimple(exprs, namedArgs, clArgs, context, substitutor);
return;
}
final GrClosureParameter[] params = signature.getParameters();
final Project project = context.getProject();
myBuilder.append('(');
boolean hasCommaAtEnd = false;
for (int i = 0; i < argInfos.length; i++) {
GrClosureSignatureUtil.ArgInfo<PsiElement> arg = argInfos[i];
if (arg == null)
continue;
final GrClosureParameter param = params[i];
boolean generated = arg.isMultiArg ? generateMultiArg(arg, param, substitutor, project, context) : generateSingeArg(arg, param);
if (generated) {
hasCommaAtEnd = true;
myBuilder.append(", ");
}
}
if (hasCommaAtEnd) {
myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
//myBuilder.removeFromTheEnd(2);
}
myBuilder.append(')');
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class ClassItemGeneratorImpl method writeMethod.
@Override
public void writeMethod(StringBuilder builder, PsiMethod method) {
if (method == null)
return;
GenerationUtil.writeDocComment(builder, method, true);
String name = method.getName();
boolean isAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT);
PsiModifierList modifierList = method.getModifierList();
final PsiClass containingClass = method.getContainingClass();
if (method.isConstructor() && containingClass != null && containingClass.isEnum()) {
ModifierListGenerator.writeModifiers(builder, modifierList, ModifierListGenerator.ENUM_CONSTRUCTOR_MODIFIERS);
} else {
ModifierListGenerator.writeModifiers(builder, modifierList);
}
if (method.hasTypeParameters()) {
GenerationUtil.writeTypeParameters(builder, method, classNameProvider);
builder.append(' ');
}
//append return type
if (!method.isConstructor()) {
PsiType retType = context.typeProvider.getReturnType(method);
TypeWriter.writeType(builder, retType, method, classNameProvider);
builder.append(' ');
}
builder.append(name);
if (method instanceof GroovyPsiElement) {
context.searchForLocalVarsToWrap((GroovyPsiElement) method);
}
GenerationUtil.writeParameterList(builder, method.getParameterList().getParameters(), classNameProvider, context);
if (method instanceof GrAnnotationMethod) {
GrAnnotationMemberValue defaultValue = ((GrAnnotationMethod) method).getDefaultValue();
if (defaultValue != null) {
builder.append("default ");
defaultValue.accept(new AnnotationGenerator(builder, context));
}
}
GenerationUtil.writeThrowsList(builder, method.getThrowsList(), getMethodExceptions(method), classNameProvider);
if (!isAbstract) {
/* ************ body ********* */
if (method instanceof GrMethod) {
if (method instanceof GrReflectedMethod && ((GrReflectedMethod) method).getSkippedParameters().length > 0) {
builder.append("{\n").append(generateDelegateCall((GrReflectedMethod) method)).append("\n}\n");
} else {
new CodeBlockGenerator(builder, context.extend()).generateMethodBody((GrMethod) method);
}
} else if (method instanceof GrAccessorMethod) {
writeAccessorBody(builder, method);
} else if (method instanceof LightMethodBuilder && containingClass instanceof GroovyScriptClass) {
if ("main".equals(method.getName())) {
writeMainScriptMethodBody(builder, method);
} else if ("run".equals(method.getName())) {
writeRunScriptMethodBody(builder, method);
}
} else {
builder.append("{//todo\n}");
}
} else {
builder.append(';');
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class SetterWriter method write.
public void write() {
final boolean isStatic = mySetter.hasModifierProperty(PsiModifier.STATIC);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myContext.project);
PsiParameter[] parameters = mySetter.getParameterList().getParameters();
PsiParameter parameter = parameters[parameters.length - 1];
final PsiType parameterType = myContext.typeProvider.getParameterType(parameter);
myBuffer.append("private static ");
processTypeParameters(parameterType);
myBuffer.append(myName);
if (!(parameterType instanceof PsiPrimitiveType)) {
parameter = factory.createParameter(parameter.getName(), "Value", null);
}
PsiParameter[] actual = inferActualParameters(isStatic, parameters, parameter);
final GroovyPsiElement place = createStubMethod(actual);
GenerationUtil.writeParameterList(myBuffer, actual, myClassNameProvider, myContext);
writeBody(isStatic, parameters, parameter, place);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class ClosureGenerator method getOwner.
@NonNls
@NotNull
private CharSequence getOwner(@NotNull GrClosableBlock closure) {
final GroovyPsiElement context = PsiTreeUtil.getParentOfType(closure, GrMember.class, GroovyFile.class);
LOG.assertTrue(context != null);
final PsiClass contextClass;
if (context instanceof GroovyFile) {
contextClass = ((GroovyFile) context).getScriptClass();
} else if (context instanceof PsiClass) {
contextClass = (PsiClass) context;
} else if (context instanceof GrMember) {
if (((GrMember) context).hasModifierProperty(PsiModifier.STATIC)) {
//no context class
contextClass = null;
} else {
contextClass = ((GrMember) context).getContainingClass();
}
} else {
contextClass = null;
}
if (contextClass == null)
return "null";
final PsiElement implicitClass = GenerationUtil.getWrappingImplicitClass(closure);
if (implicitClass == null) {
return "this";
} else {
final StringBuilder buffer = new StringBuilder();
GenerationUtil.writeThisReference(contextClass, buffer, this.context);
return buffer;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GenerationUtil method invokeMethodByResolveResult.
public static void invokeMethodByResolveResult(@Nullable GrExpression caller, @NotNull GroovyResolveResult resolveResult, @NotNull String methodName, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] closureArgs, @NotNull ExpressionGenerator expressionGenerator, @NotNull GroovyPsiElement psiContext) {
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod) {
final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
expressionGenerator.invokeMethodOn(((PsiMethod) resolved), caller, exprs, namedArgs, closureArgs, substitutor, psiContext);
return;
}
//other case
final StringBuilder builder = expressionGenerator.getBuilder();
final ExpressionContext expressionContext = expressionGenerator.getContext();
if (caller != null) {
caller.accept(expressionGenerator);
builder.append('.');
}
builder.append(methodName);
final ArgumentListGenerator argumentListGenerator = new ArgumentListGenerator(builder, expressionContext);
argumentListGenerator.generate(null, exprs, namedArgs, closureArgs, psiContext);
}
Aggregations