未验证 提交 bc692b40 编写于 作者: brucelwl's avatar brucelwl 提交者: GitHub

[ISSUE #1906] The BooleanConstantExpression might lead to class loading deadlock

[ISSUE #1906] The BooleanConstantExpression might lead to class loading deadlock
上级 1052edcc
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.rocketmq.filter.expression;
/**
* BooleanConstantExpression
*/
public class BooleanConstantExpression extends ConstantExpression implements BooleanExpression {
public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null);
public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE);
public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE);
public BooleanConstantExpression(Object value) {
super(value);
}
public boolean matches(EvaluationContext context) throws Exception {
Object object = evaluate(context);
return object != null && object == Boolean.TRUE;
}
}
...@@ -90,11 +90,11 @@ public abstract class ComparisonExpression extends BinaryExpression implements B ...@@ -90,11 +90,11 @@ public abstract class ComparisonExpression extends BinaryExpression implements B
} }
public static BooleanExpression createIsNull(Expression left) { public static BooleanExpression createIsNull(Expression left) {
return doCreateEqual(left, ConstantExpression.NULL); return doCreateEqual(left, BooleanConstantExpression.NULL);
} }
public static BooleanExpression createIsNotNull(Expression left) { public static BooleanExpression createIsNotNull(Expression left) {
return UnaryExpression.createNOT(doCreateEqual(left, ConstantExpression.NULL)); return UnaryExpression.createNOT(doCreateEqual(left, BooleanConstantExpression.NULL));
} }
public static BooleanExpression createNotEqual(Expression left, Expression right) { public static BooleanExpression createNotEqual(Expression left, Expression right) {
......
...@@ -30,21 +30,6 @@ package org.apache.rocketmq.filter.expression; ...@@ -30,21 +30,6 @@ package org.apache.rocketmq.filter.expression;
*/ */
public class ConstantExpression implements Expression { public class ConstantExpression implements Expression {
static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression {
public BooleanConstantExpression(Object value) {
super(value);
}
public boolean matches(EvaluationContext context) throws Exception {
Object object = evaluate(context);
return object != null && object == Boolean.TRUE;
}
}
public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null);
public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE);
public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE);
private Object value; private Object value;
public ConstantExpression(Object value) { public ConstantExpression(Object value) {
...@@ -60,16 +45,10 @@ public class ConstantExpression implements Expression { ...@@ -60,16 +45,10 @@ public class ConstantExpression implements Expression {
// only support Long.MIN_VALUE ~ Long.MAX_VALUE // only support Long.MIN_VALUE ~ Long.MAX_VALUE
Number value = new Long(text); Number value = new Long(text);
// try {
// value = new Long(text);
// } catch (NumberFormatException e) {
// // The number may be too big to fit in a long.
// value = new BigDecimal(text);
// }
long l = value.longValue(); long l = value.longValue();
if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) { if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
value = Integer.valueOf(value.intValue()); value = value.intValue();
} }
return new ConstantExpression(value); return new ConstantExpression(value);
} }
...@@ -106,7 +85,7 @@ public class ConstantExpression implements Expression { ...@@ -106,7 +85,7 @@ public class ConstantExpression implements Expression {
return "NULL"; return "NULL";
} }
if (value instanceof Boolean) { if (value instanceof Boolean) {
return ((Boolean) value).booleanValue() ? "TRUE" : "FALSE"; return (Boolean) value ? "TRUE" : "FALSE";
} }
if (value instanceof String) { if (value instanceof String) {
return encodeString((String) value); return encodeString((String) value);
...@@ -138,17 +117,19 @@ public class ConstantExpression implements Expression { ...@@ -138,17 +117,19 @@ public class ConstantExpression implements Expression {
* it was provided in a selector. * it was provided in a selector.
*/ */
public static String encodeString(String s) { public static String encodeString(String s) {
StringBuffer b = new StringBuffer();
b.append('\''); StringBuilder builder = new StringBuilder();
builder.append('\'');
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i); char c = s.charAt(i);
if (c == '\'') { if (c == '\'') {
b.append(c); builder.append(c);
} }
b.append(c); builder.append(c);
} }
b.append('\''); builder.append('\'');
return b.toString(); return builder.toString();
} }
} }
...@@ -20,6 +20,7 @@ package org.apache.rocketmq.filter.parser; ...@@ -20,6 +20,7 @@ package org.apache.rocketmq.filter.parser;
import com.google.common.cache.Cache; import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import org.apache.rocketmq.filter.expression.BooleanConstantExpression;
import org.apache.rocketmq.filter.expression.BooleanExpression; import org.apache.rocketmq.filter.expression.BooleanExpression;
import org.apache.rocketmq.filter.expression.ComparisonExpression; import org.apache.rocketmq.filter.expression.ComparisonExpression;
import org.apache.rocketmq.filter.expression.ConstantExpression; import org.apache.rocketmq.filter.expression.ConstantExpression;
...@@ -437,15 +438,15 @@ public class SelectorParser implements SelectorParserConstants { ...@@ -437,15 +438,15 @@ public class SelectorParser implements SelectorParserConstants {
break; break;
case TRUE: case TRUE:
jj_consume_token(TRUE); jj_consume_token(TRUE);
left = ConstantExpression.TRUE; left = BooleanConstantExpression.TRUE;
break; break;
case FALSE: case FALSE:
jj_consume_token(FALSE); jj_consume_token(FALSE);
left = ConstantExpression.FALSE; left = BooleanConstantExpression.FALSE;
break; break;
case NULL: case NULL:
jj_consume_token(NULL); jj_consume_token(NULL);
left = ConstantExpression.NULL; left = BooleanConstantExpression.NULL;
break; break;
default: default:
jjLa1[12] = jjGen; jjLa1[12] = jjGen;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册