industrial project (236504) advanced programming tools for refactoring java code in eclipse

14
Industrial Project (236504) Industrial Project (236504) Advanced programming tools Advanced programming tools for refactoring Java code in for refactoring Java code in Eclipse Eclipse Student: Alexander Libov Supervisor: Dr. Ran Ettinger, IBM. COMPUTER SCIENCE DEPARTMENT Technion - Israel Institute of Technology August 12, 2009

Upload: chung

Post on 04-Jan-2016

27 views

Category:

Documents


3 download

DESCRIPTION

COMPUTER SCIENCE DEPARTMENT Technion - Israel Institute of Technology. August 12, 2009. Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse. Student: Alexander Libov Supervisor: Dr. Ran Ettinger, IBM. Our goal. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Industrial Project (236504)Industrial Project (236504)

Advanced programming tools Advanced programming tools for refactoring Java code in for refactoring Java code in

EclipseEclipse

Student: Alexander Libov

Supervisor: Dr. Ran Ettinger, IBM.

COMPUTER SCIENCE DEPARTMENTTechnion - Israel Institute of Technology

August 12, 2009

Page 2: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Our goal

Improving code maintenance by enabling advanced refactoring methods (in Eclipse).

Implement Replace Temp with Query refactoring

Why is it important?

The benefit is increased readability (in therefactored version) and reusability (of the extracted computation)

The steps of turning procedural design to objects mainly involveintroducing new classes, extracting methods, moving variables and methods(to the new classes), inlining methods and renaming variables and methods.All those are either straightforward or already supported by modernrefactoring tools. It is the extraction of non-contiguous code (as inReplace Temp with Query) for which automation is missing and required.

Page 3: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Definitions

Sliced Variable – the variable that serves as a base to compute a slice Selected by the user.

It is important which of the occurrences is selected.

Slice – a piece of code that eventually calculates the sliced variable value in the location selected by the user

The slice does not include the statement which holds the sliced variable. The relevant code starts at the head of the current block and ends immediately before the

selected statement. Co-Slice - a piece of code that calculates the rest of the calculations done inside the

block apart from the sliced variable Can contain statements from the slice. Also located between the head of the current block and the selected statement.

Sliding – separating the slice from the co-slice in the covering block. Must preserve the observable behavior of the program.

Page 4: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Methodology part I

User chooses a variable referenceUser chooses a variable reference

The main workflow of the actions is as described below:

Use WALA to calculate the SliceUse WALA to calculate the Slice

Replace temp with queryReplace temp with query

Show SliceShow SliceUse WALA to calculate the Co-SliceUse WALA to calculate the Co-Slice

Page 5: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Methodology part II

Duplicate the covering block’s code

Duplicate the covering block’s code

The Replace Temp with Query in a bit more detail:

Leave only the slice in the first part and co-slice and rest of the code in the second part

Leave only the slice in the first part and co-slice and rest of the code in the second part

Run extract method on the slice Run extract method on the slice

Run inline temp on the declaration of the sliced variable

Run inline temp on the declaration of the sliced variable

SliceSlice Co-SliceCo-SliceCheck preconditionsCheck preconditions

a return statement for the extracted variable will be added at the end of the

extracted method, in the original block, the slice will be replaced by a declaration of that temp with a call to the

extracted method for its initialization.

Page 6: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Replace Temp with Query Example

...{  int sum = 0;  int prod = 1;  for (int i=0; i<a.length; i++) {     sum += a[i];     prod *= a[i];  }  System.out.println("The total sum is " + sum);  System.out.println("The product is " + prod);  ...

}

...

The user chose sum to be the sliced variable (that is, the temp to be replaced with a query)

Page 7: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Replace Temp with Query Example – Sliding

…{

  int sum = 0;  for (int i=0; i<a.length; i++) {     sum += a[i];  }

int prod = 1;

  for (int i=0; i<a.length; i++) {     prod *= a[i];  }

System.out.println("The total sum is " + sum); System.out.println("The product is " + prod);

}

The Slice – Extract Method fragment

The Co-Slice

Page 8: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Replace Temp with Query Example –– After Extract Method

