From 824b6963bd54b0b11def75e5fa65086281cf462e Mon Sep 17 00:00:00 2001 From: Christopher Tate Date: Wed, 22 Oct 2008 20:23:11 -0700 Subject: [PATCH] Use a typed comparison of floating point field values rather than just a string comparison. This addresses toolchain issues; see http://code.google.com/p/android/issues/detail?id=994 --- .../src/com/android/apicheck/FieldInfo.java | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/tools/apicheck/src/com/android/apicheck/FieldInfo.java b/tools/apicheck/src/com/android/apicheck/FieldInfo.java index 9b467af206..a962af51fb 100644 --- a/tools/apicheck/src/com/android/apicheck/FieldInfo.java +++ b/tools/apicheck/src/com/android/apicheck/FieldInfo.java @@ -66,6 +66,40 @@ public class FieldInfo { return parentQName + name(); } + // Check the declared value with a typed comparison, not a string comparison, + // to accommodate toolchains with different fp -> string conversions. + public boolean valueEquals(FieldInfo other) { + // Type mismatch means nonequal, as does a null/non-null mismatch + if (!mType.equals(other.mType) + || ((mValue == null) != (other.mValue == null))) { + return false; + } + + // Null values are considered equal + if (mValue == null) { + return true; + } + + // Floating point gets an implementation-type comparison; all others just use the string + // If float/double parse fails, fall back to string comparison -- it means that it's a + // canonical droiddoc-generated constant expression that represents a NaN. + try { + if (mType.equals("float")) { + float val = Float.parseFloat(mValue); + float otherVal = Float.parseFloat(other.mValue); + return (val == otherVal); + } else if (mType.equals("double")) { + double val = Double.parseDouble(mValue); + double otherVal = Double.parseDouble(other.mValue); + return (val == otherVal); + } + } catch (NumberFormatException e) { + // fall through + } + + return mValue.equals(other.mValue); + } + public boolean isConsistent(FieldInfo fInfo) { fInfo.mExistsInBoth = true; mExistsInBoth = true; @@ -75,8 +109,8 @@ public class FieldInfo { "Field " + fInfo.qualifiedName() + " has changed type"); consistent = false; } - if ((mValue != null && !mValue.equals(fInfo.mValue)) || - (mValue == null && fInfo.mValue != null)) { + + if (!this.valueEquals(fInfo)) { Errors.error(Errors.CHANGED_VALUE, fInfo.position(), "Field " + fInfo.qualifiedName() + " has changed value from " + mValue + " to " + fInfo.mValue);