IntelliJ IDEA 2020.1.2 - @Newify(pattern=...) supported :-)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
MG
Reply | Threaded
Open this post in threaded view
|

IntelliJ IDEA 2020.1.2 - @Newify(pattern=...) supported :-)

MG
Hi Groovy users,

just had time to check this out: The most current  IntelliJ build (Version: 2020.1.2, Build: 201.7846.76) from the 3rd of June finally contains support for the @Newify pattern parameter, which allows instance creation without the need for the new keyword, such as e.g. in Python G-)

Thank you again to everyone who upvoted, and to the people at Jetbrains who greenlighted/implemented this :-)

Cheers,
mg


Sample code:
@Newify(pattern=/[A-Z][A-Za-z0-9_]*/) // Instances of classes whose name conforms to the given regex pattern can be created without the need for the "new"-operator.
@CompileStatic // works under static and dynamic compilation
class NewifyTest {

    @Canonical static class A { String a }
    @Canonical class AB { String a; String b }
    @Canonical class ABC { String a; String b; String c }

    //@CompileStatic
    List createClassList() {
        listToResultList( [
                A('2018-04-08'),
                StringBuilder('*lol*'),
                AB("I am", "class AB"),
                ABC("A","B","C"),
                Object(),
                Reference(), Binding(), Double(123.456d), Integer(987), BigInteger('987654321',10),
                BigDecimal('1234.5678')
        ])
    }

    List<List<String>> listToResultList(List l) {
        [ l.collect { it.getClass().getName() }, l.collect { it.toString().replaceAll(/@[a-f0-9]+\b/,'') } ]
    }


    @Test
    void newifyCheck() {
        final List<List<String>> resultList = createClassList()
        println "resultList=$resultList"

        final String result0 = resultList[0].join('\n')
        final String result1 = resultList[1].join('\n')

        final String fullyQualifiedThisPrefix = "${this.getClass().canonicalName}\$"

        final String expected0 = [
                "${fullyQualifiedThisPrefix}A",
                'java.lang.StringBuilder',
                "${fullyQualifiedThisPrefix}AB",
                "${fullyQualifiedThisPrefix}ABC",
                'java.lang.Object',
                'groovy.lang.Reference', 'groovy.lang.Binding', 'java.lang.Double', 'java.lang.Integer', 'java.math.BigInteger',
                'java.math.BigDecimal'
        ].join('\n')

        final expected1 = [
                "${fullyQualifiedThisPrefix}A(2018-04-08)",
                '*lol*',
                "${fullyQualifiedThisPrefix}AB(I am, class AB)",
                "${fullyQualifiedThisPrefix}ABC(A, B, C)",
                'java.lang.Object',
                'groovy.lang.Reference', 'groovy.lang.Binding', '123.456', '987', '987654321',
                '1234.5678'
        ].join('\n')

        assertEquals(result0, expected0)
        assertEquals(result1, expected1)
    }
}