Package org.apache.tapestry5.ioc.util

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


        String methodInfoField = transformation.addInjectedField(
                ComponentMethodInvocationInfo.class, advisedMethod.getMethodName() + "$info", info);

        String componentResourcesField = transformation.getResourcesFieldName();

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

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

        // 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.addNewMethod(advisedMethod, builder.toString());
    }
View Full Code Here


    }

    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

        private MethodAccess createMethodAccessForTarget(String accessTarget, boolean passInstance)
        {
            boolean isVoid = sig.getReturnType().equals("void");

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

            builder.addln("%s instance = (%<s) $1;", getClassName());

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

            if (!isVoid)
            {
                builder.add("return success(($w) ");
            }

            // Call the target, even if the eventual method is void

            builder.add(accessTarget);
            builder.add("(");

            if (passInstance)
                builder.add("instance");

            int p = 0;

            for (String type : sig.getParameterTypes())
            {
                if (passInstance || p != 0)
                    builder.add(", ");

                String ref = String.format("$2[%d]", p++);
                builder.add(ClassFabUtils.castReference(ref, type));
            }

            // Balance the call to success()
            if (!isVoid)
                builder.add(")");

            builder.addln(");");

            if (isVoid)
                builder.addln("return success(null);");

            builder.end(); // try
            builder.addln("catch (java.lang.RuntimeException ex) { throw ex; }");
            builder.addln("catch (java.lang.Exception ex) { return fail(ex); }");

            builder.end();

            return instantiateMethodAccessFromBody(builder.toString());
        }
View Full Code Here

                    + Modifier.STATIC, sig.getReturnType(), methodName, parameterTypes.toArray(new String[0]),
                    sig.getExceptionTypes());

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

            BodyBuilder builder = new BodyBuilder();

            builder.begin();

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

            builder.add("$1.%s(", sig.getMethodName());

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

                builder.add("$%d", i + 2);
            }

            builder.addln(");");

            builder.end();

            addNewMethod(accessMethodSignature, builder.toString());

            return methodName;
        }
View Full Code Here

            }
        }

        private void addFieldAccessReadMethod(ClassFab cf, TransformMethod readAccess)
        {
            BodyBuilder builder = new BodyBuilder().begin();

            builder.addln("%s instance = (%<s) $1;", getClassName());
            builder.addln("return ($w) instance.%s();", readAccess.getName());

            builder.end();

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

            cf.addMethod(Modifier.PUBLIC, FIELD_ACCESS_READ_SIGNATURE, builder.toString());
        }

        private void addFieldAccessWriteMethod(ClassFab cf, TransformMethod writeAccess)
        {
            BodyBuilder builder = new BodyBuilder().begin();

            builder.addln("%s instance = (%<s) $1;", getClassName());
            builder.addln("%s value = %s;", type, ClassFabUtils.castReference("$2", type));
            builder.addln("instance.%s(value);", writeAccess.getName());

            builder.end();

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

            // The ($r) cast will convert the result to the method return type; generally
            // this does nothing. but for primitive types, it will unwrap
            // the wrapper type back to a primitive.

            BodyBuilder builder = new BodyBuilder();

            builder.begin();

            builder.addln("%s result = ($r) ((%s) %s.get());", type, cast, conduitFieldName);

            if (developmentMode)
            {
                builder.addln("%s = result;", name);
            }

            builder.addln("return result;");

            builder.end();

            addNewMethod(readSig, builder.toString());

            replaceReadAccess(readMethodName);

            String writeMethodName = newMemberName("set", name);

            TransformMethodSignature writeSig = new TransformMethodSignature(Modifier.PRIVATE, "void", writeMethodName,
                    new String[]
                    { type }, null);

            builder.clear().begin();

            if (developmentMode)
            {
                builder.addln("%s = $1;", name);
            }

            builder.addln("%s.set(($w) $1);", conduitFieldName);

            builder.end();

            addNewMethod(writeSig, builder.toString());

            replaceWriteAccess(writeMethodName);
        }
