Package org.apache.tapestry5.ioc.util

Examples of org.apache.tapestry5.ioc.util.BodyBuilder


    {
        String defaultFieldName = transformation.addField(Modifier.PRIVATE, fieldType, fieldName + "_default");

        String invariantFieldName = transformation.addField(Modifier.PRIVATE, "boolean", fieldName + "_invariant");

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

        addDefaultBindingSetup(parameterName, defaultPrefix, defaultBinding, resourcesFieldName, transformation,
                               builder);

        builder.addln("%s = %s.isInvariant(\"%s\");", invariantFieldName, resourcesFieldName, parameterName);

        // Store the current value of the field into the default field. This value will
        // be used to reset the field after rendering.

        builder.addln("%s = %s;", defaultFieldName, fieldName);
        builder.end();

        transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, builder
                .toString());

        // Now, when the component completes rendering, ensure that any variant parameters are
        // are returned to default value. This isn't necessary when the parameter is not cached,
        // because (unless the binding is invariant), there's no value to get rid of (and if it is
        // invariant, there's no need to get rid of it).

        if (cache)
        {
            builder.clear();

            builder.addln("if (! %s)", invariantFieldName);
            builder.begin();
            builder.addln("%s = %s;", fieldName, defaultFieldName);
            builder.addln("%s = false;", cachedFieldName);
            builder.end();

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

        return invariantFieldName;
    }
View Full Code Here


                                                                 advisedMethod.getMethodName() + "Info",
                                                                 info);

        String componentResourcesField = transformation.getResourcesFieldName();

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

        builder.addln("%s invocation = new %<s(%s, %s, $$);", invocationClassName, methodInfoField,
                      componentResourcesField);

        // Off into the first MethodAdvice

        builder.addln("invocation.proceed();");

        String[] exceptionTypes = advisedMethod.getExceptionTypes();
        int exceptionCount = exceptionTypes.length;

        if (exceptionCount > 0)
        {
            for (int i = 0; i < exceptionCount; i++)
            {
                String type = exceptionTypes[i];
                String name = "ex" + i;

                builder.addln("%s %s = (%1$s) invocation.getThrown(%s.getExceptionType(%d));",
                              type, name, methodInfoField, i);
                builder.addln("if (%s != null) throw %<s;", name);
            }
        }

        String returnType = advisedMethod.getReturnType();

        if (!returnType.equals("void"))
        {
            builder.addln("return %s;",
                          ClassFabUtils.castReference("invocation.getResult()", returnType));
        }


        builder.end();

        /** Replace the original method with the new implementation. */
        transformation.addMethod(advisedMethod, builder.toString());
    }
View Full Code Here

        transformation.addMethod(advisedMethod, builder.toString());
    }

    private void implementInvokeAdvisedMethod(String advisedMethodName) throws CannotCompileException
    {
        BodyBuilder builder = new BodyBuilder().begin();

        boolean isVoid = advisedMethod.getReturnType().equals("void");

        builder.addln("%s component = (%<s) getComponentResources().getComponent();", transformation.getClassName());

        String[] exceptionTypes = advisedMethod.getExceptionTypes();
        int exceptionCount = exceptionTypes.length;

        if (exceptionCount > 0)
            builder.add("try").begin();

        if (!isVoid) builder.add("overrideResult(($w) ");

        builder.add("component.%s(", advisedMethodName);

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

            builder.add("%s%d", FIELD_NAME, i);
        }

        builder.add(")");

        if (!isVoid) builder.add(")");

        builder.addln(";");

        if (exceptionCount > 0)
        {
            builder.end(); // try

            for (int i = 0; i < exceptionCount; i++)
            {
                builder.addln("catch (%s ex) { overrideThrown(ex); }", exceptionTypes[i]);
            }
        }

        builder.end();

        CtMethod method = new CtMethod(CtClass.voidType, "invokeAdvisedMethod",
                                       new CtClass[0], invocationCtClass);

        method.setModifiers(PROTECTED_FINAL);
        method.setBody(builder.toString());

        invocationCtClass.addMethod(method);
    }
