# HG changeset patch # User Bernhard Urban # Date 1369232892 -7200 # Node ID ad50389f1da4c89632fc6729352824ebd8333ce5 # Parent 1bd67c5bdab8539ec7815e2421bc5a03b8bd6801 VerifyUsageWithEquals: refactoring diff -r 1bd67c5bdab8 -r ad50389f1da4 graal/com.oracle.graal.phases/src/com/oracle/graal/phases/verify/VerifyUsageWithEquals.java --- 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;