• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
groovy & grails - lecture 7
 

groovy & grails - lecture 7

on

  • 560 views

Eclipse tips

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

Statistics

Views

Total Views
560
Views on SlideShare
558
Embed Views
2

Actions

Likes
0
Downloads
11
Comments
0

1 Embed 2

http://www.slideshare.net 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • genetic programming and cloning: back to biology\n
  • don’t hesitate to register and follow webinars: a efficient way to get into a new technology or feature in a small amount of time (maybe ask questions)\nMulti screen is a way to more poductivity\n
  • don’t hesitate to register and follow webinars: a efficient way to get into a new technology or feature in a small amount of time (maybe ask questions)\nMulti screen is a way to more poductivity\n
  • \n
  • TDD is not for the exercises! it is for daily life\nGoing back to fix a problem means putting back all the println\nDebugging goes exponential instead of dragging it to the more linear possible\n
  • TDD is not for the exercises! it is for daily life\nGoing back to fix a problem means putting back all the println\nDebugging goes exponential instead of dragging it to the more linear possible\n
  • TDD is not for the exercises! it is for daily life\nGoing back to fix a problem means putting back all the println\nDebugging goes exponential instead of dragging it to the more linear possible\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • in a map, value on the left hand side of ‘:’ is a String by default\n
  • \n
  • remember of implements comparable\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

groovy & grails - lecture 7 groovy & grails - lecture 7 Presentation Transcript

  • Groovy: Efficiency Oriented ProgrammingLecture 7Master Proteomics & Bioinformatics - University of GenevaAlexandre 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: exampleorg.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: exampleorg.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 messageorg.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 messageorg.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 messageorg.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 messageorg.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 messageorg.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 130 220 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 130 this line is never reached 220 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 130 220 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 130 220 error with (5): Negative array index [-5] too large for array size 4 error with (3): For input string: "a" 410
  • 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 130 220 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 130 220 out of bound! cannot to convert a to int 410
  • 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 Exceptionint 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)}//Scripttry{ computeFactorial(i)}catch(Exception e){....}
  • Throwing your Exceptionint 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)}//Scripttry{ 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 Abstractioninterface 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> ....