Bug 1210585 - Use equals to compare Class instances; r=snorp
authorJim Chen <nchen@mozilla.com>
Thu, 22 Oct 2015 17:45:45 -0400
changeset 269116 f1d6e64f8ae7584ef5e8abc20ea6b2537eab03fb
parent 269115 6502eda903ea9389cabea8d645e76e1c52be3b5c
child 269117 e7d74a03c8447b9fbe5c7d532368cbb5d5d9d659
push id29572
push usercbook@mozilla.com
push dateFri, 23 Oct 2015 09:34:20 +0000
treeherdermozilla-central@0625c68c0abc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1210585
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1210585 - Use equals to compare Class instances; r=snorp The code generator uses == and != to compare two instances of Class, but it really should be using equals because two distinct instances of Class can refer to the same class type.
build/annotationProcessors/CodeGenerator.java
build/annotationProcessors/utils/Utils.java
--- a/build/annotationProcessors/CodeGenerator.java
+++ b/build/annotationProcessors/CodeGenerator.java
@@ -72,17 +72,17 @@ public class CodeGenerator {
      *
      * Return null if the given type does not match any class searched.
      */
     private String getMatchingClassType(final Class<?> type) {
         Class<?> cls = this.cls;
         String clsName = this.clsName;
 
         while (cls != null) {
-            if (type == cls) {
+            if (type.equals(cls)) {
                 return clsName;
             }
             cls = cls.getDeclaringClass();
             clsName = clsName.substring(0, Math.max(0, clsName.lastIndexOf("::")));
         }
         return null;
     }
 
@@ -176,17 +176,17 @@ public class CodeGenerator {
         for (Class<?> argType : argTypes) {
             proto.append(getNativeParameterType(argType, info));
             if (includeArgName) {
                 proto.append(" a").append(argIndex++);
             }
             proto.append(", ");
         }
 
-        if (info.catchException && returnType != void.class) {
+        if (info.catchException && !returnType.equals(void.class)) {
             proto.append(getNativeReturnType(returnType, info)).append('*');
             if (includeArgName) {
                 proto.append(" a").append(argIndex++);
             }
             proto.append(", ");
         }
 
         if (proto.substring(proto.length() - 2).equals(", ")) {
@@ -234,17 +234,17 @@ public class CodeGenerator {
                                   /* isConst */ !isStatic));
         def.append("\n{\n");
 
 
         // Generate code to handle the return value, if needed.
         // We initialize rv to NS_OK instead of NS_ERROR_* because loading NS_OK (0) uses
         // fewer instructions. We are guaranteed to set rv to the correct value later.
 
-        if (info.catchException && returnType == void.class) {
+        if (info.catchException && returnType.equals(void.class)) {
             def.append(
                     "    nsresult rv = NS_OK;\n" +
                     "    ");
 
         } else if (info.catchException) {
             // Non-void return type
             final String resultArg = "a" + argTypes.length;
             def.append(
@@ -345,24 +345,24 @@ public class CodeGenerator {
                 "        mozilla::jni::MakeNativeMethod<" + traits + ">(\n" +
                 "                mozilla::jni::NativeStub<" + traits + ", Impl>\n" +
                 "                ::template Wrap<&Impl::" + info.wrapperName + ">)");
     }
 
     private String getLiteral(Object val, AnnotationInfo info) {
         final Class<?> type = val.getClass();
 
-        if (type == char.class || type == Character.class) {
+        if (type.equals(char.class) || type.equals(Character.class)) {
             final char c = (char) val;
             if (c >= 0x20 && c < 0x7F) {
                 return "'" + c + '\'';
             }
             return "u'\\u" + Integer.toHexString(0x10000 | (int) c).substring(1) + '\'';
 
-        } else if (type == CharSequence.class || type == String.class) {
+        } else if (type.equals(CharSequence.class) || type.equals(String.class)) {
             final CharSequence str = (CharSequence) val;
             final StringBuilder out = new StringBuilder(info.narrowChars ? "u8\"" : "u\"");
             for (int i = 0; i < str.length(); i++) {
                 final char c = str.charAt(i);
                 if (c >= 0x20 && c < 0x7F) {
                     out.append(c);
                 } else {
                     out.append("\\u").append(Integer.toHexString(0x10000 | (int) c).substring(1));
@@ -384,34 +384,34 @@ public class CodeGenerator {
         // It just gets in the way and stops our code from compiling.
         if (field.isSynthetic() || field.getName().equals("$VALUES")) {
             return;
         }
 
         final boolean isStatic = Utils.isStatic(field);
         final boolean isFinal = Utils.isFinal(field);
 
-        if (isStatic && isFinal && (type.isPrimitive() || type == String.class)) {
+        if (isStatic && isFinal && (type.isPrimitive() || type.equals(String.class))) {
             Object val = null;
             try {
                 field.setAccessible(true);
                 val = field.get(null);
             } catch (final IllegalAccessException e) {
             }
 
             if (val != null && type.isPrimitive()) {
                 // For static final primitive fields, we can use a "static const" declaration.
                 header.append(
                     "public:\n" +
                     "    static const " + Utils.getNativeReturnType(type, info) +
                             ' ' + info.wrapperName + " = " + getLiteral(val, info) + ";\n" +
                     "\n");
                 return;
 
-            } else if (val != null && type == String.class) {
+            } else if (val != null && type.equals(String.class)) {
                 final String nativeType = info.narrowChars ? "char" : "char16_t";
 
                 header.append(
                     "public:\n" +
                     "    static const " + nativeType + ' ' + info.wrapperName + "[];\n" +
                     "\n");
 
                 cpp.append(
--- a/build/annotationProcessors/utils/Utils.java
+++ b/build/annotationProcessors/utils/Utils.java
@@ -76,27 +76,27 @@ public class Utils {
         if (type.isArray()) {
             final String compName = type.getComponentType().getName();
             if (NATIVE_ARRAY_TYPES.containsKey(compName)) {
                 return NATIVE_ARRAY_TYPES.get(compName) + "::Param";
             }
             return "mozilla::jni::ObjectArray::Param";
         }
 
-        if (type == String.class || type == CharSequence.class) {
+        if (type.equals(String.class) || type.equals(CharSequence.class)) {
             return "mozilla::jni::String::Param";
         }
 
-        if (type == Class.class) {
+        if (type.equals(Class.class)) {
             // You're doing reflection on Java objects from inside C, returning Class objects
             // to C, generating the corresponding code using this Java program. Really?!
             return "mozilla::jni::ClassObject::Param";
         }
 
-        if (type == Throwable.class) {
+        if (type.equals(Throwable.class)) {
             return "mozilla::jni::Throwable::Param";
         }
 
         return "mozilla::jni::Object::Param";
     }
 
     public static String getNativeReturnType(Class<?> type, AnnotationInfo info) {
         final String name = type.getName().replace('.', '/');
@@ -108,27 +108,27 @@ public class Utils {
         if (type.isArray()) {
             final String compName = type.getComponentType().getName();
             if (NATIVE_ARRAY_TYPES.containsKey(compName)) {
                 return NATIVE_ARRAY_TYPES.get(compName) + "::LocalRef";
             }
             return "mozilla::jni::ObjectArray::LocalRef";
         }
 
-        if (type == String.class) {
+        if (type.equals(String.class)) {
             return "mozilla::jni::String::LocalRef";
         }
 
-        if (type == Class.class) {
+        if (type.equals(Class.class)) {
             // You're doing reflection on Java objects from inside C, returning Class objects
             // to C, generating the corresponding code using this Java program. Really?!
             return "mozilla::jni::ClassObject::LocalRef";
         }
 
-        if (type == Throwable.class) {
+        if (type.equals(Throwable.class)) {
             return "mozilla::jni::Throwable::LocalRef";
         }
 
         return "mozilla::jni::Object::LocalRef";
     }
 
     /**
      * Get the JNI class descriptor corresponding to the provided type parameter.