Skip to content

Commit

Permalink
Fix for #421: defaultsMode attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Dec 27, 2022
1 parent df14457 commit 874b8a3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.eclipse.jdt.groovy.core.tests.xform;

import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isAtLeastGroovy;
import static org.junit.Assume.assumeTrue;

import java.util.Map;

import org.eclipse.jdt.groovy.core.tests.basic.GroovyCompilerTestSuite;
Expand Down Expand Up @@ -336,4 +339,37 @@ public void testTupleConstructor12() {

runConformTest(sources);
}

@Test
public void testTupleConstructor13() {
assumeTrue(isAtLeastGroovy(40)); // "defaultsMode" added for Groovy 4
for (String value : new String[] {"groovy.transform.DefaultsMode.OFF", "DefaultsMode.OFF", "OFF"}) {
//@formatter:off
String[] sources = {
"Main.groovy",
"def p = new Person('Jane','Eyre')\n" +
"assert p.firstName == 'Jane'\n" +
"assert p.lastName == 'Eyre'\n",

"Person.groovy",
"import groovy.transform.DefaultsMode\n" +
"import static groovy.transform.DefaultsMode.*\n" +
"@groovy.transform.TupleConstructor(defaultsMode="+value+")\n" +
"class Person {\n" +
" String firstName, lastName\n" +
"}\n",
};
//@formatter:on

runConformTest(sources);

checkGCUDeclaration("Person.groovy", // expect single constructor
"class Person {\n" +
" private String firstName;\n" +
" private String lastName;\n" +
" public @groovy.transform.Generated Person(String firstName, String lastName) {\n" +
" }\n" +
"}\n");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ private static BlockStatement processNamedArgs(final ClassNode cNode, final Vari

private static DefaultsMode maybeDefaultsMode(final AnnotationNode node, final String name) {
if (node != null) {
final Expression member = node.getMember(name);
Expression member = node.getMember(name);
if (member instanceof ConstantExpression) {
ConstantExpression ce = (ConstantExpression) member;
if (ce.getValue() instanceof DefaultsMode) {
Expand All @@ -396,12 +396,22 @@ private static DefaultsMode maybeDefaultsMode(final AnnotationNode node, final S
} else if (member instanceof PropertyExpression) {
PropertyExpression prop = (PropertyExpression) member;
Expression oe = prop.getObjectExpression();
/* GRECLIPSE edit
if (oe instanceof ClassExpression) {
ClassExpression ce = (ClassExpression) oe;
if (ce.getType().getName().equals("groovy.transform.DefaultsMode")) {
return DefaultsMode.valueOf(prop.getPropertyAsString());
}
}
*/
if ("groovy.transform.DefaultsMode".equals(oe.getType().getName())
|| "groovy.transform.DefaultsMode".equals(oe.getText())
|| "DefaultsMode".equals(oe.getText())) {
return DefaultsMode.valueOf(prop.getPropertyAsString());
}
} else if (member instanceof VariableExpression) {
return DefaultsMode.valueOf(member.getText());
// GRECLIPSE end
}
}
return null;
Expand Down

0 comments on commit 874b8a3

Please sign in to comment.