use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class ExtractUtil method mustAddVariableDeclaration.
/*
To declare or not a variable to which method call result will be assigned.
*/
private static List<VariableInfo> mustAddVariableDeclaration(@NotNull GrStatement[] statements, @NotNull VariableInfo[] vars) {
Map<String, VariableInfo> names = new HashMap<>();
for (VariableInfo var : vars) {
names.put(var.getName(), var);
}
List<VariableInfo> result = new ArrayList<>();
for (GrStatement statement : statements) {
if (statement instanceof GrVariableDeclaration) {
GrVariableDeclaration declaration = (GrVariableDeclaration) statement;
for (GrVariable variable : declaration.getVariables()) {
final VariableInfo removed = names.remove(variable.getName());
if (removed != null) {
result.add(removed);
}
}
}
}
for (String varName : names.keySet()) {
if (ResolveUtil.resolveProperty(statements[statements.length - 1], varName) == null) {
result.add(names.get(varName));
}
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class ExpressionGenerator method initializeField.
private void initializeField(String varName, PsiType type, PsiClass resolved, PsiSubstitutor substitutor, String fieldName, GrExpression expression) {
StringBuilder builder = new StringBuilder();
final PsiMethod setter = GroovyPropertyUtils.findPropertySetter(resolved, fieldName, false, true);
if (setter != null) {
final GrVariableDeclaration var = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", type, varName);
final GrReferenceExpression caller = factory.createReferenceExpressionFromText(varName, var);
invokeMethodOn(setter, caller, new GrExpression[] { expression }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, substitutor, expression);
} else {
builder.append(varName).append('.').append(fieldName).append(" = ");
expression.accept(new ExpressionGenerator(builder, context));
}
context.myStatements.add(builder.toString());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class ClassGenerator method writeMembers.
public void writeMembers(StringBuilder text, PsiClass typeDefinition) {
if (typeDefinition instanceof GrEnumTypeDefinition) {
final GrEnumConstant[] enumConstants = ((GrEnumTypeDefinition) typeDefinition).getEnumConstants();
for (GrEnumConstant constant : enumConstants) {
classItemGenerator.writeEnumConstant(text, constant);
text.append(',');
}
if (enumConstants.length > 0) {
//text.removeFromTheEnd(1).append(";\n");
text.delete(text.length() - 1, text.length());
}
text.append(";\n");
}
writeAllMethods(text, classItemGenerator.collectMethods(typeDefinition), typeDefinition);
if (typeDefinition instanceof GrTypeDefinition) {
for (GrMembersDeclaration declaration : ((GrTypeDefinition) typeDefinition).getMemberDeclarations()) {
if (declaration instanceof GrVariableDeclaration) {
classItemGenerator.writeVariableDeclarations(text, (GrVariableDeclaration) declaration);
}
}
for (PsiClass inner : typeDefinition.getInnerClasses()) {
writeTypeDefinition(text, inner, false, false);
text.append('\n');
}
}
classItemGenerator.writePostponed(text, typeDefinition);
}
Aggregations