use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyPsiElementFactoryImpl method createParameterList.
@NotNull
@Override
public PsiParameterList createParameterList(@NotNull @NonNls String[] names, @NotNull PsiType[] types) throws IncorrectOperationException {
StringBuilder builder = StringBuilderSpinAllocator.alloc();
builder.append("def foo(");
for (int i = 0; i < names.length; i++) {
String name = names[i];
final PsiType type = types[i];
if (type != null) {
builder.append(type.getCanonicalText());
builder.append(' ');
}
builder.append(name);
builder.append(',');
}
if (names.length > 0) {
builder.delete(builder.length() - 1, builder.length());
}
builder.append("){}");
final GrMethod method = createMethodFromText(builder);
return method.getParameterList();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyPsiElementFactoryImpl method createMethod.
@NotNull
@Override
public GrMethod createMethod(@NotNull @NonNls String name, PsiType returnType, PsiElement context) throws IncorrectOperationException {
StringBuilder builder = StringBuilderSpinAllocator.alloc();
try {
builder.append("def <T>");
if (returnType != null) {
builder.append(returnType.getCanonicalText());
}
builder.append(' ');
if (GroovyNamesUtil.isIdentifier(name)) {
builder.append(name);
} else {
builder.append('"');
builder.append(GrStringUtil.escapeSymbolsForGString(name, true, false));
builder.append('"');
}
builder.append("(){}");
GrMethod method = createMethodFromText(builder.toString(), context);
PsiTypeParameterList typeParameterList = method.getTypeParameterList();
assert typeParameterList != null;
typeParameterList.getFirstChild().delete();
typeParameterList.getFirstChild().delete();
typeParameterList.getFirstChild().delete();
if (returnType != null) {
method.getModifierList().setModifierProperty(GrModifier.DEF, false);
}
return method;
} finally {
StringBuilderSpinAllocator.dispose(builder);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrParameterImpl method isMainMethodFirstUntypedParameter.
private boolean isMainMethodFirstUntypedParameter() {
if (getTypeElementGroovy() != null)
return false;
if (!(getParent() instanceof GrParameterList))
return false;
if (isOptional())
return false;
GrParameterList parameterList = (GrParameterList) getParent();
if (!(parameterList.getParent() instanceof GrMethod))
return false;
GrMethod method = (GrMethod) parameterList.getParent();
return PsiImplUtil.isMainMethod(method);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyPropertyUtils method generateSetterPrototype.
public static GrMethod generateSetterPrototype(PsiField field) {
Project project = field.getProject();
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
String name = field.getName();
boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
VariableKind kind = codeStyleManager.getVariableKind(field);
String propertyName = codeStyleManager.variableNameToPropertyName(name, kind);
String setName = getSetterName(field.getName());
final PsiClass containingClass = field.getContainingClass();
try {
GrMethod setMethod = factory.createMethod(setName, PsiType.VOID);
String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
final PsiType type = field instanceof GrField ? ((GrField) field).getDeclaredType() : field.getType();
GrParameter param = factory.createParameter(parameterName, type);
annotateWithNullableStuff(field, param);
setMethod.getParameterList().add(param);
PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);
@NonNls StringBuilder builder = new StringBuilder();
if (name.equals(parameterName)) {
if (!isStatic) {
builder.append("this.");
} else {
String className = containingClass.getName();
if (className != null) {
builder.append(className);
builder.append(".");
}
}
}
builder.append(name);
builder.append("=");
builder.append(parameterName);
builder.append("\n");
GrCodeBlock body = factory.createMethodBodyFromText(builder.toString());
setMethod.getBlock().replace(body);
return setMethod;
} catch (IncorrectOperationException e) {
LOG.error(e);
return null;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrTypeParameterListImpl method appendParenthesesIfNeeded.
private void appendParenthesesIfNeeded() {
PsiElement first = getFirstChild();
if (first == null) {
getNode().addLeaf(GroovyTokenTypes.mLT, "<", null);
}
PsiElement last = getLastChild();
if (last.getNode().getElementType() != GroovyTokenTypes.mGT) {
getNode().addLeaf(GroovyTokenTypes.mGT, ">", null);
}
PsiElement parent = getParent();
if (parent instanceof GrMethod) {
GrModifierList list = ((GrMethod) parent).getModifierList();
PsiElement[] modifiers = list.getModifiers();
if (modifiers.length == 0) {
list.setModifierProperty(GrModifier.DEF, true);
}
}
}
Aggregations