Skip to content

Commit

Permalink
Fix for issue #278: enum constant annotations not handled properly
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 6, 2017
1 parent f574d59 commit 3bff642
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1230,32 +1230,18 @@ public void testEnumPositions_GRE1072() {
" GREEN,\n"+
" BLUE\n"+
"}\n",
},"");
});

GroovyCompilationUnitDeclaration decl = getCUDeclFor("X.groovy");

FieldDeclaration fDecl = null;
FieldDeclaration fDecl = grabField(decl, "RED");
assertEquals("RED sourceStart>sourceEnd:30>32 declSourceStart>declSourceEnd:15>32 modifiersSourceStart=30 endPart1Position:30", stringifyFieldDecl(fDecl));

fDecl = grabField(decl,"RED");
if (GroovyUtils.GROOVY_LEVEL<18) {
assertEquals("RED sourceStart>sourceEnd:30>32 declSourceStart>declSourceEnd:30>31 modifiersSourceStart=0 endPart1Position:30",stringifyFieldDecl(fDecl));
} else {
assertEquals("RED sourceStart>sourceEnd:30>32 declSourceStart>declSourceEnd:15>31 modifiersSourceStart=30 endPart1Position:30",stringifyFieldDecl(fDecl));
fDecl = grabField(decl, "GREEN");
assertEquals("GREEN sourceStart>sourceEnd:37>41 declSourceStart>declSourceEnd:37>41 modifiersSourceStart=37 endPart1Position:37", stringifyFieldDecl(fDecl));

}

fDecl = grabField(decl,"GREEN");
if (GroovyUtils.GROOVY_LEVEL<18) {
assertEquals("GREEN sourceStart>sourceEnd:37>41 declSourceStart>declSourceEnd:37>40 modifiersSourceStart=0 endPart1Position:37",stringifyFieldDecl(fDecl));
} else {
assertEquals("GREEN sourceStart>sourceEnd:37>41 declSourceStart>declSourceEnd:37>40 modifiersSourceStart=37 endPart1Position:37",stringifyFieldDecl(fDecl));
}
fDecl = grabField(decl,"BLUE");
if (GroovyUtils.GROOVY_LEVEL<18) {
assertEquals("BLUE sourceStart>sourceEnd:46>49 declSourceStart>declSourceEnd:46>48 modifiersSourceStart=0 endPart1Position:46",stringifyFieldDecl(fDecl));
} else {
assertEquals("BLUE sourceStart>sourceEnd:46>49 declSourceStart>declSourceEnd:46>48 modifiersSourceStart=46 endPart1Position:46",stringifyFieldDecl(fDecl));
}
fDecl = grabField(decl, "BLUE");
assertEquals("BLUE sourceStart>sourceEnd:46>49 declSourceStart>declSourceEnd:46>49 modifiersSourceStart=46 endPart1Position:46", stringifyFieldDecl(fDecl));
}

