Search in sources :

Example 1 with Condition

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

the class InstructionBuilderImpl method doWhile.

@Override
public InstructionBuilder doWhile(Condition condition, final WhileCallback callback) {
    check();
    assert condition != null;
    assert callback != null;
    Label doCheck = state.newLabel();
    Label exitLoop = new Label();
    callback.buildTest(this);
    v.visitJumpInsn(conditionToOpcode.get(condition), exitLoop);
    callback.buildBody(this);
    v.visitJumpInsn(GOTO, doCheck);
    v.visitLabel(exitLoop);
    return this;
}
Also used : Label(org.apache.tapestry5.internal.plastic.asm.Label)

Example 2 with Condition

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

the class ModuleImpl method findOrCreate.

/**
 * Locates the service proxy for a particular service (from the service definition).
 *
 * @param def              defines the service
 * @param eagerLoadProxies collection into which proxies for eager loaded services are added (or null)
 * @return the service proxy
 */
private Object findOrCreate(final ServiceDef3 def, final Collection<EagerLoadServiceProxy> eagerLoadProxies) {
    final String key = def.getServiceId();
    final Invokable create = new Invokable() {

        @Override
        public Object invoke() {
            // In a race condition, two threads may try to create the same service simulatenously.
            // The second will block until after the first creates the service.
            Object result = services.get(key);
            if (result == null) {
                result = create(def, eagerLoadProxies);
                services.put(key, result);
            }
            return result;
        }
    };
    Invokable find = new Invokable() {

        @Override
        public Object invoke() {
            Object result = services.get(key);
            if (result == null)
                result = BARRIER.withWrite(create);
            return result;
        }
    };
    return BARRIER.withRead(find);
}
Also used : Invokable(org.apache.tapestry5.ioc.Invokable)

Example 3 with Condition

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

the class ChainBuilderImpl method implementMethod.

private void implementMethod(PlasticClass plasticClass, final Method method, final PlasticField commandsField) {
    plasticClass.introduceMethod(method).changeImplementation(new InstructionBuilderCallback() {

        @Override
        public void doBuild(InstructionBuilder builder) {
            builder.loadThis().getField(commandsField).iterateArray(new InstructionBuilderCallback() {

                @Override
                public void doBuild(InstructionBuilder builder) {
                    // The command is on the stack; add the elements and invoke the method.
                    builder.loadArguments().invoke(method);
                    Class returnType = method.getReturnType();
                    if (returnType == void.class)
                        return;
                    final boolean wide = returnType == long.class || returnType == double.class;
                    if (wide)
                        builder.dupeWide();
                    else
                        builder.dupe();
                    if (returnType == float.class) {
                        builder.loadConstant(0f).compareSpecial("float");
                    }
                    if (returnType == long.class) {
                        builder.loadConstant(0l).compareSpecial("long");
                    }
                    if (returnType == double.class) {
                        builder.loadConstant(0d).compareSpecial("double");
                    }
                    Condition condition = returnType.isPrimitive() ? Condition.NON_ZERO : Condition.NON_NULL;
                    builder.when(condition, new WhenCallback() {

                        @Override
                        public void ifTrue(InstructionBuilder builder) {
                            builder.returnResult();
                        }

                        @Override
                        public void ifFalse(InstructionBuilder builder) {
                            if (wide)
                                builder.popWide();
                            else
                                builder.pop();
                        }
                    });
                }
            });
            builder.returnDefaultValue();
        }
    });
}
Also used : Condition(org.apache.tapestry5.plastic.Condition) WhenCallback(org.apache.tapestry5.plastic.WhenCallback) InstructionBuilder(org.apache.tapestry5.plastic.InstructionBuilder) PlasticClass(org.apache.tapestry5.plastic.PlasticClass) InstructionBuilderCallback(org.apache.tapestry5.plastic.InstructionBuilderCallback)

Example 4 with Condition

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

the class PropertyConduitSourceImpl method build.

