Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CrSingletonViolatedOnlyPrivateConstructors |
|
| 5.0;5 |
1 | /* $Id: CrSingletonViolatedOnlyPrivateConstructors.java 17831 2010-01-12 18:56:55Z 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.pattern.cognitive.critics; | |
40 | ||
41 | import java.util.Iterator; | |
42 | ||
43 | import org.argouml.cognitive.Designer; | |
44 | import org.argouml.cognitive.ToDoItem; | |
45 | import org.argouml.model.Model; | |
46 | import org.argouml.uml.cognitive.UMLDecision; | |
47 | import org.argouml.uml.cognitive.critics.CrUML; | |
48 | ||
49 | /** | |
50 | * A critic to detect whether a class violates the conditions required for | |
51 | * using a Singleton Stereotype.<p> | |
52 | * | |
53 | * This stereotype is used to indicate a class which only ever has a single | |
54 | * instance. The critic will trigger whenever a class has stereotype | |
55 | * «Singleton» (or «singleton»), but does not | |
56 | * meet the requirements of a Singleton class. These are: | |
57 | * | |
58 | * <ol> | |
59 | * <li>An static variable to hold the sole instance of the class; | |
60 | * <li>only private constructors to create the sole instance; | |
61 | * (This critic) and | |
62 | * <li>At least one constructor to override the default constructor. | |
63 | * </ol> | |
64 | * | |
65 | * This version includes an implementation for the second tests above! | |
66 | * <p> | |
67 | * *see ArgoUML User Manual: Singleton Violated | |
68 | * | |
69 | * @author jrobbins | |
70 | */ | |
71 | public class CrSingletonViolatedOnlyPrivateConstructors extends CrUML { | |
72 | ||
73 | /** | |
74 | * Constructor for the critic. | |
75 | * | |
76 | * Sets up the resource name, which will allow headline and description | |
77 | * to be found for the current locale. Provides a design issue category | |
78 | * (PATTERNS), sets a priority for any to-do items (LOW) and adds | |
79 | * triggers for metaclasses "stereotype", "structuralFeature" and | |
80 | * "associationEnd". | |
81 | */ | |
82 | ||
83 | 900 | public CrSingletonViolatedOnlyPrivateConstructors() { |
84 | 900 | setupHeadAndDesc(); |
85 | 900 | addSupportedDecision(UMLDecision.PATTERNS); |
86 | 900 | setPriority(ToDoItem.MED_PRIORITY); |
87 | ||
88 | // These may not actually make any difference at present (the code | |
89 | // behind addTrigger needs more work). | |
90 | 900 | addTrigger("stereotype"); |
91 | 900 | addTrigger("structuralFeature"); |
92 | 900 | addTrigger("associationEnd"); |
93 | 900 | } |
94 | ||
95 | ||
96 | /** | |
97 | * The trigger for the critic.<p> | |
98 | * | |
99 | * First check we are actually stereotyped "Singleton" (or we will | |
100 | * accept "singleton").<p> | |
101 | * | |
102 | * Then check for a static attribute with the same type as the Singleton | |
103 | * class that will hold the instance of the Singleton class when its | |
104 | * created.<p> | |
105 | * | |
106 | * @param dm the {@link java.lang.Object Object} to be checked | |
107 | * against the critic. | |
108 | * | |
109 | * @param dsgr the {@link org.argouml.cognitive.Designer Designer} | |
110 | * creating the model. Not used, this is for future | |
111 | * development of ArgoUML. | |
112 | * | |
113 | * @return {@link #PROBLEM_FOUND PROBLEM_FOUND} if the critic is | |
114 | * triggered, otherwise {@link #NO_PROBLEM NO_PROBLEM}. | |
115 | */ | |
116 | public boolean predicate2(Object dm, Designer dsgr) { | |
117 | // Only look at classes | |
118 | 0 | if (!(Model.getFacade().isAClass(dm))) { |
119 | 0 | return NO_PROBLEM; |
120 | } | |
121 | ||
122 | // We only look at singletons | |
123 | 0 | if (!(Model.getFacade().isSingleton(dm))) { |
124 | 0 | return NO_PROBLEM; |
125 | } | |
126 | ||
127 | 0 | Iterator operations = Model.getFacade().getOperations(dm).iterator(); |
128 | ||
129 | 0 | while (operations.hasNext()) { |
130 | 0 | Object o = operations.next(); |
131 | ||
132 | 0 | if (!(Model.getFacade().isConstructor(o))) { |
133 | 0 | continue; |
134 | } | |
135 | ||
136 | 0 | if (!(Model.getFacade().isPrivate(o))) { |
137 | 0 | return PROBLEM_FOUND; |
138 | } | |
139 | 0 | } |
140 | ||
141 | 0 | return NO_PROBLEM; |
142 | } | |
143 | ||
144 | } /* end class CrSingletonViolatedOnlyPrivateConstructors */ | |
145 |