【Android】Android studio 小技巧:对比两段代码

有时候需要对比两段代码,找出其中的区别,用肉眼显然是不可靠的。利用 Android Studio 的 Compare with Clipboard 功能,就可以轻松做到。

例如,以下两段代码:

    // method 1
    public static Object getFieldValue(@NonNull Object obj, @NonNull Class clazz, @NonNull String fieldName) throws NoSuchFieldException {
        Field field = null;
        try {
            field = clazz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            try {
                field = clazz.getField(fieldName);
            } catch (NoSuchFieldException ignore) {
                throw new NoSuchFieldException("No method found named " + fieldName + " in class " + clazz);
            }
        }
        try {
            field.setAccessible(true);
            return field.get(obj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    // method 2
    public static Object getFieldValue(@NonNull Object obj, @NonNull Class clazz, @NonNull String fieldName) {
        Field field = null;
        try {
            field = clazz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            try {
                field = clazz.getField(fieldName);
            } catch (NoSuchFieldException ignore) {
            }
        }
        if (field == null) {
            return null;
        }
        try {
            field.setAccessible(true);
            return field.get(obj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

首先,复制第一段代码:


copy

然后,选中第二段代码,点击 Compare with Clipboard

compare

即可显示对比结果:


result

你可能感兴趣的:(【Android】Android studio 小技巧:对比两段代码)