On The Detection of Code Clone With Concern

  • Slides: 29
Download presentation
On The Detection of Code Clone With Concern Analysis Paiva, Alexandre; Alves, Johnatan; Figueiredo,

On The Detection of Code Clone With Concern Analysis Paiva, Alexandre; Alves, Johnatan; Figueiredo, Eduardo Software Engineering Lab (Lab. Soft) – Federal University of Minas Gerais (UFMG) Belo Horizonte – MG – Brazil {ampaiva, johnatan, figueiredo}@dcc. ufmg. br Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 1/29

Code Clone o Definition n n Copy and Paste [2] Similarity (not necessarily identical)

Code Clone o Definition n n Copy and Paste [2] Similarity (not necessarily identical) is clone [3] Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 2/29

Research Importance o Software Engineering n o Software quality, complexity, architecture, refactoring, evolution, licensing,

Research Importance o Software Engineering n o Software quality, complexity, architecture, refactoring, evolution, licensing, plagiarism, and so on. Workshops n n http: //softwareclones. org/iwsc 2014/ http: //iwsc 2015. usask. ca/ Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 3/29

IWSC 2014 Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 4/29

IWSC 2014 Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 4/29

IWSC 2015 Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 5/29

IWSC 2015 Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 5/29

Example of Code Clone Type 1 Software Engineering Lab (Lab. Soft) http: //labsoft. dcc.

Example of Code Clone Type 1 Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 6/29

Detection Tools o o o Text based Token based Tree based Graph based (PDG)

Detection Tools o o o Text based Token based Tree based Graph based (PDG) Metrics based Hybrid techinique Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 7/29

Concern Metrics o Quantify properties of concerns n n Scattering Tangling Candidate Date Statistics

Concern Metrics o Quantify properties of concerns n n Scattering Tangling Candidate Date Statistics Log Persistence Exception Handling Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 8/29

Motivation o o o Code clones should be avoided Concern scattering as key for

Motivation o o o Code clones should be avoided Concern scattering as key for locating code clones n Concern scattering differs from PDG when ignores local variables and statements Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 9/29

