summaryrefslogtreecommitdiffstats
path: root/src/java/net/sf/antcontrib/design/Design.java
blob: 6da018d219156402d3314b908d76bd49ad751aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (c) 2001-2005 Ant-Contrib project.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.antcontrib.design;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Location;
import org.apache.tools.ant.Project;


/*
 * Created on Aug 24, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
/**
 * FILL IN JAVADOC HERE
 *
 * @author Dean Hiller(dean@xsoftware.biz)
 */
public class Design {

    private Map nameToPackage = new HashMap();
    private Map packageNameToPackage = new HashMap();
	private boolean isCircularDesign;
	private Log log;
	private Location location;
	
	private String currentClass = null;
	private String currentPackageName = null;
	private Package currentAliasPackage = null;
	
	private HashSet primitives = new HashSet();
	
    public Design(boolean isCircularDesign, Log log, Location loc) {
    	//by default, add java as a configured package with the name java
    	Package p = new Package();
    	p.setIncludeSubpackages(true);
    	p.setName("java");
    	p.setNeedDeclarations(false);
    	p.setPackage("java");
    	addConfiguredPackage(p);
    	
    	this.isCircularDesign = isCircularDesign;
    	this.log = log;
    	this.location = loc;
    	
    	primitives.add("boolean");

    	//integral types
    	primitives.add("byte");
    	primitives.add("short");
    	primitives.add("int");
    	primitives.add("long");
    	primitives.add("char");
    	
    	//floating point types
    	primitives.add("double");
    	primitives.add("float");    	
    }
    
    public Package getPackage(String nameAttribute) {
    	return (Package)nameToPackage.get(nameAttribute);
    }
    
    private Package retreivePack(String thePackage) {
    	if(thePackage == null)
    		throw new IllegalArgumentException("Cannot retrieve null packages");
    	
    	Package result = null;
    	String currentPackage = thePackage;
    	while(!Package.DEFAULT.equals(currentPackage)) {
    		result = (Package)packageNameToPackage.get(currentPackage);
    		if(result != null) {
    			if(currentPackage.equals(thePackage))
    				return result;
    			else if(result.isIncludeSubpackages())
    				return result;
    			return null;
    		}
    		currentPackage = VerifyDesignDelegate.getPackageName(currentPackage);
    	}
    	
    	//result must now be default package
    	if(result != null && result.isIncludeSubpackages())
    		return result;

   		return null;
    }
    
    public void addConfiguredPackage(Package p) {
        
        String pack = p.getPackage();
        
        Depends[] depends = p.getDepends();
        
        if(depends != null && !isCircularDesign) {
	        //make sure all depends are in Map first
	        //circular references then are not a problem because they must
	        //put the stuff in order
	        for(int i = 0; i < depends.length; i++) {
	            Package dependsPackage = (Package)nameToPackage.get(depends[i].getName());
	            
	            if(dependsPackage == null) {
	                throw new RuntimeException("package name="+p.getName()+" did not\n" +
	                		"have "+depends[i]+" listed before it.  circularDesign is off\n"+
							"so package="+p.getName()+" must be moved up in the xml file");
	            }
	        }
        }
        
        nameToPackage.put(p.getName(), p);
        packageNameToPackage.put(p.getPackage(), p);
    }

    public void verifyDependencyOk(String className) {
    	log.log("         className="+className, Project.MSG_DEBUG);
    	if(className.startsWith("L"))
    		className = className.substring(1, className.length());    	
    	
    	String classPackage = VerifyDesignDelegate.getPackageName(className);
    	
    	//check if this is an needdeclarations="false" package, if so, the dependency is ok if it
    	//is not declared
    	log.log("         classPackage="+classPackage, Project.MSG_DEBUG);
    	Package p = retreivePack(classPackage);
    	if(p != null && !p.isNeedDeclarations())
    		return;
    	
    	String pack = currentAliasPackage.getPackage();
    	
    	log.log("         AllowedDepends="+pack, Project.MSG_DEBUG);
    	log.log("         CurrentDepends="+className, Project.MSG_DEBUG);
    	if(isClassInPackage(className, currentAliasPackage))
    		return;
    	
    	Depends[] depends = currentAliasPackage.getDepends();
    	
    	//probably want to create a regular expression out of all the depends and just match on that 
    	//each time.  for now though, just get it working and do the basic(optimize later if needed)    	
    	for(int i = 0; i < depends.length; i++) {
    		Depends d = depends[i];
    		String name = d.getName();
    		
    		Package temp = getPackage(name);
	    	log.log("         AllowedDepends="+temp.getPackage(), Project.MSG_DEBUG);
	    	log.log("         CurrentDepends="+className, Project.MSG_DEBUG);			
    		if(isClassInPackage(className, temp))
    			return;
    	}
    	
    	log.log("***************************************", Project.MSG_DEBUG);
    	log.log("***************************************", Project.MSG_DEBUG);

    	throw new BuildException(Design.getErrorMessage(currentClass, className), location);    	
    }

