Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CrUnconventionalOperName |
|
| 4.2;4.2 |
1 | /* $Id: CrUnconventionalOperName.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 | * maurelio1234 | |
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.HashSet; | |
42 | import java.util.Set; | |
43 | ||
44 | import org.argouml.cognitive.Critic; | |
45 | import org.argouml.cognitive.Designer; | |
46 | import org.argouml.cognitive.ListSet; | |
47 | import org.argouml.cognitive.ToDoItem; | |
48 | import org.argouml.cognitive.critics.Wizard; | |
49 | import org.argouml.model.Model; | |
50 | import org.argouml.uml.cognitive.UMLDecision; | |
51 | import org.argouml.uml.cognitive.UMLToDoItem; | |
52 | ||
53 | /** | |
54 | * Critic to detect whether an operation name obeys to certain rules. | |
55 | */ | |
56 | public class CrUnconventionalOperName extends AbstractCrUnconventionalName { | |
57 | ||
58 | /** | |
59 | * The constructor. | |
60 | */ | |
61 | 900 | public CrUnconventionalOperName() { |
62 | 900 | setupHeadAndDesc(); |
63 | 900 | addSupportedDecision(UMLDecision.NAMING); |
64 | 900 | setKnowledgeTypes(Critic.KT_SYNTAX); |
65 | 900 | addTrigger("feature_name"); |
66 | 900 | } |
67 | ||
68 | /* | |
69 | * @see org.argouml.uml.cognitive.critics.CrUML#predicate2( | |
70 | * java.lang.Object, org.argouml.cognitive.Designer) | |
71 | */ | |
72 | @Override | |
73 | public boolean predicate2(Object dm, Designer dsgr) { | |
74 | 0 | if (!(Model.getFacade().isAOperation(dm))) { |
75 | 0 | return NO_PROBLEM; |
76 | } | |
77 | 0 | Object oper = dm; |
78 | 0 | String myName = Model.getFacade().getName(oper); |
79 | 0 | if (myName == null || myName.equals("")) { |
80 | 0 | return NO_PROBLEM; |
81 | } | |
82 | 0 | String nameStr = myName; |
83 | 0 | if (nameStr == null || nameStr.length() == 0) { |
84 | 0 | return NO_PROBLEM; |
85 | } | |
86 | 0 | char initalChar = nameStr.charAt(0); |
87 | ||
88 | 0 | for (Object stereo : Model.getFacade().getStereotypes(oper)) { |
89 | 0 | if ("create".equals(Model.getFacade().getName(stereo)) |
90 | || "constructor".equals( | |
91 | Model.getFacade().getName(stereo))) { | |
92 | 0 | return NO_PROBLEM; |
93 | } | |
94 | } | |
95 | 0 | if (!Character.isLowerCase(initalChar)) { |
96 | 0 | return PROBLEM_FOUND; |
97 | } | |
98 | 0 | return NO_PROBLEM; |
99 | } | |
100 | ||
101 | /* | |
102 | * @see org.argouml.cognitive.critics.Critic#toDoItem( java.lang.Object, | |
103 | * org.argouml.cognitive.Designer) | |
104 | */ | |
105 | @Override | |
106 | public ToDoItem toDoItem(Object dm, Designer dsgr) { | |
107 | 0 | Object f = dm; |
108 | 0 | ListSet offs = computeOffenders(f); |
109 | 0 | return new UMLToDoItem(this, offs, dsgr); |
110 | } | |
111 | ||
112 | /** | |
113 | * @param dm the object to be checked | |
114 | * @return the set of offenders | |
115 | */ | |
116 | protected ListSet computeOffenders(Object dm) { | |
117 | 0 | ListSet offs = new ListSet(dm); |
118 | 0 | offs.add(Model.getFacade().getOwner(dm)); |
119 | 0 | return offs; |
120 | } | |
121 | ||
122 | /* | |
123 | * @see org.argouml.cognitive.Poster#stillValid( | |
124 | * org.argouml.cognitive.ToDoItem, org.argouml.cognitive.Designer) | |
125 | */ | |
126 | @Override | |
127 | public boolean stillValid(ToDoItem i, Designer dsgr) { | |
128 | 0 | if (!isActive()) { |
129 | 0 | return false; |
130 | } | |
131 | 0 | ListSet offs = i.getOffenders(); |
132 | 0 | Object f = offs.get(0); |
133 | 0 | if (!predicate(f, dsgr)) { |
134 | 0 | return false; |
135 | } | |
136 | 0 | ListSet newOffs = computeOffenders(f); |
137 | 0 | boolean res = offs.equals(newOffs); |
138 | 0 | return res; |
139 | } | |
140 | ||
141 | ||
142 | /** | |
143 | * CandidateForConstructor tests if the operation name is the same | |
144 | * as the class name. If so, an alternative path in the wizard is | |
145 | * possible where we are suggested to make the operation a constructor. | |
146 | * | |
147 | * @param me the operation to check | |
148 | * @return true if this operation looks like a constructor | |
149 | */ | |
150 | protected boolean candidateForConstructor(Object me) { | |
151 | 0 | if (!(Model.getFacade().isAOperation(me))) { |
152 | 0 | return false; |
153 | } | |
154 | 0 | Object oper = me; |
155 | 0 | String myName = Model.getFacade().getName(oper); |
156 | 0 | if (myName == null || myName.equals("")) { |
157 | 0 | return false; |
158 | } | |
159 | 0 | Object cl = Model.getFacade().getOwner(oper); |
160 | 0 | String nameCl = Model.getFacade().getName(cl); |
161 | 0 | if (nameCl == null || nameCl.equals("")) { |
162 | 0 | return false; |
163 | } | |
164 | 0 | if (myName.equals(nameCl)) { |
165 | 0 | return true; |
166 | } | |
167 | 0 | return false; |
168 | } | |
169 | ||
170 | ||
171 | /* | |
172 | * @see org.argouml.cognitive.critics.Critic#initWizard( | |
173 | * org.argouml.cognitive.ui.Wizard) | |
174 | */ | |
175 | @Override | |
176 | public void initWizard(Wizard w) { | |
177 | 0 | if (w instanceof WizOperName) { |
178 | 0 | ToDoItem item = (ToDoItem) w.getToDoItem(); |
179 | 0 | Object me = item.getOffenders().get(0); |
180 | 0 | String sug = Model.getFacade().getName(me); |
181 | 0 | sug = computeSuggestion(sug); |
182 | 0 | boolean cand = candidateForConstructor(me); |
183 | String ins; | |
184 | 0 | if (cand) { |
185 | 0 | ins = super.getLocalizedString("-ins-ext"); |
186 | } else { | |
187 | 0 | ins = super.getInstructions(); |
188 | } | |
189 | 0 | ((WizOperName) w).setInstructions(ins); |
190 | 0 | ((WizOperName) w).setSuggestion(sug); |
191 | 0 | ((WizOperName) w).setPossibleConstructor(cand); |
192 | } | |
193 | 0 | } |
194 | ||
195 | /* | |
196 | * @see org.argouml.uml.cognitive.critics.AbstractCrUnconventionalName#computeSuggestion(java.lang.String) | |
197 | */ | |
198 | public String computeSuggestion(String sug) { | |
199 | 0 | if (sug == null) { |
200 | 0 | return ""; |
201 | } | |
202 | 0 | return sug.substring(0, 1).toLowerCase() + sug.substring(1); |
203 | } | |
204 | ||
205 | /* | |
206 | * @see org.argouml.cognitive.critics.Critic#getWizardClass(org.argouml.cognitive.ToDoItem) | |
207 | */ | |
208 | @Override | |
209 | public Class getWizardClass(ToDoItem item) { | |
210 | 0 | return WizOperName.class; |
211 | } | |
212 | ||
213 | /* | |
214 | * @see org.argouml.uml.cognitive.critics.CrUML#getCriticizedDesignMaterials() | |
215 | */ | |
216 | public Set<Object> getCriticizedDesignMaterials() { | |
217 | 900 | Set<Object> ret = new HashSet<Object>(); |
218 | 900 | ret.add(Model.getMetaTypes().getOperation()); |
219 | 900 | return ret; |
220 | } | |
221 | ||
222 | } |