Groovy: Efficiency Oriented Programming
Lecture 7
Master Proteomics & Bioinformatics - University of Geneva
Alexandre Masselot - summer 2011
Contents

‣ Eclipse tips
‣ Productivity tip: back to TDD
‣ Exceptions throwing and catching
‣ reference to object and cloning
‣ OOP: interface
Eclipse tips

‣ STS (Springsource Tool Suite) dashboard:
  - keep aware of what is new in the STS environment (new releases, new
    plugins, technologies, future webinars...)
  - help > dashboard
Eclipse tips

‣ STS (Springsource Tool Suite) dashboard:
   - keep aware of what is new in the STS environment (new releases, new
     plugins, technologies, future webinars...)
   - help > dashboard
‣ Jump directly in junit view
   - run junit tests <alt> <shift> X T
   - jump to junit window <shift> <alt> Q U
   - configure your shortcuts with
    preferences > filter “keys” > filter junit
   - use multi screen to put console or junit view on the secondary display
     (drag the view tab and drop it on the secondary display)
4
Productivity : TDD

‣ Test Driven Development
Productivity : TDD

‣ Test Driven Development
‣ Even for a simple task (over 10 lines!)
   - write a simple class (in the same directory if you wish)
   - split the behavior of your problem into simple methods
   - test every method
Productivity : TDD

‣ Test Driven Development
‣ Even for a simple task (over 10 lines!)
   - write a simple class (in the same directory if you wish)
   - split the behavior of your problem into simple methods
   - test every method



       Do not rely on println to
 develop/understand your code
Exceptions: using & surviving




                                6
Exception: example
Exception: example
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method:
groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)

   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395)

   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315)

   at groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499)

   at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476)

   at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397)

   at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824)

   at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:
313)

   at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602)

   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16)

   at junit.framework.TestCase.runBare(TestCase.java:128)

   at junit.framework.TestResult$1.protect(TestResult.java:106)

   at junit.framework.TestResult.runProtected(TestResult.java:124)

   at junit.framework.TestResult.run(TestResult.java:109)

   at junit.framework.TestCase.run(TestCase.java:120)

   at junit.framework.TestSuite.runTest(TestSuite.java:230)

   at junit.framework.TestSuite.run(TestSuite.java:225)

   at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)

   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd()
is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
Exception: example
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method:
groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315)

   at   groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397)

   at   org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824)

   at   org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16)

   at   junit.framework.TestCase.runBare(TestCase.java:128)

   at   junit.framework.TestResult$1.protect(TestResult.java:106)

   at   junit.framework.TestResult.runProtected(TestResult.java:124)

   at   junit.framework.TestResult.run(TestResult.java:109)

   at   junit.framework.TestCase.run(TestCase.java:120)

   at   junit.framework.TestSuite.runTest(TestSuite.java:230)

   at   junit.framework.TestSuite.run(TestSuite.java:225)

   at   org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)

   at   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd()
is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)

   at   org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51)

   at   org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer
$_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64)

   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

   at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

   at   java.lang.reflect.Method.invoke(Method.java:597)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)

   at   groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)

   at   groovy.lang.Closure.call(Closure.java:276)

   at   groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194)

   at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

   at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

   at   java.lang.reflect.Method.invoke(Method.java:597)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
Exception: example                                                                                                           message
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method:
groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315)

   at   groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397)

   at   org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824)

   at   org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16)

   at   junit.framework.TestCase.runBare(TestCase.java:128)

   at   junit.framework.TestResult$1.protect(TestResult.java:106)

   at   junit.framework.TestResult.runProtected(TestResult.java:124)

   at   junit.framework.TestResult.run(TestResult.java:109)

   at   junit.framework.TestCase.run(TestCase.java:120)

   at   junit.framework.TestSuite.runTest(TestSuite.java:230)

   at   junit.framework.TestSuite.run(TestSuite.java:225)

   at   org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)

   at   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd()
is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)

   at   org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51)

   at   org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer
$_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64)

   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

   at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

   at   java.lang.reflect.Method.invoke(Method.java:597)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)

   at   groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)

   at   groovy.lang.Closure.call(Closure.java:276)

   at   groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194)

   at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

   at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

   at   java.lang.reflect.Method.invoke(Method.java:597)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
Exception: example                                                                                                                message
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method:
groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315)

   at   groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397)

   at   org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824)

   at   org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16)

   at   junit.framework.TestCase.runBare(TestCase.java:128)

   at   junit.framework.TestResult$1.protect(TestResult.java:106)

   at   junit.framework.TestResult.runProtected(TestResult.java:124)





    at
    at
    at
    at
    at
         junit.framework.TestResult.run(TestResult.java:109)
         junit.framework.TestCase.run(TestCase.java:120)
         junit.framework.TestSuite.runTest(TestSuite.java:230)
         junit.framework.TestSuite.run(TestSuite.java:225)
         org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
                                                                                                                             cause(s) cascade

   at   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd()
is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)

   at   org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51)

   at   org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer
$_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64)

   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

   at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

   at   java.lang.reflect.Method.invoke(Method.java:597)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)

   at   groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)

   at   groovy.lang.Closure.call(Closure.java:276)

   at   groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194)

   at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

   at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

   at   java.lang.reflect.Method.invoke(Method.java:597)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
Exception: example                                                                                                           message
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method:
groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315)

   at   groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397)

   at   org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824)

   at   org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16)

   at   junit.framework.TestCase.runBare(TestCase.java:128)

   at   junit.framework.TestResult$1.protect(TestResult.java:106)

   at   junit.framework.TestResult.runProtected(TestResult.java:124)





    at
    at
    at
    at
    at
         junit.framework.TestResult.run(TestResult.java:109)
         junit.framework.TestCase.run(TestCase.java:120)
         junit.framework.TestSuite.runTest(TestSuite.java:230)
         junit.framework.TestSuite.run(TestSuite.java:225)
                                                                                             exception name
         org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
                                                                                                            cause(s) cascade

   at   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd()
is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)

   at   org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51)

   at   org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer
$_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64)

   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

   at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

   at   java.lang.reflect.Method.invoke(Method.java:597)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)

   at   groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)

   at   groovy.lang.Closure.call(Closure.java:276)

   at   groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194)

   at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

   at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

   at   java.lang.reflect.Method.invoke(Method.java:597)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
