Android 14 新特性代码 UUID.fromString Matcher.matches 的细节改动(扒源码)

本文主要是介绍Android 14 新特性代码 UUID.fromString Matcher.matches 的细节改动(扒源码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 前言
    • UUID 处理的更改
    • 正则表达式的更改
    • 结束


前言

Android 14 已经出来好久好久了…

今天其他的暂且不论,单纯的讲一下 OpenJDK 17 更新的两点变更(扒源代码)~

  • 对正则表达式的更改
  • UUID 处理

首先,正则表达式的更改:现在,为了更严格地遵循 OpenJDK 的语义,不允许无效的组引用。您可能会看到 java.util.regex.Matcher 类抛出 IllegalArgumentException 的新情况,因此请务必测试应用中使用正则表达式的情形。如需在测试期间启用或停用此变更,请使用兼容性框架工具切换 DISALLOW_INVALID_GROUP_REFERENCE 标志。

其次,UUID 处理:现在,验证输入参数时,java.util.UUID.fromString() 方法会执行更严格的检查,因此您可能会在反序列化期间看到 IllegalArgumentException。如需在测试期间启用或停用此变更,请使用兼容性框架工具切换 ENABLE_STRICT_VALIDATION 标志。

巴拉巴拉,以上是官网的言论,诸君可以看官方的描述地址


UUID 处理的更改

原有代码逻辑~

 /*** Creates a {@code UUID} from the string standard representation as* described in the {@link #toString} method.** @param  name*         A string that specifies a {@code UUID}** @return  A {@code UUID} with the specified value** @throws  IllegalArgumentException*          If name does not conform to the string representation as*          described in {@link #toString}**/public static UUID fromString(String name) {String[] components = name.split("-");if (components.length != 5)//仅会判断以-分割的数组长度是否等于5来抛出异常throw new IllegalArgumentException("Invalid UUID string: "+name);for (int i=0; i<5; i++)components[i] = "0x"+components[i];long mostSigBits = Long.decode(components[0]).longValue();mostSigBits <<= 16;mostSigBits |= Long.decode(components[1]).longValue();mostSigBits <<= 16;mostSigBits |= Long.decode(components[2]).longValue();long leastSigBits = Long.decode(components[3]).longValue();leastSigBits <<= 48;leastSigBits |= Long.decode(components[4]).longValue();return new UUID(mostSigBits, leastSigBits);}

新代码逻辑~

/*** Creates a {@code UUID} from the string standard representation as* described in the {@link #toString} method.** @param  name*         A string that specifies a {@code UUID}** @return  A {@code UUID} with the specified value** @throws  IllegalArgumentException*          If name does not conform to the string representation as*          described in {@link #toString}**/public static UUID fromString(String name) {// BEGIN Android-changed: Java 8 behaviour is more lenient and the new implementation// might break apps (b/254278943).// Using old implementation for apps targeting Android older than U.// 取反! 如果设备的sdk版本,大于等于  UPSIDE_DOWN_CAKE(34)的话,且启用严格验证if (!(VMRuntime.getSdkVersion() >= VersionCodes.UPSIDE_DOWN_CAKE&& Compatibility.isChangeEnabled(ENABLE_STRICT_VALIDATION))) {return fromStringJava8(name);}//如果小于或者没开启严格验证,则执行其他的验证 return fromStringCurrentJava(name);// END Android-changed: Java 8 behaviour is more lenient and the new implementation// might break apps (b/254278943).}

如下是java8 的判断方式(跟原来一致)

  /*** Extracted for testing purposes only.* @hide*/public static UUID fromStringJava8(String name) {String[] components = name.split("-");if (components.length != 5)throw new IllegalArgumentException("Invalid UUID string: "+ name);for (int i=0; i<5; i++)components[i] = "0x"+components[i];long mostSigBits = Long.decode(components[0]).longValue();mostSigBits <<= 16;mostSigBits |= Long.decode(components[1]).longValue();mostSigBits <<= 16;mostSigBits |= Long.decode(components[2]).longValue();long leastSigBits = Long.decode(components[3]).longValue();leastSigBits <<= 48;leastSigBits |= Long.decode(components[4]).longValue();return new UUID(mostSigBits, leastSigBits);}

如下是执行34或者开启严格验证的方法:

/*** Extracted for testing purposes only.* @hide*/public static UUID fromStringCurrentJava(String name) {if (name.length() == 36) {char ch1 = name.charAt(8);char ch2 = name.charAt(13);char ch3 = name.charAt(18);char ch4 = name.charAt(23);if (ch1 == '-' && ch2 == '-' && ch3 == '-' && ch4 == '-') {long msb1 = parse4Nibbles(name, 0);long msb2 = parse4Nibbles(name, 4);long msb3 = parse4Nibbles(name, 9);long msb4 = parse4Nibbles(name, 14);long lsb1 = parse4Nibbles(name, 19);long lsb2 = parse4Nibbles(name, 24);long lsb3 = parse4Nibbles(name, 28);long lsb4 = parse4Nibbles(name, 32);if ((msb1 | msb2 | msb3 | msb4 | lsb1 | lsb2 | lsb3 | lsb4) >= 0) {return new UUID(msb1 << 48 | msb2 << 32 | msb3 << 16 | msb4,lsb1 << 48 | lsb2 << 32 | lsb3 << 16 | lsb4);}}}return fromString1(name);}private static UUID fromString1(String name) {int len = name.length();if (len > 36) {throw new IllegalArgumentException("UUID string too large");}int dash1 = name.indexOf('-', 0);int dash2 = name.indexOf('-', dash1 + 1);int dash3 = name.indexOf('-', dash2 + 1);int dash4 = name.indexOf('-', dash3 + 1);int dash5 = name.indexOf('-', dash4 + 1);// For any valid input, dash1 through dash4 will be positive and dash5// negative, but it's enough to check dash4 and dash5:// - if dash1 is -1, dash4 will be -1// - if dash1 is positive but dash2 is -1, dash4 will be -1// - if dash1 and dash2 is positive, dash3 will be -1, dash4 will be//   positive, but so will dash5if (dash4 < 0 || dash5 >= 0) {throw new IllegalArgumentException("Invalid UUID string: " + name);}long mostSigBits = Long.parseLong(name, 0, dash1, 16) & 0xffffffffL;mostSigBits <<= 16;mostSigBits |= Long.parseLong(name, dash1 + 1, dash2, 16) & 0xffffL;mostSigBits <<= 16;mostSigBits |= Long.parseLong(name, dash2 + 1, dash3, 16) & 0xffffL;long leastSigBits = Long.parseLong(name, dash3 + 1, dash4, 16) & 0xffffL;leastSigBits <<= 48;leastSigBits |= Long.parseLong(name, dash4 + 1, len, 16) & 0xffffffffffffL;return new UUID(mostSigBits, leastSigBits);}

解析:
从代码的角度看起来是多了好多验证,梳理一下:

1. 判断是否34(sdk版本),且未开启严格验证的情况下,还是按照原有代码执行,仅仅判断 以-分割的数组长度是否等于5,若不等于5,则主动抛出异常;2. 如果sdk版本大于等于34,且开启了严格验证,则会执行fromStringCurrentJava()& fromString1()方法,fromStringCurrentJava方法中没有主动抛出异常的代码,判断长度是否=36,不等于则执行 fromString1,若等于则继续执行,获取字符串下标8、13、18、23的值,若这四个值不等于 - ,则执行 fromString1;
在fromString1; 方法中,传递的字符串长度不得大于36,若超出则主动抛异常;继续判断;对于任何有效输入,dash1 到 dash4 将为正数,dash5 将为正数,负数,但检查 dash4 和 dash5 就足够了:
若是dash4小于0,dash5大于等于0,则主动抛出异常;

正则表达式的更改

原有代码逻辑~

/*** Attempts to match the entire region against the pattern.** <p> If the match succeeds then more information can be obtained via the* <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods.  </p>** @return  <tt>true</tt> if, and only if, the entire region sequence*          matches this matcher's pattern*/public boolean matches() {synchronized (this) {matchFound = nativeMatcher.matches(groups);}return matchFound;

新代码逻辑~

 /*** Attempts to match the entire region against the pattern.** <p> If the match succeeds then more information can be obtained via the* {@code start}, {@code end}, and {@code group} methods.  </p>** @return  {@code true} if, and only if, the entire region sequence*          matches this matcher's pattern*/public boolean matches() {synchronized (this) {matchFound = nativeMatcher.matches(groups);}//主要增加一个这个modCount++;return matchFound;}

modCount的描述 :记录该匹配器状态被修改的次数

/*** Number of times this matcher's state has been modified*/int modCount;

结束

不是我说哈,你都是系统代码了,还起个 fromString1 这样的方法名称,丢人不~
就这,有问题私聊我~
再会~

这篇关于Android 14 新特性代码 UUID.fromString Matcher.matches 的细节改动(扒源码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/568984

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

MySQL 添加索引5种方式示例详解(实用sql代码)

《MySQL添加索引5种方式示例详解(实用sql代码)》在MySQL数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中,下面给大家分享MySQL添加索引5种方式示例详解(实用sql代码),... 在mysql数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中。索引可以在创建表时定义,也可