Package org.apache.tapestry.ioc.util

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


                + "_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


    private void addWriterMethod(String fieldName, String cachedFieldName, 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("if (%s.isBound(\"%s\"))", resourcesFieldName, parameterName);
        builder.addln("  %s.writeParameter(\"%s\", ($w)$1);", resourcesFieldName, parameterName);

        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

    /** Adds a private method that will be the replacement for read-access to the field. */
    private void addReaderMethod(String fieldName, String cachedFieldName,
            String invariantFieldName, 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(\"%s\")) return %s;",
                cachedFieldName,
                resourcesFieldName,
                parameterName,
                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.

        builder.addln(
                "%s result = ($r) ((%s) %s.readParameter(\"%s\", $type));",
                fieldType,
                cast,
                resourcesFieldName,
                parameterName);

        // 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", invariantFieldName);

        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

                .resolvePageClassNameToPageName(fieldType) : pageName;

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

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

        builder.add(
                "%s page = %s.get(\"%s\");",
                Page.class.getName(),
                cacheFieldName,
                injectedPageName);

        builder.addln("return (%s) page.getRootElement().getComponent();", fieldType);

        builder.end();

        transformation.addMethod(sig, builder.toString());
        transformation.replaceReadAccess(fieldName, methodName);
        transformation.makeReadOnly(fieldName);
        transformation.removeField(fieldName);

        transformation.claimField(fieldName, annotation);
View Full Code Here

        List<String> fieldNames = transformation.findFieldsOfType(BLOCK_TYPE_NAME);

        if (fieldNames.isEmpty())
            return;

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

        int count = 0;

        String resourcesFieldName = transformation.getResourcesFieldName();

        for (String fieldName : fieldNames)
        {
            Inject annotation = transformation.getFieldAnnotation(fieldName, Inject.class);

            if (annotation == null)
                continue;

            String blockId = getBlockId(fieldName, annotation);

            builder.addln("%s = %s.getBlock(\"%s\");", fieldName, resourcesFieldName, blockId);

            transformation.makeReadOnly(fieldName);
            transformation.claimField(fieldName, annotation);

            count++;
        }

        // Fields yes, but no annotations, so nothing to really do.

        if (count == 0)
            return;

        builder.end();

        transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, builder
                .toString());
    }
View Full Code Here

        String resourcesFieldName = transformation.getResourcesFieldName();

        String writeMethodName = transformation.newMemberName("write", fieldName);

        BodyBuilder builder = new BodyBuilder();

        builder.begin();
        builder.addln(
                "%s.persistFieldChange(\"%s\", ($w) $1);",
                resourcesFieldName,
                logicalFieldName);
        builder.addln("%s = $1;", fieldName);
        builder.end();

        transformation.addMethod(new MethodSignature(Modifier.PRIVATE, "void", writeMethodName,
                new String[]
                { fieldType }, null), builder.toString());

        transformation.replaceWriteAccess(fieldName, writeMethodName);

        builder.clear();
        builder.begin();

        // Check to see if there's a recorded change for this component, this field.

        builder.addln("if (%s.hasFieldChange(\"%s\"))", resourcesFieldName, logicalFieldName);

        String wrapperType = TransformUtils.getWrapperTypeName(fieldType);

        // Get the value, cast it to the correct type (or wrapper type)
        builder.add(
                "  %s = ((%s) %s.getFieldChange(\"%s\"))",
                fieldName,
                wrapperType,
                resourcesFieldName,
                logicalFieldName);

        // For primtive types, add in the method call to unwrap the wrapper type to a primitive type

        String unwrapMethodName = TransformUtils.getUnwrapperMethodName(fieldType);

        if (unwrapMethodName == null)
            builder.addln(";");
        else
            builder.addln(".%s();", unwrapMethodName);

        builder.end();

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

        transformation.claimField(fieldName, annotation);
    }
View Full Code Here

        // Except in the root class, don't bother to add a new method unless there's something to
        // call (beside super).

        if (methods.isEmpty()) return;

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

        // If in a subclass, and in normal order mode, invoke the super class version first.

        if (!(_reverse || model.isRootClass()))
        {
            builder.addln("super.%s($$);", _lifecycleMethodName);
            builder.addln(CHECK_ABORT_FLAG);
        }

        Iterator<MethodSignature> i = _reverse ? InternalUtils.reverseIterator(methods) : methods
                .iterator();

        while (i.hasNext())
            addMethodCallToBody(builder, i.next(), transformation);

        // In reverse order in a a subclass, invoke the super method last.

        if (_reverse && !model.isRootClass()) builder.addln("super.%s($$);", _lifecycleMethodName);

        builder.end();

        // Let's see if this works; for base classes, we are adding an empty method the adding a
        // non-empty
        // method "on top of it".

        transformation.addMethod(_lifecycleMethodSignature, builder.toString());
    }
