| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MethodInvocationBuilder |
|
| 0.0;0 |
| 1 | // Copyright 2006, 2007 The Apache Software Foundation | |
| 2 | // | |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 | // you may not use this file except in compliance with the License. | |
| 5 | // You may obtain a copy of the License at | |
| 6 | // | |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | // | |
| 9 | // 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.tapestry5.internal.util; | |
| 16 | ||
| 17 | import org.apache.tapestry5.ioc.internal.util.CollectionFactory; | |
| 18 | import org.apache.tapestry5.services.ClassTransformation; | |
| 19 | import org.apache.tapestry5.services.TransformMethodSignature; | |
| 20 | import org.apache.tapestry5.services.TransformUtils; | |
| 21 | ||
| 22 | import java.util.Map; | |
| 23 | ||
| 24 | /** | |
| 25 | * A utility class for building part of a method body to invoke a method. Analyzes the method and matches parameter | |
| 26 | * types to ParameterBuilders. | |
| 27 | */ | |
| 28 | 648 | public final class MethodInvocationBuilder |
| 29 | { | |
| 30 | 648 | private final Map<String, ParameterBuilder> builders = CollectionFactory.newMap(); |
| 31 | ||
| 32 | /** | |
| 33 | * Maps a parameter type to a {@link ParameterBuilder}. | |
| 34 | */ | |
| 35 | public void addParameter(String parameterType, ParameterBuilder builder) | |
| 36 | { | |
| 37 | // TODO: Name conflicts | |
| 38 | ||
| 39 | 474 | builders.put(parameterType, builder); |
| 40 | 474 | } |
| 41 | ||
| 42 | /** | |
| 43 | * Maps a parameter type to a literal string to be used for the parameter expression. | |
| 44 | * | |
| 45 | * @see StringParameterBuilder | |
| 46 | */ | |
| 47 | public void addParameter(String parameterType, String expression) | |
| 48 | { | |
| 49 | 474 | addParameter(parameterType, new StringParameterBuilder(expression)); |
| 50 | 474 | } |
| 51 | ||
| 52 | /** | |
| 53 | * Builds the method invocation. Analyzes the type of each parameter to the method, and uses a {@link | |
| 54 | * ParameterBuilder} to provide the expression. Supplies a default value (usually null) for any parameters that do | |
| 55 | * not have parameter builders. | |
| 56 | * | |
| 57 | * @param signature of the method to invoke | |
| 58 | * @param transformation | |
| 59 | * @return method invocation expression | |
| 60 | * @see TransformUtils#getDefaultValue(String) | |
| 61 | */ | |
| 62 | public String buildMethodInvocation(TransformMethodSignature signature, | |
| 63 | ClassTransformation transformation) | |
| 64 | { | |
| 65 | 620 | StringBuilder builder = new StringBuilder(signature.getMethodName()); |
| 66 | ||
| 67 | 620 | builder.append("("); |
| 68 | ||
| 69 | 620 | String[] parameterTypes = signature.getParameterTypes(); |
| 70 | ||
| 71 | 986 | for (int i = 0; i < parameterTypes.length; i++) |
| 72 | { | |
| 73 | 366 | if (i > 0) builder.append(", "); |
| 74 | ||
| 75 | 366 | String type = parameterTypes[i]; |
| 76 | ||
| 77 | 366 | ParameterBuilder parameterBuilder = builders.get(type); |
| 78 | ||
| 79 | 366 | if (parameterBuilder == null) |
| 80 | { | |
| 81 | // TODO: Log an error | |
| 82 | ||
| 83 | 2 | builder.append(TransformUtils.getDefaultValue(type)); |
| 84 | } | |
| 85 | else | |
| 86 | { | |
| 87 | 364 | builder.append(parameterBuilder.buildParameter(transformation)); |
| 88 | } | |
| 89 | } | |
| 90 | ||
| 91 | 620 | builder.append(")"); |
| 92 | ||
| 93 | 620 | return builder.toString(); |
| 94 | } | |
| 95 | ||
| 96 | } |