Package org.apache.tapestry.ioc.util

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


            return;
        }

        String defaultValue = defaultForReturnType(returnType);

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

        builder.addln("%s result = %s;", ClassFabUtils.toJavaClassName(returnType), defaultValue);
        builder.addln("for (int i = 0; i < _commands.length; i++)");

        builder.begin();
        builder.addln("result = _commands[i].%s($$);", sig.getName());

        builder.addln("if (result != %s) break;", defaultValue);

        builder.end();

        builder.addln("return result;");
        builder.end();

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


        return "0";
    }

    private void addVoidMethod(ClassFab cf, Class commandInterface, MethodSignature sig)
    {
        BodyBuilder builder = new BodyBuilder();

        builder.begin();

        builder.addln("for (int i = 0; i < _commands.length; i++)");
        builder.addln("  _commands[i].%s($$);", sig.getName());

        builder.end();

        cf.addMethod(Modifier.PUBLIC, sig, 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.CONTAINING_PAGE_DID_ATTACH_SIGNATURE,
                builder.toString());

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

        Class returnType = signature.getReturnType();
        boolean isVoid = returnType.equals(void.class);

        // We'll see how well Javassist handles void methods with this setup

        BodyBuilder builder = new BodyBuilder();
        builder.begin();
        builder.addln("boolean debug = _logger.isDebugEnabled();");

        builder.addln("if (debug)");
        builder.addln("  _logger.entry(%s, $args);", name);

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

        if (!isVoid) builder.add("%s result = ", toJavaClassName(returnType));

        builder.addln("_delegate.%s($$);", signature.getName());

        if (isVoid)
        {
            builder.addln("if (debug)");
            builder.addln(format("  _logger.voidExit(%s);", name));
            builder.addln("return;");
        }
        else
        {
            builder.addln("if (debug)");
            builder.addln(format("  _logger.exit(%s, ($w)result);", name));
            builder.addln("return result;");
        }

        builder.end(); // try

        // Now, a catch for each declared exception (if any)

        if (signature.getExceptionTypes() != null)
            for (Class exceptionType : signature.getExceptionTypes())
                addExceptionHandler(builder, name, exceptionType);

        // And a catch for RuntimeException

        addExceptionHandler(builder, name, RuntimeException.class);

        builder.end();

        cf.addMethod(Modifier.PUBLIC, signature, builder.toString());
    }
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

        return cf.createClass();
    }

    private void addDelegateGetter(ClassFab cf, Class serviceInterface, String serviceId)
    {
        BodyBuilder builder = new BodyBuilder();
        builder.begin();

        // Check to see if the registry has shutdown. The registryShutdown() method
        // throws IllegalStateException.

        builder.addln("if (_shutdown) %s.registryShutdown(\"%s\");", IOCProxyUtilities.class
                .getName(), serviceId);

        // We can release the creator after invoking it, we only create the service once.

        builder.addln("if (_delegate == null)");
        builder.begin();
        builder.addln("_delegate = (%s) _creator.createObject();", serviceInterface.getName());
        builder.addln("_creator = null;");
        builder.end();

        builder.addln("return _delegate;");
        builder.end();

        MethodSignature sig = new MethodSignature(serviceInterface, "_delegate", null, null);

        // Here's the rub, this _delegate() method has to be synchronized. But after the first
        // time through (when we create the service), the time inside the method is infintesmal.
        // Let's hope that they aren't lying when they say that synchronized is now super cheap!

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

        }

        cf.addInterface(commandInterface);
        cf.addField("_commands", array);

        BodyBuilder builder = new BodyBuilder();
        builder.addln("_commands = (%s[]) $1.toArray(new %<s[0]);", commandInterface.getName());

        cf.addConstructor(new Class[]
        { List.class }, null, builder.toString());
    }
View Full Code Here

            return;
        }

        String defaultValue = defaultForReturnType(returnType);

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

        builder.addln("%s result = %s;", ClassFabUtils.getJavaClassName(returnType), defaultValue);
        builder.addln("for (int i = 0; i < _commands.length; i++)");

        builder.begin();
        builder.addln("result = _commands[i].%s($$);", sig.getName());

        builder.addln("if (result != %s) break;", defaultValue);

        builder.end();

        builder.addln("return result;");
        builder.end();

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

        return "0";
    }

    private void addVoidMethod(ClassFab cf, Class commandInterface, MethodSignature sig)
    {
        BodyBuilder builder = new BodyBuilder();

        builder.begin();

        builder.addln("for (int i = 0; i < _commands.length; i++)");
        builder.addln("  _commands[i].%s($$);", sig.getName());

        builder.end();

        cf.addMethod(Modifier.PUBLIC, sig, 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("%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.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.