View Full Code Here

        // Except in the root class, don't bother to add a new method unless there's something to
        // call (beside super).

        if (methods.isEmpty()) return;

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

        // If in a subclass, and in normal order mode, invoke the super class version first.

        if (!(_reverse || model.isRootClass()))
        {
            builder.addln("super.%s($$);", _lifecycleMethodName);
            builder.addln(CHECK_ABORT_FLAG);
        }

        Iterator<TransformMethodSignature> i = _reverse ? InternalUtils.reverseIterator(methods) : methods
                .iterator();

        while (i.hasNext())
            addMethodCallToBody(builder, i.next(), transformation);

        // In reverse order in a a subclass, invoke the super method last.

        if (_reverse && !model.isRootClass()) builder.addln("super.%s($$);", _lifecycleMethodName);

        builder.end();

        // Let's see if this works; for base classes, we are adding an empty method the adding a
        // non-empty
        // method "on top of it".

        transformation.addMethod(_lifecycleMethodSignature, builder.toString());
    }
View Full Code Here

    }

    private Method buildGetter(Class rootClass, ClassFab classFab, String expression, String[] terms)
    {
        BodyBuilder builder = new BodyBuilder();
        builder.begin();

        builder.addln("%s root = (%<s) $1;", ClassFabUtils.toJavaClassName(rootClass));
        String previousStep = "root";

        Class activeType = rootClass;
        Method result = null;
        boolean writeOnly = false;

        for (int i = 0; i < terms.length; i++)
        {
            String thisStep = "step" + (i + 1);
            String term = terms[i];

            boolean nullable = term.endsWith("?");
            if (nullable) term = term.substring(0, term.length() - 1);

            Method readMethod = readMethodForTerm(activeType, expression, term, (i < terms.length - 1));

            if (readMethod == null)
            {
                writeOnly = true;
                break;
            }

            // If a primitive type, convert to wrapper type

            Class termType = ClassFabUtils.getWrapperType(readMethod.getReturnType());

            // $w is harmless for non-wrapper types.

            builder.addln("%s %s = ($w) %s.%s();", ClassFabUtils.toJavaClassName(termType), thisStep, previousStep,
                          readMethod.getName());

            if (nullable) builder.addln("if (%s == null) return null;", thisStep);

            activeType = termType;
            result = readMethod;
            previousStep = thisStep;
        }

        builder.addln("return %s;", previousStep);

        builder.end();

        if (writeOnly)
        {
            builder.clear();
            builder
                    .addln("throw new java.lang.RuntimeException(\"Expression %s for class %s is write-only.\");",
                           expression, rootClass.getName());
        }

        classFab.addMethod(Modifier.PUBLIC, GET_SIGNATURE, builder.toString());

        return result;
    }
View Full Code Here

        return result;
    }

    private Method buildSetter(Class rootClass, ClassFab classFab, String expression, String[] terms)
    {
        BodyBuilder builder = new BodyBuilder();
        builder.begin();

        builder.addln("%s root = (%<s) $1;", ClassFabUtils.toJavaClassName(rootClass));
        String previousStep = "root";

        Class activeType = rootClass;

        for (int i = 0; i < terms.length - 1; i++)
        {
            String thisStep = "step" + (i + 1);
            String term = terms[i];

            boolean nullable = term.endsWith("?");
            if (nullable) term = term.substring(0, term.length() - 1);

            Method readMethod = readMethodForTerm(activeType, expression, term, true);

            // If a primitive type, convert to wrapper type

            Class termType = ClassFabUtils.getWrapperType(readMethod.getReturnType());

            // $w is harmless for non-wrapper types.

            builder.addln("%s %s = ($w) %s.%s();", ClassFabUtils.toJavaClassName(termType), thisStep, previousStep,
                          readMethod.getName());

            if (nullable) builder.addln("if (%s == null) return;", thisStep);

            activeType = termType;
            previousStep = thisStep;
        }

        // When writing, the last step is different.

        Method writeMethod = writeMethodForTerm(activeType, expression, terms[terms.length - 1]);

        if (writeMethod == null)
        {
            builder.clear();
            builder
                    .addln("throw new java.lang.RuntimeException(\"Expression %s for class %s is read-only.\");",
                           expression, rootClass.getName());
            classFab.addMethod(Modifier.PUBLIC, SET_SIGNATURE, builder.toString());

            return null;
        }

        Class parameterType = writeMethod.getParameterTypes()[0];

        Class wrapperType = ClassFabUtils.getWrapperType(parameterType);

        builder.addln("%s value = (%<s) $2;", ClassFabUtils.toJavaClassName(wrapperType));

        builder.add("%s.%s(value", previousStep, writeMethod.getName());

        if (parameterType != wrapperType)
            builder.add(".%s()", ClassFabUtils.getUnwrapMethodName(parameterType.getName()));

        builder.addln(");");

        builder.end();

        classFab.addMethod(Modifier.PUBLIC, SET_SIGNATURE, builder.toString());

        return writeMethod;
    }
View Full Code Here

TOP

Related Classes of org.apache.tapestry.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.