Re: Newify classNamePattern support: Non-static inner classes - there is something to be said for this argument...
I have added support for non-static inner classes through automatically adding 'this' as the first argument for calls to inner class ctors for now. Cheers,mg Ursprüngliche Nachricht Von: MG Datum: 14.04.18 14:40 (GMT+01:00) An: dev@groovy.apache.org Betreff: Newify classNamePattern support: Non-static inner classes - there is something to be said for this argument... Passing the outer class' this reference explicitely to the non-static inner classes' ctors makes the test green (see below; evidently this is not necessary when using the new keyword). Any suggestions on how to best adapt the code to achieve this behavior ? Here is the Github of the current state of Newify classNamePattern support: https://github.com/mgroovy/groovy/commit/66431efbe748781d9d86f79454c40918e941f937#diff-e8608c32eafec3c224382636704a74de Cheers, mg @Test void testInnerClassesNewifyWithNamePattern() { final String script = """ import groovy.transform.Canonical import groovy.transform.CompileStatic import groovy.lang.Newify import groovy.transform.ASTTest import static org.codehaus.groovy.control.CompilePhase.SEMANTIC_ANALYSIS @Newify(classNamePattern=/[A-Z].*/) class Foo { @Canonical class A { String a } @Canonical class AB { String a; String b } @Canonical class ABC { String a; String b; String c } List createClassList() { //final l = [ A('2018-04-08'), AB("I am", "class AB"), ABC("A","B","C") ] final l = [ A(this, '2018-04-08'), AB(this, "I am", "class AB"), ABC(this, "A","B","C") ] [ l.collect { it.getClass().getCanonicalName() }, l.collect { it.toString() } ] } } final Foo foo = new Foo() foo.createClassList() """ println "script=|$script|" final List resultList = (List) evalScript(script) println "result=$resultList" assert resultList[0] == ['Foo.A', 'Foo.AB', 'Foo.ABC'] assert resultList[1] == ['Foo$A(2018-04-08)', 'Foo$AB(I am, class AB)', 'Foo$ABC(A, B, C)'] }
Newify classNamePattern support: Non-static inner classes - there is something to be said for this argument...
Passing the outer class' this reference explicitely to the non-static inner classes' ctors makes the test green (see below; evidently this is not necessary when using the new keyword). Any suggestions on how to best adapt the code to achieve this behavior ? Here is the Github of the current state of Newify classNamePattern support: https://github.com/mgroovy/groovy/commit/66431efbe748781d9d86f79454c40918e941f937#diff-e8608c32eafec3c224382636704a74de Cheers, mg @Test void testInnerClassesNewifyWithNamePattern() { final String script =""" import groovy.transform.Canonical import groovy.transform.CompileStatic import groovy.lang.Newify import groovy.transform.ASTTest import static org.codehaus.groovy.control.CompilePhase.SEMANTIC_ANALYSIS @Newify(classNamePattern=/[A-Z].*/) class Foo { @Canonical class A { String a } @Canonical class AB { String a; String b } @Canonical class ABC { String a; String b; String c } List createClassList() { //final l = [ A('2018-04-08'), AB("I am", "class AB"), ABC("A","B","C") ] final l = [ A(this, '2018-04-08'), AB(this, "I am", "class AB"), ABC(this, "A","B","C") ] [ l.collect { it.getClass().getCanonicalName() }, l.collect { it.toString() } ] } } final Foo foo = new Foo() foo.createClassList() """ println"script=|$script|" final List resultList = (List)evalScript(script) println"result=$resultList" assert resultList[0] == ['Foo.A','Foo.AB','Foo.ABC'] assert resultList[1] == ['Foo$A(2018-04-08)','Foo$AB(I am, class AB)','Foo$ABC(A, B, C)'] }
Newify classNamePattern support: Non-static inner classes ?
@Newify classNamePattern support (https://issues.apache.org/jira/browse/GROOVY-8490) looks to be working (I am using maps for lookup everywhere and have added globally available types explicitely in NewifyASTTransformation), except for non-static inner classes. The test below fails with: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: Foo$A(String) The same test with static inner classes (e.g. @Canonical static class A { String a }) is green. How do I make this work ? Or should we not support non-static inner classes for Newify with classNamePattern for now ? mg @Test void testInnerClassNewifyWithNamePattern() { final String script =""" import groovy.transform.Canonical import groovy.transform.CompileStatic import groovy.lang.Newify import groovy.transform.ASTTest import static org.codehaus.groovy.control.CompilePhase.SEMANTIC_ANALYSIS @Newify(classNamePattern=/[A-Z].*/) class Foo { @Canonical class A { String a } @Canonical class AB { String a; String b } @Canonical class ABC { String a; String b; String c } List createClassList() { final l = [ A('2018-04-08'), AB("I am", "class AB"), ABC("A","B","C") ] [ l.collect { it.getClass().getCanonicalName() }, l.collect { it.toString() } ] } } final Foo foo = new Foo() foo.createClassList() """ println"script=|$script|" final List resultList = (List)evalScript(script) println"result=$resultList" assert resultList[0] == ['Foo.A','Foo.AB','Foo.ABC'] assert resultList[1] == ['Foo$A(2018-04-08)','Foo$AB(I am, class AB)','Foo$ABC(A, B, C)'] }