Exception: example                                                                                                                    message
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method:
groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315)

   at   groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397)

   at   org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824)

   at   org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16)

   at   junit.framework.TestCase.runBare(TestCase.java:128)

   at   junit.framework.TestResult$1.protect(TestResult.java:106)

   at   junit.framework.TestResult.runProtected(TestResult.java:124)





    at
    at
    at
    at
    at
         junit.framework.TestResult.run(TestResult.java:109)
         junit.framework.TestCase.run(TestCase.java:120)
         junit.framework.TestSuite.runTest(TestSuite.java:230)
         junit.framework.TestSuite.run(TestSuite.java:225)
                                                                                             exception name
         org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
                                                                                                            cause(s) cascade

   at   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd()
is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)

   at   org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51)

   at   org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer
$_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64)

   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)




    at
    at
    at
    at
         sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         java.lang.reflect.Method.invoke(Method.java:597)
         org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
                                                                                                                      search for your packages

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)

   at   groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)

   at   groovy.lang.Closure.call(Closure.java:276)

   at   groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194)

   at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

   at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

   at   java.lang.reflect.Method.invoke(Method.java:597)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
Exception: example                                                                                                                    message
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method:
groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395)

   at   groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315)

   at   groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476)

   at   groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397)

   at   org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824)

   at   org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16)

   at   junit.framework.TestCase.runBare(TestCase.java:128)

   at   junit.framework.TestResult$1.protect(TestResult.java:106)

   at   junit.framework.TestResult.runProtected(TestResult.java:124)





    at
    at
    at
    at
    at
         junit.framework.TestResult.run(TestResult.java:109)
         junit.framework.TestCase.run(TestCase.java:120)
         junit.framework.TestSuite.runTest(TestSuite.java:230)
         junit.framework.TestSuite.run(TestSuite.java:225)
                                                                                             exception name
         org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
                                                                                                            cause(s) cascade

   at   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

   at   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd()
is applicable for argument types: () values: []
Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String)

   at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)

   at   org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51)

   at   org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)

   at   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)


   at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer
$_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64)

   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)




    at
    at
    at
    at
         sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         java.lang.reflect.Method.invoke(Method.java:597)
         org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
                                                                                                                      search for your packages

   at   groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)

   at   org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)


                                                                                               code line number

   at   groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)

   at   groovy.lang.Closure.call(Closure.java:276)

   at   groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194)

   at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

   at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

   at   java.lang.reflect.Method.invoke(Method.java:597)

   at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
Exceptions: surviving & using

‣ In exceptional situations, an event is thrown
 def list = ['a', 'b', 'c']
 def x = list[-5]
Exceptions: surviving & using

‣ In exceptional situations, an event is thrown
 def list = ['a', 'b', 'c']
 def x = list[-5]
‣ Output
   java.lang.ArrayIndexOutOfBoundsException:
   Negative array index [-5] too large for array size 3
 
   at ConsoleScript7.run(ConsoleScript7:3)
Exceptions: surviving & using

‣ In exceptional situations, an event is thrown
 def list = ['a', 'b', 'c']
 def x = list[-5]
‣ Output
   java.lang.ArrayIndexOutOfBoundsException:
   Negative array index [-5] too large for array size 3
 
   at ConsoleScript7.run(ConsoleScript7:3)
‣ An exception carries three types of information:

  - nature: through it’s class (extends Exception),
  - message: detailing the circumstances of the incident
  - stack trace: the stack of classes (possibly with line number) to track
    where the exception was thrown
Catching exceptions: try / catch

‣ An exception does not mean exiting the program (default behavior)
Catching exceptions: try / catch

‣ An exception does not mean exiting the program (default behavior)
‣ It is possible to catch an exception
 def list = ['10', 'a', '20', '30']
 [1, 2, 5, 3, 4].each{ i ->
         println "$it"+list[-i].toInteger()
 }
 println “coincoin”
Catching exceptions: try / catch

‣ An exception does not mean exiting the program (default behavior)
‣ It is possible to catch an exception   impossible!!!
 def list = ['10', 'a', '20', '30']
 [1, 2, 5, 3, 4].each{ i ->
         println "$it"+list[-i].toInteger()
 }
 println “coincoin”
Catching exceptions: try / catch

‣ An exception does not mean exiting the program (default behavior)
‣ It is possible to catch an exception   impossible!!!
 def list = ['10', 'a', '20', '30']
 [1, 2, 5, 3, 4].each{ i ->
         println "$it"+list[-i].toInteger()
 }
 println “coincoin”
‣ Output
 1
30
 2
20
 java.lang.ArrayIndexOutOfBoundsException: Negative array
 index [-5] too large for array size 4
 
 at ConsoleScript17$_run_closure1.doCall(ConsoleScript17:3)
 
 at ConsoleScript17.run(ConsoleScript17:2)
 and exit (with code > 0)
Catching exceptions: try / catch

‣ An exception does not mean exiting the program (default behavior)
‣ It is possible to catch an exception   impossible!!!
 def list = ['10', 'a', '20', '30']
 [1, 2, 5, 3, 4].each{ i ->
         println "$it"+list[-i].toInteger()
 }
 println “coincoin”
‣ Output
 1
30                          this line is never reached
 2
20
 java.lang.ArrayIndexOutOfBoundsException: Negative array
 index [-5] too large for array size 4
 
 at ConsoleScript17$_run_closure1.doCall(ConsoleScript17:3)
 
 at ConsoleScript17.run(ConsoleScript17:2)
 and exit (with code > 0)
Catching exceptions: try / catch
                           (cont’d)

‣ Surround the piece of code you want to secure with try/catch
 def list = ['10', 'a', '20', '30']
 try{
     [1, 2, 5, 3, 4].each{ i ->
         println "$it"+list[-i].toInteger()
     }
 }catch(Exception e){
    println "error: " + e.message
 }
 println “coincoin”
Catching exceptions: try / catch
                           (cont’d)

‣ Surround the piece of code you want to secure with try/catch
 def list = ['10', 'a', '20', '30']
 try{
     [1, 2, 5, 3, 4].each{ i ->
         println "$it"+list[-i].toInteger()
     }
 }catch(Exception e){
    println "error: " + e.message
 }
 println “coincoin”
‣ Output
 1
30
 2
20
 error: Negative array index [-5] too large for array size 4
 coincoin
Catching exceptions: try / catch
             (cont’d)



def list = ['10', 'a', '20', '30']
try{
    [1, 2, 5, 3, 4].each{ i ->
        println "$it"+list[-i].toInteger()
    }
}catch(Exception e){
   println "error: " + e.message
}
println “coincoin”
Catching exceptions: try / catch
                  (cont’d)



