Package org.apache.tapestry.ioc.util

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


        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


        String assetSourceFieldName = null;
        String baseResourceFieldName = null;
        String resourcesFieldName = null;

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

        for (String name : names)
        {

            Inject annotation = transformation.getFieldAnnotation(name, Inject.class);

            // If the field has no annotation, or no value for its annotation, that's probably
            // a programmer error, but we'll let the later Inject-related workers complain about it.

            if (annotation == null)
                continue;

            String path = annotation.value();

            if (path.equals(""))
                continue;

            // This is tricky because we support sublcasses; if we ask the component at runtime for
            // its Resource (via the ComponentModel), we get the subclass, which will break the link
            // to any Asset resources from super-classes.

            if (assetSourceFieldName == null)
            {
                assetSourceFieldName = transformation.addInjectedField(
                        AssetSource.class,
                        "assetSource",
                        _assetSource);
                baseResourceFieldName = transformation.addInjectedField(
                        Resource.class,
                        "baseResource",
                        model.getBaseResource());
                resourcesFieldName = transformation.getResourcesFieldName();
            }

            builder.addln(
                    "%s = %s.findAsset(%s, \"%s\", %s.getLocale());",
                    name,
                    assetSourceFieldName,
                    baseResourceFieldName,
                    path,
                    resourcesFieldName);

            transformation.makeReadOnly(name);

            // Keep InjectNamedWorker from doing anything to it.

            transformation.claimField(name, annotation);
        }

        // If no matches

        if (assetSourceFieldName == null)
            return;

        builder.end();

        transformation.extendConstructor(builder.toString());

    }
View Full Code Here

        // 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

    }

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

        builder.addln("%s root = (%<s) $1;", ClassFabUtils.getJavaClassName(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.getJavaClassName(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.getJavaClassName(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.getJavaClassName(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.getJavaClassName(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

        // No methods, no work.

        if (methods.isEmpty())
            return;

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

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

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

        builder.end();

        transformation.extendMethod(TransformConstants.HANDLE_COMPONENT_EVENT, builder.toString());
    }
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

        // No methods, no work.

        if (methods.isEmpty()) return;

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

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

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

        builder.end();

        transformation.extendMethod(TransformConstants.HANDLE_COMPONENT_EVENT, 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;

        // I can't imagine a scenario where a component would have more than one
        // field with InjectComponent, but that's the way these APIs work, lists of names.

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

        builder.addln("%s core = %s.getCoreComponent();", Component.class.getName(), transformation
                .getResourcesFieldName());

        for (String fieldName : names)
        {
            InjectComponent annotation = transformation.getFieldAnnotation(
                    fieldName,
                    InjectComponent.class);

            String fieldType = transformation.getFieldType(fieldName);

            builder.addln("try");
            builder.begin();
            builder.addln("%s = (%s) core;", fieldName, fieldType);
            builder.end();
            builder.addln("catch (ClassCastException ex)");
            builder.begin();
            builder.addln(
                    "String message = %s.buildCastExceptionMessage(core, \"%s.%s\", \"%s\");",
                    InjectComponentWorker.class.getName(),
                    model.getComponentClassName(),
                    fieldName,
                    fieldType);
            builder.addln("throw new RuntimeException(message, ex);");
            builder.end();

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

        builder.end();

        transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, builder
                .toString());
    }
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.