Top Banner
Debugging lab Mariano Ceccato
23

Debugging lab

Dec 30, 2015

Download

Documents

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 - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Debugging lab

Debugging lab

Mariano Ceccato

Page 2: Debugging lab

OutlineOutline

2

• Java– Inheritance– Reflection– Exception handling

• Laboratory of debugging

• Application: Jtopas– Tokens, tokenizer

Page 3: Debugging lab

OverloadingOverloading

3

• 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” );called passing a String

a.f ( 1.0 );called passing a double

Page 4: Debugging lab

InheritanceInheritance

4

• 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

A

x: int

f(): void

B

z: double

g(String): int

class A { int x; void f ( ) { … }}

class B extends A { double z; int g (String p) { … }}

Page 5: Debugging lab

OverridingOverriding

5

• A class can refine (override) methods already defined by the superclass

A

x: int

f(): void

B

f(): void

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

Page 6: Debugging lab

OverridingOverriding

6

A

f(): void

B

f(): void

class A { void f ( ) { System.out.println( “ I am A“ ); } void g ( ) { f ( ); }}

class B extends A { void f ( ) { System.out.println( “ I am B“ ); }}

A a = new A( ); a.g ( )

I am A

A b = new B( ); b.g ( )I am B

Page 7: Debugging lab

InterfacesInterfaces

7

• 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 ( );}

Page 8: Debugging lab

Abstract classesAbstract classes

8

• 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; }}

Page 9: Debugging lab

ConstructorConstructor

9

• 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

Page 10: Debugging lab

Constructor delegationConstructor delegation

10

• 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 ); }

}

Page 11: Debugging lab

ReflectionReflection

11

• 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)

A

g( x:String ): void

Class c = Class.forName("A");Object o = c.newInstance( );

1) Get an instance of the target class

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

Object[ ] params = new Object[1];params[0] = "xxxx";m.invoke(o, params);

3) Call the method with proper arguments

Page 12: Debugging lab

ExceptionsExceptions

12

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

Page 13: Debugging lab

ExceptionsExceptions

13

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

Page 14: Debugging lab

ExceptionsExceptions

14

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 (ArithmeticException exception) { System.out.println(“Argument not valid " ); }

Argument not valid

Page 15: Debugging lab

ExceptionsExceptions

15

class Math { int divide (int a, int b) throws WrongArgument { if (b==0) throw new WrongArgument ( ); return a/b; }}

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 " ); }

class WrongArgument extentds Exception { …}

Page 16: Debugging lab

Exception hierarchyException hierarchy

16

Exception

Exception (String)

IOException

IOException( String)

Throwable

message: String

Throwable( String )getMessage( ): String

Page 17: Debugging lab

Right click -> Run as -> Junit test

Page 18: Debugging lab
Page 19: Debugging lab
Page 20: Debugging lab

Jtopas - Java tokenizer Jtopas - Java tokenizer and parser tooland parser tool

20

• Line/block comments

• Keyword

• Case sensitive/insensitive

class A { //this is a comment int x; void f ( ) { System.out.println( “ I am A“ ); }}

Page 21: Debugging lab

TokenizerTokenizer

21

while ( tokenizer.hasMoreToken( ) ) { Token t = tokenizer.nextToken( ); if ( t.getType( ) == Token.NUMBER ) … else …}

tokenizer.setSource( inputStream );tokenizer.setParseFlags( … );

1) Initialization

2) Tokens iteration

Page 22: Debugging lab

TokensTokens

22

Token

typelength

startPositionstartLine

startColumnendLine

endColumn

get/setTypeget/setLength

get/setStartPositionget/setStartLine

get/setStartColumnget/setEndLine

get/setEndColumn

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

Page 23: Debugging lab

LaboratoryLaboratory

23

• Download Jtopas fromhttp://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

[email protected]