Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CrConstructorNeeded |
|
| 5.5;5.5 |
1 | /* $Id: CrConstructorNeeded.java 17849 2010-01-12 19:50:34Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2007 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.uml.cognitive.critics; | |
40 | ||
41 | import java.util.Collection; | |
42 | import java.util.Iterator; | |
43 | ||
44 | import org.argouml.cognitive.Critic; | |
45 | import org.argouml.cognitive.Designer; | |
46 | import org.argouml.cognitive.ToDoItem; | |
47 | import org.argouml.cognitive.critics.Wizard; | |
48 | import org.argouml.model.Model; | |
49 | import org.argouml.uml.cognitive.UMLDecision; | |
50 | ||
51 | /** | |
52 | * A critic to detect when a class requires a constructor.<p> | |
53 | * | |
54 | * The critic will trigger whenever a class has instance variables that are | |
55 | * uninitialised and there is no constructor. It will not trigger for | |
56 | * certain stereotyped classes.<p> | |
57 | * | |
58 | * This critic is part of a compound critic.<p> | |
59 | * | |
60 | * See the ArgoUML User Manual: Constructor Needed | |
61 | */ | |
62 | public class CrConstructorNeeded extends CrUML { | |
63 | ||
64 | /** | |
65 | * Constructor for the critic.<p> | |
66 | * | |
67 | * Sets up the resource name, which will allow headline and description | |
68 | * to found for the current locale. Provides a design issue category | |
69 | * (STORAGE) and adds triggers for metaclasses "behaviouralFeature" and | |
70 | * "structuralFeature". | |
71 | */ | |
72 | 900 | public CrConstructorNeeded() { |
73 | 900 | setupHeadAndDesc(); |
74 | 900 | addSupportedDecision(UMLDecision.STORAGE); |
75 | 900 | addKnowledgeType(Critic.KT_CORRECTNESS); |
76 | ||
77 | // These may not actually make any difference at present (the code | |
78 | // behind addTrigger needs more work). | |
79 | ||
80 | 900 | addTrigger("behavioralFeature"); |
81 | 900 | addTrigger("structuralFeature"); |
82 | 900 | } |
83 | ||
84 | /** | |
85 | * The trigger for the critic.<p> | |
86 | * | |
87 | * First see if we have any instance variables that are not | |
88 | * initialised. If not there is no problem. If there are any uninitialised | |
89 | * instance variables, then look for a constructor.<p> | |
90 | * | |
91 | * @param dm the {@link java.lang.Object Object} to be checked against | |
92 | * the critic. | |
93 | * | |
94 | * @param dsgr the {@link org.argouml.cognitive.Designer Designer} | |
95 | * creating the model. Not used, this is for future | |
96 | * development of ArgoUML. | |
97 | * | |
98 | * @return {@link #PROBLEM_FOUND PROBLEM_FOUND} if the critic is | |
99 | * triggered, otherwise {@link #NO_PROBLEM NO_PROBLEM}. | |
100 | */ | |
101 | @Override | |
102 | public boolean predicate2(Object dm, Designer dsgr) { | |
103 | ||
104 | // Only look at classes | |
105 | 0 | if (!(Model.getFacade().isAClass(dm))) { |
106 | 0 | return NO_PROBLEM; |
107 | } | |
108 | ||
109 | ||
110 | // We don't consider secondary stuff. | |
111 | 0 | if (!(Model.getFacade().isPrimaryObject(dm))) |
112 | 0 | return NO_PROBLEM; |
113 | ||
114 | // Types don't need a constructor. | |
115 | 0 | if (Model.getFacade().isType(dm)) { |
116 | 0 | return NO_PROBLEM; |
117 | } | |
118 | ||
119 | // Utilities usually do not require a constructor either | |
120 | 0 | if (Model.getFacade().isUtility(dm)) { |
121 | 0 | return NO_PROBLEM; |
122 | } | |
123 | ||
124 | // Check for uninitialised instance variables and | |
125 | // constructor. | |
126 | 0 | Collection operations = Model.getFacade().getOperations(dm); |
127 | ||
128 | 0 | Iterator opers = operations.iterator(); |
129 | ||
130 | 0 | while (opers.hasNext()) { |
131 | 0 | if (Model.getFacade().isConstructor(opers.next())) { |
132 | // There is a constructor. | |
133 | 0 | return NO_PROBLEM; |
134 | } | |
135 | } | |
136 | ||
137 | 0 | Iterator attrs = Model.getFacade().getAttributes(dm).iterator(); |
138 | ||
139 | 0 | while (attrs.hasNext()) { |
140 | 0 | Object attr = attrs.next(); |
141 | ||
142 | 0 | if (Model.getFacade().isStatic(attr)) |
143 | 0 | continue; |
144 | ||
145 | 0 | if (Model.getFacade().isInitialized(attr)) |
146 | 0 | continue; |
147 | ||
148 | // We have found a non-static one that is not initialized. | |
149 | 0 | return PROBLEM_FOUND; |
150 | } | |
151 | ||
152 | // yeah right...we don't have an operation (and thus no | |
153 | 0 | return NO_PROBLEM; |
154 | } | |
155 | ||
156 | ||
157 | /* | |
158 | * @see org.argouml.cognitive.critics.Critic#initWizard( | |
159 | * org.argouml.cognitive.ui.Wizard) | |
160 | */ | |
161 | @Override | |
162 | public void initWizard(Wizard w) { | |
163 | 0 | if (w instanceof WizAddConstructor) { |
164 | 0 | ToDoItem item = (ToDoItem) w.getToDoItem(); |
165 | 0 | Object me = item.getOffenders().get(0); |
166 | 0 | String ins = super.getInstructions(); |
167 | 0 | String sug = null; |
168 | 0 | if (me != null) |
169 | 0 | sug = Model.getFacade().getName(me); |
170 | 0 | if ("".equals(sug)) { |
171 | 0 | sug = super.getDefaultSuggestion(); |
172 | } | |
173 | 0 | ((WizAddConstructor) w).setInstructions(ins); |
174 | 0 | ((WizAddConstructor) w).setSuggestion(sug); |
175 | } | |
176 | 0 | } |
177 | ||
178 | /* | |
179 | * @see org.argouml.cognitive.critics.Critic#getWizardClass(org.argouml.cognitive.ToDoItem) | |
180 | */ | |
181 | @Override | |
182 | public Class getWizardClass(ToDoItem item) { | |
183 | 0 | return WizAddConstructor.class; |
184 | } | |
185 | } |