Use a class literal instead of
Class.forName.My naive hope is that this uses the same class loader and that it differs only in not causing the class to be immediately initialized.
If we were to want to preserve the eager initialization, we could use
Reflection.initialize. But initialization seems to mainly be a matter of some reflective lookups that are allowed to fail, so my guess is that it’s actually better to postpone that on the general principle of not slowing down startup time, especially on Android where reflection has at least historically been slow. (Perhaps even the direct use of the class literal improves performance, though we still need to look up its method reflectively later on, at least unless we use performance as a reason to makeFinalizerimplement some interface whose method we can call directly, as contemplated in https://github.com/google/guava/commit/9744861cb9b985bb832b81da91233b423b812276.)RELNOTES=n/a PiperOrigin-RevId: 878788447
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802032778号
Guava: Google Core Libraries for Java
Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, hashing, primitives, strings, and more! It is widely used on most Java projects within Google, and widely used by many other companies as well.
Guava comes in two flavors:
androiddirectory.Adding Guava to your build
Guava’s Maven group ID is
com.google.guava, and its artifact ID isguava. Guava provides two different “flavors”: one for use on a (Java 8+) JRE and one for use on Android or by any library that wants to be compatible with Android. These flavors are specified in the Maven version field as either33.5.0-jreor33.5.0-android. For more about depending on Guava, see using Guava in your build.To add a dependency on Guava using Maven, use the following:
To add a dependency using Gradle:
For more information on when to use
apiand when to useimplementation, consult the Gradle documentation on API and implementation separation.Snapshots and Documentation
Snapshots of Guava built from the
masterbranch are available through Maven using version999.0.0-HEAD-jre-SNAPSHOT, or999.0.0-HEAD-android-SNAPSHOTfor the Android flavor.Snapshot API Javadoc as well as Snapshot API Diffs are also available.
Another easy way to get to the Javadoc is to open guava.dev/api. You can also jump right to a specific class by appending the class name to guava.dev. For example, guava.dev/ImmutableList!
Learn about Guava
Links
IMPORTANT WARNINGS
APIs marked with the
@Betaannotation at the class or method level are subject to change. They can be modified in any way, or even removed, at any time. If your code is a library itself (i.e., it is used on the CLASSPATH of users outside your own control), you should not use beta APIs unless you repackage them. If your code is a library, we strongly recommend using the Guava Beta Checker to ensure that you do not use any@BetaAPIs!APIs without
@Betawill remain binary-compatible for the indefinite future. (Previously, we sometimes removed such APIs after a deprecation period. The last release to remove non-@BetaAPIs was Guava 21.0.) Even@DeprecatedAPIs will remain (again, unless they are@Beta). We have no plans to start removing things again, but officially, we’re leaving our options open in case of surprises (like, say, a serious security problem).Guava has one dependency that is needed for linkage at runtime:
com.google.guava:failureaccess:1.0.3. It also has some annotation-only dependencies, which we discuss in more detail at that link.Serialized forms of ALL objects are subject to change unless noted otherwise. Do not persist these and assume they can be read by a future version of the library.
Our classes are not designed to protect against a malicious caller. You should not use them for communication between trusted and untrusted code.
For the mainline flavor, we test the libraries using OpenJDK 8, 11, and 17 on Linux, with some additional testing on newer JDKs and on Windows. Some features, especially in
com.google.common.io, may not work correctly in non-Linux environments. For the Android flavor, our unit tests also run on API level 23 (Marshmallow).