Package org.apache.tapestry.ioc.util

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


     */
    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("%s page = %s.get(\"%s\");", Page.class.getName(), cacheFieldName, injectedPageName);

        builder.addln("return (%s) page.getRootElement().getComponent();", fieldType);

        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

        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

        // Except in the root class, don't bother to add a new method unless there's something to
        // 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<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

            TapestryConstants.PROP_BINDING_PREFIX,
            once.watch());
        transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, body);
      }
     
      BodyBuilder b = new BodyBuilder();

      // on cleanup, reset the field values
      b.begin();
           
      if (!TransformUtils.isPrimitive(method.getReturnType()))
        b.addln("%s = null;", fieldName);
      b.addln("%s = false;", calledField);
     
      if (watching)
        b.addln("%s = null;", bindingValueField);
     
      b.end();
      transformation.extendMethod(TransformConstants.POST_RENDER_CLEANUP_SIGNATURE, b.toString());
           
      // prefix the existing method to cache the result
      b.clear();
      b.begin();
     
      // if it has been called and watch is set and the old value is the same as the new value then return
      // get the old value and cache it
      /* NOTE: evaluates the binding twice when checking the new value.
       * this is probably not a problem because in most cases properties
       * that are being watched are not expensive operations. plus, we
       * never guaranteed that it would be called exactly once when
       * watching.
       */
      if (watching) {
        b.addln("if (%s && %s == %s.get()) return %s;",
            calledField, bindingValueField, bindingField, fieldName);
        b.addln("%s = %s.get();", bindingValueField, bindingField);
      }
      else {
        b.addln("if (%s) return %s;", calledField, fieldName);
      }
     
      b.addln("%s = true;", calledField);     
      b.end();     
      transformation.prefixMethod(method, b.toString());
     
      // cache the return value
      b.clear();
      b.begin();
      b.addln("%s = $_;", fieldName);
      b.end();
      transformation.extendExistingMethod(method, b.toString());
    }
  }
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

        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 = ", getJavaClassName(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", 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

        }

        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

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.