def list = ['10', 'a', '20', '30']
try{
    [1, 2, 5, 3, 4].each{ i ->
        println "$it"+list[-i].toInteger()
    }
}catch(Exception e){               exit the loop
   println "error: " + e.message
}
println “coincoin”
Goal: catch exceptions but continue the loop




                                               13
Catching exceptions: try / catch
   (cont’d)

‣ Adapt the depth of catching
Catching exceptions: try / catch
                                  (cont’d)

‣ Adapt the depth of catching
‣ In the following case, the iteration will continue after an exception is caught
 def list = ['10', 'a', '20', '30']
 [1, 2, 5, 3, 4].each{ i ->
    try{
         println "$it"+list[-i].toInteger()
    }catch(Exception e){
         println "error with ($i): "+e.message
    }
  }
Catching exceptions: try / catch
                                  (cont’d)

‣ Adapt the depth of catching
‣ In the following case, the iteration will continue after an exception is caught
 def list = ['10', 'a', '20', '30']
 [1, 2, 5, 3, 4].each{ i ->
    try{
         println "$it"+list[-i].toInteger()
    }catch(Exception e){
         println "error with ($i): "+e.message
    }
  }
‣ Output
 1
30
 2
20
 error with (5): Negative array index [-5] too        large for array size 4
 error with (3): For input string: "a"
 4
10
catch(Exception e)
is generic (all exceptions are caught)




                                         15
Goal: catch specific exceptions




                                  16
Catching exceptions: try / catch
                    (cont’d)

‣ You can catch only certain classes of Exception
 def list = ['10', 'a', '20', '30']
 [1, 2, 5, 3, 4].each{ i ->
    try{
         println "$it"+list[-i].toInteger()
         }catch(ArrayIndexOutOfBoundsException e){
           println "error with ($i): "+e.message
     }
 }
Catching exceptions: try / catch
                    (cont’d)

‣ You can catch only certain classes of Exception
 def list = ['10', 'a', '20', '30']
 [1, 2, 5, 3, 4].each{ i ->
    try{
         println "$it"+list[-i].toInteger()
         }catch(ArrayIndexOutOfBoundsException e){
           println "error with ($i): "+e.message
     }
 }

‣ Output
 1
30
 2
20
 error with (5): Negative array index [-5] too large for
 array size 4
 + unrecovered stack trace and exit because of
 java.lang.NumberFormatException
=> code exits
Catching exceptions: try / catch
                 (cont’d)

‣ Or even cascade catching
 def list = ['10', 'a', '20', '30']
 [1, 2, 5, 3, 4].each{ i ->
    try{
         println "$it"+list[-i].toInteger()
    }catch(ArrayIndexOutOfBoundsException e){
         println "out of bound!"
    }catch(NumberFormatException e){
         println "cannot to convert '${list[-i]}' to int"
    }
 }
Catching exceptions: try / catch
                 (cont’d)

‣ Or even cascade catching
 def list = ['10', 'a', '20', '30']
 [1, 2, 5, 3, 4].each{ i ->
    try{
         println "$it"+list[-i].toInteger()
    }catch(ArrayIndexOutOfBoundsException e){
         println "out of bound!"
    }catch(NumberFormatException e){
         println "cannot to convert '${list[-i]}' to int"
    }
 }
‣ Output
 1
30
 2
20
 out of bound!
 cannot to convert 'a' to int
 4
10
Throwing your Exception

‣ In some situation, it is powerful to throw an exception in your code (to be
  hopefully caught at a meaningful place...)
Throwing your Exception

‣ In some situation, it is powerful to throw an exception in your code (to be
  hopefully caught at a meaningful place...)
‣ Example:
  - you are in the third level of nested function when some illegal action happens,
  - you do not want to manage all the return statements of your functions up to
    the place where the high level action is to be undertaken
  - you want to carry a meaningful message
Throwing your Exception

‣ In some situation, it is powerful to throw an exception in your code (to be
  hopefully caught at a meaningful place...)
‣ Example:
  - you are in the third level of nested function when some illegal action happens,
  - you do not want to manage all the return statements of your functions up to
    the place where the high level action is to be undertaken
  - you want to carry a meaningful message
‣ Example
 int factorial(int n){
   if(n<0)
     throw new RuntimeException(“cannot pass negative value
 ($n)”)
   //continue
 }
Throwing your Exception

int factorial(int n){
  if(n<0)
     throw new RuntimeException(“cannot pass negative value
($n)”)
  //continue
}
def computeFactorial(int n){
  if(n>100)
      return -1
  factorial(n)
}
//Script
try{
   computeFactorial(i)
}catch(Exception e){....}
Throwing your Exception

int factorial(int n){
  if(n<0)
     throw new RuntimeException(“cannot pass negative value
($n)”)
  //continue
}
def computeFactorial(int n){
  if(n>100)
     throw new IllegalArgumentException(“no fact for >100”)
  factorial(n)
}
//Script
try{
   computeFactorial(i)
}catch(Exception e){....}
Throwing your Exception

‣ You code is more clear & robust:
  - 10 lines per function limits complex if / else construct.
Throwing your Exception

‣ You code is more clear & robust:
  - 10 lines per function limits complex if / else construct.
‣ Document the exception that can be thrown in your function header
  comment
 /**
 ...
 @Throws RuntimeException if argument is negative
 */
Throwing your Exception

‣ You code is more clear & robust:
  - 10 lines per function limits complex if / else construct.
‣ Document the exception that can be thrown in your function header
  comment
 /**
 ...
 @Throws RuntimeException if argument is negative
 */
‣ Make the message meaningful.
Throwing your Exception

‣ You code is more clear & robust:
  - 10 lines per function limits complex if / else construct.
‣ Document the exception that can be thrown in your function header
  comment
 /**
 ...
 @Throws RuntimeException if argument is negative
 */
‣ Make the message meaningful.
‣ You can create your own Exception class that extends whatever existing
  Exception:

  - to have generic behavior or catch only your own exception.
Copying an Object

‣ We want to copy a list, then modify its copy without altering the original
 def l_1 = ('a'..'e').toList()
 def l_2 = l_1

 l_2[2] = 'Z'
 println "l_1t$l_1"
 println "l_2t$l_2"
Copying an Object

‣ We want to copy a list, then modify its copy without altering the original
 def l_1 = ('a'..'e').toList()
 def l_2 = l_1

 l_2[2] = 'Z'
 println "l_1t$l_1"
 println "l_2t$l_2"
‣ Output
 l_1
 [a, b, Z, d, e]            // arghhh! we have a ‘Z’ in l_1
 l_2
 [a, b, Z, d, e]
Copying an Object