View Full Code Here

        CtClass[] parameterTypes = new CtClass[parameterCount + 2];

        parameterTypes[0] = toCtClass(MethodInvocationInfo.class);
        parameterTypes[1] = toCtClass(ComponentResources.class);

        BodyBuilder builder = new BodyBuilder().begin().addln("super($1,$2);");

        for (int i = 0; i < parameterCount; i++)
        {
            String name = FIELD_NAME + i;

            String parameterTypeName = advisedMethod.getParameterTypes()[i];

            CtClass parameterType = classSource.toCtClass(parameterTypeName);

            CtField field = new CtField(parameterType, name, invocationCtClass);
            field.setModifiers(Modifier.PRIVATE);
            invocationCtClass.addField(field);

            parameterTypes[2 + i] = parameterType;

            builder.addln("%s = $%d;", name, 3 + i);
        }

        builder.end();

        CtConstructor constructor = new CtConstructor(parameterTypes, invocationCtClass);
        constructor.setBody(builder.toString());

        invocationCtClass.addConstructor(constructor);

    }
View Full Code Here

        return classSource.toCtClass(input);
    }

    private void implementOverride() throws CannotCompileException
    {
        BodyBuilder builder = new BodyBuilder().begin();

        builder.addln("switch ($1)").begin();

        int count = advisedMethod.getParameterTypes().length;

        for (int i = 0; i < count; i++)
        {
            String type = advisedMethod.getParameterTypes()[i];

            builder.addln("case %d: %s = %s; break;", i, FIELD_NAME + i, ClassFabUtils.castReference("$2", type));
        }

        builder.addln("default: throw new IllegalArgumentException(\"Index out of range.\");");

        builder.end().end();

        CtMethod method = new CtMethod(CtClass.voidType, "override",
                                       new CtClass[] { CtClass.intType, toCtClass(Object.class) }, invocationCtClass);

        method.setModifiers(PUBLIC_FINAL);
        method.setBody(builder.toString());

        invocationCtClass.addMethod(method);
    }
View Full Code Here

        invocationCtClass.addMethod(method);
    }

    private void implementGetParameter() throws CannotCompileException
    {
        BodyBuilder builder = new BodyBuilder().begin();

        builder.addln("switch ($1)").begin();

        int count = advisedMethod.getParameterTypes().length;

        for (int i = 0; i < count; i++)
        {
            builder.addln("case %d: return ($w) %s;", i, FIELD_NAME + i);
        }

        builder.addln("default: throw new IllegalArgumentException(\"Index out of range.\");");

        builder.end().end();

        CtMethod method = new CtMethod(toCtClass(Object.class), "getParameter",
                                       new CtClass[] { CtClass.intType }, invocationCtClass);

        method.setModifiers(PUBLIC_FINAL);
        method.setBody(builder.toString());

        invocationCtClass.addMethod(method);
    }
View Full Code Here

        }

        cf.addInterface(commandInterface);
        cf.addField("_commands", Modifier.PRIVATE | Modifier.FINAL, array);

        BodyBuilder builder = new BodyBuilder();
        builder.addln("_commands = (%s[]) $1.toArray(new %<s[0]);", commandInterface.getName());

        cf.addConstructor(new Class[] { List.class }, null, builder.toString());
    }
View Full Code Here

            return;
        }

        String defaultValue = defaultForReturnType(returnType);

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

        builder.addln("%s result = %s;", ClassFabUtils.toJavaClassName(returnType), defaultValue);
        builder.addln("for (int i = 0; i < _commands.length; i++)");

        builder.begin();
        builder.addln("result = _commands[i].%s($$);", sig.getName());

        builder.addln("if (result != %s) break;", defaultValue);

        builder.end();

        builder.addln("return result;");
        builder.end();

        cf.addMethod(Modifier.PUBLIC, sig, builder.toString());
    }
View Full Code Here

        return "0";
    }

    private void addVoidMethod(ClassFab cf, Class commandInterface, MethodSignature sig)
    {
        BodyBuilder builder = new BodyBuilder();

        builder.begin();

        builder.addln("for (int i = 0; i < _commands.length; i++)");
        builder.addln("  _commands[i].%s($$);", sig.getName());

        builder.end();

        cf.addMethod(Modifier.PUBLIC, sig, builder.toString());
    }
View Full Code Here

        ct.addMethod(reader, "return 66;");

        ct.replaceReadAccess("_targetField", "read_target_value");

        BodyBuilder builder = new BodyBuilder();
        builder.begin();
        builder.addln("%s.run();", name);
        builder.addln("return $_ + 1;");
        builder.end();

        ct.extendExistingMethod(sig, builder.toString());

        ct.finish();

        Object target = instantiate(MethodPrefixTarget.class, ct, null);
View Full Code Here

TOP

Related Classes of org.apache.tapestry5.ioc.util.BodyBuilder

Copyright © 2018 www.massapicom. 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.