Search in sources :

Example 1 with FieldConduit

use of org.apache.tapestry5.plastic.FieldConduit in project tapestry-5 by apache.

the class BlockInjectionProvider method createConduit.

private FieldConduit<Object> createConduit(PlasticField field, final String blockId) {
    final String className = field.getPlasticClass().getClassName();
    final String fieldName = field.getName();
    return new ReadOnlyComponentFieldConduit(className, fieldName) {

        public Object get(Object instance, InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            return resources.getBlock(blockId);
        }
    };
}
Also used : ReadOnlyComponentFieldConduit(org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit) InstanceContext(org.apache.tapestry5.plastic.InstanceContext) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 2 with FieldConduit

use of org.apache.tapestry5.plastic.FieldConduit in project tapestry-5 by apache.

the class PlasticFieldImpl method replaceFieldReadAccess.

private void replaceFieldReadAccess(String conduitFieldName) {
    ensureNotPublic();
    boolean writeBehindEnabled = isWriteBehindEnabled();
    String getAccessName = plasticClass.makeUnique(plasticClass.methodNames, "conduit_get_" + node.name);
    getAccess = new MethodNode(accessForMethod(), getAccessName, "()" + node.desc, null, null);
    InstructionBuilder builder = plasticClass.newBuilder(getAccess);
    // Get the correct FieldConduit object on the stack
    pushFieldConduitOntoStack(conduitFieldName, builder);
    builder.loadThis();
    // Now push the instance context on the stack
    plasticClass.pushInstanceContextFieldOntoStack(builder);
    builder.invoke(FieldConduit.class, Object.class, "get", Object.class, InstanceContext.class).castOrUnbox(typeName);
    if (writeBehindEnabled) {
        if (isWide()) {
            // Dupe this under the wide value, then pop the wide value
            builder.dupeWide().loadThis().dupe(2).pop();
        } else {
            builder.dupe().loadThis().swap();
        }
        // At which point the stack is the result value, this, the result value
        builder.putField(plasticClass.className, node.name, typeName);
    // And now it is just the result value
    }
    builder.returnResult();
    plasticClass.addMethod(getAccess);
    plasticClass.redirectFieldRead(node.name, isPrivate(), getAccess);
}
Also used : MethodNode(org.apache.tapestry5.internal.plastic.asm.tree.MethodNode)

Example 3 with FieldConduit

use of org.apache.tapestry5.plastic.FieldConduit in project tapestry-5 by apache.

the class MixinWorker method createMixinFieldProvider.

private ComputedValue<FieldConduit<Object>> createMixinFieldProvider(final String fieldName, final String mixinClassName) {
    return new ComputedValue<FieldConduit<Object>>() {

        public FieldConduit get(InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            final InternalComponentResources icr = (InternalComponentResources) resources;
            return new ReadOnlyComponentFieldConduit(resources, fieldName) {

                public Object get(Object instance, InstanceContext context) {
                    return icr.getMixinByClassName(mixinClassName);
                }
            };
        }
    };
}
Also used : InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 4 with FieldConduit

use of org.apache.tapestry5.plastic.FieldConduit in project tapestry-5 by apache.

the class PageActivationContextWorker method transformFields.

private void transformFields(TransformationSupport support, List<PlasticField> fields) {
    List<PlasticField> sortedFields = CollectionFactory.newList(fields);
    Collections.sort(sortedFields, INDEX_COMPARATOR);
    validateSortedFields(sortedFields);
    PlasticField firstField = sortedFields.get(0);
    PageActivationContext firstAnnotation = firstField.getAnnotation(PageActivationContext.class);
    // these arrays reduce memory usage and allow the PlasticField instances to be garbage collected
    FieldHandle[] handles = new FieldHandle[sortedFields.size()];
    String[] typeNames = new String[sortedFields.size()];
    int i = 0;
    for (PlasticField field : sortedFields) {
        handles[i] = field.getHandle();
        typeNames[i] = field.getTypeName();
        ++i;
    }
    if (firstAnnotation.activate()) {
        support.addEventHandler(EventConstants.ACTIVATE, 1, "PageActivationContextWorker activate event handler", createActivationHandler(handles, typeNames));
    }
    if (firstAnnotation.passivate()) {
        support.addEventHandler(EventConstants.PASSIVATE, 0, "PageActivationContextWorker passivate event handler", createPassivateHandler(handles));
    }
// We don't claim the field, and other workers may even replace it with a FieldConduit.
}
Also used : PageActivationContext(org.apache.tapestry5.annotations.PageActivationContext) PlasticField(org.apache.tapestry5.plastic.PlasticField) FieldHandle(org.apache.tapestry5.plastic.FieldHandle)

Example 5 with FieldConduit

use of org.apache.tapestry5.plastic.FieldConduit in project tapestry-5 by apache.

the class BindParameterWorker method convertFieldIntoContainerBoundParameter.

private void convertFieldIntoContainerBoundParameter(PlasticField field) {
    BindParameter annotation = field.getAnnotation(BindParameter.class);
    field.claim(annotation);
    final String[] possibleNames = annotation.value();
    final String fieldTypeName = field.getTypeName();
    final String fieldName = field.getName();
    ComputedValue<FieldConduit<Object>> computedConduit = new ComputedValue<FieldConduit<Object>>() {

        public FieldConduit<Object> get(InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            try {
                return createConduit(resources, fieldTypeName, fieldName, possibleNames);
            } catch (Exception ex) {
                throw new TapestryException(String.format("Failure binding parameter field '%s' of mixin %s (type %s): %s", fieldName, resources.getCompleteId(), resources.getComponentModel().getComponentClassName(), ExceptionUtils.toMessage(ex)), ex);
            }
        }
    };
    field.setComputedConduit(computedConduit);
}
Also used : BindParameter(org.apache.tapestry5.annotations.BindParameter) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) ComponentResources(org.apache.tapestry5.ComponentResources)

Aggregations

InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)6 ComponentResources (org.apache.tapestry5.ComponentResources)5 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)3 InstanceContext (org.apache.tapestry5.plastic.InstanceContext)3 PlasticField (org.apache.tapestry5.plastic.PlasticField)2 Binding (org.apache.tapestry5.Binding)1 BindParameter (org.apache.tapestry5.annotations.BindParameter)1 Environmental (org.apache.tapestry5.annotations.Environmental)1 Id (org.apache.tapestry5.annotations.Id)1 InjectComponent (org.apache.tapestry5.annotations.InjectComponent)1 InjectContainer (org.apache.tapestry5.annotations.InjectContainer)1 PageActivationContext (org.apache.tapestry5.annotations.PageActivationContext)1 Parameter (org.apache.tapestry5.annotations.Parameter)1 Persist (org.apache.tapestry5.annotations.Persist)1 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)1 Predicate (org.apache.tapestry5.func.Predicate)1 LiteralBinding (org.apache.tapestry5.internal.bindings.LiteralBinding)1 MethodNode (org.apache.tapestry5.internal.plastic.asm.tree.MethodNode)1 ReadOnlyComponentFieldConduit (org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit)1 ComputedValue (org.apache.tapestry5.plastic.ComputedValue)1