changeset 9789:ad50389f1da4

VerifyUsageWithEquals: refactoring
author Bernhard Urban <bernhard.urban@jku.at>
date Wed, 22 May 2013 16:28:12 +0200
parents 1bd67c5bdab8
children 6b515c453646
files graal/com.oracle.graal.phases/src/com/oracle/graal/phases/verify/VerifyUsageWithEquals.java
diffstat 1 files changed, 18 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.phases/src/com/oracle/graal/phases/verify/VerifyUsageWithEquals.java	Tue May 21 18:03:24 2013 +0200
+++ b/graal/com.oracle.graal.phases/src/com/oracle/graal/phases/verify/VerifyUsageWithEquals.java	Wed May 22 16:28:12 2013 +0200
@@ -38,7 +38,7 @@
         this.klass = klass;
     }
 
-    private boolean checkType(ValueNode node) {
+    private boolean isAssignableType(ValueNode node) {
         if (node.stamp() instanceof ObjectStamp) {
             ResolvedJavaType valueType = runtime.lookupJavaType(klass);
             ResolvedJavaType nodeType = node.objectStamp().type();
@@ -50,13 +50,26 @@
         return false;
     }
 
+    private static boolean isNullConstant(ValueNode node) {
+        return node.isConstant() && node.asConstant().isNull();
+    }
+
+    private boolean checkUsage(ValueNode x, ValueNode y) {
+        return isAssignableType(x) && !isNullConstant(y);
+    }
+
+    private static boolean isEqualsMethod(StructuredGraph graph) {
+        Signature signature = graph.method().getSignature();
+        return graph.method().getName().equals("equals") && signature.getParameterCount(false) == 1 && signature.getParameterKind(0).equals(Kind.Object);
+    }
+
     @Override
     protected boolean verify(StructuredGraph graph) {
         for (ObjectEqualsNode cn : graph.getNodes().filter(ObjectEqualsNode.class)) {
-            Signature signature = graph.method().getSignature();
-            if (!(graph.method().getName().equals("equals") && signature.getParameterCount(false) == 1 && signature.getParameterKind(0).equals(Kind.Object))) {
-                assert !((checkType(cn.x()) && !(cn.y() instanceof ConstantNode)) || (checkType(cn.y()) && !(cn.x() instanceof ConstantNode))) : "VerifyUsage of " + klass.getName() + ": " + cn.x() +
-                                " or " + cn.y() + " in " + graph.method() + " uses object identity. Should use equals() instead.";
+            if (!isEqualsMethod(graph)) {
+                // bail out if we compare an object of type klass with == or != (except null checks)
+                assert !(checkUsage(cn.x(), cn.y()) && checkUsage(cn.y(), cn.x())) : "VerifyUsage of " + klass.getName() + ": " + cn.x() + " or " + cn.y() + " in " + graph.method() +
+                                " uses object identity. Should use equals() instead.";
             }
         }
         return true;