Class ArbitraryInstances
Covers arrays, enums and common types defined in java.lang
, java.lang.reflect
,
java.io
, java.nio
, java.math
, java.util
,
java.util.concurrent
, java.util.regex
, com.google.common.base
,
com.google.common.collect
and com.google.common.primitives
. In addition, if the type
exposes at least one public static final constant of the same type, one of the constants will be
used; or if the class exposes a public parameter-less constructor then it will be "new"d and
returned.
All default instances returned by get(java.lang.Class<T>)
are generics-safe. Clients won't get type
errors for using get(Comparator.class)
as a Comparator<Foo>
, for example.
Immutable empty instances are returned for collection types; ""
for string; 0
for
number types; reasonable default instance for other stateless types. For mutable types, a fresh
instance is created each time get()
is called.
- Since:
- 12.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
private static final class
private static final class
private static final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ClassToInstanceMap
<Object> private static final ConcurrentMap
<Class<?>, Class<?>> type → implementation.private static final Logger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static <T> T
arbitraryConstantInstanceOrNull
(Class<T> type) private static <T> T
createEmptyArray
(Class<T> arrayType) private static MatchResult
Returns a newMatchResult
that corresponds to a successful match.static <T> T
Returns an arbitrary instance fortype
, ornull
if no arbitrary instance can be determined.private static <T> Class
<? extends T> getImplementation
(Class<T> type) private static <T> void
setImplementation
(Class<T> type, Class<? extends T> implementation)
-
Field Details
-
BY_FIELD_NAME
-
DEFAULTS
-
implementations
type → implementation. Inherently mutable interfaces and abstract classes are mapped to their default implementations and are "new"d upon get(). -
logger
-
-
Constructor Details
-
ArbitraryInstances
private ArbitraryInstances()
-
-
Method Details
-
createMatchResult
Returns a newMatchResult
that corresponds to a successful match. Apache Harmony (used in Android) requires a successful match in order to generate aMatchResult
: https://cs.android.com/android/platform/superproject/+/android-2.3.7_r1:libcore/luni/src/main/java/java/util/regex/Matcher.java;l=550;drc=5850271b4ab93ebc27c1d49169a348c6be3c7f04 -
setImplementation
-
getImplementation
-
get
Returns an arbitrary instance fortype
, ornull
if no arbitrary instance can be determined. -
arbitraryConstantInstanceOrNull
-
createEmptyArray
-