‣ We want to copy a list, then modify its copy without altering the original
 def l_1 = ('a'..'e').toList()
 def l_2 = l_1

 l_2[2] = 'Z'
 println "l_1t$l_1"
 println "l_2t$l_2"
‣ Output
 l_1
 [a, b, Z, d, e]            // arghhh! we have a ‘Z’ in l_1
 l_2
 [a, b, Z, d, e]
‣ We do not copy all elements of l_1 to l_2, but rather let l_2 be a
  reference to the same Object as l_1
Copying an Object

‣ We want to copy a list, then modify its copy without altering the original
 def l_1 = ('a'..'e').toList()
 def l_2 = l_1

 l_2[2] = 'Z'
 println "l_1t$l_1"
 println "l_2t$l_2"
‣ Output
 l_1
 [a, b, Z, d, e]            // arghhh! we have a ‘Z’ in l_1
 l_2
 [a, b, Z, d, e]
‣ We do not copy all elements of l_1 to l_2, but rather let l_2 be a
  reference to the same Object as l_1

‣ Modifying l_1 modifies l_2 as a side effect
Copying an Object

‣ We want to copy a list, then modify its copy without altering the original
 def l_1 = ('a'..'e').toList()
 def l_2 = l_1

 l_2[2] = 'Z'
 println "l_1t$l_1"
 println "l_2t$l_2"
‣ Output
 l_1
 [a, b, Z, d, e]            // arghhh! we have a ‘Z’ in l_1
 l_2
 [a, b, Z, d, e]
‣ We do not copy all elements of l_1 to l_2, but rather let l_2 be a
  reference to the same Object as l_1

‣ Modifying l_1 modifies l_2 as a side effect
‣ This feature can be meaningful (memory, consistency...)
Cloning an Object: o.clone()

‣ The solution is to explicitly clone the object
 def l_1 = ('a'..'e').toList()
 def l_2 = l_1.clone()
 l_2[2] = 'Z'
 println "l_1t$l_1"
 println "l_2t$l_2"
Cloning an Object: o.clone()

‣ The solution is to explicitly clone the object
 def l_1 = ('a'..'e').toList()
 def l_2 = l_1.clone()
 l_2[2] = 'Z'
 println "l_1t$l_1"
 println "l_2t$l_2"
‣ Output
 l_1
 [a, b, c, d, e]
 l_2
 [a, b, Z, d, e]
Cloning an Object: o.clone()

‣ The solution is to explicitly clone the object
 def l_1 = ('a'..'e').toList()
 def l_2 = l_1.clone()
 l_2[2] = 'Z'
 println "l_1t$l_1"
 println "l_2t$l_2"
‣ Output
 l_1
 [a, b, c, d, e]
 l_2
 [a, b, Z, d, e]
‣ l_1 is effectively duplicated: further modifications on l_2 do not alter l_1
Cloning an Object                          (cont’d)

‣ The same applies to a class
 class Foo{
     String name
     List values
 }
 Foo f_1=[name:'donald', values:[1,2,3]]
 Foo f_2=f_1
 f_2.name='mickey'

 println "f_1t$f_1.namet$f_1.values"
 println "f_2t$f_2.namet$f_2.values"
Cloning an Object                          (cont’d)

‣ The same applies to a class
 class Foo{
     String name
     List values
 }
 Foo f_1=[name:'donald', values:[1,2,3]]
 Foo f_2=f_1
 f_2.name='mickey'

 println "f_1t$f_1.namet$f_1.values"
 println "f_2t$f_2.namet$f_2.values"
‣ Output
 f_1
 mickey
     [1, 2, 3]
 f_2
 mickey
     [1, 2, 3]
Cloning an Object                               (cont’d)




‣ If we try
 f_2 = f_1.clone()
‣ We obtain
 java.lang.CloneNotSupportedException: Foo
 
 at ConsoleScript39.run(ConsoleScript39:13)
We need to define what we mean by cloneable




                                              27
Cloning an Object                                        (cont’d)



‣ The class Foo must implements an interface Cloneable
 class Foo implements Cloneable{
   String name
   List values

     Foo clone(){
       return new Foo(‘name’: name, ‘values’: values.clone())
     }    //explicitly duplicate all fields
 }
Cloning an Object                                        (cont’d)



‣ The class Foo must implements an interface Cloneable
 class Foo implements Cloneable{
   String name
   List values

     Foo clone(){
       return new Foo(‘name’: name, ‘values’: values.clone())
     }    //explicitly duplicate all fields
 }
Cloning an Object   (cont’d)
Cloning an Object                          (cont’d)

‣ We can now safely clone
 Foo f_1=[name:'donald', values:[1,2,3]]
 Foo f_2=f_1.clone()

 f_2.name='mickey'
 f_2.values[1] = 9

 println "f_1t$f_1.namet$f_1.values"
 println "f_2t$f_2.namet$f_2.values"
Cloning an Object                          (cont’d)

‣ We can now safely clone
 Foo f_1=[name:'donald', values:[1,2,3]]
 Foo f_2=f_1.clone()

 f_2.name='mickey'
 f_2.values[1] = 9

 println "f_1t$f_1.namet$f_1.values"
 println "f_2t$f_2.namet$f_2.values"
‣ Output
 f_1
 donald
     [1, 2, 3]
 f_2
 mickey
     [1, 9, 3]
OOP : Interface




‣ An interface is a manner to specify what behavior(s) a class should
  have, without writing any specific code
OOP : Interface




‣ An interface is a manner to specify what behavior(s) a class should
  have, without writing any specific code
‣ A class later implements the interface, thus it is enforced to fulfill the
  mentioned mechanism
How to make a class enforce a behavior?




                                          31
Interface examples

‣ Two examples we already met
Interface examples

‣ Two examples we already met
‣ objects can be compared with <, <=, == etc.
 class MyClass implements Comparable{
   ...
   int compareTo(o){
     ...
   }
 }
Interface examples

‣ Two examples we already met
‣ objects can be compared with <, <=, == etc.
 class MyClass implements Comparable{
   ...
   int compareTo(o){
     ...
   }
 }
‣ objects can be duplicated safely
 class MyClass implements Cloneable{
   ...
   MyClass clone(){
     ...
   }
 }
Interface examples

‣ They can be combined
Interface examples

‣ They can be combined
‣ objects can be compared and cloned
 class MyClass implements Comparable, Cloneable{
   ...
   int compareTo(o){
     ...
   }
   MyClass clone(){
     ...
   }
 }
How to define an interface?




                              34
Interface definition



‣ Like a class file (class is replaced by interface)
Interface definition



