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;
}
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);
}
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();
}
});
}
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);
}
}
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;
}
Aggregations