private void scanStrings(BibtexDatabase inMem, BibtexDatabase onTmp, BibtexDatabase onDisk) { int nTmp = onTmp.getStringCount(), nDisk = onDisk.getStringCount(); if ((nTmp == 0) && (nDisk == 0)) return; HashSet<Object> used = new HashSet<Object>(); HashSet<Object> usedInMem = new HashSet<Object>(); HashSet<String> notMatched = new HashSet<String>(onTmp.getStringCount()); // First try to match by string names. //int piv2 = -1; mainLoop: for (String key : onTmp.getStringKeySet()){ BibtexString tmp = onTmp.getString(key); // for (int j=piv2+1; j<nDisk; j++) for (String diskId : onDisk.getStringKeySet()){ if (!used.contains(diskId)) { BibtexString disk = onDisk.getString(diskId); if (disk.getName().equals(tmp.getName())) { // We have found a string with a matching name. if ((tmp.getContent() != null) && !tmp.getContent().equals(disk.getContent())) { // But they have nonmatching contents, so we've found a change. BibtexString mem = findString(inMem, tmp.getName(), usedInMem); if (mem != null) changes.add(new StringChange(mem, tmp, tmp.getName(), mem.getContent(), tmp.getContent(), disk.getContent())); else changes.add(new StringChange(null, tmp, tmp.getName(), null, tmp.getContent(), disk.getContent())); } used.add(diskId); //if (j==piv2) // piv2++; continue mainLoop; } } } // If we get here, there was no match for this string. notMatched.add(tmp.getId()); } // See if we can detect a name change for those entries that we couldn't match. if (notMatched.size() > 0) { for (Iterator<String> i = notMatched.iterator(); i.hasNext();){ BibtexString tmp = onTmp.getString(i.next()); // If we get to this point, we found no string with matching name. See if we // can find one with matching content. for (String diskId : onDisk.getStringKeySet()){ if (!used.contains(diskId)) { BibtexString disk = onDisk.getString(diskId); if (disk.getContent().equals(tmp.getContent())) { // We have found a string with the same content. It cannot have the same // name, or we would have found it above. // Try to find the matching one in memory: BibtexString bsMem = null; for (String memId : inMem.getStringKeySet()){ BibtexString bsMem_cand = inMem.getString(memId); if (bsMem_cand.getContent().equals(disk.getContent()) && !usedInMem.contains(memId)) { usedInMem.add(memId); bsMem = bsMem_cand; break; } } changes.add(new StringNameChange(bsMem, tmp, bsMem.getName(), tmp.getName(), disk.getName(), tmp.getContent())); i.remove(); used.add(diskId); } } } } } if (notMatched.size() > 0) { // Still one or more non-matched strings. So they must have been removed. for (Iterator<String> i = notMatched.iterator(); i.hasNext(); ) { String nmId = i.next(); BibtexString tmp = onTmp.getString(nmId); BibtexString mem = findString(inMem, tmp.getName(), usedInMem); if (mem != null) { // The removed string is not removed from the mem version. changes.add(new StringRemoveChange(tmp, tmp, mem)); } } } // Finally, see if there are remaining strings in the disk database. They // must have been added. for (Iterator<String> i=onDisk.getStringKeySet().iterator(); i.hasNext();) { String diskId = i.next(); if (!used.contains(diskId)) { BibtexString disk = onDisk.getString(diskId); //System.out.println(disk.getName()); used.add(diskId); changes.add(new StringAddChange(disk)); } } }
private BibtexString findString(BibtexDatabase base, String name, HashSet<Object> used) { if (!base.hasStringLabel(name)) return null; for (Iterator<String> i=base.getStringKeySet().iterator(); i.hasNext();) { String key = i.next(); BibtexString bs = base.getString(key); if (bs.getName().equals(name) && !used.contains(key)) { used.add(key); return bs; } } return null; }
Clone fragments detected by clone detection tool
File path: /jabref-2.10/src/java/net/sf/jabref/collab/ChangeScanner.java File path: /jabref-2.10/src/java/net/sf/jabref/collab/ChangeScanner.java
Method name: void scanStrings(BibtexDatabase, BibtexDatabase, BibtexDatabase) Method name: BibtexString findString(BibtexDatabase, String, HashSet)
Number of AST nodes: 6 Number of AST nodes: 6
1
private void scanStrings(BibtexDatabase inMem, BibtexDatabase onTmp, BibtexDatabase onDisk) {
1
private BibtexString findString(BibtexDatabase base, String name, HashSet<Object> used) {
2
        int nTmp = onTmp.getStringCount(),
2
        if (!base.hasStringLabel(name))
3
        nDisk = onDisk.getStringCount();
3
            return null;
4
        if ((nTmp == 0) && (nDisk == 0))
4
        for (Iterator<String> i=base.getStringKeySet().iterator(); i.hasNext();) {
5
            return;
5
            String key = i.next();
6
6
            BibtexString bs = base.getString(key);
7
        HashSet<Object> used = new HashSet<Object>();
7
            if (bs.getName().equals(name) && !used.contains(key)) {
8
        HashSet<Object> usedInMem = new HashSet<Object>();
8
                used.add(key);
9
        HashSet<String> notMatched = new HashSet<String>(onTmp.getStringCount());
9
                return bs;
10
10
            }
11
        // First try to match by string names.
11
        }
12
        //int piv2 = -1;
12
        return null;
13
        mainLoop: for (String key : onTmp.getStringKeySet()){
13
    }
14
            BibtexString tmp = onTmp.getString(key);
15
16
            //      for (int j=piv2+1; j<nDisk; j++)
17
            for (String diskId : onDisk.getStringKeySet()){
18
                if (!used.contains(diskId)) {
19
                    BibtexString disk = onDisk.getString(diskId);
20
                    if (disk.getName().equals(tmp.getName())) {
21
                        // We have found a string with a matching name.
22
                        if ((tmp.getContent() != null) && !tmp.getContent().equals(disk.getContent())) {
23
                            // But they have nonmatching contents, so we've found a change.
24
                            BibtexString mem = findString(inMem, tmp.getName(), usedInMem);
25
                            if (mem != null)
26
                                changes.add(new StringChange(mem, tmp, tmp.getName(),
27
                                mem.getContent(),
28
                                tmp.getContent(), disk.getContent()));
29
                            else
30
                                changes.add(new StringChange(null, tmp, tmp.getName(), null, tmp.getContent(), disk.getContent()));
31
                        }
32
                        used.add(diskId);
33
                        //if (j==piv2)
34
                        //  piv2++;
35
                        continue mainLoop;
36
                    }
37
38
                }
39
            }
40
            // If we get here, there was no match for this string.
41
            notMatched.add(tmp.getId());
42
        }
43
44
        // See if we can detect a name change for those entries that we couldn't match.
45
        if (notMatched.size() > 0) {
46
            for (Iterator<String> i = notMatched.iterator(); i.hasNext();){
47
                BibtexString tmp = onTmp.getString(i.next());
48
49
                // If we get to this point, we found no string with matching name. See if we
50
                // can find one with matching content.
51
                for (String diskId : onDisk.getStringKeySet()){
52
53
                	if (!used.contains(diskId)) {
54
                        BibtexString disk = onDisk.getString(diskId);
55
56
                        if (disk.getContent().equals(tmp.getContent())) {
57
                            // We have found a string with the same content. It cannot have the same
58
                            // name, or we would have found it above.
59
60
                            // Try to find the matching one in memory:
61
                            BibtexString bsMem = null;
62
                            
63
                            for (String memId : inMem.getStringKeySet()){
64
                                BibtexString bsMem_cand = inMem.getString(memId);
65
                                if (bsMem_cand.getContent().equals(disk.getContent()) &&
66
                                !usedInMem.contains(memId)) {
67
                                    usedInMem.add(memId);
68
                                    bsMem = bsMem_cand;
69
                                    break;
70
                                }
71
                            }
72
73
                            changes.add(new StringNameChange(bsMem, tmp, bsMem.getName(),
74
                            tmp.getName(), disk.getName(),
75
                            tmp.getContent()));
76
                            i.remove();
77
                            used.add(diskId);
78
79
                        }
80
                    }
81
                }
82
            }
83
        }
84
85
        if (notMatched.size() > 0) {
86
            // Still one or more non-matched strings. So they must have been removed.
87
            for (Iterator<String> i = notMatched.iterator(); i.hasNext(); ) {
88
                String nmId = i.next();
89
                BibtexString tmp = onTmp.getString(nmId);
90
                BibtexString mem = findString(inMem, tmp.getName(), usedInMem);
91
                if (mem != null) { // The removed string is not removed from the mem version.
92
                    changes.add(new StringRemoveChange(tmp, tmp, mem));
93
                }
94
            }
95
        }
96
97
98
        // Finally, see if there are remaining strings in the disk database. They
99
        // must have been added.
100
        for (Iterator<String> i=onDisk.getStringKeySet().iterator(); i.hasNext();) {
101
            String diskId = i.next();
102
            if (!used.contains(diskId)) {
103
                BibtexString disk = onDisk.getString(diskId);
104
                //System.out.println(disk.getName());
105
                used.add(diskId);
106
                changes.add(new StringAddChange(disk));
107
            }
108
        }
109
    }
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements5
    Number of unmapped statements in the first code fragment2
    Number of unmapped statements in the second code fragment1
    Time elapsed for statement mapping (ms)0.0
    Similarity Score0.727
    Clone typeType 3
    Mapped Statements
    ID Statement ID Statement
    29
    for (String memId : inMem.getStringKeySet())
    29
    for (String memId : inMem.getStringKeySet())
    3
    for (Iterator<String> i = base.getStringKeySet().iterator(); i.hasNext(); )
    Differences
    Expression1Expression2Difference
    memIdkeyVARIABLE_NAME_MISMATCH
    inMembaseVARIABLE_NAME_MISMATCH
    3
    for (Iterator<String> i = base.getStringKeySet().iterator(); i.hasNext(); )
                                                  
    4
    String key = i.next();
    Differences
    Expression1Expression2Difference
    memIdkeyVARIABLE_NAME_MISMATCH
    4
    String key = i.next();
    30
    BibtexString bsMem_cand = inMem.getString(memId);
    30
    BibtexString bsMem_cand = inMem.getString(memId);
    5
    BibtexString bs = base.getString(key);
    Differences
    Expression1Expression2Difference
    bsMem_candbsVARIABLE_NAME_MISMATCH
    memIdkeyVARIABLE_NAME_MISMATCH
    inMembaseVARIABLE_NAME_MISMATCH
    5
    BibtexString bs = base.getString(key);
    31
    if (bsMem_cand.getContent().equals(disk.getContent()) && !usedInMem.contains(memId))
    31
    if (bsMem_cand.getContent().equals(disk.getContent()) && !usedInMem.contains(memId))
    6
    if (bs.getName().equals(name) && !used.contains(key))
    Differences
    Expression1Expression2Difference
    disk.getContent()nameTYPE_COMPATIBLE_REPLACEMENT
    getContentgetNameMETHOD_INVOCATION_NAME_MISMATCH
    bsMem_candbsVARIABLE_NAME_MISMATCH
    memIdkeyVARIABLE_NAME_MISMATCH
    usedInMemusedVARIABLE_NAME_MISMATCH
    Preondition Violations
    Expression bsMem_cand.getContent() cannot be parameterized, because it has dependencies to/from statements that will be extracted
    Expression bs.getName() cannot be parameterized, because it has dependencies to/from statements that will be extracted
    6
    if (bs.getName().equals(name) && !used.contains(key))
    32
    usedInMem.add(memId);
    32
    usedInMem.add(memId);
    7
    used.add(key);
    Differences
    Expression1Expression2Difference
    memIdkeyVARIABLE_NAME_MISMATCH
    usedInMemusedVARIABLE_NAME_MISMATCH
    7
    used.add(key);
                              
    8
    return bs;
    Preondition Violations
    Unmatched statement return bs; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    Unmatched return bs;
    8
    return bs;
    33
    bsMem = bsMem_cand;
    33
    bsMem = bsMem_cand;
    Preondition Violations
    Unmatched statement bsMem=bsMem_cand; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                              
    34
    break;
    34
    break;
    Preondition Violations
    Unmatched break;
                        
    Precondition Violations (6)
    Row Violation
    1Expression bsMem_cand.getContent() cannot be parameterized, because it has dependencies to/from statements that will be extracted
    2Expression bs.getName() cannot be parameterized, because it has dependencies to/from statements that will be extracted
    3Unmatched statement return bs; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    4Unmatched return bs;
    5Unmatched statement bsMem=bsMem_cand; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    6Unmatched break;