GrVariableDeclaration (@NotNull String name, @NotNull String typeText) { String classText; if (Character.isLowerCase(typeText.charAt(0))) { classText = "class A { def " + typeText + " " + name + "}"; } else { classText = "class A { " + typeText + " " + name + "}"; } GroovyFileBase file = createGroovyFileChecked(classText); final GrTypeDefinitionBody body = file.getTypeDefinitions()[0].getBody(); return (GrVariableDeclaration) body.getMemberDeclarations()[0]; }