View Full Code Here

        String accessFieldName = transformation.addField(Modifier.PRIVATE, ParameterAccess.class.getName(),
                                                         fieldName + "_access");

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

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

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

        // Order is (alas) important here: must invoke getParameterAccess() after the binding setup, as
        // that code may invoke InternalComponentResources.bindParameter().

        builder.addln("%s = %s.getParameterAccess(\"%s\");", accessFieldName, 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.isInvariant())", accessFieldName);
            builder.begin();
            builder.addln("%s = %s;", fieldName, defaultFieldName);
            builder.addln("%s = false;", cachedFieldName);
            builder.end();

            // Clean up after the component renders.

            String body = builder.toString();

            transformation.extendMethod(TransformConstants.POST_RENDER_CLEANUP_SIGNATURE, body);

            // And again, when the page is detached (TAPESTRY-2460)

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

        return accessFieldName;
    }
View Full Code Here

    private void addWriterMethod(String fieldName, String cachedFieldName, String accessFieldName, boolean cache,
                                 String parameterName,
                                 String fieldType, String resourcesFieldName,
                                 ClassTransformation transformation)
    {
        BodyBuilder builder = new BodyBuilder();
        builder.begin();

        // Before the component is loaded, updating the property sets the default value
        // for the parameter. The value is stored in the field, but will be
        // rolled into default field inside containingPageDidLoad().

        builder.addln("if (! %s.isLoaded())", resourcesFieldName);
        builder.begin();
        builder.addln("%s = $1;", fieldName);
        builder.addln("return;");
        builder.end();

        // Always start by updating the parameter; this will implicitly check for
        // read-only or unbound parameters. $1 is the single parameter
        // to the method.

        builder.addln("%s.write(($w)$1);", accessFieldName);

        builder.addln("%s = $1;", fieldName);

        if (cache) builder.addln("%s = %s.isRendering();", cachedFieldName, resourcesFieldName);

        builder.end();

        String methodName = transformation.newMemberName("update_parameter", parameterName);

        TransformMethodSignature signature = new TransformMethodSignature(Modifier.PRIVATE, "void", methodName,
                                                                          new String[] {fieldType}, null);

        transformation.addMethod(signature, builder.toString());

        transformation.replaceWriteAccess(fieldName, methodName);
    }
View Full Code Here

     */
    private void addReaderMethod(String fieldName, String cachedFieldName, String accessFieldName, boolean cache,
                                 String parameterName, String fieldType, String resourcesFieldName,
                                 ClassTransformation transformation)
    {
        BodyBuilder builder = new BodyBuilder();
        builder.begin();

        // While the component is still loading, or when the value for the component is cached,
        // or if the value is not bound, then return the current value of the field.

        builder.addln("if (%s || ! %s.isLoaded() || ! %s.isBound()) return %s;", cachedFieldName,
                      resourcesFieldName, accessFieldName, fieldName);

        String cast = TransformUtils.getWrapperTypeName(fieldType);

        // The ($r) cast will convert the result to the method return type; generally
        // this does nothing. but for primitive types, it will unwrap
        // the wrapper type back to a primitive.  We pass the desired type name
        // to readParameter(), since its easier to convert it properly to
        // a type on that end than in the generated code.

        builder.addln("%s result = ($r) ((%s) %s.read(\"%2$s\"));", fieldType, cast, accessFieldName);

        // If the binding is invariant, then it's ok to cache. Othewise, its only
        // ok to cache if a) the @Parameter says to cache and b) the component
        // is rendering at the point when field is accessed.

        builder.add("if (%s.isInvariant()", accessFieldName);

        if (cache) builder.add(" || %s.isRendering()", resourcesFieldName);

        builder.addln(")");
        builder.begin();
        builder.addln("%s = result;", fieldName);
        builder.addln("%s = true;", cachedFieldName);
        builder.end();

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

        String methodName = transformation.newMemberName("read_parameter", parameterName);

        TransformMethodSignature signature = new TransformMethodSignature(Modifier.PRIVATE, fieldType, methodName, null,
                                                                          null);

        transformation.addMethod(signature, builder.toString());

        transformation.replaceReadAccess(fieldName, methodName);
    }
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.