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 | } |