/** * Searches for attributes that are changeable instance attributes. * * @param dm The classifier to examine. * @param depth Number of levels searched. * @return true if an attribute can be found in this class * or in any of its generalizations. */ private boolean findChangeableInstanceAttributeInInherited(Object dm, int depth) { Iterator attribs = Model.getFacade().getAttributes(dm).iterator(); while (attribs.hasNext()) { Object attr = attribs.next(); // If we find an instance variable that is not a constant // we have succeeded if (!Model.getFacade().isStatic(attr) && !Model.getFacade().isReadOnly(attr)) { return true; } } // I am only prepared to go this far. if (depth > MAX_DEPTH) { return false; } Iterator iter = Model.getFacade().getGeneralizations(dm).iterator(); while (iter.hasNext()) { Object parent = Model.getFacade().getGeneral(iter.next()); if (parent == dm) { continue; } if (Model.getFacade().isAClassifier(parent) && findChangeableInstanceAttributeInInherited( parent, depth + 1)) { return true; } } return false; }
/** * Generates the import statements of a source file * @param cls The file * @param packagePath the path to the package * @return The generated import statement */ private String generateImports(Object cls, String packagePath) { // TODO: check also generalizations StringBuffer sb = new StringBuffer(80); Set<String> importSet = new HashSet<String>(); for (Object mFeature : Model.getFacade().getFeatures(cls)) { if (Model.getFacade().isAAttribute(mFeature)) { addImportType(importSet, mFeature, packagePath); } else if (Model.getFacade().isAOperation(mFeature)) { // check the parameter types for (Object parameter : Model.getFacade().getParameters(mFeature)) { addImportType(importSet, parameter, packagePath); } // check the return parameter types for (Object parameter : Model.getCoreHelper().getReturnParameters(mFeature)) { addImportType(importSet, parameter, packagePath); } // check raised signals for (Object signal : Model.getFacade().getRaisedSignals(mFeature)) { if (!Model.getFacade().isAException(signal)) { continue; } addImport(importSet, signal, packagePath); } } } // now check packages of all generalized types for (Object gen : Model.getFacade().getGeneralizations(cls)) { Object parent = Model.getFacade().getGeneral(gen); if (parent == cls) { continue; } addImport(importSet, parent, packagePath); } // now check packages of the interfaces for (Object iface : Model.getFacade().getSpecifications(cls)) { addImport(importSet, iface, packagePath); } // check association end types for (Object associationEnd : Model.getFacade().getAssociationEnds(cls)) { Object association = Model.getFacade().getAssociation(associationEnd); for (Object associationEnd2 : Model.getFacade().getConnections(association)) { if (associationEnd2 != associationEnd && Model.getFacade().isNavigable(associationEnd2) && !Model.getFacade().isAbstract( Model.getFacade().getAssociation( associationEnd2))) { // association end found if (isCollection(associationEnd2)) { importSet.add("System.Collections"); } else { addImportType(importSet, associationEnd2, packagePath); } } } } for (String imp : importSet) { sb.append("using ").append(imp).append(";"); sb.append(LINE_SEPARATOR); } if (!importSet.isEmpty()) { sb.append(LINE_SEPARATOR); } // Generate user section for using statements sb.append("// In this section you can add your own using directives"); sb.append(LINE_SEPARATOR); sb.append(generateSection(cls)); return sb.toString(); }
Clone fragments detected by clone detection tool
File path: /ArgoUML-0.34-src/argouml/src/argouml-app/src/org/argouml/uml/cognitive/critics/CrNoInstanceVariables.java File path: /ArgoUML-0.34-src/argouml-csharp/src/org/argouml/language/csharp/generator/GeneratorCSharp.java
Method name: boolean findChangeableInstanceAttributeInInherited(Object, int) Method name: String generateImports(Object, String)
Number of AST nodes: 6 Number of AST nodes: 5
1
/**
1
/**
2
     * Searches for attributes that are changeable instance attributes.
2
     * Generates the import statements of a source file
3
     *
3
     * @param cls The file
4
     * @param dm The classifier to examine.
4
     * @param packagePath the path to the package
5
     * @param depth Number of levels searched.
5
     * @return The generated import statement
6
     * @return true if an attribute can be found in this class
6
     */
7
     *		or in any of its generalizations.
7
    private String generateImports(Object cls, String packagePath) {
8
     */
8
        // TODO: check also generalizations
9
    private boolean findChangeableInstanceAttributeInInherited(Object dm,
9
        StringBuffer sb = new StringBuffer(80);
10
            int depth) {
10
        Set<String> importSet = new HashSet<String>();
11
11
12
	Iterator attribs = Model.getFacade().getAttributes(dm).iterator();
12
        for (Object mFeature : Model.getFacade().getFeatures(cls)) {
13
13
            if (Model.getFacade().isAAttribute(mFeature)) {
14
	while (attribs.hasNext()) {
14
                addImportType(importSet, mFeature, packagePath);
15
	    Object attr = attribs.next();
15
            } else if (Model.getFacade().isAOperation(mFeature)) {
16
16
                // check the parameter types
17
	    // If we find an instance variable that is not a constant
17
                for (Object parameter 
18
	    // we have succeeded
18
                        : Model.getFacade().getParameters(mFeature)) {
19
	    if (!Model.getFacade().isStatic(attr)
19
                    addImportType(importSet, parameter, packagePath);
20
                    && !Model.getFacade().isReadOnly(attr)) {
20
                }
21
                return true;
21
22
            }
22
                // check the return parameter types
23
	}
23
                for (Object parameter 
24
24
                        : Model.getCoreHelper().getReturnParameters(mFeature)) {
25
	// I am only prepared to go this far.
25
                    addImportType(importSet, parameter, packagePath);
26
	if (depth > MAX_DEPTH) {
26
                }
27
	    return false;
27
28
	}
28
                // check raised signals
29
29
                for (Object signal 
30
	Iterator iter = Model.getFacade().getGeneralizations(dm).iterator();
30
                        : Model.getFacade().getRaisedSignals(mFeature)) {
31
31
                    if (!Model.getFacade().isAException(signal)) {
32
	while (iter.hasNext()) {
32
                        continue;
33
	    Object parent = Model.getFacade().getGeneral(iter.next());
33
                    }
34
34
                    addImport(importSet, signal, packagePath);
35
	    if (parent == dm) {
35
                }
36
	        continue;
36
            }
37
	    }
37
        }
38
38
39
	    if (Model.getFacade().isAClassifier(parent)
39
        // now check packages of all generalized types
40
                    && findChangeableInstanceAttributeInInherited(
40
        for (Object gen : Model.getFacade().getGeneralizations(cls)) {
41
                            parent, depth + 1)) {
41
            Object parent = Model.getFacade().getGeneral(gen);
42
                return true;
42
            if (parent == cls) {
43
            }
43
                continue;
44
	}
44
            }
45
45
            addImport(importSet, parent, packagePath);
46
	return false;
46
        }
47
    }
47
48
        // now check packages of the interfaces
49
        for (Object iface : Model.getFacade().getSpecifications(cls)) {
50
            addImport(importSet, iface, packagePath);
51
        }
52
53
        // check association end types
54
        for (Object associationEnd 
55
                : Model.getFacade().getAssociationEnds(cls)) {
56
            Object association = 
57
                Model.getFacade().getAssociation(associationEnd);
58
            for (Object associationEnd2 
59
                    : Model.getFacade().getConnections(association)) {
60
                if (associationEnd2 != associationEnd
61
                        && Model.getFacade().isNavigable(associationEnd2)
62
                        && !Model.getFacade().isAbstract(
63
                                Model.getFacade().getAssociation(
64
                                        associationEnd2))) {
65
                    // association end found
66
                    if (isCollection(associationEnd2)) {
67
                        importSet.add("System.Collections");
68
                    } else {
69
                        addImportType(importSet, associationEnd2, packagePath);
70
                    }
71
                }
72
            }
73
        }
74
        for (String imp : importSet) {
75
            sb.append("using ").append(imp).append(";");
76
	    sb.append(LINE_SEPARATOR);
77
        }
78
        if (!importSet.isEmpty()) {
79
            sb.append(LINE_SEPARATOR);
80
        }
81
        // Generate user section for using statements
82
        sb.append("// In this section you can add your own using directives");
83
        sb.append(LINE_SEPARATOR);
84
        sb.append(generateSection(cls));
85
        return sb.toString();
86
    }
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements5
    Number of unmapped statements in the first code fragment0
    Number of unmapped statements in the second code fragment1
    Time elapsed for statement mapping (ms)0.0
    Similarity Score0.364
    Clone typeType 3
    Mapped Statements
    ID Statement ID Statement
    9
    while (iter.hasNext())
    9
    while (iter.hasNext())
    15
    for (Object gen : Model.getFacade().getGeneralizations(cls))
    Differences
    Expression1Expression2Difference
    dmclsVARIABLE_NAME_MISMATCH
    15
    for (Object gen : Model.getFacade().getGeneralizations(cls))
    10
    Object parent = Model.getFacade().getGeneral(iter.next());
    10
    Object parent = Model.getFacade().getGeneral(iter.next());
    16
    Object parent = Model.getFacade().getGeneral(gen);
    Differences
    Expression1Expression2Difference
    iter.next()genTYPE_COMPATIBLE_REPLACEMENT
    Preondition Violations
    Expression gen cannot be parameterized, because it has dependencies to/from statements that will be extracted
    16
    Object parent = Model.getFacade().getGeneral(gen);
    11
    if (parent == dm)
    11
    if (parent == dm)
    17
    if (parent == cls)
    Differences
    Expression1Expression2Difference
    dmclsVARIABLE_NAME_MISMATCH
    17
    if (parent == cls)
    12
    continue;
    18
    continue;
                                                                                            
    19
    addImport(importSet, parent, packagePath);
    Preondition Violations
    Unmatched statement addImport(importSet,parent,packagePath); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    19
    addImport(importSet, parent, packagePath);
    Precondition Violations (2)
    Row Violation
    1Expression gen cannot be parameterized, because it has dependencies to/from statements that will be extracted
    2Unmatched statement addImport(importSet,parent,packagePath); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted