300 likes | 321 Views
On The Detection of Code Clone With Concern Analysis. Paiva, Alexandre; Alves, Johnatan ; Figueiredo, Eduardo Software Engineering Lab ( LabSoft ) – Federal University of Minas Gerais (UFMG) Belo Horizonte – MG – Brazil { ampaiva , johnatan , figueiredo }@dcc.ufmg.br. Code Clone.
E N D
On The Detection of Code Clone With Concern Analysis Paiva, Alexandre; Alves, Johnatan; Figueiredo, Eduardo Software Engineering Lab (LabSoft) – Federal University of Minas Gerais (UFMG)Belo Horizonte – MG – Brazil {ampaiva, johnatan, figueiredo}@dcc.ufmg.br
Code Clone • Definition • Copyand Paste [2] • Similarity (notnecessarilyidentical) is clone [3]
ResearchImportance • Software Engineering • Software quality, complexity, architecture, refactoring, evolution, licensing, plagiarism, and so on. • Workshops • http://softwareclones.org/iwsc2014/ • http://iwsc2015.usask.ca/
Example ofCode Clone Type 1
Detection Tools • Textbased • Token based • Treebased • Graphbased (PDG) • Metricsbased • Hybridtechinique
ConcernMetrics • Quantify properties of concerns • Scattering • Tangling Statistics Candidate Date Log Persistence ExceptionHandling
Motivation • Code clones shouldbeavoided • Concernscatteringshouldbeavoided • Concernscattering as key for locatingcode clones • Concernscatteringdiffersfrom PDG when ignores local variablesandstatements
Type 4 • Semanticisthesame intfoo1(List<Clone> clones){ inttotal = 0; for(inti = 0; i < clones.size(); i++) { Clone clone = clones.get(i); if(!clone.isAvailable()) continue; total++; } returntotal; } int foo2(List<Clone> list) { intcount = 1, clones = 0; while(count <= list.size()) { if (list.get(count-1).isAvailable()) { clones= clones+1; } count= count+1; } returnclones; }
Type 4 • Semanticisthesame intfoo1(List<Clone> clones){ inttotal = 0; for(inti = 0; i < clones.size(); i++) { Clone clone = clones.get(i); if(!clone.isAvailable()) continue; total++; } returntotal; } int foo2(List<Clone> list) { intcount = 1, clones = 0; while(count <= list.size()) { if (list.get(count-1).isAvailable()) { clones= clones+1; } count= count+1; } returnclones; }
PROPOSAL APPROACH I GitHub BitBucket Other HashofCalls ListofMethods Stringname Listcalls Stringcall Listmethods (position) .java Comparision Concerns II call: java.util.List.add methods: health.data.UnitRDB.getPartial 2 health.view.Address.mount 3 health.data.Employee.save 6 name: health.data.UnitRDB.getPartial calls: java.util.ArrayList.ArrayList lib.util.Iterator.Iterator java.util.List.add java.sql.ResultSet.next • Duplications
SequenceConcept • 3 calls in sequence intfoo1(List<Clone> clones){ inttotal = 0; for(inti = 0; i < clones.size(); i++) { Clone clone = clones.get(i); if(!clone.isAvailable()) continue; total++; } returntotal; } int foo2(List<Clone> list) { intcount = 1, clones = 0; while(count <= list.size()) { if (list.get(count-1).isAvailable()) { clones= clones+1; } count= count+1; } returnclones; }
Clone Detected – Petstore - getCategories public Page getCategories(int start, intcount, Localelocale) throwsCatalogDAOSysException { Connection connection = null; PreparedStatementstatement = null; ResultSetresultSet = null; try { connection = getDataSource().getConnection(); String[] parameterValues = newString[] { locale.toString() }; if (TRACE) { printSQLStatement(sqlStatements, XML_GET_CATEGORIES, parameterValues); } statement = buildSQLStatement(connection, sqlStatements, XML_GET_CATEGORIES, parameterValues); resultSet = statement.executeQuery(); if (start >= 0 && resultSet.absolute(start + 1)) { booleanhasNext = false; Listcategories = newArrayList(); do { categories.add(newCategory(resultSet.getString(1).trim(), resultSet.getString(2), resultSet.getString(3))); } while ((hasNext = resultSet.next()) && (--count > 0)); returnnew Page(categories, start, hasNext); } returnPage.EMPTY_PAGE; ...
Clone Detected – Petstore – getItems public Page getItems(StringproductID, int start, intcount, Localelocale) throwsCatalogDAOSysException { Connection connection = null; PreparedStatementstatement = null; ResultSetresultSet = null; try { connection = getDataSource().getConnection(); String[] parameterValues = newString[] { locale.toString(), productID }; if (TRACE) { printSQLStatement(sqlStatements, XML_GET_ITEMS, parameterValues); } statement = buildSQLStatement(connection, sqlStatements, XML_GET_ITEMS, parameterValues); resultSet = statement.executeQuery(); if (start >= 0 && resultSet.absolute(start + 1)) { booleanhasNext = false; Listitems = newArrayList(); do { int i = 1; items.add(new Item(...)); } while ((hasNext = resultSet.next()) && (--count > 0)); returnnew Page(items, start, hasNext); } returnPage.EMPTY_PAGE; ...
ComparisonwithOtherDetectors • CloneDR • http://www.semanticdesigns.com/Products/Clone • iClones • http://www.softwareclones.org
ExerciseGoal • 12 snippets • To decide: Iscode clone?
Expectedanswers • Codeis similar, butisnotcloned. • The sequencebelowshould NOT bemovedto a common method void foo1() { System.out.println("fileName: "+fileName+", orderId="+invoiceXDE.getOrderId()+", lineItemIds="+invoiceCDE.getLineItemIds()); System.exit(0); } voidfoo2() { System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Address: " + address); System.exit(0); }
Expectedanswers • Codeis clone. • The sequencebelowshouldbemovedto a common method void foo1() { System.out.println("fileName: "+fileName+", orderId="+invoiceXDE.getOrderId()+", lineItemIds="+invoiceCDE.getLineItemIds()); System.exit(0); } voidfoo2() { System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Address: " + address); System.exit(0); }
Final Tips • The code clone isnottheentiremethod • Code clone isjust a pieceofcode, maybethreeor more lines
ExerciseSurvey • http://esurv.org/?u=iscodeclone
ThreatsofValidation • Cases wererandomicallyanalysed • This approach canbealreadycoveredbyanotherdetectionmethod • Surveywasnotrepresentative • Fewsubjects • Bad cases • Background
Conclusions • Potential tool for Type 4 clone detection • Found clones whichwerenotfoundbyother tools • Performance wasnotfocus, butlarge system wasanalysed • Tool canbeimproved • Hybridsolutionmaybeanoption
Thanks! This work was partially supported by CNPq (grant Universal 485907/2013-5) and FAPEMIG (grants APQ-02532-12 and PPM-00382-14)