OPENJPA-2320: Remove static initializers because recursive Predicate constructor deadlocks

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1430117 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2013-01-08 02:40:07 +00:00
parent 58f24badcc
commit 0ca38be727
2 changed files with 30 additions and 15 deletions

View File

@ -332,7 +332,7 @@ public class CriteriaBuilderImpl implements OpenJPACriteriaBuilder, ExpressionPa
if (x instanceof PredicateImpl) {
PredicateImpl predicate = (PredicateImpl)x;
if (predicate.isEmpty()) {
return predicate.getOperator() == BooleanOperator.AND ? PredicateImpl.TRUE : PredicateImpl.FALSE;
return predicate.getOperator() == BooleanOperator.AND ? PredicateImpl.TRUE() : PredicateImpl.FALSE();
}
}
return new Expressions.Equal(x, true);
@ -403,9 +403,9 @@ public class CriteriaBuilderImpl implements OpenJPACriteriaBuilder, ExpressionPa
public <T> Expression<T> literal(T value) {
if (Boolean.TRUE.equals(value))
return (Expression<T>)PredicateImpl.TRUE;
return (Expression<T>)PredicateImpl.TRUE();
if (Boolean.FALSE.equals(value))
return (Expression<T>)PredicateImpl.FALSE;
return (Expression<T>)PredicateImpl.FALSE();
return new Expressions.Constant<T>(value);
}

View File

@ -45,17 +45,13 @@ import org.apache.openjpa.kernel.exps.Literal;
* @since 2.0.0
*/
abstract class PredicateImpl extends ExpressionImpl<Boolean> implements Predicate {
private static final ExpressionImpl<Integer> ONE = new Expressions.Constant<Integer>(1);
public static final Predicate TRUE = new Expressions.Equal(ONE,ONE);
public static final Predicate FALSE = new Expressions.NotEqual(ONE,ONE);
private static Predicate TRUE;
private static Predicate FALSE;
protected final List<Predicate> _exps = Collections.synchronizedList(new ArrayList<Predicate>());
private final BooleanOperator _op;
private boolean _negated = false;
static {
System.err.println("WARNING: You are using a unofficial version of PredicateImpl.class");
}
/**
* An AND predicate with no arguments.
*/
@ -76,17 +72,20 @@ abstract class PredicateImpl extends ExpressionImpl<Boolean> implements Predicat
*/
protected PredicateImpl(BooleanOperator op, Predicate...restrictions) {
this(op);
if (restrictions != null) {
for (Predicate p : restrictions)
add(p);
}
if (restrictions == null || restrictions.length == 0) return;
for (Predicate p : restrictions) {
add(p);
}
}
/**
* Adds the given predicate expression.
*/
public PredicateImpl add(Expression<Boolean> s) {
_exps.add((Predicate)s); // all boolean expressions are Predicate
synchronized (_exps) {
_exps.add((Predicate)s); // all boolean expressions are Predicate
}
return this;
}
@ -130,6 +129,22 @@ abstract class PredicateImpl extends ExpressionImpl<Boolean> implements Predicat
return this;
}
public static Predicate TRUE() {
if (TRUE == null) {
ExpressionImpl<Integer> ONE = new Expressions.Constant<Integer>(1);
TRUE = new Expressions.Equal(ONE, ONE);
}
return TRUE;
}
public static Predicate FALSE() {
if (FALSE == null) {
ExpressionImpl<Integer> ONE = new Expressions.Constant<Integer>(1);
FALSE = new Expressions.NotEqual(ONE, ONE);
}
return FALSE;
}
@Override
org.apache.openjpa.kernel.exps.Value toValue(ExpressionFactory factory, CriteriaQueryImpl<?> q) {
if (_exps.isEmpty()) {
@ -141,7 +156,7 @@ abstract class PredicateImpl extends ExpressionImpl<Boolean> implements Predicat
@Override
org.apache.openjpa.kernel.exps.Expression toKernelExpression(ExpressionFactory factory, CriteriaQueryImpl<?> q) {
if (_exps.isEmpty()) {
Predicate nil = _op == BooleanOperator.AND ? TRUE : FALSE;
Predicate nil = _op == BooleanOperator.AND ? TRUE() : FALSE();
return ((PredicateImpl)nil).toKernelExpression(factory, q);
}
if (_exps.size() == 1) {