01: // Copyright 2006 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.util;
16:
17: import java.util.Map;
18:
19: import org.apache.tapestry.ioc.internal.util.CollectionFactory;
20: import org.apache.tapestry.services.ClassTransformation;
21: import org.apache.tapestry.services.MethodSignature;
22: import org.apache.tapestry.services.TransformUtils;
23:
24: /**
25: * A utility class for building part of a method body to invoke a method. Analyzes the method and
26: * matches parameter types to ParameterBuilders.
27: *
28: *
29: */
30: public final class MethodInvocationBuilder {
31: private final Map<String, ParameterBuilder> _builders = CollectionFactory
32: .newMap();
33:
34: /**
35: * Maps a parameter type to a {@link ParameterBuilder}.
36: */
37: public void addParameter(String parameterType,
38: ParameterBuilder builder) {
39: // TODO: Name conflicts
40:
41: _builders.put(parameterType, builder);
42: }
43:
44: /**
45: * Maps a parameter type to a literal string to be used for the parameter expression.
46: *
47: * @see StringParameterBuilder
48: */
49: public void addParameter(String parameterType, String expression) {
50: addParameter(parameterType, new StringParameterBuilder(
51: expression));
52: }
53:
54: /**
55: * Builds the method invocation. Analyzes the type of each parameter to the method, and uses a
56: * {@link ParameterBuilder} to provide the expression. Supplies a default value (usually null)
57: * for any parameters that do not have parameter builders.
58: *
59: * @param signature
60: * of the method to invoke
61: * @param transformation
62: * @return method invocation expression
63: * @see TransformUtils#getDefaultValue(String)
64: */
65: public String buildMethodInvocation(MethodSignature signature,
66: ClassTransformation transformation) {
67: StringBuilder builder = new StringBuilder(signature
68: .getMethodName());
69:
70: builder.append("(");
71:
72: String[] parameterTypes = signature.getParameterTypes();
73:
74: for (int i = 0; i < parameterTypes.length; i++) {
75: if (i > 0)
76: builder.append(", ");
77:
78: String type = parameterTypes[i];
79:
80: ParameterBuilder parameterBuilder = _builders.get(type);
81:
82: if (parameterBuilder == null) {
83: // TODO: Log an error
84:
85: builder.append(TransformUtils.getDefaultValue(type));
86: } else {
87: builder.append(parameterBuilder
88: .buildParameter(transformation));
89: }
90: }
91:
92: builder.append(")");
93:
94: return builder.toString();
95: }
96:
97: }
|