Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The report of JavaOne2011 about groovy

The report of JavaOne2011 about groovy

Avatar for Yasuharu Nakano

Yasuharu Nakano

March 25, 2023
Tweet

More Decks by Yasuharu Nakano

Other Decks in Programming

Transcript

  1. $ time groovy -e 'println "Hello, world!";' Hello, world! real

    0m1.177s user 0m1.321s sys 0m0.171s $ time groovyclient -e 'println "Hello, world!";' Hello, world! real 0m0.029s user 0m0.001s sys 0m0.002s
  2. (SPPWZʙࣣͭͷಋೖύλʔϯ ສೳ઀ணࡎ 4VQFS(MVF ΍ΘΒ͔ͳ৺ଁ -JRVJE)FBSU  ϦοϓεςΟοΫ -JQTUJDL ಺ࢹڸखज़ ,FZIPMF4VSHFSZ

     ແ੍ݶͷղ์ 6OMJNJUFE0QFOOFTT ΰʔετϥΠλʔ (IPTU8SJUFS খਓ͞ΜεΫϦϓτ )PVTF&MG ೥౓൛
  3. (SPPWZίʔυ // this pipe generated by kiyotaka def url =

    'http://pipes.yahoo.com/pipes/pipe.run?_id=081f8f5039 194af0f25909129d6d007d&_render=rss' def items = new XmlParser().parse(url).channel.item def cols = 'pubDate title description'.tokenize() groovy.swing.SwingBuilder.build { frame(id:'f', title: 'Japanese Groovy Blogs', visible:true) { scrollPane { table { tableModel(list: items) { cols.each { col -> closureColumn header: col, read: { it[col].text() } } } } } } f.pack() }
  4. ϦοϓεςΟοΫ -JQTUJDL (SPPWZͷ͋ΒΏΔػೳɾϓϩμΫτΛ࢖ͬͯίʔυ ΛಡΈ΍͘͢͢Δ (%, (SPPWZ#FBOT (1BUI -JTUT .BQT $BMMBCMF

     9.- 42- $PNNBOEDIBJOT (1BST 4DSJQUPN ͳͲ ͳͲ ಛʹʮ%4-ʯ (SPPWZ͔Βɺ%4-͕ΑΓॻ͖΍͘͢ͳͬͨ ʢಛఆ৚݅ԼͰʣϝιουݺग़ͷׅހ͕লུͰ͖Δ
  5. ίʔυҰࣜ def invite(subject) { [ to : { target ->

    target.add(subject) } ] } def javaone = [] assert javaone == ["Nakano”, "<Your Name>"] invite(“You”).to(javaone)
  6. ·ͣ͸JOWJUFϝιουͷݺग़ def invite(subject) { [ to : { target ->

    target.add(subject) } ] } def javaone = [] assert javaone == ["Nakano”, "<Your Name>"] invite(“You”).to(javaone)
  7. ΫϩʔδϟΛؚΉ.BQΛฦͯ͠ def invite(subject) { [ to : { target ->

    target.add(subject) } ] } def javaone = [] assert javaone == ["Nakano”, "<Your Name>"] invite(“You”).to(javaone) Ωʔ͕lUPzɺ஋͕ΫϩʔδϟͷΤϯ τϦ͚ͩΛ࣋ͭ.BQ͕SFUVSO͞ΕΔ
  8. ͦͷΫϩʔδϟΛݺग़͢ʂ def invite(subject) { [ to : { target ->

    target.add(subject) } ] } def javaone = [] assert javaone == ["Nakano”, "<Your Name>"] invite(“You”).to(javaone) ͜ͷΫϩʔδϟͷݺग़ʹͳΔ
  9. ݁ہ͜͏͍͏͜ͱ def invite(subject) { [ to : { target ->

    target.add(subject) } ] } def javaone = [] assert javaone == ["Nakano”, "<Your Name>"] invite(“You”).to(javaone) ݁ہ͸͜Εˣ͕࣮ߦ͞Εͨͷͱಉ͡ javaone.add(“You”)
  10. ˏ-PH @groovy.util.logging.Log class Foo { def method() { log.info('info message')

    log.fine('fine message') log.severe('severe message') } } new Foo().method() $ groovy astLog.groovy Nov 9, 2011 1:09:10 PM java_util_logging_Logger$info call INFO: info message Nov 9, 2011 1:09:10 PM java_util_logging_Logger$severe call SEVERE: severe message
  11. ˏ*NNVUBCMF @Immutable class Foo { int x Date date =

    new Date(1000) } def a = new Foo(x:1) assert a.x == 1 assert a.date.getTime() == 1000 a.date.setTime(2000) assert a.date.getTime() == 1000 a.x = 3 ๷ޚతίϐʔ͞ΕΔͨ ΊɺΦϒδΣΫτͷঢ়ଶ Λมߋͯ͠΋ɺݩͷΦϒ δΣΫτʹӨڹ͸ͳ͍ ϑΟʔϧυʹ࠶୅ೖ͠Α͏ͱ͢Δͱ 3FBE0OMZ1SPQFSUZ&YDFQUJPO͕ൃੜ͢Δ
  12. ˏ4JOHMFUPO @Singleton class Foo { def hello() { "hello" }

    } assert Foo.instance.hello() == "hello" // => Caught: java.lang.RuntimeException: // Can't instantiate singleton Foo. Use Foo.instance new Foo() HFU*OTUBODF Λ௨ͯ͠औಘ͢Δ ࣗ෼ͰOFX͠Α͏ͱ͢Δͱ 3VOUJNF&YDFQUJPO͕ൃੜ͢Δ
  13. ˏ%FMFHBUF class StringList { @Delegate List<String> list = new ArrayList<String>()

    } def stringList = new StringList() stringList.add("A") stringList.add("B") assert stringList == ["A", "B"] stringList.each { print it } // => AB BEEϝιου͸ఆ͍ٛͯ͠ͳ͍͕ɺ ϑΟʔϧυͷMJTUʹҕৡ͞Ε͍ͯΔ
  14. ΦϦδφϧͷ"45ม׵΋ͭ͘ΕΔ @GroovyASTTransformationClass("org.jggug.kobo.gastah.HelloWorldASTTransformation") @Retention(RetentionPolicy.RUNTIME) public @interface HelloWorld { String value() }

    @GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION) class HelloWorldASTTransformation extends ClassCodeVisitorSupport implements ASTTransformation { SourceUnit sourceUnit // forced to override by super class public void visit(ASTNode[] nodes, SourceUnit source) { AnnotatedNode target = (AnnotatedNode) nodes[1] AnnotationNode includeAnnotation = (AnnotationNode) nodes[0] def additionNode = new AstBuilder().buildFromCode({ println "Hello, World!" })[0] def statements = additionNode.statements // ϒϩοΫ಺ͷ׬શϦϓϨʔε source.getAST().getStatementBlock().getStatements().clear() source.getAST().getStatementBlock().getStatements().addAll(0, statements) } } https://github.com/nobeans/jggug-20110617 !)FMMP8PSME ͙Β͍ͳΒ ͜ͷ͙Β͍ͷ ίʔυྔͰ0,