Clover coverage report - Code Coverage for tapestry-annotations release 4.0-beta-1
Coverage timestamp: Fri Jun 24 2005 14:37:32 EDT
file stats: LOC: 124   Methods: 3
NCLOC: 71   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MessageAnnotationWorker.java 100% 100% 100% 100%
coverage
 1    // Copyright 2005 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.tapestry.annotations;
 16   
 17    import java.lang.reflect.Method;
 18    import java.lang.reflect.Modifier;
 19   
 20    import org.apache.hivemind.ApplicationRuntimeException;
 21    import org.apache.hivemind.service.BodyBuilder;
 22    import org.apache.hivemind.service.MethodSignature;
 23    import org.apache.tapestry.Tapestry;
 24    import org.apache.tapestry.enhance.EnhancementOperation;
 25    import org.apache.tapestry.spec.IComponentSpecification;
 26   
 27    /**
 28    * Builds a method that accesses component messages.
 29    *
 30    * @author Howard Lewis Ship
 31    * @since 4.0
 32    */
 33    public class MessageAnnotationWorker implements MethodAnnotationEnhancementWorker
 34    {
 35   
 36  6 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
 37    Method method)
 38    {
 39  6 if (!method.getReturnType().equals(String.class))
 40  1 throw new ApplicationRuntimeException(AnnotationMessages.returnStringOnly(method
 41    .getReturnType()));
 42   
 43  5 Message message = method.getAnnotation(Message.class);
 44   
 45  5 String keyName = message.value();
 46   
 47  5 if (keyName.equals(""))
 48  4 keyName = convertMethodNameToKeyName(method.getName());
 49   
 50  5 BodyBuilder builder = new BodyBuilder();
 51   
 52  5 builder.add("return getMessages().");
 53   
 54  5 Class[] parameterTypes = method.getParameterTypes();
 55  5 int paramCount = Tapestry.size(parameterTypes);
 56   
 57  5 builder.add(paramCount == 0 ? "getMessage" : "format");
 58   
 59  5 builder.add("(\"{0}\"", keyName);
 60   
 61  5 for (int i = 0; i < paramCount; i++)
 62    {
 63  4 if (i == 0)
 64  2 builder.add(", new java.lang.Object[] { ");
 65    else
 66  2 builder.add(", ");
 67   
 68    // Javassist is kind enough to automatically wrapper stuff if we
 69    // ask it.
 70   
 71  4 if (parameterTypes[i].isPrimitive())
 72  2 builder.add("($w) ");
 73   
 74    // Method parameter are numbered from 1, $0 is this
 75  4 builder.add("$" + (i + 1));
 76    }
 77   
 78  5 if (paramCount > 0)
 79  2 builder.add(" }");
 80   
 81  5 builder.add(");");
 82   
 83  5 op.addMethod(Modifier.PUBLIC, new MethodSignature(method), builder.toString());
 84   
 85  5 if (isGetter(method))
 86  1 op.claimProperty(AnnotationUtils.getPropertyName(method));
 87    }
 88   
 89  5 boolean isGetter(Method method)
 90    {
 91    // We already know the return type is String
 92   
 93  5 return method.getName().startsWith("get") && method.getParameterTypes().length == 0;
 94    }
 95   
 96  8 String convertMethodNameToKeyName(String methodName)
 97    {
 98  8 StringBuffer buffer = new StringBuffer();
 99   
 100  8 int cursorx = methodName.startsWith("get") ? 3 : 0;
 101  8 int length = methodName.length();
 102  8 boolean atStart = true;
 103   
 104  8 while (cursorx < length)
 105    {
 106  86 char ch = methodName.charAt(cursorx);
 107   
 108  86 if (Character.isUpperCase(ch))
 109    {
 110  13 if (!atStart)
 111  10 buffer.append('-');
 112  13 buffer.append(Character.toLowerCase(ch));
 113    }
 114    else
 115  73 buffer.append(ch);
 116   
 117  86 atStart = false;
 118   
 119  86 cursorx++;
 120    }
 121   
 122  8 return buffer.toString();
 123    }
 124    }