Package org.apache.tapestry5.ioc.util

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


    private void addOverride(Method method, ClassFab fab)
    {
        Class[] parameterTypes = method.getParameterTypes();

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

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

        for (int i = 0; i < parameterTypes.length; i++)
        {
            Class type = parameterTypes[i];
            String typeName = ClassFabUtils.toJavaClassName(type);

            builder.addln("case %d: %s%d = %s; return;",
                          i, PARAMETER_FIELD, i,
                          ClassFabUtils.castReference("$2", typeName));
        }

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

        builder.end().end(); // switch and method

        fab.addMethod(Modifier.PUBLIC,
                      new MethodSignature(void.class, "override", new Class[] { int.class, Object.class }, null),
                      builder.toString());
    }
View Full Code Here


        // Time to add the constructor.

        Class[] parameterTypes = new Class[injections.size()];
        Object[] parameters = new Object[injections.size()];

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

        for (int i = 0; i < injections.size(); i++)
        {
            Injection injection = injections.get(i);

            builder.addln("%s = $%d;", injection._fieldName, i + 1);

            parameterTypes[i] = injection._fieldType;
            parameters[i] = injection._injectedValue;
        }

        builder.end();

        interceptorFab.addConstructor(parameterTypes, null, builder.toString());

        return parameters;
    }
View Full Code Here

        cf.addField("_registry", Modifier.PRIVATE | Modifier.FINAL, StrategyRegistry.class);
        cf.addConstructor(new Class[]
                { StrategyRegistry.class }, null, "_registry = $1;");

        BodyBuilder builder = new BodyBuilder();

        MethodIterator mi = new MethodIterator(interfaceClass);

        while (mi.hasNext())
        {
            MethodSignature sig = mi.next();

            // TODO: Checks for methods that don't have at least one parameter, or don't have a
            // compatible 1st parameter. Currently, we'll get a Javassist exception.

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

            builder.addln("Object selector = $1;");
            builder.addln(
                    "%s adapter = (%<s) _registry.getByInstance(selector);",
                    interfaceClassName);
            builder.addln("return ($r) adapter.%s($$);", sig.getName());

            builder.end();

            cf.addMethod(Modifier.PUBLIC, sig, builder.toString());

        }

        if (!mi.getToString())
            cf.addToString(String.format("<Strategy for %s>", interfaceClassName));
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

        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 TransformMethodSignature(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

    }

    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

     */
    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.  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.readParameter(\"%s\", \"%2$s\"));", 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.addln("return (%s) %s.getPage(\"%s\");", fieldType, componentSource, injectedPageName);

        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

            if (InternalUtils.isBlank(componentId))
                componentId = InternalUtils.stripMemberPrefix(fieldName);

            transformation.makeReadOnly(fieldName);

            BodyBuilder builder = new BodyBuilder().addln("try").begin();

            builder.addln(
                    "%s = (%s) %s.getEmbeddedComponent(\"%s\");",
                    fieldName,
                    type,
                    resourcesFieldName,
                    componentId);
            builder.end();
            builder.addln("catch (ClassCastException ex)").begin();
            builder.addln("throw new RuntimeException(%s.formatMessage(%s, \"%s\", \"%s\", \"%s\"), ex);",
                          getClass().getName(), resourcesFieldName, fieldName, type, componentId);
            builder.end();

            transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, builder.toString());
        }
    }
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 container = %s.getContainer();", Component.class.getName(), transformation
                .getResourcesFieldName());

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

            String fieldType = transformation.getFieldType(fieldName);

            builder.addln("try");
            builder.begin();
            builder.addln("this.%s = (%s) container;", fieldName, fieldType);
            builder.end();
            builder.addln("catch (ClassCastException ex)");
            builder.begin();
            builder.addln(
                    "String message = %s.buildCastExceptionMessage(container, \"%s.%s\", \"%s\");",
                    InjectContainerWorker.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.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.