Author: nbelaevski
Date: 2010-02-07 18:14:28 -0500 (Sun, 07 Feb 2010)
New Revision: 16423
Added:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/EqualityTestTreeNode.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/EqualsCheckingMethodBodyStatement.java
root/cdk/trunk/plugins/generator/src/main/resources/META-INF/templates/java/equals-check-method.ftl
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELNodeConstants.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELParserUtils.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELVisitor.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/parser/el/test/ELParserTest.java
Log:
https://jira.jboss.org/jira/browse/RF-7732
Fixed incorrect handling of equality/inequality for non-primitive types
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELNodeConstants.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELNodeConstants.java 2010-02-05
18:42:09 UTC (rev 16422)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELNodeConstants.java 2010-02-07
23:14:28 UTC (rev 16423)
@@ -47,6 +47,7 @@
public static final String COLON = " : ";
public static final String COMMA = ",";
+ public static final String IS_EQUAL_FUNCTION = "isEqual";
public static final String CONVERT_TO_STRING_FUNCTION = "convertToString";
public static final String DOT = ".";
public static final String DOUBLE_VALUE_OF_FUNCTION = "Double.valueOf";
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELParserUtils.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELParserUtils.java 2010-02-05
18:42:09 UTC (rev 16422)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELParserUtils.java 2010-02-07
23:14:28 UTC (rev 16423)
@@ -89,6 +89,7 @@
import org.richfaces.cdk.parser.el.node.BinaryArithmeticOperationTreeNode;
import org.richfaces.cdk.parser.el.node.BinaryBooleanOperationTreeNode;
import org.richfaces.cdk.parser.el.node.ConstantValueTreeNode;
+import org.richfaces.cdk.parser.el.node.EqualityTestTreeNode;
import org.richfaces.cdk.parser.el.node.ITreeNode;
import org.richfaces.cdk.parser.el.types.TypesFactory;
@@ -206,7 +207,7 @@
} else if (child instanceof AstAnd) {
treeNode = new BinaryBooleanOperationTreeNode(child,
ELNodeConstants.AND_OPERATOR);
} else if (child instanceof AstEqual) {
- treeNode = new BinaryBooleanOperationTreeNode(child,
ELNodeConstants.EQUALITY_OPERATOR);
+ treeNode = new EqualityTestTreeNode(child);
} else if (child instanceof AstGreaterThan) {
treeNode = new BinaryBooleanOperationTreeNode(child,
ELNodeConstants.GREATER_THEN_OPERATOR);
} else if (child instanceof AstGreaterThanEqual) {
@@ -216,7 +217,7 @@
} else if (child instanceof AstLessThanEqual) {
treeNode = new BinaryBooleanOperationTreeNode(child,
ELNodeConstants.LESS_THEN_OR_EQUALITY_OPERATOR);
} else if (child instanceof AstNotEqual) {
- treeNode = new BinaryBooleanOperationTreeNode(child,
ELNodeConstants.INEQUALITY_OPERATOR);
+ treeNode = new EqualityTestTreeNode(child, true);
} else if (child instanceof AstOr) {
treeNode = new BinaryBooleanOperationTreeNode(child,
ELNodeConstants.OR_OPERATOR);
} else if (child instanceof AstDiv) {
@@ -239,8 +240,6 @@
treeNode = new AstMethodSuffixTreeNode(child);
} else if (child instanceof AstPropertySuffix) {
treeNode = new AstPropertySuffixTreeNode(child);
- } else if (child instanceof AstBracketSuffix) {
- treeNode = new AstBracketSuffixTreeNode(child);
} else {
throw new ParsingException("Node " +
child.getClass().getSimpleName() + "[" + child.getImage()
+ "] is not recognized;");
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELVisitor.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELVisitor.java 2010-02-05
18:42:09 UTC (rev 16422)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELVisitor.java 2010-02-07
23:14:28 UTC (rev 16423)
@@ -37,7 +37,8 @@
boolean useConversionToString;
boolean useEmptinessCheck;
-
+ boolean useEqualsCheck;
+
private String parsedExpression = null;
private Type variableType = null;
@@ -56,6 +57,14 @@
public void setUseEmptinessCheck(boolean useCheckForEmpty) {
this.useEmptinessCheck = useCheckForEmpty;
}
+
+ public boolean isUseEqualsCheck() {
+ return useEqualsCheck;
+ }
+
+ public void setUseEqualsCheck(boolean useEqualsCheck) {
+ this.useEqualsCheck = useEqualsCheck;
+ }
public Type getVariableType() {
return variableType;
@@ -116,6 +125,7 @@
parsedExpression = null;
useConversionToString = false;
useEmptinessCheck = false;
+ useEqualsCheck = false;
variableType = null;
}
}
Added:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/EqualityTestTreeNode.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/EqualityTestTreeNode.java
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/EqualityTestTreeNode.java 2010-02-07
23:14:28 UTC (rev 16423)
@@ -0,0 +1,101 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.cdk.parser.el.node;
+
+import java.util.Map;
+
+import org.jboss.el.parser.Node;
+import org.richfaces.cdk.parser.el.ELNodeConstants;
+import org.richfaces.cdk.parser.el.ELVisitor;
+import org.richfaces.cdk.parser.el.ParsingException;
+import org.richfaces.cdk.parser.el.Type;
+import org.richfaces.cdk.parser.el.types.TypesFactory;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class EqualityTestTreeNode extends AbstractTreeNode {
+
+ private boolean negateValue = false;
+
+ /**
+ * @param node
+ */
+ public EqualityTestTreeNode(Node node) {
+ super(node);
+ }
+
+ public EqualityTestTreeNode(Node node, boolean negateValue) {
+ super(node);
+
+ this.negateValue = negateValue;
+ }
+
+ /* (non-Javadoc)
+ * @see
org.richfaces.cdk.parser.el.node.AbstractTreeNode#visit(java.lang.StringBuilder,
java.util.Map, org.richfaces.cdk.parser.el.ELVisitor)
+ */
+ @Override
+ public void visit(StringBuilder sb, Map<String, Type> context, ELVisitor
visitor) throws ParsingException {
+ String firstChildOutput = getChildOutput(0, context, visitor);
+ Type firstChildType = visitor.getVariableType();
+ String secondChildOutput = getChildOutput(1, context, visitor);
+ Type secondChildType = visitor.getVariableType();
+
+ if (!firstChildType.getRawType().isPrimitive() ||
+ !secondChildType.getRawType().isPrimitive()) {
+
+ if (negateValue) {
+ sb.append(ELNodeConstants.EXCLAMATION_MARK);
+ } else {
+ //do nothing
+ }
+
+ sb.append(ELNodeConstants.IS_EQUAL_FUNCTION);
+
+ sb.append(ELNodeConstants.LEFT_BRACKET);
+ sb.append(firstChildOutput);
+ sb.append(ELNodeConstants.COMMA);
+ sb.append(secondChildOutput);
+ sb.append(ELNodeConstants.RIGHT_BRACKET);
+
+ visitor.setUseEqualsCheck(true);
+ } else {
+ sb.append(ELNodeConstants.LEFT_BRACKET);
+
+ sb.append(firstChildOutput);
+
+ if (negateValue) {
+ sb.append(ELNodeConstants.INEQUALITY_OPERATOR);
+ } else {
+ sb.append(ELNodeConstants.EQUALITY_OPERATOR);
+ }
+
+ sb.append(secondChildOutput);
+
+ sb.append(ELNodeConstants.RIGHT_BRACKET);
+ }
+
+ visitor.setVariableType(TypesFactory.getType(Boolean.TYPE));
+ }
+
+}
Added:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/EqualsCheckingMethodBodyStatement.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/EqualsCheckingMethodBodyStatement.java
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/EqualsCheckingMethodBodyStatement.java 2010-02-07
23:14:28 UTC (rev 16423)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.cdk.templatecompiler;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class EqualsCheckingMethodBodyStatement extends
AbstractTemplateMethodBodyStatement {
+
+ public EqualsCheckingMethodBodyStatement() {
+ super("equals-check-method");
+ }
+}
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java 2010-02-05
18:42:09 UTC (rev 16422)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java 2010-02-07
23:14:28 UTC (rev 16423)
@@ -136,7 +136,8 @@
private ClassLoader classLoader;
private boolean isAddedMethodForConversionToString;
private boolean isAddedMethodForCheckingEmptiness;
-
+ private boolean isAddedMethodForCheckingEquals;
+
private Type lastCompiledExpressionType;
private int passThroughCounter;
private Map<String, Property> attributes;
@@ -204,6 +205,24 @@
}
}
+ private void addMethodForCheckingEquals() {
+ if (!isAddedMethodForCheckingEquals) {
+ isAddedMethodForCheckingEquals = true;
+
+ JavaMethod checkingMethod = new JavaMethod("isEqual",
boolean.class, new Argument("o1", Object.class),
+ new Argument("o2", Object.class));
+
+ checkingMethod.addModifier(JavaModifier.PRIVATE);
+ checkingMethod.addModifier(JavaModifier.FINAL);
+
+ MethodBody checkingMethodBody = new MethodBody(checkingMethod);
+ checkingMethod.setMethodBody(checkingMethodBody);
+ checkingMethodBody.addStatement(new EqualsCheckingMethodBodyStatement());
+
+ generatedClass.addMethod(checkingMethod);
+ }
+ }
+
private String compileEl(String expression, Class<?> type) {
try {
ELVisitor elVisitor = new ELVisitor();
@@ -220,6 +239,10 @@
addMethodForCheckingEmptiness();
}
+ if (elVisitor.isUseEqualsCheck()) {
+ addMethodForCheckingEquals();
+ }
+
return parsedExpression + "/* " + expression.trim() + "
*/";
} catch (ParsingException e) {
// TODO Auto-generated catch block
Added:
root/cdk/trunk/plugins/generator/src/main/resources/META-INF/templates/java/equals-check-method.ftl
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/resources/META-INF/templates/java/equals-check-method.ftl
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/main/resources/META-INF/templates/java/equals-check-method.ftl 2010-02-07
23:14:28 UTC (rev 16423)
@@ -0,0 +1,6 @@
+if (o1 != null) {
+ return o1.equals(o2);
+} else {
+ //o1 == null
+ return o2 == null;
+}
\ No newline at end of file
Modified:
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/parser/el/test/ELParserTest.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/parser/el/test/ELParserTest.java 2010-02-05
18:42:09 UTC (rev 16422)
+++
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/parser/el/test/ELParserTest.java 2010-02-07
23:14:28 UTC (rev 16423)
@@ -23,6 +23,7 @@
package org.richfaces.cdk.parser.el.test;
+import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -108,6 +109,16 @@
parseExpression("#{2 != 3}");
assertEquals("(2 != 3)", visitor.getParsedExpression());
assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+
+ parseExpression("#{action != clientId}");
+ assertEquals("!isEqual(action,clientId)",
visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertTrue(visitor.isUseEqualsCheck());
+
+ parseExpression("#{action ne clientId}");
+ assertEquals("!isEqual(action,clientId)",
visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertTrue(visitor.isUseEqualsCheck());
}
@Test
@@ -188,6 +199,16 @@
parseExpression("#{3 == 2}");
assertEquals("(3 == 2)", visitor.getParsedExpression());
assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+
+ parseExpression("#{action == clientId}");
+ assertEquals("isEqual(action,clientId)",
visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertTrue(visitor.isUseEqualsCheck());
+
+ parseExpression("#{action eq clientId}");
+ assertEquals("isEqual(action,clientId)",
visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertTrue(visitor.isUseEqualsCheck());
}
@Test