001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal.services;
016:
017: import static java.lang.String.format;
018: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
019: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newSet;
020:
021: import java.lang.reflect.Modifier;
022: import java.util.Formatter;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import javassist.CannotCompileException;
027: import javassist.CtClass;
028: import javassist.CtConstructor;
029: import javassist.CtField;
030: import javassist.CtMethod;
031: import javassist.NotFoundException;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.tapestry.ioc.internal.util.Defense;
035: import org.apache.tapestry.ioc.internal.util.InternalUtils;
036: import org.apache.tapestry.ioc.services.ClassFab;
037: import org.apache.tapestry.ioc.services.ClassFabUtils;
038: import org.apache.tapestry.ioc.services.MethodIterator;
039: import org.apache.tapestry.ioc.services.MethodSignature;
040:
041: /**
042: * Implementation of {@link org.apache.tapestry.ioc.services.ClassFab}. Hides, as much as possible,
043: * the underlying library (Javassist).
044: */
045: public class ClassFabImpl extends AbstractFab implements ClassFab {
046: private static final Map<Class, String> DEFAULT_RETURN = newMap();
047:
048: static {
049: DEFAULT_RETURN.put(boolean.class, "false");
050: DEFAULT_RETURN.put(long.class, "0L");
051: DEFAULT_RETURN.put(float.class, "0.0f");
052: DEFAULT_RETURN.put(double.class, "0.0d");
053: }
054:
055: /**
056: * Add fields, methods, and constructors are added, their psuedo-code is appended to this
057: * description, which is used by toString().
058: */
059: private final StringBuilder _description = new StringBuilder();
060:
061: private final Formatter _formatter = new Formatter(_description);
062:
063: private final Set<MethodSignature> _addedSignatures = newSet();
064:
065: public ClassFabImpl(CtClassSource source, CtClass ctClass, Log log) {
066: super (source, ctClass, log);
067: }
068:
069: /**
070: * Returns a representation of the fabricated class, including inheritance, fields,
071: * constructors, methods and method bodies.
072: *
073: * @since 1.1
074: */
075: @Override
076: public String toString() {
077: StringBuilder buffer = new StringBuilder("ClassFab[\n");
078:
079: try {
080: buffer.append(buildClassAndInheritance());
081:
082: buffer.append(_description.toString());
083: } catch (Exception ex) {
084: buffer.append(" *** ");
085: buffer.append(ex);
086: }
087:
088: buffer.append("\n]");
089:
090: return buffer.toString();
091: }
092:
093: private String buildClassAndInheritance() throws NotFoundException {
094: StringBuilder buffer = new StringBuilder();
095:
096: buffer.append(Modifier.toString(getCtClass().getModifiers()));
097: buffer.append(" class ");
098: buffer.append(getName());
099: buffer.append(" extends ");
100: buffer.append(getCtClass().getSuperclass().getName());
101: buffer.append("\n");
102:
103: CtClass[] interfaces = getCtClass().getInterfaces();
104:
105: if (interfaces.length > 0) {
106: buffer.append(" implements ");
107:
108: for (int i = 0; i < interfaces.length; i++) {
109: if (i > 0)
110: buffer.append(", ");
111:
112: buffer.append(interfaces[i].getName());
113: }
114:
115: buffer.append("\n\n");
116: }
117:
118: return buffer.toString();
119: }
120:
121: /**
122: * Returns the name of the class fabricated by this instance.
123: */
124: String getName() {
125: return getCtClass().getName();
126: }
127:
128: public void addField(String name, Class type) {
129: addField(name, Modifier.PRIVATE, type);
130: }
131:
132: public void addField(String name, int modifiers, Class type) {
133: _lock.check();
134:
135: CtClass ctType = convertClass(type);
136:
137: try {
138: CtField field = new CtField(ctType, name, getCtClass());
139: field.setModifiers(modifiers);
140:
141: getCtClass().addField(field);
142: } catch (CannotCompileException ex) {
143: // Have yet to find a way to make this happen!
144: throw new RuntimeException(ServiceMessages
145: .unableToAddField(name, getCtClass(), ex), ex);
146: }
147:
148: _formatter.format("%s %s %s;\n\n",
149: Modifier.toString(modifiers), ClassFabUtils
150: .toJavaClassName(type), name);
151: }
152:
153: public void proxyMethodsToDelegate(Class serviceInterface,
154: String delegateExpression, String toString) {
155: _lock.check();
156:
157: addInterface(serviceInterface);
158:
159: MethodIterator mi = new MethodIterator(serviceInterface);
160:
161: while (mi.hasNext()) {
162: MethodSignature sig = mi.next();
163:
164: // ($r) properly handles void methods for us, which keeps this simple.
165:
166: String body = format("return ($r) %s.%s($$);",
167: delegateExpression, sig.getName());
168:
169: addMethod(Modifier.PUBLIC, sig, body);
170: }
171:
172: if (!mi.getToString())
173: addToString(toString);
174: }
175:
176: public void addToString(String toString) {
177: _lock.check();
178:
179: MethodSignature sig = new MethodSignature(String.class,
180: "toString", null, null);
181:
182: // TODO: Very simple quoting here, will break down if the string itself contains
183: // double quotes or various other characters that need escaping.
184:
185: addMethod(Modifier.PUBLIC, sig, format("return \"%s\";",
186: toString));
187: }
188:
189: public void addMethod(int modifiers, MethodSignature ms, String body) {
190: _lock.check();
191:
192: if (_addedSignatures.contains(ms))
193: throw new RuntimeException(ServiceMessages
194: .duplicateMethodInClass(ms, this ));
195:
196: CtClass ctReturnType = convertClass(ms.getReturnType());
197:
198: CtClass[] ctParameters = convertClasses(ms.getParameterTypes());
199: CtClass[] ctExceptions = convertClasses(ms.getExceptionTypes());
200:
201: CtMethod method = new CtMethod(ctReturnType, ms.getName(),
202: ctParameters, getCtClass());
203:
204: try {
205: method.setModifiers(modifiers);
206: method.setBody(body);
207: method.setExceptionTypes(ctExceptions);
208:
209: getCtClass().addMethod(method);
210: } catch (Exception ex) {
211: throw new RuntimeException(ServiceMessages
212: .unableToAddMethod(ms, getCtClass(), ex), ex);
213: }
214:
215: _addedSignatures.add(ms);
216:
217: // modifiers, return type, name
218:
219: _formatter.format("%s %s %s", Modifier.toString(modifiers),
220: ClassFabUtils.toJavaClassName(ms.getReturnType()), ms
221: .getName());
222:
223: // parameters, exceptions and body from this:
224: addMethodDetailsToDescription(ms.getParameterTypes(), ms
225: .getExceptionTypes(), body);
226:
227: _description.append("\n\n");
228: }
229:
230: public void addNoOpMethod(MethodSignature signature) {
231: _lock.check();
232:
233: Class returnType = signature.getReturnType();
234:
235: if (returnType.equals(void.class)) {
236: addMethod(Modifier.PUBLIC, signature, "return;");
237: return;
238: }
239:
240: String value = "null";
241: if (returnType.isPrimitive()) {
242: value = DEFAULT_RETURN.get(returnType);
243: if (value == null)
244: value = "0";
245: }
246:
247: addMethod(Modifier.PUBLIC, signature, "return " + value + ";");
248: }
249:
250: public void addConstructor(Class[] parameterTypes,
251: Class[] exceptions, String body) {
252: Defense.notBlank(body, "body");
253:
254: _lock.check();
255:
256: CtClass[] ctParameters = convertClasses(parameterTypes);
257: CtClass[] ctExceptions = convertClasses(exceptions);
258:
259: try {
260: CtConstructor constructor = new CtConstructor(ctParameters,
261: getCtClass());
262: constructor.setExceptionTypes(ctExceptions);
263: constructor.setBody(body);
264:
265: getCtClass().addConstructor(constructor);
266: } catch (Exception ex) {
267: throw new RuntimeException(ServiceMessages
268: .unableToAddConstructor(getCtClass(), ex), ex);
269: }
270:
271: _description.append("public ");
272:
273: // This isn't quite right; we should strip the package portion off of the name.
274: // However, fabricated classes are almost always in the "default" package, so
275: // this is OK.
276:
277: _description.append(getName());
278:
279: addMethodDetailsToDescription(parameterTypes, exceptions, body);
280:
281: _description.append("\n\n");
282: }
283:
284: /**
285: * Adds a listing of method (or constructor) parameters and thrown exceptions, and the body, to
286: * the description
287: *
288: * @param parameterTypes
289: * types of method parameters, or null
290: * @param exceptions
291: * types of throw exceptions, or null
292: * @param body
293: * body of method or constructor
294: */
295: private void addMethodDetailsToDescription(Class[] parameterTypes,
296: Class[] exceptions, String body) {
297: _description.append("(");
298:
299: int count = InternalUtils.size(parameterTypes);
300: for (int i = 0; i < count; i++) {
301: if (i > 0)
302: _description.append(", ");
303:
304: _description.append(ClassFabUtils
305: .toJavaClassName(parameterTypes[i]));
306:
307: _description.append(" $");
308: _description.append(i + 1);
309: }
310:
311: _description.append(")");
312:
313: count = InternalUtils.size(exceptions);
314: for (int i = 0; i < count; i++) {
315: if (i == 0)
316: _description.append("\n throws ");
317: else
318: _description.append(", ");
319:
320: // Since this can never be an array type, we don't need to use getJavaClassName
321:
322: _description.append(exceptions[i].getName());
323: }
324:
325: _description.append("\n");
326: _description.append(body);
327: }
328: }
|