230 likes | 332 Views
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
E N D
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 • 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
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) { … } }
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
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
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 ( ); }
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; } }
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
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 ); } }
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);
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
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
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
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 " ); }
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“ ); } }
Tokenizer tokenizer.setSource( inputStream ); tokenizer.setParseFlags( … ); 1) Initialization while ( tokenizer.hasMoreToken( ) ) { Token t = tokenizer.nextToken( ); if ( t.getType( ) == Token.NUMBER ) … else … } 2) Tokens iteration
Tokens • NORMAL • KEYWORD • STRING • NUMBER • SPECIAL_SEQUENCE • SEPARATOR • WHITESPACE • LINE_COMMENT • BLOCK_COMMENT • EOF • UNKNOWN
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