180 likes | 280 Views
Refactoring Support Tool: Cancer. Yoshiki Higo Osaka University. Gemini & Cancer. Gemini shows all code clones detected by CCFinder. gives user panoramic views of code clones in source code. has several quantitative information of code clones of each file and clone class. Cancer
E N D
Refactoring Support Tool:Cancer Yoshiki Higo Osaka University
Gemini & Cancer • Gemini • shows all code clones detected by CCFinder. • gives user panoramic views of code clones in source code. • has several quantitative information of code clones of each file and clone class. • Cancer • extracts refactoring-oriented-code clones from ones detected by CCFinder. • appends several metric values to extracted code clones. • enables user to know how to remove code clones if certain degree of its metric conditions is satisfied.
Declaration unit code clone • class declaration、interface declaration • Method unit code clone • method body、constructor、static initializer • Statement unit code clone • do 、for 、if 、switch 、synchronized 、try 、while Code clones in Cancer • All code clones ( which are extracted from ones detected by CCFinder) correspond to structural blocks of programming language. • Currently, Cancer can apply only Java language.
Metrics in Cancer(1/3) • Cancer characterizes extracted code clones using 6 metrics. • Using these metrics, we can get how to remove them. • 6 metrics are RVK,RVN,DCH,LEN,POP,DFL
Class A{ ・・・ void foo( ・・・ ){ ・・・ } ・・・ void bar( ・・・ ){ ・・・ } ・・・ } int a; MyClass myClass = new MyClass(); ・・・ for( int i = a ; i < 10 ; i++ ){ int c = a + 1; myClass.set(c); } ・・・ Class A{ ・・・ } Class B extends A{ ・・・ void foo( ・・・ ){ ・・・ } ・・・ } Class C extends A{ ・・・ void bar( ・・・ ){ ・・・ } ・・・ } code clone code clone The value of DCH : 0 RVK variable The value of RVK : 2 The value of RVN : 2 + 1 = 3 code clone The value of DCH : 1 Metrics in Cancer(2/3) • Variables which are used in code clone, but defined outside. • RVK : the number of such variables. • RVN : the sum of used count of such variables. • Relation of code clone on class hierarchy • DCH : the degree of dispersion on class hierarchy DCH takes into account only class hierarchy of target software (don’t include class hierarchy of library).
new sub routine caller statements Metrics in Cancer(3/3) • LEN: the average of length of code clones in a clone class • POP: the number of code clones in a clone class • DFL: Estimation of how many tokens would be removed from source files when all code clones of a clone class are replaced with caller statements of a new identical routine
Removal of Code clone using Refactoring Pattern • We use Refactoring Pattern[1] for removal of code clones with 6 metrics • Following patterns are applicable. • Extract Method • Pull Up Method [1] M. Fowler: Refactoring: Improving the Design of Existing Code, Addison-Wesley, 1999.
Target Unit : Method Unit, Statement Unit • DCH == 0 • RVK <= 1 int i; ・・・ ・・・ for( int j = 0 ; j < i ; j++ ){ ・・・ } ・・・ int i; ・・・ ・・・ i = newMethod(i); ・・・ int newMethod(int a){ for( int j = 0 ; j < a ; j++ ){ ・・・ } return a; } ・・・ The value of metrics for “Extract Method”
Target Unit : Method Unit • DCH >= 1 The value of metrics for “Pull up Method”
ShapShot of Cancer Metric Graph Clone class list clone category selection Panel
Specifying code clones • User can specify code clones using Metric Graph and Clone Category Selection Panel • Metrics Graph : Specifying based on metric values • Clone Category Selection Panel : Specifying based on unit of code clone • Clone class list shows only specified clone classes
LEN POP DFL RVK RVN DCH Specifying based on metrics values
Method Unit While Unit LEN POP DFL RVK RVN DCH Specifying based on unit of code clone Method While ・・・・・・・・・・・・・・ ・・・・・・・・・・・・・
Case study • Target software : Ant 1.5.4, which is an open source java software. • LOC : about 15k • The result • Using “Pull up Method”, 2 clone classes are removed. • Using “Extract Method”, 1 clone class is removed
Clone Pair Clone Class Code Clone • Code clone is a code fragment in source files that is identical or similar to another • Code clone is one of factors that make software maintenance more difficult. • If some faults are found in a code clone, it is necessary to consider pros and cons of modification in its all code clones.
Example Value of RVK, RVN • Variables, “a” and “b” which are used in code clone, but defined outside, are used 2 and 3 times. • RVK : 1 + 1 = 2 • RVN : 2 + 3 = 5
Example values of DCH • If all code fragments in a clone class are in same class, • DCH : 0 • If all code fragments in a clone class are in a certain class and its child classes, • DCH : 1 • If classes which include code fragments of a certain clone class don’t have common parent class, • DCH : -1