int computeSum(int[] a) {

  int sum = 0;

for (int i=0; i<a.length; i++) {     sum += a[i];

}

return sum;

}

... {  int sum = computeSum(a);  int prod = 1;  for (int i=0; i<a.length; i++) {     prod *= a[i];  }  System.out.println("The total sum is " + sum);  System.out.println("The product is " + prod);  ...

}

...

The extracted method

Inline Temp selection

Page 9: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Replace Temp with Query Example –– After Inline Temp

... {  int prod = 1;  for (int i=0; i<a.length; i++) {     prod *= a[i];  }  System.out.println("The total sum is " + computeSum(a));  System.out.println("The product is " + prod);  ...

}

...

The sum variable is replaced with a computeSum query

Int computeSum(int[] a) {

  int sum = 0;

for (int i=0; i<a.length; i++) {     sum += a[i];

}

return sum;

}

Page 10: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Replace Temp with Query Example - Recap

... {  int sum = 0;  int prod = 1;  for (int i=0; i<a.length; i++) {     sum += a[i];     prod *= a[i];  }  System.out.println("The total sum is " + sum);  System.out.println("The product is " + prod);  ...}

...

…{

  int sum = 0;  for (int i=0; i<a.length; i++) {     sum += a[i];  }

int prod = 1;

  for (int i=0; i<a.length; i++) {     prod *= a[i];  }

System.out.println("The total sum is " + sum);System.out.println("The product is " + prod);

}

Int computeSum(int[] a) {

  int sum = 0;  for (int i=0; i<a.length; i++) {     sum += a[i];  }

return sum;

}

... {  int sum = computeSum(a);  int prod = 1;  for (int i=0; i<a.length; i++) {     prod *= a[i];  }  System.out.println("The total sum is " + sum);  System.out.println("The product is " + prod);  ...}

...

... {  int prod = 1;  for (int i=0; i<a.length; i++) {     prod *= a[i];  }  System.out.println("The total sum is " + computeSum(a));  System.out.println("The product is " + prod);  ...}

...

Page 11: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Replace Temp with Query Example – Final result

Int computeSum(int[] a) {

  int sum = 0;  for (int i=0; i<a.length; i++) {     sum += a[i];  }

return sum;

}

... {  System.out.println("The total sum is " + computeSum(a));  System.out.println("The product is " + computeProd(a));  ...

}

...

Int computeProd(int[] a) {

  int prod = 1;  for (int i=0; i<a.length; i++) {     prod *= a[i];  }

return prod;

}

Page 12: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Achievements

A fully working “Show Slice” action Generates breakpoints on the slice’s lines.

Shows pop-up with more detail about the slice.

Works on entire compilation unit (not method only).

A rudimentary “Replace temp With Query” refactoring Works on simple examples. more work is needed on identifying and implementing preconditions to guarantee

behavior preservation. Calculates slice and co-slice only in the covering block (as it should). Runs existing refactoring tools – “Extract Method” and “Inline Temp”.

Page 13: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Limitations

WALA limitations Variable declarations are not part of the slice.

Conditional branches are part of the slice when they have nested statements that are not part of the block.

Works slowly ( to WALA's defense slicing is known to be complex, it actually analyses the whole program, not only the selected method).

Works only on methods that are reachable from the main method.

Replace Temp with Query limitations Needs better pre-condition checking and extension to more use cases. An import must be updated on the move from one Eclipse version to another. Need to handle variable declarations better. Function calls handled wrong by the slicer (probably because of the default use of the

pointer analysis).

A very basic sliding algorithm is implemented -- in the spirit of the older Tucking, a transformation due to Lakhotia and Deprez.

Page 14: Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse

Conclusions

It is hard to work in a team (even of two) without source control

Working with large scale software is difficult One can easily be lost inside the code. Hard to tell what the flow is.

The refactoring framework in Eclipse/JDT is not easily ready for composition of refactoring tools

Code analysis is an interesting and complicated subject that doesn’t get enough coverage during undergraduate studies

Industrial Project course offers a real-life experience of how software development occurs

Needed experience for all software engineering students. It can contribute to better blending of the student in the

industry afterwards.