001// Copyright 2005 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
015package org.apache.tapestry.annotations;
016
017import java.lang.reflect.Method;
018import java.lang.reflect.Modifier;
019
020import org.apache.hivemind.ApplicationRuntimeException;
021import org.apache.hivemind.Location;
022import org.apache.hivemind.service.BodyBuilder;
023import org.apache.hivemind.service.MethodSignature;
024import org.apache.tapestry.Tapestry;
025import org.apache.tapestry.enhance.EnhancementOperation;
026import org.apache.tapestry.spec.IComponentSpecification;
027
028/**
029 * Builds a method that accesses component messages.
030 * 
031 * @author Howard Lewis Ship
032 * @since 4.0
033 */
034public class MessageAnnotationWorker implements MethodAnnotationEnhancementWorker
035{
036
037    public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
038            Method method, Location location)
039    {
040        if (!method.getReturnType().equals(String.class))
041            throw new ApplicationRuntimeException(AnnotationMessages.returnStringOnly(method
042                    .getReturnType()));
043
044        Message message = method.getAnnotation(Message.class);
045
046        String keyName = message.value();
047
048        if (keyName.equals(""))
049            keyName = convertMethodNameToKeyName(method.getName());
050
051        BodyBuilder builder = new BodyBuilder();
052
053        builder.begin();
054
055        Class[] parameterTypes = method.getParameterTypes();
056        int paramCount = Tapestry.size(parameterTypes);
057
058        if (paramCount > 0)
059        {
060            builder.addln("java.lang.Object[] params = new java.lang.Object[{0}];", paramCount);
061            for (int i = 0; i < paramCount; i++)
062            {
063                builder.add("params[{0}] = ", i);
064
065                if (parameterTypes[i].isPrimitive())
066                    builder.add("($w) ");
067
068                // Parameter $0 is this, so $1 is the first real parameter.
069
070                builder.addln("${0};", i + 1);
071            }
072        }
073
074        builder.add("return getMessages().");
075
076        if (paramCount == 0)
077            builder.addln("getMessage(\"{0}\");", keyName);
078        else
079            builder.addln("format(\"{0}\", params);", keyName);
080
081        builder.end();
082
083        op.addMethod(Modifier.PUBLIC, new MethodSignature(method), builder.toString(), location);
084
085        if (isGetter(method))
086            op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));
087    }
088
089    boolean isGetter(Method method)
090    {
091        // We already know the return type is String
092
093        return method.getName().startsWith("get") && method.getParameterTypes().length == 0;
094    }
095
096    String convertMethodNameToKeyName(String methodName)
097    {
098        StringBuffer buffer = new StringBuffer();
099
100        int cursorx = methodName.startsWith("get") ? 3 : 0;
101        int length = methodName.length();
102        boolean atStart = true;
103
104        while (cursorx < length)
105        {
106            char ch = methodName.charAt(cursorx);
107
108            if (Character.isUpperCase(ch))
109            {
110                if (!atStart)
111                    buffer.append('-');
112                buffer.append(Character.toLowerCase(ch));
113            }
114            else
115                buffer.append(ch);
116
117            atStart = false;
118
119            cursorx++;
120        }
121
122        return buffer.toString();
123    }
124}