public static OneLocalsArray mergeLocals(OneLocalsArray locals1, OneLocalsArray locals2) {
    if (locals1 == locals2) {
      // Easy out.
      return locals1;
    }

    int sz = locals1.getMaxLocals();
    OneLocalsArray result = null;

    if (locals2.getMaxLocals() != sz) {
      throw new SimException("mismatched maxLocals values");
    }

    for (int i = 0; i < sz; i++) {
      TypeBearer tb1 = locals1.getOrNull(i);
      TypeBearer tb2 = locals2.getOrNull(i);
      TypeBearer resultType = mergeType(tb1, tb2);
      if (resultType != tb1) {
        /*
         * We only need to do anything when the result differs
         * from what is in the first array, since that's what the
         * result gets initialized to.
         */
        if (result == null) {
          result = locals1.copy();
        }

        if (resultType == null) {
          result.invalidate(i);
        } else {
          result.set(i, resultType);
        }
      }
    }

    if (result == null) {
      return locals1;
    }

    result.setImmutable();
    return result;
  }
Merges two locals arrays. If the merged result is the same as the first argument, then return
the first argument (not a copy).

@param locals1 {@code non-null;} a locals array
@param locals2 {@code non-null;} another locals array
@return {@code non-null;} the result of merging the two locals arrays
android/android_framework/dex-maker/src/main/java/com/android/dx/cf/code/Merger.java