Package org.apache.tapestry5.ioc.util

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


        }


        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

                createNoOp(classFab, GET_SIGNATURE, "Expression %s for class %s is write-only.", expression,
                           rootType.getName());
                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 null;");

            builder.addln("return ($w) target.%s;", createMethodInvocation(builder, node, method));

            builder.end();

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

    }

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

        builder.begin();

        PropertyNavigationResult result = writePropertyNavigationCode(builder, rootClass, expression, terms, false);


        if (result == null)
        {
            builder.clear();
            builder
                    .addln("throw new RuntimeException(\"Expression %s for class %s is write-only.\");", expression,
                           rootClass.getName());
        }
        else
        {
            builder.addln("return %s;", result.getFinalStepVariable());

            builder.end();
        }

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


        return result == null ? null : result.getFinalReadInfo();
    }
View Full Code Here

        };
    }

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

        PropertyNavigationResult result = writePropertyNavigationCode(builder, rootClass, expression, terms, true);

        // Because we pass true for the forSetter parameter, we know that the expression for the leading
        // terms is a chain of readable expressions.  But is the final term writable?

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

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

            return null;
        }

        Class propertyType = writeMethod.getParameterTypes()[0];
        String propertyTypeName = ClassFabUtils.toJavaClassName(propertyType);

        // Cast the parameter from Object to the expected type for the method.

        builder.addln("%s value = %s;", propertyTypeName, ClassFabUtils.castReference("$2", propertyTypeName));

        // Invoke the method.

        builder.addln("%s.%s(value);", result.getFinalStepVariable(), writeMethod.getName());

        builder.end();

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

        return writeMethod;
    }
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

        sawToString |= ClassFabUtils.isToString(method);

        String invocationClassName = createInvocationClass(method);

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

        String methodFieldName = inject(Method.class, method);
        String aspectFieldName = inject(MethodAdvice.class, advice);

        builder.addln("%s invocation = new %s(%s, %s, $$);", Invocation.class.getName(), invocationClassName,
                      methodFieldName, delegateFieldName);

        builder.addln("%s.advise(invocation);", aspectFieldName);

        Class[] exceptionTypes = method.getExceptionTypes();

        builder.addln("if (invocation.isFail())").begin();

        for (Class exceptionType : exceptionTypes)
        {
            String name = exceptionType.getSimpleName().toLowerCase();

            String exceptionTypeFieldName = inject(Class.class, exceptionType);

            builder.addln("%s %s = (%s) invocation.getThrown(%s);", exceptionType.getName(), name,
                          exceptionType.getName(), exceptionTypeFieldName);
            builder.addln("if (%s != null) throw %s;", name, name);
        }

        builder.addln(
                "throw new IllegalStateException(\"Impossible exception thrown from intercepted invocation.\");");

        builder.end(); // if fail

        builder.addln("return ($r) invocation.getResult();");

        builder.end();

        interceptorFab.addMethod(Modifier.PUBLIC, new MethodSignature(method), builder.toString());

        remainingMethods.remove(method);
        advisedMethods.add(method);
    }
View Full Code Here

        constructorTypes.add(Method.class); // And passed up to the super class

        invocationFab.addField("_delegate", PRIVATE_FINAL, serviceInterface);
        constructorTypes.add(serviceInterface);

        BodyBuilder constructorBuilder = new BodyBuilder().begin().addln("super($1);").addln("_delegate = $2;");

        for (int i = 0; i < method.getParameterTypes().length; i++)
        {
            Class type = method.getParameterTypes()[i];

            String name = PARAMETER_FIELD + i;

            invocationFab.addField(name, type);

            constructorTypes.add(type);

            // $0 is this
            // $1 is Method
            // $2 is delegate
            // $3 is first method parameter ...

            constructorBuilder.addln("%s = $%d;", name, i + 3);
        }

        addProceed(method, invocationFab);
        addGetParameter(method, invocationFab);
        addOverride(method, invocationFab);

        constructorBuilder.end(); // constructor

        Class[] typesArray = constructorTypes.toArray(new Class[constructorTypes.size()]);

        invocationFab.addConstructor(typesArray, null, constructorBuilder.toString());

        invocationFab.createClass();

        return className;
    }
View Full Code Here

        Class[] exceptionTypes = method.getExceptionTypes();

        boolean isNonVoid = !returnType.equals(void.class);
        boolean hasChecked = exceptionTypes.length > 0;

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

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

        if (isNonVoid)
            builder.add("%s result = ", ClassFabUtils.toJavaClassName(returnType));

        builder.add("_delegate.%s(", method.getName());

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

            builder.add(PARAMETER_FIELD + i);
        }

        builder.addln(");"); // Call on delegate

        if (isNonVoid)
        {
            builder.add("overrideResult(($w) result);");
        }

        if (hasChecked)
        {
            builder.end();   // try

            for (Class exception : exceptionTypes)
            {
                builder.addln("catch (%s ex) { overrideThrown(ex); }", exception.getName());
            }
        }

        builder.end(); // method

        MethodSignature sig = new MethodSignature(void.class, "proceed", null, null);

        fab.addMethod(Modifier.PUBLIC, sig, builder.toString());
    }
View Full Code Here

    private void addGetParameter(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++)
        {
            // ($w) will wrap a primitive as a wrapper type
            builder.addln("case %d: return ($w) %s%d;", i, PARAMETER_FIELD, i);
        }

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

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

        fab.addMethod(Modifier.PUBLIC,
                      new MethodSignature(Object.class, "getParameter", new Class[] { int.class }, null),
                      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.