‣ Like a class file (class is replaced by interface)
‣ There is no execution code inside the file (on specification)
Interface definition



‣ Like a class file (class is replaced by interface)
‣ There is no execution code inside the file (on specification)
‣ File HasSequence.groovy
 interface HasSequence{
   String getSequence();
   String fastaString();
 }
Interface definition



‣ class file then need to implements those two methods Protein.groovy
 class Protein implements HasSequence{
   String accessionCode
   String sequence   //getSequence() will be automatically
                     //defined

     String fastaString(){
        “>$accessionCoden$sequencen”
     }
 }
Interface versus Abstraction

interface MyInterface {...}
abstract class MyAbstractClass{...}
Interface versus Abstraction

 interface MyInterface {...}
 abstract class MyAbstractClass{...}
‣ Syntax
 class MyClass implements MyInterface{...}
 class MyClass extends MyAbstractClass{...}
Interface versus Abstraction

 interface MyInterface {...}
 abstract class MyAbstractClass{...}
‣ Syntax
 class MyClass implements MyInterface{...}
 class MyClass extends MyAbstractClass{...}
‣ interface cannot contains explicit code (only specifications)
Interface versus Abstraction

 interface MyInterface {...}
 abstract class MyAbstractClass{...}
‣ Syntax
 class MyClass implements MyInterface{...}
 class MyClass extends MyAbstractClass{...}
‣ interface cannot contains explicit code (only specifications)
‣ abstract class contains explicit code (see AminoAcidSequence), but can
  also contain abstract methods (to be implemented by children classes)
Interface versus Abstraction

 interface MyInterface {...}
 abstract class MyAbstractClass{...}
‣ Syntax
 class MyClass implements MyInterface{...}
 class MyClass extends MyAbstractClass{...}
‣ interface cannot contains explicit code (only specifications)
‣ abstract class contains explicit code (see AminoAcidSequence), but can
  also contain abstract methods (to be implemented by children classes)
‣ A class can implements several interfaces, but only one abstract parent
Interface versus Abstraction




‣ Dynamic typing in groovy => interfaces not as important as in java
Interface versus Abstraction




‣ Dynamic typing in groovy => interfaces not as important as in java
‣ Strengthen the code readability and safety
 List<HasSequence> ....