Sonar (Purchase. Order. EJB. java x Supplier. Order. EJB. java) Software Engineering Lab (Lab.

Sonar (Purchase. Order. EJB. java x Supplier. Order. EJB. java) Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 10/29

Type 4 o Semantic is the same int foo 1(List<Clone> clones){ int total =

Type 4 o Semantic is the same int foo 1(List<Clone> clones){ int total = 0; for (int i = 0; i < clones. size(); i++) { Clone clone = clones. get(i); if (!clone. is. Available()) continue; total++; } return total; } int foo 2(List<Clone> list) { int count = 1, clones = 0; while (count <= list. size()) { if (list. get(count-1). is. Available()) { clones = clones+1; } count = count+1; } return clones; } Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 11/29

Type 4 o Semantic is the same int foo 1(List<Clone> clones){ int total =

Type 4 o Semantic is the same int foo 1(List<Clone> clones){ int total = 0; for (int i = 0; i < clones. size(); i++) { Clone clone = clones. get(i); if (!clone. is. Available()) continue; total++; } return total; } int foo 2(List<Clone> list) { int count = 1, clones = 0; while (count <= list. size()) { if (list. get(count-1). is. Available()) { clones = clones+1; } count = count+1; } return clones; } Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 12/29

PROPOSAL APPROACH List of Methods I String name List calls Git. Hub Bit. Bucket

PROPOSAL APPROACH List of Methods I String name List calls Git. Hub Bit. Bucket Other Project Sources png jav a xml java Comparision Search I I Concerns Hash I . java . . . name: health. data. Unit. RDB. get. Partial calls: java. util. Array. List lib. util. Iterator java. util. List. add java. sql. Result. Set. next II Parser Hash of Calls String call List methods (position) Duplications call: java. util. List. add Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ methods: health. data. Unit. RDB. get. Partial 2 health. view. Address. mount 3 health. data. Employee. save 6 13/29

Systems Analyzed Calls System Total Distinct Restaurantr 399 230 Telestrada 688 304 Learn Engh

Systems Analyzed Calls System Total Distinct Restaurantr 399 230 Telestrada 688 304 Learn Engh 920 381 Mobile Media 1419 449 Ecommerce 21 1706 561 Health Watcher 2301 491 Ecommerce 2 5987 1982 Pet. Store 6105 2352 Junit 8221 2932 Facebook Android 12773 4428 Restaurant Open 19416 5616 Maven 31267 9543 Argo. UML 82435 23582 160040 51755 JBoss Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 14/29

Sequence Concept o 3 calls in sequence int foo 1(List<Clone> clones){ int total =

Sequence Concept o 3 calls in sequence int foo 1(List<Clone> clones){ int total = 0; for (int i = 0; i < clones. size(); i++) { Clone clone = clones. get(i); if (!clone. is. Available()) continue; total++; } return total; } int foo 2(List<Clone> list) { int count = 1, clones = 0; while (count <= list. size()) { if (list. get(count-1). is. Available()) { clones = clones+1; } count = count+1; } return clones; } Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 15/29

Clones per Sequence Calls System Total Sequences of Calls Distinct 3 4 5 1

Clones per Sequence Calls System Total Sequences of Calls Distinct 3 4 5 1 7 8 9 10 11 12 13 14 15 Restaurantr 399 230 Telestrada 688 304 25 12 2 Learn Engh 920 381 50 31 7 8 6 5 5 6 5 3 Mobile Media 1419 449 101 60 47 16 5 6 3 3 3 1 Ecommerce 21 1706 561 617 308 106 68 77 73 69 67 70 25 3 1 3 Health Watcher 2301 491 373 207 154 135 30 29 75 75 7 10 8 5 3 Ecommerce 2 5987 1982 292 184 104 77 62 46 46 38 41 28 31 33 26 Pet. Store 6105 2352 867 266 165 104 49 33 34 13 7 8 3 4 3 Junit 8221 2932 895 291 190 160 85 63 62 32 24 16 13 1 1 Facebook Android 12773 4428 1041 361 210 153 88 71 56 45 29 16 15 18 10 Restaurant Open 19416 5616 3105 2225 1460 1041 893 142 138 89 42 30 31 18 14 Maven 31267 9543 4362 1924 1894 847 889 488 627 277 344 142 104 75 Argo. UML 82435 23582 18726 8159 3771 2189 1646 441 338 300 91 73 54 45 27 160040 51755 46333 18986 7378 4308 2554 1815 1414 866 664 561 405 347 253 JBoss 1 6 1 Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 2 1 1 16/29

Clone Detected – Petstore - get. Categories public Page get. Categories(int start, int count,

Clone Detected – Petstore - get. Categories public Page get. Categories(int start, int count, Locale locale) throws Catalog. DAOSys. Exce ption { Connection connection = null; Prepared. Statement statement = null; Result. Set result. Set = null; try { connection = get. Data. Source(). get. Connection(); String[] parameter. Values = new String[] { locale. to. String() }; if (TRACE) { print. SQLStatement(sql. Statements, XML_GET_CATEGORIES, parameter. Values); } statement = build. SQLStatement(connection, sql. Statements, XML_GET_CATEGORIES, pa rameter. Values); result. Set = statement. execute. Query(); if (start >= 0 && result. Set. absolute(start + 1)) { boolean has. Next = false; List categories = new Array. List(); do { categories. add( new Category(result. Set. get. String(1). trim(), result. Set. ge t. String(2), result. Set. get. String(3))); } while ((has. Next = result. Set. next()) && (--count > 0)); return new Page(categories, start, has. Next); } return Page. EMPTY_PAGE; . . . Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 17/29

Clone Detected – Petstore – get. Items public Page get. Items(String product. ID, int

Clone Detected – Petstore – get. Items public Page get. Items(String product. ID, int start, int count, Locale locale) throws Cata log. DAOSys. Exception { Connection connection = null; Prepared. Statement statement = null; Result. Set result. Set = null; try { connection = get. Data. Source(). get. Connection(); String[] parameter. Values = new String[] { locale. to. String(), product. ID }; if (TRACE) { print. SQLStatement(sql. Statements, XML_GET_ITEMS, parameter. Values); } statement = build. SQLStatement(connection, sql. Statements, XML_GET_ITEMS, paramet er. Values); result. Set = statement. execute. Query(); if (start >= 0 && result. Set. absolute(start + 1)) { boolean has. Next = false; List items = new Array. List(); do { int i = 1; items. add( new Item(. . . )); } while ((has. Next = result. Set. next()) && (--count > 0)); return new Page(items, start, has. Next); } return Page. EMPTY_PAGE; . . . Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 18/29

Comparison with Other Detectors o Clone. DR n o http: //www. semanticdesigns. com/Products/Clone i.

Comparison with Other Detectors o Clone. DR n o http: //www. semanticdesigns. com/Products/Clone i. Clones n http: //www. softwareclones. org Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 19/29

Exercise Goal o o 12 snippets To decide: Is code clone? Software Engineering Lab

Exercise Goal o o 12 snippets To decide: Is code clone? Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 20/29

Exercise Example Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 21/29

Exercise Example Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 21/29

Expected answers o Code is similar, but is not cloned. n The sequence below

Expected answers o Code is similar, but is not cloned. n The sequence below should NOT be moved to a common method void foo 1() { System. out. println("file. Name: "+file. Name+", order. Id="+invoice. XDE. get. Order. Id()+ ", line. Item. Ids="+invoice. CDE. get. Line. Item. Ids()); System. exit(0); } void foo 2() { System. out. println("Name: " + name); System. out. println("Age: " + age); System. out. println("Address: " + address); System. exit(0); } Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 22/29

Expected answers o Code is clone. n The sequence below should be moved to

Expected answers o Code is clone. n The sequence below should be moved to a common method void foo 1() { System. out. println("file. Name: "+file. Name+", order. Id="+invoice. XDE. get. Order. Id()+ ", line. Item. Ids="+invoice. CDE. get. Line. Item. Ids()); System. exit(0); } void foo 2() { System. out. println("Name: " + name); System. out. println("Age: " + age); System. out. println("Address: " + address); System. exit(0); } Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 23/29

Final Tips o o The code clone is not the entire method Code clone

Final Tips o o The code clone is not the entire method Code clone is just a piece of code, maybe three or more lines Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 24/29

Exercise Survey o http: //esurv. org/? u=iscodeclone Software Engineering Lab (Lab. Soft) http: //labsoft.

Exercise Survey o http: //esurv. org/? u=iscodeclone Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 25/29

Survey Results Agreements per Case 25 Agreements 20 15 10 5 0 1 2

Survey Results Agreements per Case 25 Agreements 20 15 10 5 0 1 2 3 4 5 6 7 8 9 10 11 12 Case Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 26/29

Threats of Validation o o o Cases were randomically analysed This approach can be

Threats of Validation o o o Cases were randomically analysed This approach can be already covered by another detection method Survey was not representative n n n Few subjects Bad cases Background Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 27/29

Conclusions o o o Potential tool for Type 4 clone detection Found clones which

Conclusions o o o Potential tool for Type 4 clone detection Found clones which were not found by other tools Performance was not focus, but large system was analysed Tool can be improved Hybrid solution may be an option Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 28/29

Thanks! This work was partially supported by CNPq (grant Universal 485907/2013 -5) and FAPEMIG

Thanks! This work was partially supported by CNPq (grant Universal 485907/2013 -5) and FAPEMIG (grants APQ-02532 -12 and PPM-00382 -14) Software Engineering Lab (Lab. Soft) http: //labsoft. dcc. ufmg. br/ 29/29