/**
 * Builds a subclass of {@link PropertyConduitDelegate} that implements the
 * get() and set() methods and overrides the
 * constructor. In a worst-case race condition, we may build two (or more)
 * conduits for the same
 * rootClass/expression, and it will get sorted out when the conduit is
 * stored into the cache.
 *
 * @param rootClass
 *         class of root object for expression evaluation
 * @param expression
 *         expression to be evaluated
 * @return the conduit
 */
private PropertyConduit build(final Class rootClass, String expression) {
    Tree tree = parse(expression);
    try {
        switch(tree.getType()) {
            case TRUE:
                return literalTrue;
            case FALSE:
                return literalFalse;
            case NULL:
                return literalNull;
            case INTEGER:
                return createLiteralConduit(Long.class, new Long(tree.getText()));
            case DECIMAL:
                return createLiteralConduit(Double.class, new Double(tree.getText()));
            case STRING:
                return createLiteralConduit(String.class, tree.getText());
            case RANGEOP:
                Tree fromNode = tree.getChild(0);
                Tree toNode = tree.getChild(1);
                if (fromNode.getType() != INTEGER || toNode.getType() != INTEGER)
                    break;
                int from = Integer.parseInt(fromNode.getText());
                int to = Integer.parseInt(toNode.getText());
                IntegerRange ir = new IntegerRange(from, to);
                return createLiteralConduit(IntegerRange.class, ir);
            case THIS:
                return createLiteralThisPropertyConduit(rootClass);
            default:
                break;
        }
        return proxyFactory.createProxy(InternalPropertyConduit.class, new PropertyConduitBuilder(rootClass, expression, tree)).newInstance();
    } catch (Exception ex) {
        throw new PropertyExpressionException(String.format("Exception generating conduit for expression '%s': %s", expression, ExceptionUtils.toMessage(ex)), expression, ex);
    }
}
Also used : InternalPropertyConduit(org.apache.tapestry5.beanmodel.internal.InternalPropertyConduit) IntegerRange(org.apache.tapestry5.commons.util.IntegerRange) Tree(org.antlr.runtime.tree.Tree) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) IOException(java.io.IOException)

Example 5 with Condition

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

the class AbstractServiceCreator method findParameterizedTypeFromGenericType.

/**
 * "Sniffs" a generic type to find the underlying parameterized type. If the Type is a class, then Object.class is
 * returned. Otherwise, the type must be a ParameterizedType. We check to make sure it has the correct number of a
 * actual types (1 for a Collection or List, 2 for a Map). The actual types must be classes (wildcards just aren't
 * supported)
 *
 * @param type      a Class or ParameterizedType to inspect
 * @param typeIndex the index within the ParameterizedType to extract
 * @return the actual type, or Object.class if the input type is not generic, or null if any other pre-condition is
 *         not met
 */
private static Class findParameterizedTypeFromGenericType(Type type, int typeIndex) {
    if (type instanceof Class)
        return Object.class;
    if (!(type instanceof ParameterizedType))
        return null;
    ParameterizedType pt = (ParameterizedType) type;
    Type[] types = pt.getActualTypeArguments();
    Type actualType = types[typeIndex];
    return actualType instanceof Class ? (Class) actualType : null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ConfigurationType(org.apache.tapestry5.ioc.internal.ConfigurationType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Aggregations

Label (org.apache.tapestry5.internal.plastic.asm.Label)2 IOException (java.io.IOException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 Tree (org.antlr.runtime.tree.Tree)1 Asset (org.apache.tapestry5.Asset)1 Block (org.apache.tapestry5.Block)1 ComponentResources (org.apache.tapestry5.ComponentResources)1 MarkupWriter (org.apache.tapestry5.MarkupWriter)1 PropertyOverrides (org.apache.tapestry5.PropertyOverrides)1 Renderable (org.apache.tapestry5.Renderable)1 SelectModel (org.apache.tapestry5.SelectModel)1