groovy & grails - lecture 7

  • 1.
    Groovy: Efficiency OrientedProgramming Lecture 7 Master Proteomics & Bioinformatics - University of Geneva Alexandre Masselot - summer 2011
  • 2.
    Contents ‣ Eclipse tips ‣Productivity tip: back to TDD ‣ Exceptions throwing and catching ‣ reference to object and cloning ‣ OOP: interface
  • 3.
    Eclipse tips ‣ STS(Springsource Tool Suite) dashboard: - keep aware of what is new in the STS environment (new releases, new plugins, technologies, future webinars...) - help > dashboard
  • 4.
    Eclipse tips ‣ STS(Springsource Tool Suite) dashboard: - keep aware of what is new in the STS environment (new releases, new plugins, technologies, future webinars...) - help > dashboard ‣ Jump directly in junit view - run junit tests <alt> <shift> X T - jump to junit window <shift> <alt> Q U - configure your shortcuts with preferences > filter “keys” > filter junit - use multi screen to put console or junit view on the secondary display (drag the view tab and drop it on the secondary display)
  • 5.
  • 6.
    Productivity : TDD ‣Test Driven Development
  • 7.
    Productivity : TDD ‣Test Driven Development ‣ Even for a simple task (over 10 lines!) - write a simple class (in the same directory if you wish) - split the behavior of your problem into simple methods - test every method
  • 8.
    Productivity : TDD ‣Test Driven Development ‣ Even for a simple task (over 10 lines!) - write a simple class (in the same directory if you wish) - split the behavior of your problem into simple methods - test every method Do not rely on println to develop/understand your code
  • 9.
  • 10.
  • 11.
    Exception: example org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException:No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315) at groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397) at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java: 313) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16) at junit.framework.TestCase.runBare(TestCase.java:128) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:120) at junit.framework.TestSuite.runTest(TestSuite.java:230) at junit.framework.TestSuite.run(TestSuite.java:225) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
  • 12.
    Exception: example org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException:No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315) at groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397) at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16) at junit.framework.TestCase.runBare(TestCase.java:128) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:120) at junit.framework.TestSuite.runTest(TestSuite.java:230) at junit.framework.TestSuite.run(TestSuite.java:225) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer $_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886) at groovy.lang.Closure.call(Closure.java:276) at groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
  • 13.
    Exception: example message org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315) at groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397) at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16) at junit.framework.TestCase.runBare(TestCase.java:128) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:120) at junit.framework.TestSuite.runTest(TestSuite.java:230) at junit.framework.TestSuite.run(TestSuite.java:225) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer $_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886) at groovy.lang.Closure.call(Closure.java:276) at groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
  • 14.
    Exception: example message org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315) at groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397) at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16) at junit.framework.TestCase.runBare(TestCase.java:128) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at at at at at junit.framework.TestResult.run(TestResult.java:109) junit.framework.TestCase.run(TestCase.java:120) junit.framework.TestSuite.runTest(TestSuite.java:230) junit.framework.TestSuite.run(TestSuite.java:225) org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) cause(s) cascade at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer $_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886) at groovy.lang.Closure.call(Closure.java:276) at groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
  • 15.
    Exception: example message org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315) at groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397) at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16) at junit.framework.TestCase.runBare(TestCase.java:128) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at at at at at junit.framework.TestResult.run(TestResult.java:109) junit.framework.TestCase.run(TestCase.java:120) junit.framework.TestSuite.runTest(TestSuite.java:230) junit.framework.TestSuite.run(TestSuite.java:225) exception name org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) cause(s) cascade at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer $_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886) at groovy.lang.Closure.call(Closure.java:276) at groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
  • 16.
    Exception: example message org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315) at groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397) at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16) at junit.framework.TestCase.runBare(TestCase.java:128) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at at at at at junit.framework.TestResult.run(TestResult.java:109) junit.framework.TestCase.run(TestCase.java:120) junit.framework.TestSuite.runTest(TestSuite.java:230) junit.framework.TestSuite.run(TestSuite.java:225) exception name org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) cause(s) cascade at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer $_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at at at at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:597) org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88) search for your packages at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886) at groovy.lang.Closure.call(Closure.java:276) at groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
  • 17.
    Exception: example message org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2395) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3315) at groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1499) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1476) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1397) at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:313) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicerTest.setUp(UniprotEntrySplicerTest.groovy:16) at junit.framework.TestCase.runBare(TestCase.java:128) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at at at at at junit.framework.TestResult.run(TestResult.java:109) junit.framework.TestCase.run(TestCase.java:120) junit.framework.TestSuite.runTest(TestSuite.java:230) junit.framework.TestSuite.run(TestSuite.java:225) exception name org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) cause(s) cascade at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.Attributes.textd() is applicable for argument types: () values: [] Possible solutions: text(), text(), text(), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120) at unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer $_extractIsoformDescriptors_closure1.doCall(UniprotEntrySplicer.groovy:64) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at at at at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:597) org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88) search for your packages at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273) code line number at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886) at groovy.lang.Closure.call(Closure.java:276) at groovy.util.slurpersupport.NodeChildren.find(NodeChildren.java:194) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
  • 18.
    Exceptions: surviving &using ‣ In exceptional situations, an event is thrown def list = ['a', 'b', 'c'] def x = list[-5]
  • 19.
    Exceptions: surviving &using ‣ In exceptional situations, an event is thrown def list = ['a', 'b', 'c'] def x = list[-5] ‣ Output java.lang.ArrayIndexOutOfBoundsException: Negative array index [-5] too large for array size 3 at ConsoleScript7.run(ConsoleScript7:3)
  • 20.
    Exceptions: surviving &using ‣ In exceptional situations, an event is thrown def list = ['a', 'b', 'c'] def x = list[-5] ‣ Output java.lang.ArrayIndexOutOfBoundsException: Negative array index [-5] too large for array size 3 at ConsoleScript7.run(ConsoleScript7:3) ‣ An exception carries three types of information: - nature: through it’s class (extends Exception), - message: detailing the circumstances of the incident - stack trace: the stack of classes (possibly with line number) to track where the exception was thrown
  • 21.
    Catching exceptions: try/ catch ‣ An exception does not mean exiting the program (default behavior)
  • 22.
    Catching exceptions: try/ catch ‣ An exception does not mean exiting the program (default behavior) ‣ It is possible to catch an exception def list = ['10', 'a', '20', '30'] [1, 2, 5, 3, 4].each{ i -> println "$it"+list[-i].toInteger() } println “coincoin”
  • 23.
    Catching exceptions: try/ catch ‣ An exception does not mean exiting the program (default behavior) ‣ It is possible to catch an exception impossible!!! def list = ['10', 'a', '20', '30'] [1, 2, 5, 3, 4].each{ i -> println "$it"+list[-i].toInteger() } println “coincoin”
  • 24.
    Catching exceptions: try/ catch ‣ An exception does not mean exiting the program (default behavior) ‣ It is possible to catch an exception impossible!!! def list = ['10', 'a', '20', '30'] [1, 2, 5, 3, 4].each{ i -> println "$it"+list[-i].toInteger() } println “coincoin” ‣ Output 1 30 2 20 java.lang.ArrayIndexOutOfBoundsException: Negative array index [-5] too large for array size 4 at ConsoleScript17$_run_closure1.doCall(ConsoleScript17:3) at ConsoleScript17.run(ConsoleScript17:2) and exit (with code > 0)
  • 25.
    Catching exceptions: try/ catch ‣ An exception does not mean exiting the program (default behavior) ‣ It is possible to catch an exception impossible!!! def list = ['10', 'a', '20', '30'] [1, 2, 5, 3, 4].each{ i -> println "$it"+list[-i].toInteger() } println “coincoin” ‣ Output 1 30 this line is never reached 2 20 java.lang.ArrayIndexOutOfBoundsException: Negative array index [-5] too large for array size 4 at ConsoleScript17$_run_closure1.doCall(ConsoleScript17:3) at ConsoleScript17.run(ConsoleScript17:2) and exit (with code > 0)
  • 26.
    Catching exceptions: try/ catch (cont’d) ‣ Surround the piece of code you want to secure with try/catch def list = ['10', 'a', '20', '30'] try{ [1, 2, 5, 3, 4].each{ i -> println "$it"+list[-i].toInteger() } }catch(Exception e){ println "error: " + e.message } println “coincoin”
  • 27.
    Catching exceptions: try/ catch (cont’d) ‣ Surround the piece of code you want to secure with try/catch def list = ['10', 'a', '20', '30'] try{ [1, 2, 5, 3, 4].each{ i -> println "$it"+list[-i].toInteger() } }catch(Exception e){ println "error: " + e.message } println “coincoin” ‣ Output 1 30 2 20 error: Negative array index [-5] too large for array size 4 coincoin
  • 28.
    Catching exceptions: try/ catch (cont’d) def list = ['10', 'a', '20', '30'] try{ [1, 2, 5, 3, 4].each{ i -> println "$it"+list[-i].toInteger() } }catch(Exception e){ println "error: " + e.message } println “coincoin”
  • 29.
    Catching exceptions: try/ catch (cont’d) def list = ['10', 'a', '20', '30'] try{ [1, 2, 5, 3, 4].each{ i -> println "$it"+list[-i].toInteger() } }catch(Exception e){ exit the loop println "error: " + e.message } println “coincoin”
  • 30.
    Goal: catch exceptionsbut continue the loop 13
  • 31.
    Catching exceptions: try/ catch (cont’d) ‣ Adapt the depth of catching
  • 32.
    Catching exceptions: try/ catch (cont’d) ‣ Adapt the depth of catching ‣ In the following case, the iteration will continue after an exception is caught def list = ['10', 'a', '20', '30'] [1, 2, 5, 3, 4].each{ i -> try{ println "$it"+list[-i].toInteger() }catch(Exception e){ println "error with ($i): "+e.message } }
  • 33.
    Catching exceptions: try/ catch (cont’d) ‣ Adapt the depth of catching ‣ In the following case, the iteration will continue after an exception is caught def list = ['10', 'a', '20', '30'] [1, 2, 5, 3, 4].each{ i -> try{ println "$it"+list[-i].toInteger() }catch(Exception e){ println "error with ($i): "+e.message } } ‣ Output 1 30 2 20 error with (5): Negative array index [-5] too large for array size 4 error with (3): For input string: "a" 4 10
  • 34.
    catch(Exception e) is generic(all exceptions are caught) 15
  • 35.
    Goal: catch specificexceptions 16
  • 36.
    Catching exceptions: try/ catch (cont’d) ‣ You can catch only certain classes of Exception def list = ['10', 'a', '20', '30'] [1, 2, 5, 3, 4].each{ i -> try{ println "$it"+list[-i].toInteger() }catch(ArrayIndexOutOfBoundsException e){ println "error with ($i): "+e.message } }
  • 37.
    Catching exceptions: try/ catch (cont’d) ‣ You can catch only certain classes of Exception def list = ['10', 'a', '20', '30'] [1, 2, 5, 3, 4].each{ i -> try{ println "$it"+list[-i].toInteger() }catch(ArrayIndexOutOfBoundsException e){ println "error with ($i): "+e.message } } ‣ Output 1 30 2 20 error with (5): Negative array index [-5] too large for array size 4 + unrecovered stack trace and exit because of java.lang.NumberFormatException => code exits
  • 38.
    Catching exceptions: try/ catch (cont’d) ‣ Or even cascade catching def list = ['10', 'a', '20', '30'] [1, 2, 5, 3, 4].each{ i -> try{ println "$it"+list[-i].toInteger() }catch(ArrayIndexOutOfBoundsException e){ println "out of bound!" }catch(NumberFormatException e){ println "cannot to convert '${list[-i]}' to int" } }
  • 39.
    Catching exceptions: try/ catch (cont’d) ‣ Or even cascade catching def list = ['10', 'a', '20', '30'] [1, 2, 5, 3, 4].each{ i -> try{ println "$it"+list[-i].toInteger() }catch(ArrayIndexOutOfBoundsException e){ println "out of bound!" }catch(NumberFormatException e){ println "cannot to convert '${list[-i]}' to int" } } ‣ Output 1 30 2 20 out of bound! cannot to convert 'a' to int 4 10
  • 40.
    Throwing your Exception ‣In some situation, it is powerful to throw an exception in your code (to be hopefully caught at a meaningful place...)
  • 41.
    Throwing your Exception ‣In some situation, it is powerful to throw an exception in your code (to be hopefully caught at a meaningful place...) ‣ Example: - you are in the third level of nested function when some illegal action happens, - you do not want to manage all the return statements of your functions up to the place where the high level action is to be undertaken - you want to carry a meaningful message
  • 42.
    Throwing your Exception ‣In some situation, it is powerful to throw an exception in your code (to be hopefully caught at a meaningful place...) ‣ Example: - you are in the third level of nested function when some illegal action happens, - you do not want to manage all the return statements of your functions up to the place where the high level action is to be undertaken - you want to carry a meaningful message ‣ Example int factorial(int n){ if(n<0) throw new RuntimeException(“cannot pass negative value ($n)”) //continue }
  • 43.
    Throwing your Exception intfactorial(int n){ if(n<0) throw new RuntimeException(“cannot pass negative value ($n)”) //continue } def computeFactorial(int n){ if(n>100) return -1 factorial(n) } //Script try{ computeFactorial(i) }catch(Exception e){....}
  • 44.
    Throwing your Exception intfactorial(int n){ if(n<0) throw new RuntimeException(“cannot pass negative value ($n)”) //continue } def computeFactorial(int n){ if(n>100) throw new IllegalArgumentException(“no fact for >100”) factorial(n) } //Script try{ computeFactorial(i) }catch(Exception e){....}
  • 45.
    Throwing your Exception ‣You code is more clear & robust: - 10 lines per function limits complex if / else construct.
  • 46.
    Throwing your Exception ‣You code is more clear & robust: - 10 lines per function limits complex if / else construct. ‣ Document the exception that can be thrown in your function header comment /** ... @Throws RuntimeException if argument is negative */
  • 47.
    Throwing your Exception ‣You code is more clear & robust: - 10 lines per function limits complex if / else construct. ‣ Document the exception that can be thrown in your function header comment /** ... @Throws RuntimeException if argument is negative */ ‣ Make the message meaningful.
  • 48.
    Throwing your Exception ‣You code is more clear & robust: - 10 lines per function limits complex if / else construct. ‣ Document the exception that can be thrown in your function header comment /** ... @Throws RuntimeException if argument is negative */ ‣ Make the message meaningful. ‣ You can create your own Exception class that extends whatever existing Exception: - to have generic behavior or catch only your own exception.
  • 49.
    Copying an Object ‣We want to copy a list, then modify its copy without altering the original def l_1 = ('a'..'e').toList() def l_2 = l_1 l_2[2] = 'Z' println "l_1t$l_1" println "l_2t$l_2"
  • 50.
    Copying an Object ‣We want to copy a list, then modify its copy without altering the original def l_1 = ('a'..'e').toList() def l_2 = l_1 l_2[2] = 'Z' println "l_1t$l_1" println "l_2t$l_2" ‣ Output l_1 [a, b, Z, d, e] // arghhh! we have a ‘Z’ in l_1 l_2 [a, b, Z, d, e]
  • 51.
    Copying an Object ‣We want to copy a list, then modify its copy without altering the original def l_1 = ('a'..'e').toList() def l_2 = l_1 l_2[2] = 'Z' println "l_1t$l_1" println "l_2t$l_2" ‣ Output l_1 [a, b, Z, d, e] // arghhh! we have a ‘Z’ in l_1 l_2 [a, b, Z, d, e] ‣ We do not copy all elements of l_1 to l_2, but rather let l_2 be a reference to the same Object as l_1
  • 52.
    Copying an Object ‣We want to copy a list, then modify its copy without altering the original def l_1 = ('a'..'e').toList() def l_2 = l_1 l_2[2] = 'Z' println "l_1t$l_1" println "l_2t$l_2" ‣ Output l_1 [a, b, Z, d, e] // arghhh! we have a ‘Z’ in l_1 l_2 [a, b, Z, d, e] ‣ We do not copy all elements of l_1 to l_2, but rather let l_2 be a reference to the same Object as l_1 ‣ Modifying l_1 modifies l_2 as a side effect
  • 53.
    Copying an Object ‣We want to copy a list, then modify its copy without altering the original def l_1 = ('a'..'e').toList() def l_2 = l_1 l_2[2] = 'Z' println "l_1t$l_1" println "l_2t$l_2" ‣ Output l_1 [a, b, Z, d, e] // arghhh! we have a ‘Z’ in l_1 l_2 [a, b, Z, d, e] ‣ We do not copy all elements of l_1 to l_2, but rather let l_2 be a reference to the same Object as l_1 ‣ Modifying l_1 modifies l_2 as a side effect ‣ This feature can be meaningful (memory, consistency...)
  • 54.
    Cloning an Object:o.clone() ‣ The solution is to explicitly clone the object def l_1 = ('a'..'e').toList() def l_2 = l_1.clone() l_2[2] = 'Z' println "l_1t$l_1" println "l_2t$l_2"
  • 55.
    Cloning an Object:o.clone() ‣ The solution is to explicitly clone the object def l_1 = ('a'..'e').toList() def l_2 = l_1.clone() l_2[2] = 'Z' println "l_1t$l_1" println "l_2t$l_2" ‣ Output l_1 [a, b, c, d, e] l_2 [a, b, Z, d, e]
  • 56.
    Cloning an Object:o.clone() ‣ The solution is to explicitly clone the object def l_1 = ('a'..'e').toList() def l_2 = l_1.clone() l_2[2] = 'Z' println "l_1t$l_1" println "l_2t$l_2" ‣ Output l_1 [a, b, c, d, e] l_2 [a, b, Z, d, e] ‣ l_1 is effectively duplicated: further modifications on l_2 do not alter l_1
  • 57.
    Cloning an Object (cont’d) ‣ The same applies to a class class Foo{ String name List values } Foo f_1=[name:'donald', values:[1,2,3]] Foo f_2=f_1 f_2.name='mickey' println "f_1t$f_1.namet$f_1.values" println "f_2t$f_2.namet$f_2.values"
  • 58.
    Cloning an Object (cont’d) ‣ The same applies to a class class Foo{ String name List values } Foo f_1=[name:'donald', values:[1,2,3]] Foo f_2=f_1 f_2.name='mickey' println "f_1t$f_1.namet$f_1.values" println "f_2t$f_2.namet$f_2.values" ‣ Output f_1 mickey [1, 2, 3] f_2 mickey [1, 2, 3]
  • 59.
    Cloning an Object (cont’d) ‣ If we try f_2 = f_1.clone() ‣ We obtain java.lang.CloneNotSupportedException: Foo at ConsoleScript39.run(ConsoleScript39:13)
  • 60.
    We need todefine what we mean by cloneable 27
  • 61.
    Cloning an Object (cont’d) ‣ The class Foo must implements an interface Cloneable class Foo implements Cloneable{ String name List values Foo clone(){ return new Foo(‘name’: name, ‘values’: values.clone()) } //explicitly duplicate all fields }
  • 62.
    Cloning an Object (cont’d) ‣ The class Foo must implements an interface Cloneable class Foo implements Cloneable{ String name List values Foo clone(){ return new Foo(‘name’: name, ‘values’: values.clone()) } //explicitly duplicate all fields }
  • 63.
    Cloning an Object (cont’d)
  • 64.
    Cloning an Object (cont’d) ‣ We can now safely clone Foo f_1=[name:'donald', values:[1,2,3]] Foo f_2=f_1.clone() f_2.name='mickey' f_2.values[1] = 9 println "f_1t$f_1.namet$f_1.values" println "f_2t$f_2.namet$f_2.values"
  • 65.
    Cloning an Object (cont’d) ‣ We can now safely clone Foo f_1=[name:'donald', values:[1,2,3]] Foo f_2=f_1.clone() f_2.name='mickey' f_2.values[1] = 9 println "f_1t$f_1.namet$f_1.values" println "f_2t$f_2.namet$f_2.values" ‣ Output f_1 donald [1, 2, 3] f_2 mickey [1, 9, 3]
  • 66.
    OOP : Interface ‣An interface is a manner to specify what behavior(s) a class should have, without writing any specific code
  • 67.
    OOP : Interface ‣An interface is a manner to specify what behavior(s) a class should have, without writing any specific code ‣ A class later implements the interface, thus it is enforced to fulfill the mentioned mechanism
  • 68.
    How to makea class enforce a behavior? 31
  • 69.
    Interface examples ‣ Twoexamples we already met
  • 70.
    Interface examples ‣ Twoexamples we already met ‣ objects can be compared with <, <=, == etc. class MyClass implements Comparable{ ... int compareTo(o){ ... } }
  • 71.
    Interface examples ‣ Twoexamples we already met ‣ objects can be compared with <, <=, == etc. class MyClass implements Comparable{ ... int compareTo(o){ ... } } ‣ objects can be duplicated safely class MyClass implements Cloneable{ ... MyClass clone(){ ... } }
  • 72.
  • 73.
    Interface examples ‣ Theycan be combined ‣ objects can be compared and cloned class MyClass implements Comparable, Cloneable{ ... int compareTo(o){ ... } MyClass clone(){ ... } }
  • 74.
    How to definean interface? 34
  • 75.
    Interface definition ‣ Likea class file (class is replaced by interface)
  • 76.
    Interface definition ‣ Likea class file (class is replaced by interface) ‣ There is no execution code inside the file (on specification)
  • 77.
    Interface definition ‣ Likea class file (class is replaced by interface) ‣ There is no execution code inside the file (on specification) ‣ File HasSequence.groovy interface HasSequence{ String getSequence(); String fastaString(); }
  • 78.
    Interface definition ‣ classfile then need to implements those two methods Protein.groovy class Protein implements HasSequence{ String accessionCode String sequence //getSequence() will be automatically //defined String fastaString(){ “>$accessionCoden$sequencen” } }
  • 79.
    Interface versus Abstraction interfaceMyInterface {...} abstract class MyAbstractClass{...}
  • 80.
    Interface versus Abstraction interface MyInterface {...} abstract class MyAbstractClass{...} ‣ Syntax class MyClass implements MyInterface{...} class MyClass extends MyAbstractClass{...}
  • 81.
    Interface versus Abstraction interface MyInterface {...} abstract class MyAbstractClass{...} ‣ Syntax class MyClass implements MyInterface{...} class MyClass extends MyAbstractClass{...} ‣ interface cannot contains explicit code (only specifications)
  • 82.
    Interface versus Abstraction interface MyInterface {...} abstract class MyAbstractClass{...} ‣ Syntax class MyClass implements MyInterface{...} class MyClass extends MyAbstractClass{...} ‣ interface cannot contains explicit code (only specifications) ‣ abstract class contains explicit code (see AminoAcidSequence), but can also contain abstract methods (to be implemented by children classes)
  • 83.
    Interface versus Abstraction interface MyInterface {...} abstract class MyAbstractClass{...} ‣ Syntax class MyClass implements MyInterface{...} class MyClass extends MyAbstractClass{...} ‣ interface cannot contains explicit code (only specifications) ‣ abstract class contains explicit code (see AminoAcidSequence), but can also contain abstract methods (to be implemented by children classes) ‣ A class can implements several interfaces, but only one abstract parent
  • 84.
    Interface versus Abstraction ‣Dynamic typing in groovy => interfaces not as important as in java
  • 85.
    Interface versus Abstraction ‣Dynamic typing in groovy => interfaces not as important as in java ‣ Strengthen the code readability and safety List<HasSequence> ....

Editor's Notes