public void testEnumValues_GRE1071() {
Expand Down Expand Up @@ -1785,8 +1771,8 @@ public void testInvalidScripts_GRE323_1b() {
// def x
// ^
public void testInvalidScripts_GRE323_2() {
if (GroovyUtils.GROOVY_LEVEL<18) {
runNegativeTest(new String[] {
// command expression syntax now allows this but it looks weird as
runConformTest(new String[] {
"One.groovy",
"def moo(closure) {\n" +
" closure();\n" +
Expand All @@ -1799,31 +1785,7 @@ public void testInvalidScripts_GRE323_2() {
" def secBoardRep = session2.\n" +
" def x\n" +
"}\n"
},
"----------\n" +
"1. ERROR in One.groovy (at line 10)\n" +
"\tdef x\n" +
"\t ^\n" +
"Groovy:expecting \'}\', found \'x\' @ line 10, column 7.\n" +
"----------\n");
} else {
// command expression syntax now allows this but it looks weird as
runConformTest(new String[] {
"One.groovy",
"def moo(closure) {\n" +
" closure();\n" +
"}\n" +
"\n" +
"moo {\n" +
" final session2 = null\n" +
" \n" +
" // Define scenarios\n" +
" def secBoardRep = session2.\n" +
" def x\n" +
"}\n"
},
"");
}
});
}

// removed surrounding method
Expand Down Expand Up @@ -1997,8 +1959,7 @@ public void testInvalidScripts_GRE323_5b() {
}

public void testInvalidScripts_GRE323_6() {
if (GroovyUtils.GROOVY_LEVEL<18) {
runNegativeTest(new String[] {
runConformTest(new String[] {
"Six.groovy",
"def moo(closure) {\n" +
" closure();\n" +
Expand All @@ -2011,29 +1972,7 @@ public void testInvalidScripts_GRE323_6() {
" // Define scenarios\n" +
" final y = session2.def x\n" +
"}\n"
},
"----------\n" +
"1. ERROR in Six.groovy (at line 10)\n" +
"\tfinal y = session2.def x\n" +
"\t ^\n" +
"Groovy:expecting \'}\', found \'x\' @ line 10, column 26.\n" +
"----------\n");
} else {
runConformTest(new String[] {
"Six.groovy",
"def moo(closure) {\n" +
" closure();\n" +
"}\n" +
"\n" +
"moo {\n" +
" final session2 = [\"def\": { println \"DEF\" }]\n" +
" \n" +
" final x = 1\n"+
" // Define scenarios\n" +
" final y = session2.def x\n" +
"}\n"
},"DEF");
}
},"DEF");
}

public void testBridgeMethods_GRE336() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -974,34 +974,25 @@ protected void enumConstantDef(AST node) {
ListExpression le = new ListExpression();
le.addExpression(init);
init = le;
}
}
}
}

// GRECLIPSE: start
/*old{
EnumHelper.addEnumConstant(classNode, identifier, init);
}new */
GroovySourceAST groovySourceAST = (GroovySourceAST) node;
int nameStart = locations.findOffset(groovySourceAST.getLine(), groovySourceAST.getColumn());
int nameEnd = nameStart + identifier.length()-1;

ClassNode fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue = ClassHelper.make(classNode.getName());
fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue.setRedirect(classNode);

FieldNode fn =
// end
EnumHelper.addEnumConstant(fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue, classNode, identifier, init, savedLine, savedColumn);
// GRECLIPSE: start
// GRECLIPSE edit
//FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
//enumField.addAnnotations(annotations);
//configureAST(enumField, node);
ClassNode nonDeclaredTypeOfEnumValue =
ClassHelper.make(classNode.getName());
nonDeclaredTypeOfEnumValue.setRedirect(classNode);
FieldNode fn = EnumHelper.addEnumConstant(nonDeclaredTypeOfEnumValue, classNode, identifier, init, savedLine, savedColumn);
fn.setNameStart(locations.findOffset(savedLine, savedColumn));
fn.setNameEnd(fn.getNameStart() + identifier.length() - 1);
fn.addAnnotations(annotations);
configureAST(fn, node);
fn.setNameStart(nameStart);
fn.setNameEnd(nameEnd);
fn.setStart(nameStart);
fn.setEnd(nameEnd);
// end
// GRECLIPSE end
enumConstantBeingDef = false;
}

protected void throwsList(AST node, List<ClassNode> list) {
String name;
if (isType(DOT, node)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,42 +979,25 @@ protected void enumConstantDef(AST node) {
ListExpression le = new ListExpression();
le.addExpression(init);
init = le;
}
}
}
}

// GRECLIPSE: start
/*old{
EnumHelper.addEnumConstant(classNode, identifier, init);
// looks like this is 3 lines in groovy 2.2. now: do we care?
FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
enumField.addAnnotations(annotations);
configureAST(enumField, node);
}new */
GroovySourceAST groovySourceAST = (GroovySourceAST) node;
int nameStart = locations.findOffset(groovySourceAST.getLine(), groovySourceAST.getColumn());
int nameEnd = nameStart + identifier.length()-1;