    public boolean isClassInPackage(String className, Package p) {
    	String classPackage = VerifyDesignDelegate.getPackageName(className);
		if(p.isIncludeSubpackages()) {
			if(className.startsWith(p.getPackage()))
				return true;
		} else { //if not including subpackages, the it must be the exact package.
			if(classPackage.equals(p.getPackage()))
				return true;
		}
		return false;
    }
	/**
	 * @param className
	 * @return whether or not this class needs to be checked. (ie. if the
	 * attribute needdepends=false, we don't care about this package.
	 */
	public boolean needEvalCurrentClass(String className) {
		currentClass = className;
    	String packageName = VerifyDesignDelegate.getPackageName(className);
//    	log("class="+className, Project.MSG_DEBUG);
		if(!packageName.equals(currentPackageName) || currentAliasPackage == null) {
			currentPackageName = packageName;
	    	log.log("\nEvaluating package="+currentPackageName, Project.MSG_INFO);
	    	currentAliasPackage = retreivePack(packageName);
	    	//DEANDO: test this scenario
	    	if(currentAliasPackage == null) {
	        	log.log("   class="+className, Project.MSG_VERBOSE);
	    		throw new BuildException(getNoDefinitionError(className), location);
	    	}
		}
    	log.log("   class="+className, Project.MSG_VERBOSE);
    	
    	if(!className.startsWith(currentPackageName))
    		throw new RuntimeException("Internal Error");
    	
    	if(!currentAliasPackage.getNeedDepends())
    		return false;
    	return true;
	}
	
    public String getCurrentClass() {
    	return currentClass;
    }	
    
    void checkClass(String dependsOn) {
    	log.log("         dependsOn1="+dependsOn, Project.MSG_DEBUG);
    	if(dependsOn.endsWith("[]")) {
    		int index = dependsOn.indexOf("[");
    		dependsOn = dependsOn.substring(0, index);
        	log.log("         dependsOn2="+dependsOn, Project.MSG_DEBUG);    		
    	}
    	
    	if(primitives.contains(dependsOn))
    		return;
    	
    	//Anything in java.lang package seems to be passed in as just the 
    	//className with no package like Object, String or Class, so here we try to
    	//see if the name is a java.lang class....
    	String tempTry = "java.lang."+dependsOn;
    	try {
    		Class c = VerifyDesign.class.getClassLoader().loadClass(tempTry);
    		return;
    	} catch(ClassNotFoundException e) {
    		//not found, continue on...
    	}
    	//sometimes instead of passing java.lang.String or java.lang.Object, the bcel 
    	//passes just String or Object
//    	if("String".equals(dependsOn) || "Object".equals(dependsOn))
//    		return;
    	
    	verifyDependencyOk(dependsOn);
    		
    }

	public static String getErrorMessage(String className, String dependsOnClass) {
		String s =  "\nYou are violating your own design...." +
				    "\nClass = "+className+" depends on\nClass = "+dependsOnClass+
			        "\nThe dependency to allow this is not defined in your design" +
			        "\nPackage="+VerifyDesignDelegate.getPackageName(className)+" is not defined to depend on"+
					"\nPackage="+VerifyDesignDelegate.getPackageName(dependsOnClass)+
					"\nChange the code or the design";
		return s;
	}
	
	public static String getNoDefinitionError(String className) {
		String s = "\nPackage="+VerifyDesignDelegate.getPackageName(className)+" is not defined in the design.\n"+
					"All packages with classes must be declared in the design file\n"+
					"Class found in the offending package="+className;
		return s;
	}
	
	public static String getWrapperMsg(File originalFile, String message) {
		String s = "\nThe file '" + originalFile.getAbsolutePath() + "' failed due to: " + message;
		return s;
	}
}