You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by MG <mg...@arscreat.com> on 2020/06/14 16:07:07 UTC

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

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