ClassNode fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue = ClassHelper.make(classNode.getName());
fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue.setRedirect(classNode);

FieldNode fn =
// end
EnumHelper.addEnumConstant(fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue, classNode, identifier, init, savedLine, savedColumn);
// GRECLIPSE: start
// GRECLIPSE edit
//FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
//enumField.addAnnotations(annotations);
//configureAST(enumField, node);
ClassNode nonDeclaredTypeOfEnumValue =
ClassHelper.make(classNode.getName());
nonDeclaredTypeOfEnumValue.setRedirect(classNode);
FieldNode fn = EnumHelper.addEnumConstant(nonDeclaredTypeOfEnumValue, classNode, identifier, init, savedLine, savedColumn);
fn.setNameStart(locations.findOffset(savedLine, savedColumn));
fn.setNameEnd(fn.getNameStart() + identifier.length() - 1);
fn.addAnnotations(annotations);
configureAST(fn, node);
fn.setNameStart(nameStart);
fn.setNameEnd(nameEnd);
fn.setStart(nameStart);
fn.setEnd(nameEnd);
// end
// GRECLIPSE end
enumConstantBeingDef = false;
}

protected void throwsList(AST node, List<ClassNode> list) {
String name;
if (isType(DOT, node)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -986,54 +986,37 @@ protected void enumConstantDef(AST node) {
ListExpression le = new ListExpression();
le.addExpression(init);
init = le;
}
}
}
}

// GRECLIPSE: start
/*old{
EnumHelper.addEnumConstant(classNode, identifier, init);
// looks like this is 3 lines in groovy 2.2. now: do we care?
FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
enumField.addAnnotations(annotations);
configureAST(enumField, node);
}new */
GroovySourceAST groovySourceAST = (GroovySourceAST) node;
int nameStart = locations.findOffset(groovySourceAST.getLine(), groovySourceAST.getColumn());
int nameEnd = nameStart + identifier.length()-1;

ClassNode fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue = ClassHelper.make(classNode.getName());
fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue.setRedirect(classNode);

FieldNode fn =
// end
EnumHelper.addEnumConstant(fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue, classNode, identifier, init, savedLine, savedColumn);
// GRECLIPSE: start
// GRECLIPSE edit
//FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
//enumField.addAnnotations(annotations);
//configureAST(enumField, node);
ClassNode nonDeclaredTypeOfEnumValue =
ClassHelper.make(classNode.getName());
nonDeclaredTypeOfEnumValue.setRedirect(classNode);
FieldNode fn = EnumHelper.addEnumConstant(nonDeclaredTypeOfEnumValue, classNode, identifier, init, savedLine, savedColumn);
fn.setNameStart(locations.findOffset(savedLine, savedColumn));
fn.setNameEnd(fn.getNameStart() + identifier.length() - 1);
fn.addAnnotations(annotations);
configureAST(fn, node);
fn.setNameStart(nameStart);
fn.setNameEnd(nameEnd);
fn.setStart(nameStart);
fn.setEnd(nameEnd);
// end
// GRECLIPSE end
enumConstantBeingDef = false;
}

protected void throwsList(AST node, List<ClassNode> list) {
String name;
if (isType(DOT, node)) {
name = qualifiedName(node);
} else {
name = identifier(node);
}
ClassNode exception = ClassHelper.make(name);
configureAST(exception, node);
list.add(exception);
AST next = node.getNextSibling();
if (next!=null) throwsList(next, list);
String name;
if (isType(DOT, node)) {
name = qualifiedName(node);
} else {
name = identifier(node);
}
ClassNode exception = ClassHelper.make(name);
configureAST(exception, node);
list.add(exception);
AST next = node.getNextSibling();
if (next!=null) throwsList(next, list);
}

