Skip to content

Commit 891c35f

Browse files
committed
GROOVY-8245: @newify(auto=false) not transforming declarations (closes groovy#571)
1 parent 51f9294 commit 891c35f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/main/org/codehaus/groovy/transform/NewifyASTTransformation.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public Expression transform(Expression expr) {
178178
}
179179
} else if (expr instanceof DeclarationExpression) {
180180
DeclarationExpression de = (DeclarationExpression) expr;
181-
if (de == candidate || auto) {
181+
if (shouldTransform(de)) {
182182
candidate = null;
183183
Expression left = de.getLeftExpression();
184184
Expression right = transform(de.getRightExpression());
@@ -191,6 +191,14 @@ public Expression transform(Expression expr) {
191191
return expr.transformExpression(this);
192192
}
193193

194+
private boolean shouldTransform(DeclarationExpression exp) {
195+
return exp == candidate || auto || hasClassesToNewify();
196+
}
197+
198+
private boolean hasClassesToNewify() {
199+
return classesToNewify != null && !classesToNewify.getExpressions().isEmpty();
200+
}
201+
194202
private void newifyClass(ClassNode cNode, boolean autoFlag, ListExpression list) {
195203
String cName = cNode.getName();
196204
if (cNode.isInterface()) {

src/test/org/codehaus/groovy/transform/NewifyTransformTest.groovy

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,26 @@ class NewifyTransformTest extends GroovyShellTestCase {
189189
assert !new Z().foo()
190190
'''
191191
}
192+
193+
// GROOVY-8245
194+
void testDeclarationWhenAutoIsFalse() {
195+
assertScript '''
196+
class Foo {
197+
static int answer = 7
198+
Foo() {
199+
answer = 42
200+
}
201+
}
202+
@Newify(auto=false, value=Foo)
203+
class Bar {
204+
static {
205+
Foo foo = Foo()
206+
}
207+
static void method() {}
208+
}
209+
assert Foo.answer == 7
210+
Bar.method()
211+
assert Foo.answer == 42
212+
'''
213+
}
192214
}

0 commit comments

Comments
 (0)