Debian Codegraph @

src/main/java/net/sf/antcontrib/design/Design.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2005 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.design;
17
18 import java.io.File;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.Vector;
26
27 import net.sf.antcontrib.logic.ProjectDelegate;
28
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Location;
31 import org.apache.tools.ant.Project;
32
33
34 /*
35 * Created on Aug 24, 2003
36 *
37 * To change the template for this generated file go to
38 * Window>Preferences>Java>Code Generation>Code and Comments
39 */
40 /**
41 * FILL IN JAVADOC HERE
42 *
43 * @author Dean Hiller([email protected])
44 */
45 public class Design {
46
47 private Map nameToPackage = new HashMap();
48 private Map packageNameToPackage = new HashMap();
49 private boolean isCircularDesign;
50 private Log log;
51 private Location location;
52
53 private String currentClass = null;
54 private String currentPackageName = null;
55 private Package currentAliasPackage = null;
56
57 private HashSet primitives = new HashSet();
58
59 public Design(boolean isCircularDesign, Log log, Location loc) {
60 //by default, add java as a configured package with the name java
61 Package p = new Package();
62 p.setIncludeSubpackages(true);
63 p.setName("java");
64 p.setUsed(true);
65 p.setNeedDeclarations(false);
66 p.setPackage("java");
67 addConfiguredPackage(p);
68
69 this.isCircularDesign = isCircularDesign;
70 this.log = log;
71 this.location = loc;
72
73 primitives.add("boolean");
74
75 //integral types
76 primitives.add("byte");
77 primitives.add("short");
78 primitives.add("int");
79 primitives.add("long");
80 primitives.add("char");
81
82 //floating point types
83 primitives.add("double");
84 primitives.add("float");
85 }
86
87 public Package getPackage(String nameAttribute) {
88 return (Package)nameToPackage.get(nameAttribute);
89 }
90
91 private Package retreivePack(String thePackage) {
92 if(thePackage == null)
93 throw new IllegalArgumentException("Cannot retrieve null packages");
94
95 String currentPackage = thePackage;
96 Package result = (Package)packageNameToPackage.get(currentPackage);
97 while(!Package.DEFAULT.equals(currentPackage)) {
98 log.log("p="+currentPackage+"result="+result, Project.MSG_DEBUG);
99 if(result != null) {
100 if(currentPackage.equals(thePackage))
101 return result;
102 else if(result.isIncludeSubpackages())
103 return result;
104 return null;
105 }
106 currentPackage = VerifyDesignDelegate.getPackageName(currentPackage);
107 result = (Package)packageNameToPackage.get(currentPackage);
108 }
109
110 //result must now be default package
111 if(result != null && result.isIncludeSubpackages())
112 return result;
113
114 return null;
115 }
116
117 public void addConfiguredPackage(Package p) {
118
119 String pack = p.getPackage();
120
121 Depends[] depends = p.getDepends();
122
123 if(depends != null && !isCircularDesign) {
124 //make sure all depends are in Map first
125 //circular references then are not a problem because they must
126 //put the stuff in order
127 for(int i = 0; i < depends.length; i++) {
128 Package dependsPackage = (Package)nameToPackage.get(depends[i].getName());
129
130 if(dependsPackage == null) {
131 throw new RuntimeException("package name="+p.getName()+" did not\n" +
132 "have "+depends[i]+" listed before it. circularDesign is off\n"+
133 "so package="+p.getName()+" must be moved up in the xml file");
134 }
135 }
136 }
137
138 nameToPackage.put(p.getName(), p);
139 packageNameToPackage.put(p.getPackage(), p);
140 }
141
142 /**
143 * @param className Class name of a class our currentAliasPackage depends on.
144 */
145 public void verifyDependencyOk(String className) {
146 log.log(" className="+className, Project.MSG_DEBUG);
147 if(className.startsWith("L"))
148 className = className.substring(1, className.length());
149
150 //get the classPackage our currentAliasPackage depends on....
151 String classPackage = VerifyDesignDelegate.getPackageName(className);
152
153 //check if this is an needdeclarations="false" package, if so, the dependency is ok if it
154 //is not declared
155 log.log(" classPackage="+classPackage, Project.MSG_DEBUG);
156 Package p = retreivePack(classPackage);
157 if(p == null) {
158 throw new BuildException(getErrorMessage(currentClass, className), location);
159 }
160 p.setUsed(true); //set package to used since we have classes in it
161 if(p != null && !p.isNeedDeclarations())
162 return;
163
164 String pack = currentAliasPackage.getPackage();
165
166 log.log(" AllowedDepends="+pack, Project.MSG_DEBUG);
167 log.log(" CurrentDepends="+className, Project.MSG_DEBUG);
168 if(isClassInPackage(className, currentAliasPackage))
169 return;
170
171 Depends[] depends = currentAliasPackage.getDepends();
172
173 //probably want to create a regular expression out of all the depends and just match on that
174 //each time. for now though, just get it working and do the basic(optimize later if needed)
175 for(int i = 0; i < depends.length; i++) {
176 Depends d = depends[i];
177 String name = d.getName();
178
179 Package temp = getPackage(name);
180 log.log(" AllowedDepends="+temp.getPackage(), Project.MSG_DEBUG);
181 log.log(" CurrentDepends="+className, Project.MSG_DEBUG);
182 if(isClassInPackage(className, temp)) {
183 temp.setUsed(true); //set package to used since we are depending on it(could be external package like junit)
184 currentAliasPackage.addUsedDependency(d);
185 return;
186 }
187 }
188
189 log.log("***************************************", Project.MSG_DEBUG);
190 log.log("***************************************", Project.MSG_DEBUG);
191
192 throw new BuildException(Design.getErrorMessage(currentClass, className), location);
193 }
194
195 public boolean isClassInPackage(String className, Package p) {
196 String classPackage = VerifyDesignDelegate.getPackageName(className);
197 if(p.isIncludeSubpackages()) {
198 if(className.startsWith(p.getPackage()))
199 return true;
200 } else { //if not including subpackages, the it must be the exact package.
201 if(classPackage.equals(p.getPackage()))
202 return true;
203 }
204 return false;
205 }
206 /**
207 * @param className
208 * @return whether or not this class needs to be checked. (ie. if the
209 * attribute needdepends=false, we don't care about this package.
210 */
211 public boolean needEvalCurrentClass(String className) {
212 currentClass = className;
213 String packageName = VerifyDesignDelegate.getPackageName(className);
214 // log("class="+className, Project.MSG_DEBUG);
215 if(!packageName.equals(currentPackageName) || currentAliasPackage == null) {
216 currentPackageName = packageName;
217 log.log("\nEvaluating package="+currentPackageName, Project.MSG_INFO);
218 currentAliasPackage = retreivePack(packageName);
219 //DEANDO: test this scenario
220 if(currentAliasPackage == null) {
221 log.log(" class="+className, Project.MSG_VERBOSE);
222 throw new BuildException(getNoDefinitionError(className), location);
223 }
224
225 currentAliasPackage.setUsed(true);
226 }
227 log.log(" class="+className, Project.MSG_VERBOSE);
228
229 if(packageName.equals(Package.DEFAULT)) {
230 if(className.indexOf('.') != -1) {
231 throw new RuntimeException("Internal Error");
232 }
233 } else if(!className.startsWith(currentPackageName))
234 throw new RuntimeException("Internal Error");
235
236 if(!currentAliasPackage.getNeedDepends())
237 return false;
238 return true;
239 }
240
241 public String getCurrentClass() {
242 return currentClass;
243 }
244
245 void checkClass(String dependsOn) {
246 log.log(" dependsOn1="+dependsOn, Project.MSG_DEBUG);
247 if(dependsOn.endsWith("[]")) {
248 int index = dependsOn.indexOf("[");
249 dependsOn = dependsOn.substring(0, index);
250 log.log(" dependsOn2="+dependsOn, Project.MSG_DEBUG);
251 }
252
253 if(primitives.contains(dependsOn))
254 return;
255
256 //Anything in java.lang package seems to be passed in as just the
257 //className with no package like Object, String or Class, so here we try to
258 //see if the name is a java.lang class....
259 String tempTry = "java.lang."+dependsOn;
260 try {
261 Class c = VerifyDesign.class.getClassLoader().loadClass(tempTry);
262 return;
263 } catch(ClassNotFoundException e) {
264 //not found, continue on...
265 }
266 //sometimes instead of passing java.lang.String or java.lang.Object, the bcel
267 //passes just String or Object
268 // if("String".equals(dependsOn) || "Object".equals(dependsOn))
269 // return;
270
271 verifyDependencyOk(dependsOn);
272
273 }
274
275 public static String getErrorMessage(String className, String dependsOnClass) {
276 String s = "\nYou are violating your own design...." +
277 "\nClass = "+className+" depends on\nClass = "+dependsOnClass+
278 "\nThe dependency to allow this is not defined in your design" +
279 "\nPackage="+VerifyDesignDelegate.getPackageName(className)+" is not defined to depend on"+
280 "\nPackage="+VerifyDesignDelegate.getPackageName(dependsOnClass)+
281 "\nChange the code or the design";
282 return s;
283 }
284
285 public static String getNoDefinitionError(String className) {
286 String s = "\nPackage="+VerifyDesignDelegate.getPackageName(className)+" is not defined in the design.\n"+
287 "All packages with classes must be declared in the design file\n"+
288 "Class found in the offending package="+className;
289 return s;
290 }
291
292 public static String getWrapperMsg(File originalFile, String message) {
293 String s = "\nThe file '" + originalFile.getAbsolutePath() + "' failed due to: " + message;
294 return s;
295 }
296
297 /**
298 * @param designErrors
299 */
300 public void fillInUnusedPackages(Vector designErrors)
301 {
302 Collection values = nameToPackage.values();
303 Iterator iterator = values.iterator();
304 while(iterator.hasNext()) {
305 Package pack = (Package)iterator.next();
306 if(!pack.isUsed()) {
307 String msg = "Package name="+pack.getName()+" is unused. Full package="+pack.getPackage();
308 log.log(msg, Project.MSG_ERR);
309 designErrors.add(new BuildException(msg));
310 } else {
311 fillInUnusedDepends(designErrors, pack);
312 }
313 }
314 }
315
316 /**
317 * @param designErrors
318 * @param pack
319 */
320 private void fillInUnusedDepends(Vector designErrors, Package pack)
321 {
322 Iterator iterator = pack.getUnusedDepends().iterator();
323 while(iterator.hasNext()) {
324 Depends depends = (Depends)iterator.next();
325 String msg = "Package name="+pack.getName()+" has a dependency declared that is not true anymore. Please erase the dependency <depends>"+depends.getName()+"</depends> from package="+pack.getName();
326 log.log(msg, Project.MSG_ERR);
327 designErrors.add(new BuildException(msg));
328 }
329 }
330 }