protected void methodDef(AST methodDef) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -967,20 +967,15 @@ protected void enumConstantDef(AST node) {
}
// GRECLIPSE edit
//FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
//enumField.addAnnotations(annotations);
//configureAST(enumField, node);
GroovySourceAST groovySourceAST = (GroovySourceAST) node;
int nameStart = locations.findOffset(groovySourceAST.getLine(), groovySourceAST.getColumn());
int nameEnd = nameStart + identifier.length() - 1;
ClassNode fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue = ClassHelper.make(classNode.getName());
fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue.setRedirect(classNode);
FieldNode fn = EnumHelper.addEnumConstant(fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue, classNode, identifier, init, savedLine, savedColumn);
configureAST(fn, node);
fn.setNameStart(nameStart);
fn.setNameEnd(nameEnd);
fn.setStart(nameStart);
fn.setEnd(nameEnd);
ClassNode nonDeclaredTypeOfEnumValue =
ClassHelper.make(classNode.getName());
nonDeclaredTypeOfEnumValue.setRedirect(classNode);
FieldNode enumField = EnumHelper.addEnumConstant(nonDeclaredTypeOfEnumValue, classNode, identifier, init, savedLine, savedColumn);
enumField.setNameStart(locations.findOffset(savedLine, savedColumn));
enumField.setNameEnd(enumField.getNameStart() + identifier.length() - 1);
// GRECLIPSE end
enumField.addAnnotations(annotations);
configureAST(enumField, node);
enumConstantBeingDef = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1014,20 +1014,15 @@ protected void enumConstantDef(AST node) {
}
// GRECLIPSE edit
//FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
//enumField.addAnnotations(annotations);
//configureAST(enumField, node);
GroovySourceAST groovySourceAST = (GroovySourceAST) node;
int nameStart = locations.findOffset(groovySourceAST.getLine(), groovySourceAST.getColumn());
int nameEnd = nameStart + identifier.length() - 1;
ClassNode fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue = ClassHelper.make(classNode.getName());
fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue.setRedirect(classNode);
FieldNode fn = EnumHelper.addEnumConstant(fakeNodeToRepresentTheNonDeclaredTypeOfEnumValue, classNode, identifier, init, savedLine, savedColumn);
configureAST(fn, node);
fn.setNameStart(nameStart);
fn.setNameEnd(nameEnd);
fn.setStart(nameStart);
fn.setEnd(nameEnd);
ClassNode nonDeclaredTypeOfEnumValue =
ClassHelper.make(classNode.getName());
nonDeclaredTypeOfEnumValue.setRedirect(classNode);
FieldNode enumField = EnumHelper.addEnumConstant(nonDeclaredTypeOfEnumValue, classNode, identifier, init, savedLine, savedColumn);
enumField.setNameStart(locations.findOffset(savedLine, savedColumn));
enumField.setNameEnd(enumField.getNameStart() + identifier.length() - 1);
// GRECLIPSE end
enumField.addAnnotations(annotations);
configureAST(enumField, node);
enumConstantBeingDef = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2363,8 +2363,8 @@ private void fixupSourceLocationsForFieldDeclaration(FieldDeclaration fieldDecla

if (isEnumField) {
// they have no 'leading' type declaration or modifiers
fieldDeclaration.declarationSourceStart = doc == null ? fieldNode.getNameStart() : doc.sourceStart;
fieldDeclaration.declarationSourceEnd = fieldNode.getNameEnd() - 1;
fieldDeclaration.declarationSourceStart = doc == null ? fieldNode.getStart() : doc.sourceStart;
fieldDeclaration.declarationSourceEnd = fieldNode.getEnd() - 1;
} else {
fieldDeclaration.declarationSourceStart = doc == null ? fieldNode.getStart() : doc.sourceStart;
// the end of the fragment including initializer (and trailing ',')
Expand Down
Loading

0 comments on commit 3bff642

Please sign in to comment.