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

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

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

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 :-)


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 }

    List createClassList() {
        listToResultList( [
                AB("I am", "class AB"),
                Reference(), Binding(), Double(123.456d), Integer(987), BigInteger('987654321',10),

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

    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 = [
                'groovy.lang.Reference', 'groovy.lang.Binding', 'java.lang.Double', 'java.lang.Integer', 'java.math.BigInteger',

        final expected1 = [
                "${fullyQualifiedThisPrefix}AB(I am, class AB)",
                "${fullyQualifiedThisPrefix}ABC(A, B, C)",
                'groovy.lang.Reference', 'groovy.lang.Binding', '123.456', '987', '987654321',

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