Package org.apache.tapestry5.ioc.util

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


        if (methods.isEmpty()) return;

        model.addRenderPhase(methodAnnotation);

        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();

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

        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(); // try

        // Let runtime exceptions work up (they'll be caught at a higher level.
        // Wrap checked exceptions for later reporting.

        builder.addln("catch (RuntimeException ex) { throw ex; }");
        builder.addln("catch (Exception ex) { throw new RuntimeException(ex); }");

        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


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

        // No methods, no work.

        if (methods.isEmpty()) return;

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

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

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

        for (TransformMethodSignature method : methods)
            addCodeForMethod(builder, method, transformation, model);

        builder.end(); // try

        // Runtime exceptions pass right through.

        builder.addln("catch (RuntimeException ex) { throw ex; }");

        // Wrap others in a RuntimeException to communicate them up.

        builder.addln("catch (Exception ex) { throw new RuntimeException(ex); } ");

        builder.end();

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

        String name = ClassFabUtils.generateClassName("Instantiator");

        ClassFab cf = classFactory.newClass(name, AbstractInstantiator.class);

        BodyBuilder constructor = new BodyBuilder();

        // This is realy -1 + 2: The first value in constructorArgs is the InternalComponentResources, which doesn't
        // count toward's the Instantiator's constructor ... then we add in the Model and String description.
        // It's tricky because there's the constructor parameters for the Instantiator, most of which are stored
        // in fields and then used as the constructor parameters for the Component.

        Class[] constructorParameterTypes = new Class[constructorArgs.size() + 1];
        Object[] constructorParameterValues = new Object[constructorArgs.size() + 1];

        constructorParameterTypes[0] = ComponentModel.class;
        constructorParameterValues[0] = componentModel;

        constructorParameterTypes[1] = String.class;
        constructorParameterValues[1] = String.format("Instantiator[%s]", componentClassName);

        BodyBuilder newInstance = new BodyBuilder();

        newInstance.add("return new %s($1", componentClassName);

        constructor.begin();

        // Pass the model and description to AbstractInstantiator

        constructor.addln("super($1, $2);");

        // Again, skip the (implicit) InternalComponentResources field, that's
        // supplied to the Instantiator's newInstance() method.

        for (int i = 1; i < constructorArgs.size(); i++)
        {
            ConstructorArg arg = constructorArgs.get(i);

            CtClass argCtType = arg.getType();
            Class argType = toClass(argCtType.getName());

            boolean primitive = argCtType.isPrimitive();

            Class fieldType = primitive ? ClassFabUtils.getPrimitiveType(argType) : argType;

            String fieldName = "_param_" + i;

            constructorParameterTypes[i + 1] = argType;
            constructorParameterValues[i + 1] = arg.getValue();

            cf.addField(fieldName, fieldType);

            // $1 is model, $2 is description, to $3 is first dynamic parameter.

            // The arguments may be wrapper types, so we cast down to
            // the primitive type.

            String parameterReference = "$" + (i + 2);

            constructor.addln("%s = %s;",
                              fieldName,
                              ClassFabUtils.castReference(parameterReference, fieldType.getName()));

            newInstance.add(", %s", fieldName);
        }

        constructor.end();
        newInstance.addln(");");

        cf.addConstructor(constructorParameterTypes, null, constructor.toString());

        cf.addMethod(Modifier.PUBLIC, NEW_INSTANCE_SIGNATURE, newInstance.toString());

        Class instantiatorClass = cf.createClass();

        try
        {
View Full Code Here

            values.add(conduitPropertyType);
            values.add(annotationProvider);
            values.add(interner.format("PropertyConduit[%s %s]", rootType.getName(), expression));
            values.add(typeCoercer);

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

            builder.addln("super($1,$2,$3,$4);");

            int index = 5;

            for (ConstructorParameter p : parameters)
            {
                types.add(p.getType());
                values.add(p.getValue());

                builder.addln("%s = $%d;", p.getFieldName(), index++);
            }


            builder.end();

            Class[] arrayOfTypes = types.toArray(new Class[0]);


            classFab.addConstructor(arrayOfTypes, null, builder.toString());

            return values.toArray();
        }
View Full Code Here

            return type != DEREF && type != SAFEDEREF;
        }

        private void createGetRoot()
        {
            BodyBuilder builder = new BodyBuilder().begin();

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

            builder.addln(
                    "if (root == null) throw new NullPointerException(\"Root object of property expression '%s' is null.\");",
                    expression);

            builder.addln("return root;");

            builder.end();

            MethodSignature sig = new MethodSignature(rootType, "getRoot", new Class[] { Object.class }, null);

            classFab.addMethod(Modifier.PRIVATE, sig, builder.toString());
        }
View Full Code Here

            }
        }

        private void createRangeOpGetter(Tree node)
        {
            BodyBuilder builder = new BodyBuilder().begin();

            addRootVariable(builder);

            builder.addln("return %s;", createMethodInvocation(builder, node, 0, RANGE));

            builder.end();

            classFab.addMethod(Modifier.PUBLIC, GET_SIGNATURE, builder.toString());
        }
View Full Code Here

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

        private void createNotOpGetter(Tree node)
        {
            BodyBuilder builder = new BodyBuilder().begin();

            addRootVariable(builder);

            builder.addln("return %s;", createMethodInvocation(builder, node, 0, INVERT));

            builder.end();

            classFab.addMethod(Modifier.PUBLIC, GET_SIGNATURE, builder.toString());
        }
View Full Code Here

        }


        public void createListGetter(Tree node)
        {
            BodyBuilder builder = new BodyBuilder().begin();

            addRootVariable(builder);

            builder.addln("return %s;", createListConstructor(builder, node));

            builder.end();

            classFab.addMethod(Modifier.PUBLIC, GET_SIGNATURE, builder.toString());
        }
View Full Code Here

            {
                createNoOpSetter();
                return;
            }

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

            addRootVariable(builder);

            builder.addln("%s target = navigate(root);",
                          ClassFabUtils.toJavaClassName(navigateMethod.getReturnType()));

            // I.e. due to ?. operator. The navigate method will already have checked for nulls
            // if they are not allowed.

            builder.addln("if (target == null) return;");

            String propertyTypeName = ClassFabUtils.toJavaClassName(info.getType());

            builder.addln("target.%s(%s);", method.getName(), ClassFabUtils.castReference("$2", propertyTypeName));

            builder.end();

            classFab.addMethod(Modifier.PUBLIC, SET_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.