1 / 23

Debugging lab

Debugging lab. Mariano Ceccato. Outline. Java Inheritance Reflection Exception handling Laboratory of debugging Application: Jtopas Tokens, tokenizer. Overloading. Names can be reused to define distinct entities

Download Presentation

Debugging lab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Debugging lab Mariano Ceccato

  2. Outline • Java • Inheritance • Reflection • Exception handling • Laboratory of debugging • Application: Jtopas • Tokens, tokenizer

  3. Overloading • Names can be reused to define distinct entities • Different parameter types can be used to distinguish among methods with the same names • Methods and fields can have the same name class A { int f ; void f ( int x ) { System.out.println( “ called passing an integer“ ); } void f ( double x ) { System.out.println( “ called passing a double“ ); } void f ( String x ) { System.out.println( “ called passing a String” ); } } • A a = new A( ); • a.f ( 1 ); • called passing an integer • a.f ( 1.0 ); • called passing a double • a.f ( “1” ); • called passing a String

  4. Inheritance • Allows programmers to customize a class for a specific purpose, without actually modifying the original class (the superclass) • The derived class (subclass) is allowed to add methods or redefine them • The subclass can add variables, but cannot redefine them class A { int x; void f ( ) { … } } class B extends A { double z; int g (String p) { … } }

  5. Overriding • A class can refine (override) methods already defined by the superclass class A { int x; void f ( ) { System.out.println( “ I am A“ ); } } class B extends A { void f ( ) { System.out.println( “ I am B“ ); } } f is overridden

  6. Overriding class A { void f ( ) { System.out.println( “ I am A“ ); } void g ( ) { f ( ); } } • A a = new A( ); • a.g ( ) • I am A class B extends A { void f ( ) { System.out.println( “ I am B“ ); } } • A b = new B( ); • b.g ( ) • I am B

  7. Interfaces • An interface • Specifies methods only by signatures (no body) • Does not define any constructor • A class that implements an interface must provide an implementation for interface methods class B implements B_Int { void f ( ) { System.out.println( “ I am B“ ); } void g ( ) { f ( ); } } interface B_Int { void f ( ); void g ( ); }

  8. Abstract classes • An abstract class • Contains methods that can be abstract (without body) • its constructors can not be directly called • A Class that extends an abstract class must provide an implementation for abstract methods abstract class A { int x; void f ( ) { System.out.println( “ I am A“ ); } abstract void g ( ) ; } class B extends A { void g ( ) { x = x + 1; } }

  9. Constructor • Used to create an instance of the current class • Has the same name as the current class • Has no type class A { A ( ) { … } void f ( ) { … } void g ( ) { … } } class A { A ( ) { … } A (int x) { … } A (double y) { … } void f ( ) { … } void g ( ) { … } } Constructors can be overloaded

  10. Constructor delegation • Different constructor on the same class (“this” keyword) • Constructor from the superclass (“super” keyword) class A { A ( ) { … } A (int x) { this ( ); } } class B extends A { B ( ) { … } B (int y) { super ( y ); } }

  11. Reflection • To inspect/access class code when it is known at run-time • Binding is not done at compile-time • The compiler does not check type correctness (possible runtime errors) 1) Get an instance of the target class Class c = Class.forName("A"); Object o = c.newInstance( ); Class[ ] sig = new Class[1]; sig[0] = Class.forName("java.lang.String"); Method m = c.getMethod("g", sig); 2) Get an instance of the method to call 3) Call the method with proper arguments Object[ ] params = new Object[1]; params[0] = "xxxx"; m.invoke(o, params);

  12. Exceptions Exceptions: • Exceptions are things that are not supposed to occur in the normal control flow • Some exceptions (like division by zero) are avoidable through careful programming • Some exceptions (like losing a network connection) are not avoidable or predictable • Java allows programmers to define their own means of handling exceptions when they occur Exception handling: • Mechanism for creating special exception classes (whose instances are called exception objects) • The statement throw e is used to signal the occurrence of an exception and return control to the calling method and e refers to an exception object • The statement try/catch allows the calling method to “catch” the “thrown” exception object and take appropriate actions

  13. Exceptions class Math { int divide (int a, int b) { return a/b; } } Math m = new Math( ); int result = m.divide (4, 0); System.out.println("the result is " + result); Exception in thread "main" java.lang.ArithmeticException: / by zero

  14. Exceptions class Math { int divide (int a, int b) { return a/b; } } try { Math m = new Math( ); int result = m.divide (4, 0); System.out.println("the result is " + result); } catch (ArithmeticExceptionexception) { System.out.println(“Argument not valid " ); } Argument not valid

  15. Exceptions class Math { int divide (int a, int b) throws WrongArgument { if (b==0) throw new WrongArgument ( ); return a/b; } } class WrongArgument extentds Exception { … } try { Math m = new Math( ); int result = m.divide (4, 0); System.out.println("the result is " + result); } catch (WrongArgument exception) { System.out.println(“Argument not valid " ); }

  16. Exception hierarchy

  17. Right click -> Run as -> Junit test

  18. Jtopas - Java tokenizer and parser tool • Line/block comments • Keyword • Case sensitive/insensitive class A { //this is a comment int x; void f ( ) { System.out.println( “ I am A“ ); } }

  19. Tokenizer tokenizer.setSource( inputStream ); tokenizer.setParseFlags( … ); 1) Initialization while ( tokenizer.hasMoreToken( ) ) { Token t = tokenizer.nextToken( ); if ( t.getType( ) == Token.NUMBER ) … else … } 2) Tokens iteration

  20. Tokens • NORMAL • KEYWORD • STRING • NUMBER • SPECIAL_SEQUENCE • SEPARATOR • WHITESPACE • LINE_COMMENT • BLOCK_COMMENT • EOF • UNKNOWN

  21. Laboratory • Download Jtopas from http://selab.fbk.eu/swat/debugging/jTopasTraining.zip • Import the project in Eclipse • Fix the bugs reposted by the test cases • Record start time • Fix the bug in the application (do no change test cases) • Record stop time • Deliver the paper sheet • Export the eclipse project and send it to studio.empirico@gmail.com

More Related