Software Engineering Lab (LabSoft) 1/29 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,
Software Engineering Lab (LabSoft) 2/29 Code Clone Definition Copy and Paste [2] Similarity (not necessarily identical) is clone [3]
Software Engineering Lab (LabSoft) 3/29 Research Importance Software Engineering Software quality, complexity, architecture, refactoring, evolution, licensing, plagiarism, and so on. Workshops
Software Engineering Lab (LabSoft) 4/29 IWSC2014
Software Engineering Lab (LabSoft) 5/29 IWSC2015
Software Engineering Lab (LabSoft) 6/29 Example of Code Clone Type 1
Software Engineering Lab (LabSoft) 7/29 Detection Tools Text based Token based Tree based Graph based (PDG) Metrics based Hybrid techinique
Software Engineering Lab (LabSoft) 8/29 Concern Metrics Quantify properties of concerns Scattering Tangling CandidateDate Statistics Log Persistence Exception Handling
Software Engineering Lab (LabSoft) 9/29 Motivation Code clones should be avoided Concern scattering should be avoided Concern scattering as key for locating code clones Concern scattering differs from PDG when ignores local variables and statements
Software Engineering Lab (LabSoft) 10/29 Sonar (PurchaseOrderEJB.java x SupplierOrderEJB.java)
Software Engineering Lab (LabSoft) 11/29 Type 4 Semantic is the same int foo1(List clones){ int total = 0; for (int i = 0; i < clones.size(); i++) { Clone clone = clones.get(i); if (!clone.isAvailable()) continue; total++; } return total; } int foo2(List list) { int count = 1, clones = 0; while (count <= list.size()) { if (list.get(count-1).isAvailable()) { clones = clones+1; } count = count+1; } return clones; }
Software Engineering Lab (LabSoft) 12/29 Type 4 Semantic is the same int foo1(List clones){ int total = 0; for (int i = 0; i < clones.size(); i++) { Clone clone = clones.get(i); if (!clone.isAvailable()) continue; total++; } return total; } int foo2(List list) { int count = 1, clones = 0; while (count <= list.size()) { if (list.get(count-1).isAvailable()) { clones = clones+1; } count = count+1; } return clones; }
Software Engineering Lab (LabSoft) 13/29 I I String name List calls String name List calls name: health.data.UnitRDB.getPartial calls: java.util.ArrayList.ArrayList lib.util.Iterator.Iterator java.util.List.add java.sql.ResultSet.next List of Methods II String call List methods (position) String call List methods (position) call: java.util.List.add methods: health.data.UnitRDB.getPartial 2 health.view.Address.mount 3 health.data.Employee.save 6 Hash of Calls PROPOSAL APPROACH Project Sources... java xmlpngjava Duplications GitHub BitBucket Other GitHub BitBucket Other ConcernsComparision.java
Software Engineering Lab (LabSoft) 14/29 Systems Analyzed System Calls TotalDistinct Restaurantr Telestrada Learn Engh Mobile Media Ecommerce Health Watcher Ecommerce PetStore Junit Facebook Android Restaurant Open Maven ArgoUML JBoss
Software Engineering Lab (LabSoft) 15/29 Sequence Concept 3 calls in sequence int foo1(List clones){ int total = 0; for (int i = 0; i < clones.size(); i++) { Clone clone = clones.get(i); if (!clone.isAvailable()) continue; total++; } return total; } int foo2(List list) { int count = 1, clones = 0; while (count <= list.size()) { if (list.get(count-1).isAvailable()) { clones = clones+1; } count = count+1; } return clones; }
Software Engineering Lab (LabSoft) 16/29 Clones per Sequence System CallsSequences of Calls TotalDistinct Restaurantr Telestrada Learn Engh Mobile Media Ecommerce Health Watcher Ecommerce PetStore Junit Facebook Android Restaurant Open Maven ArgoUML JBoss
Software Engineering Lab (LabSoft) 17/29 public Page getCategories(int start, int count, Locale locale) throws CatalogDAOSysExce ption { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; try { connection = getDataSource().getConnection(); String[] parameterValues = new String[] { locale.toString() }; if (TRACE) { printSQLStatement(sqlStatements, XML_GET_CATEGORIES, parameterValues); } statement = buildSQLStatement(connection, sqlStatements, XML_GET_CATEGORIES, pa rameterValues); resultSet = statement.executeQuery(); if (start >= 0 && resultSet.absolute(start + 1)) { boolean hasNext = false; List categories = new ArrayList(); do { categories.add(new Category(resultSet.getString(1).trim(), resultSet.ge tString(2), resultSet.getString(3))); } while ((hasNext = resultSet.next()) && (--count > 0)); return new Page(categories, start, hasNext); } return Page.EMPTY_PAGE;... Clone Detected – Petstore - getCategories
Software Engineering Lab (LabSoft) 18/29 Clone Detected – Petstore – getItems public Page getItems(String productID, int start, int count, Locale locale) throws Cata logDAOSysException { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; try { connection = getDataSource().getConnection(); String[] parameterValues = new String[] { locale.toString(), productID }; if (TRACE) { printSQLStatement(sqlStatements, XML_GET_ITEMS, parameterValues); } statement = buildSQLStatement(connection, sqlStatements, XML_GET_ITEMS, paramet erValues); resultSet = statement.executeQuery(); if (start >= 0 && resultSet.absolute(start + 1)) { boolean hasNext = false; List items = new ArrayList(); do { int i = 1; items.add(new Item(...)); } while ((hasNext = resultSet.next()) && (--count > 0)); return new Page(items, start, hasNext); } return Page.EMPTY_PAGE;...
Software Engineering Lab (LabSoft) 19/29 Comparison with Other Detectors CloneDR iClones
Software Engineering Lab (LabSoft) 20/29 Exercise Goal 12 snippets To decide: Is code clone?
Software Engineering Lab (LabSoft) 21/29 Exercise Example
Software Engineering Lab (LabSoft) 22/29 Expected answers Code is similar, but is not cloned. The sequence below should NOT be moved to a common method void foo1() { System.out.println("fileName: "+fileName+", orderId="+invoiceXDE.getOrderId()+", lineItemIds="+invoiceCDE.getLineItemIds()); System.exit(0); } void foo2() { System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Address: " + address); System.exit(0); }
Software Engineering Lab (LabSoft) 23/29 Expected answers Code is clone. The sequence below should be moved to a common method void foo1() { System.out.println("fileName: "+fileName+", orderId="+invoiceXDE.getOrderId()+", lineItemIds="+invoiceCDE.getLineItemIds()); System.exit(0); } void foo2() { System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Address: " + address); System.exit(0); }
Software Engineering Lab (LabSoft) 24/29 Final Tips The code clone is not the entire method Code clone is just a piece of code, maybe three or more lines
Software Engineering Lab (LabSoft) 25/29 Exercise Survey
Software Engineering Lab (LabSoft) 26/29 Survey Results
Software Engineering Lab (LabSoft) 27/29 Threats of Validation Cases were randomically analysed This approach can be already covered by another detection method Survey was not representative Few subjects Bad cases Background
Software Engineering Lab (LabSoft) 28/29 Conclusions 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 (LabSoft) 29/29 Thanks! This work was partially supported by CNPq (grant Universal /2013-5) and FAPEMIG (grants APQ and PPM )