public Collection generateFiles(Collection elements, String path, boolean deps) { List ret = new ArrayList(); startFileGeneration(); for (Iterator it = elements.iterator(); it.hasNext(); ) { Object elem = it.next(); ret.addAll(generateFilesForElem(elem, path, deps)); } endFileGeneration(); return ret; }
private Collection generateFilesForElem(Object o, String path, boolean deps) { Vector ret = new Vector(); if (generatedFiles.contains(o)) { return ret; // generated already } if (!getFacade().isAClass(o) && !getFacade().isAInterface(o)) { return ret; // not a class or interface } while (isAInnerClass(o)) { o = getFacade().getNamespace(o); } String pathname = null; // use unique section for both passes -> allow move of // normal function body to inline and vice versa if (Section.getUseSect() != Section.SECT_NONE) { sect = new Section(); /* * 2002-11-28 Achim Spangler * first read header and source file into global/unique section */ for (generatorPass = HEADER_PASS; generatorPass <= SOURCE_PASS; generatorPass++) { pathname = createDirectoriesPathname(o, path); //String pathname = path + filename; // TODO: package, project basepath, tagged values to configure File f = new File(pathname); if (f.exists()) { LOG.info("Generating (updated) " + f.getPath()); sect.read(pathname); File bakFile = new File(pathname + ".bak"); if (bakFile.exists()) { bakFile.delete(); } f.renameTo(bakFile); } else { LOG.info("Generating (new) " + f.getPath()); } } } Set dependencies = null; if (deps) dependencies = new TreeSet(); /** * 2002-11-28 Achim Spangler * run basic generation function two times for header and implementation */ for (generatorPass = HEADER_PASS; generatorPass <= SOURCE_PASS; generatorPass++) { pathname = createDirectoriesPathname(o, path); String fileContent = generateFileAsString(o, pathname); if (fileContent.length() == 0) continue; BufferedWriter fos = null; //String pathname = path + filename; // TODO: package, project basepath, tagged values to configure File f = new File(pathname); try { // TODO: This is using the default platform character encoding // specifying an encoding will produce more predictable results fos = new BufferedWriter (new FileWriter (f)); writeTemplate(o, path, fos); fos.write(fileContent); fos.newLine(); } catch (IOException exp) { } finally { try { if (fos != null) fos.close(); } catch (IOException exp) { LOG.error("FAILED: " + f.getPath()); } } LOG.info("written: " + pathname); if (Section.getUseSect() != Section.SECT_NONE) { // output lost sections only in the second path // -> sections which are moved from header(inline) to source // file are prevented to be outputted in header pass File outFile = new File(pathname + ".out"); if (outFile.exists()) { outFile.delete(); // remove junk } if (generatorPass == HEADER_PASS) { sect.write(pathname, indent, false); } else { sect.write(pathname, indent, true); } if (outFile.exists()) { assert f.exists(); f.delete(); outFile.renameTo(f); LOG.info("added sections to: " + pathname); } } LOG.info("----- end updating " + pathname + "-----"); ret.add(pathname); if (deps) { dependencies.add(includeCls); dependencies.add(predeclCls); } } cleanupGenerator(); // reset generator pass to NONE for the notation to be correct generatorPass = NONE_PASS; generatedFiles.add(o); if (deps) { Iterator it = dependencies.iterator(); while (it.hasNext()) { ret.add(generateFilesForElem(it.next(), path, deps)); } } return ret; }
Clone fragments detected by clone detection tool
File path: /ArgoUML-0.34-src/argouml-cpp/src/org/argouml/language/cpp/generator/GeneratorCpp.java File path: /ArgoUML-0.34-src/argouml-cpp/src/org/argouml/language/cpp/generator/GeneratorCpp.java
Method name: Collection generateFiles(Collection, String, boolean) Method name: Collection generateFilesForElem(Object, String, boolean)
Number of AST nodes: 3 Number of AST nodes: 2
1
public Collection generateFiles(Collection elements, String path,
1
private Collection generateFilesForElem(Object o,
2
            boolean deps) {
2
                                            String path, boolean deps) {
3
        List ret = new ArrayList();
3
        Vector ret = new Vector();
4
        startFileGeneration();
4
        if (generatedFiles.contains(o)) {
5
        for (Iterator it = elements.iterator(); it.hasNext(); ) {
5
            return ret; // generated already
6
            Object elem = it.next();
6
        }
7
            ret.addAll(generateFilesForElem(elem, path, deps));
7
        if (!getFacade().isAClass(o) && !getFacade().isAInterface(o)) {
8
        }
8
            return ret; // not a class or interface
9
        endFileGeneration();
9
        }
10
        return ret;
10
        while (isAInnerClass(o)) {
11
    }
11
            o = getFacade().getNamespace(o);
12
        }
13
        String pathname = null;
14
15
        // use unique section for both passes -> allow move of
16
        // normal function body to inline and vice versa
17
        if (Section.getUseSect() != Section.SECT_NONE) {
18
            sect = new Section();
19
20
            /*
21
             * 2002-11-28 Achim Spangler
22
             * first read header and source file into global/unique section
23
             */
24
            for (generatorPass = HEADER_PASS;
25
                 generatorPass <= SOURCE_PASS;
26
                 generatorPass++) {
27
                pathname = createDirectoriesPathname(o, path);
28
                //String pathname = path + filename;
29
                // TODO: package, project basepath, tagged values to configure
30
                File f = new File(pathname);
31
                if (f.exists()) {
32
                    LOG.info("Generating (updated) " + f.getPath());
33
                    sect.read(pathname);
34
                    File bakFile = new File(pathname + ".bak");
35
                    if (bakFile.exists()) {
36
                        bakFile.delete();
37
                    }
38
                    f.renameTo(bakFile);
39
                } else {
40
                    LOG.info("Generating (new) " + f.getPath());
41
                }
42
            }
43
        }
44
45
        Set dependencies = null;
46
        if (deps) dependencies = new TreeSet();
47
        /**
48
         * 2002-11-28 Achim Spangler
49
         * run basic generation function two times for header and implementation
50
         */
51
        for (generatorPass = HEADER_PASS;
52
             generatorPass <= SOURCE_PASS;
53
             generatorPass++) {
54
            pathname = createDirectoriesPathname(o, path);
55
            String fileContent = generateFileAsString(o, pathname);
56
            if (fileContent.length() == 0) continue;
57
            BufferedWriter fos = null;
58
            //String pathname = path + filename;
59
            // TODO: package, project basepath, tagged values to configure
60
            File f = new File(pathname);
61
            try {
62
                // TODO: This is using the default platform character encoding
63
                // specifying an encoding will produce more predictable results
64
                fos = new BufferedWriter (new FileWriter (f));
65
                writeTemplate(o, path, fos);
66
                fos.write(fileContent);
67
                fos.newLine();
68
            }
69
            catch (IOException exp) { }
70
            finally {
71
                try {
72
                    if (fos != null) fos.close();
73
                }
74
                catch (IOException exp) {
75
                    LOG.error("FAILED: " + f.getPath());
76
                }
77
            }
78
79
            LOG.info("written: " + pathname);
80
81
            if (Section.getUseSect() != Section.SECT_NONE) {
82
                // output lost sections only in the second path
83
                // -> sections which are moved from header(inline) to source
84
                // file are prevented to be outputted in header pass
85
                File outFile = new File(pathname + ".out");
86
                if (outFile.exists()) {
87
                    outFile.delete(); // remove junk
88
                }
89
                if (generatorPass == HEADER_PASS)   {
90
                    sect.write(pathname, indent, false);
91
                } else {
92
                    sect.write(pathname, indent, true);
93
                }
94
95
                if (outFile.exists()) {
96
                    assert f.exists();
97
                    f.delete();
98
                    outFile.renameTo(f);
99
                    LOG.info("added sections to: " + pathname);
100
                }
101
            }
102
            LOG.info("----- end updating " + pathname + "-----");
103
            ret.add(pathname);
104
            if (deps) {
105
                dependencies.add(includeCls);
106
                dependencies.add(predeclCls);
107
            }
108
        }
109
        cleanupGenerator();
110
        // reset generator pass to NONE for the notation to be correct
111
        generatorPass = NONE_PASS;
112
        generatedFiles.add(o);
113
        if (deps) {
114
            Iterator it = dependencies.iterator();
115
            while (it.hasNext()) {
116
                ret.add(generateFilesForElem(it.next(), path, deps));
117
            }
118
        }
119
        
120
        return ret;
121
    }
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements4
    Number of unmapped statements in the first code fragment0
    Number of unmapped statements in the second code fragment0
    Time elapsed for statement mapping (ms)0.0
    Similarity Score0.571
    Clone typeType 2
    Mapped Statements
    ID Statement ID Statement
    3
    for (Iterator it = elements.iterator(); it.hasNext(); )
    3
    for (Iterator it = elements.iterator(); it.hasNext(); )
    60
    while (it.hasNext())
    Differences
    Expression1Expression2Difference
    java.util.Collectionjava.util.SetVARIABLE_TYPE_MISMATCH
    Preondition Violations
    Type java.util.Collection of variable elements does not match with type java.util.Set of variable dependencies
    • Make classes java.util.Collection and java.util.Set extend a common superclass
    60
    while (it.hasNext())
    4
    Object elem = it.next();
                                                      
    5
    ret.addAll(generateFilesForElem(elem, path, deps));
    5
    ret.addAll(generateFilesForElem(elem, path, deps));
    61
    ret.add(generateFilesForElem(it.next(), path, deps));
    Differences
    Expression1Expression2Difference
    addAlladdMETHOD_INVOCATION_NAME_MISMATCH
    elemit.next()TYPE_COMPATIBLE_REPLACEMENT
    java.util.Listjava.util.VectorSUBCLASS_TYPE_MISMATCH
    61
    ret.add(generateFilesForElem(it.next(), path, deps));
    Precondition Violations (1)
    Row Violation
    1Type java.util.Collection of variable elements does not match with type java.util.Set of variable dependencies