Package org.apache.tapestry.internal.services

Source Code of org.apache.tapestry.internal.services.OnEventWorker

// Copyright 2006, 2007 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry.internal.services;

import org.apache.tapestry.annotations.OnEvent;
import org.apache.tapestry.ioc.util.BodyBuilder;
import org.apache.tapestry.model.MutableComponentModel;
import org.apache.tapestry.runtime.Component;
import org.apache.tapestry.runtime.ComponentEventException;
import org.apache.tapestry.services.*;

import java.util.List;

/**
* Provides implementations of the
* {@link Component#handleComponentEvent(org.apache.tapestry.runtime.ComponentEvent)} method, based
* on {@link OnEvent} annotations.
*/
public class OnEventWorker implements ComponentClassTransformWorker
{
    static final String OBJECT_ARRAY_TYPE = "java.lang.Object[]";

    private final static int ANY_NUMBER_OF_PARAMETERS = -1;

    public void transform(final ClassTransformation transformation, MutableComponentModel model)
    {
        MethodFilter filter = new MethodFilter()
        {
            public boolean accept(TransformMethodSignature signature)
            {
                return signature.getMethodName().startsWith("on") || transformation.getMethodAnnotation(signature,
                                                                                                        OnEvent.class) != null;
            }
        };

        List<TransformMethodSignature> methods = transformation.findMethods(filter);

        // No methods, no work.

        if (methods.isEmpty()) return;

        BodyBuilder builder = new BodyBuilder();
        builder.begin();

        builder.addln("if ($1.isAborted()) return $_;");

        builder.addln("String methodDescription = null;");

        // Because we track the methodDescription inside a local variable, we can have a single
        // encompassing try..catch for all possible events for this component.

        builder.addln("try");
        builder.begin();

        for (TransformMethodSignature method : methods)
            addCodeForMethod(builder, method, transformation);

        builder.end(); // try

        builder.addln("catch (Exception ex)");
        builder.begin();
        builder.addln("throw new %s(ex.getMessage(), methodDescription, ex);", ComponentEventException.class.getName());
        builder.end();

        builder.end();

        transformation.extendMethod(TransformConstants.HANDLE_COMPONENT_EVENT, builder.toString());
    }

    private void addCodeForMethod(BodyBuilder builder, TransformMethodSignature method,
                                  ClassTransformation transformation)
    {
        // $1 is the event

        int parameterCount = getParameterCount(method);

        OnEvent annotation = transformation.getMethodAnnotation(method, OnEvent.class);

        String eventType = extractEventType(method, annotation);


        String componentId = extractComponentId(method, annotation);


        builder.addln("if ($1.matches(\"%s\", \"%s\", %d))", eventType, componentId, parameterCount);
        builder.begin();

        // Ensure that we return true, because *some* event handler method was invoked,
        // even if it chose not to abort the event.

        builder.addln("$_ = true;");

        // Several subsequent calls need to know the method name.

        builder.addln("methodDescription = \"%s\";", transformation.getMethodIdentifier(method));
        builder.addln("$1.setSource(this, methodDescription);");


        boolean isNonVoid = !method.getReturnType().equals("void");

        // Store the result, converting primitives to wrappers automatically.

        if (isNonVoid) builder.add("if ($1.storeResult(($w) ");

        builder.add("%s(", method.getMethodName());

        buildMethodParameters(builder, method);

        if (isNonVoid) builder.addln("))) return true;");
        else builder.addln(");");

        builder.end();
    }

    private String extractComponentId(TransformMethodSignature method, OnEvent annotation)
    {
        if (annotation != null) return annotation.component();

        // Method name started with "on". Extract the component id, if present.

        String name = method.getMethodName();

        int fromx = name.indexOf("From");

        if (fromx < 0) return "";

        return name.substring(fromx + 4);
    }

    private String extractEventType(TransformMethodSignature method, OnEvent annotation)
    {
        if (annotation != null) return annotation.value();

        // Method name started with "on". Extract the event type.

        String name = method.getMethodName();

        int fromx = name.indexOf("From");

        return fromx == -1 ? name.substring(2) : name.substring(2, fromx);
    }

    private int getParameterCount(TransformMethodSignature method)
    {
        String[] types = method.getParameterTypes();

        if (types.length == 0) return 0;

        if (types[0].equals(OBJECT_ARRAY_TYPE)) return ANY_NUMBER_OF_PARAMETERS;

        // TODO: If the first parameter is Object[], that should be the only parameter.
        // Otherwise, Object[] should not be allowed.

        return types.length;
    }

    private void buildMethodParameters(BodyBuilder builder, TransformMethodSignature method)
    {
        int contextIndex = 0;

        for (int i = 0; i < method.getParameterTypes().length; i++)
        {
            if (i > 0) builder.add(", ");

            String type = method.getParameterTypes()[i];

            // Type Object[] is a special case, it gets all of the context parameters in one go.

            if (type.equals(OBJECT_ARRAY_TYPE))
            {
                builder.add("$1.getContext()");
                continue;
            }

            boolean isPrimitive = TransformUtils.isPrimitive(type);
            String wrapperType = TransformUtils.getWrapperTypeName(type);

            // Add a cast to the wrapper type up front

            if (isPrimitive) builder.add("(");

            // A cast is always needed (i.e. from java.lang.Object to, say, java.lang.String, etc.).
            // The wrapper type will be the actual type unless its a primitive, in which case it
            // really will be the wrapper type.

            builder.add("(%s)", wrapperType);

            // The strings for desired type name will likely repeat a bit; it may be
            // worth it to inject them as final fields. Could increase the number
            // of constructor parameters pretty dramatically, however, and will reduce
            // the readability of the output method bodies.

            builder.add("$1.coerceContext(%d, \"%s\")", contextIndex++, wrapperType);

            // and invoke a method on the cast value to get back to primitive
            if (isPrimitive) builder.add(").%s()", TransformUtils.getUnwrapperMethodName(type));
        }
    }
}
TOP

Related Classes of org.apache.tapestry.internal.services.OnEventWorker

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.