Top Description Fields Constructors Methods
org.eclipse.jetty.util

public Class TypeUtil

extends Object
Class Inheritance
Imports
java.io.IOException, java.lang.invoke.MethodHandle, .MethodHandles, .MethodType, java.lang.module.Configuration, .ModuleReference, .ResolvedModule, java.lang.reflect.Constructor, .InvocationTargetException, java.net.URI, .URISyntaxException, .URL, java.nio.charset.StandardCharsets, java.security.CodeSource, .ProtectionDomain, java.util.ArrayList, .Arrays, .Collections, .HashMap, .Iterator, .List, .Objects, .Optional, .ServiceConfigurationError, .ServiceLoader, java.util.function.Function, .Predicate, java.util.regex.Matcher, .Pattern, java.util.stream.Stream, .StreamSupport, org.slf4j.Logger, .LoggerFactory

TYPE Utilities. Provides various static utility methods for manipulating types and their string representations.
Since
Jetty 4.1

Field Summary

Modifier and TypeField and Description
private static final HashMap<Class<?>, Function<String, Object>>
public static final int
public static final int
private static final MethodHandle[]
private static final Logger
private static final HashMap<String, Class<?>>
public static final Class<?>[]
private static final Pattern

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static void
public static <
the array and list entry type
T
>
List<T>

Returns:

a list backed by the array.
asList
(T[]
the array to convert to a list
a
)

Array to List.

public static <T> Iterator<T>
concat(Iterator<T> i1, Iterator<T> i2)

public static byte
convertHexDigit(byte
An ASCII encoded character 0-9 a-f A-F
c
)

Returns: The byte value of the character 0-16.

public static int
convertHexDigit(char
An ASCII encoded character 0-9 a-f A-F
c
)

Returns: The byte value of the character 0-16.

public static int
convertHexDigit(int
An ASCII encoded character 0-9 a-f A-F
c
)

Returns: The byte value of the character 0-16.

public static void
dump(Class<?> c)

public static void
public static <
The type of the predicate test
T
>
Predicate<T>

Returns:

true
falsePredicate
()

A Predicate that is always false, with optimized and/or/not methods.

public static Class<?>

Returns:

A class , which may be a primitive TYPE field..
fromName
(String
A class or type name.
name
)

Class from a canonical name for a type.

public static URI
public static URI
public static URI
public static URI

Returns:

the location as a URI (this is a URI pointing to a holder of the class: a directory, a jar file, a jrt:// resource, etc), or null of no location available.
getLocationOfClass
(Class<?>
the loaded class to find a location for.
clazz
)

Attempt to find the Location of a loaded Class.

public static URI
public static URI
public static boolean
isFalse(Object
Object to test for false
o
)

Returns: True if passed object is not null and is either a Boolean with value false or evaluates to a string that evaluates to false.

public static boolean
isHex(String str, int offset, int len)

public static boolean
isTrue(Object
Object to test for true
o
)

Returns: True if passed object is not null and is either a Boolean with value true or evaluates to a string that evaluates to true.

private static <
The class of the service type.
T
>
Stream<T>

Returns:

a stream of the loaded service providers.
mapToService
(ServiceLoader.Provider<T>
The service provider to instantiate.
provider
)

Used on a ServiceLoader#stream() with Stream#flatMap(Function), so that in the case a ServiceConfigurationError is thrown it warns and continues iterating through the service loader.

public static int

Returns:

the parsed integer
parseInt
(String
String
s
,
int
Offset within string
offset
,
int
Length of integer or -1 for remainder of string
length
,
int
base of the integer
base
)

Parse an int from a substring.

public static int

Returns:

the parsed integer
parseInt
(byte[]
byte array
b
,
int
Offset within string
offset
,
int
Length of integer or -1 for remainder of string
length
,
int
base of the integer
base
)

Parse an int from a byte array of ascii characters.

public static <
the type of the service to load.
T
>
Stream<ServiceLoader.Provider<T>>

Returns:

A stream that lazily loads providers for this loader's service
serviceProviderStream
(ServiceLoader<T>
the ServiceLoader instance to use.
serviceLoader
)

Utility to create a stream which provides the same functionality as ServiceLoader#stream().

public static <
the type of the service to load.
T
>
Stream<T>

Returns:

a stream of the service type which will not throw ServiceConfigurationError.
serviceStream
(ServiceLoader<T>
the ServiceLoader instance to use.
serviceLoader
)

Utility method to provide a stream of the service type from a ServiceLoader.

public static String

Returns:

the classpath reference syntax for the class file
toClassReference
(Class<?>
the class to reference
clazz
)

Return the Classpath / Classloader reference for the provided class file.

public static String

Returns:

the classpath reference syntax for the class file
toClassReference
(String
the class to reference
className
)

Return the Classpath / Classloader reference for the provided class file.

public static void
toHex(byte b, Appendable buf)

public static void
toHex(int value, Appendable buf)

public static void
toHex(long value, Appendable buf)

public static String
toShortName(Class<?> type)

public static String
toString(byte[] bytes, int base)

public static <
The type of the predicate test
T
>
Predicate<T>

Returns:

true
truePredicate
()

A Predicate that is always true, with optimized and/or/not methods.

public static Object

Returns:

The value as an Object.
valueOf
(Class<?>
The class of the instance, which may be a primitive TYPE field.
type
,
String
The value as a string.
value
)

Convert String value to instance.

public static Object

Returns:

The value as an Object.
valueOf
(String
classname or type (eg int)
type
,
String
The value as a string.
value
)

Convert String value to instance.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

class2Valueback to summary
private static final HashMap<Class<?>, Function<String, Object>> class2Value
CRback to summary
public static final int CR
LFback to summary
public static final int LF
LOCATION_METHODSback to summary
private static final MethodHandle[] LOCATION_METHODS
LOGback to summary
private static final Logger LOG
name2Classback to summary
private static final HashMap<String, Class<?>> name2Class
NO_ARGSback to summary
public static final Class<?>[] NO_ARGS
TRAILING_DIGITSback to summary
private static final Pattern TRAILING_DIGITS

Constructor Detail

TypeUtilback to summary
private TypeUtil()

Method Detail

appendEnclosingback to summary
private static void appendEnclosing(StringBuilder b, Class<?> enclosing)
asListback to summary
public static <T> List<T> asList(T[] a)

Array to List.

Works like Arrays#asList(Object...), but handles null arrays.

Parameters
<T>
the array and list entry type
a:T[]

the array to convert to a list

Returns:List<T>

a list backed by the array.

concatback to summary
public static <T> Iterator<T> concat(Iterator<T> i1, Iterator<T> i2)
convertHexDigitback to summary
public static byte convertHexDigit(byte c)
Parameters
c:byte

An ASCII encoded character 0-9 a-f A-F

Returns:byte

The byte value of the character 0-16.

convertHexDigitback to summary
public static int convertHexDigit(char c)
Parameters
c:char

An ASCII encoded character 0-9 a-f A-F

Returns:int

The byte value of the character 0-16.

convertHexDigitback to summary
public static int convertHexDigit(int c)
Parameters
c:int

An ASCII encoded character 0-9 a-f A-F

Returns:int

The byte value of the character 0-16.

dumpback to summary
public static void dump(Class<?> c)
dumpback to summary
public static void dump(ClassLoader cl)
falsePredicateback to summary
public static <T> Predicate<T> falsePredicate()

A Predicate that is always false, with optimized and/or/not methods.

Parameters
<T>
The type of the predicate test
Returns:Predicate<T>

true

fromNameback to summary
public static Class<?> fromName(String name)

Class from a canonical name for a type.

Parameters
name:String

A class or type name.

Returns:Class<?>

A class , which may be a primitive TYPE field..

getClassLoaderLocationback to summary
public static URI getClassLoaderLocation(Class<?> clazz)
getClassLoaderLocationback to summary
public static URI getClassLoaderLocation(Class<?> clazz, ClassLoader loader)
getCodeSourceLocationback to summary
public static URI getCodeSourceLocation(Class<?> clazz)
getLocationOfClassback to summary
public static URI getLocationOfClass(Class<?> clazz)

Attempt to find the Location of a loaded Class.

This can be null for primitives, void, and in-memory classes.

Parameters
clazz:Class<?>

the loaded class to find a location for.

Returns:URI

the location as a URI (this is a URI pointing to a holder of the class: a directory, a jar file, a jrt:// resource, etc), or null of no location available.

getModuleLocationback to summary
public static URI getModuleLocation(Class<?> clazz)
getSystemClassLoaderLocationback to summary
public static URI getSystemClassLoaderLocation(Class<?> clazz)
isFalseback to summary
public static boolean isFalse(Object o)
Parameters
o:Object

Object to test for false

Returns:boolean

True if passed object is not null and is either a Boolean with value false or evaluates to a string that evaluates to false.

isHexback to summary
public static boolean isHex(String str, int offset, int len)
isTrueback to summary
public static boolean isTrue(Object o)
Parameters
o:Object

Object to test for true

Returns:boolean

True if passed object is not null and is either a Boolean with value true or evaluates to a string that evaluates to true.

mapToServiceback to summary
private static <T> Stream<T> mapToService(ServiceLoader.Provider<T> provider)

Used on a ServiceLoader#stream() with Stream#flatMap(Function), so that in the case a ServiceConfigurationError is thrown it warns and continues iterating through the service loader.
Usage Example:

ServiceLoader.load(Service.class).stream().flatMap(TypeUtil::providerMap).collect(Collectors.toList());

Parameters
<T>
The class of the service type.
provider:ServiceLoader.Provider<T>

The service provider to instantiate.

Returns:Stream<T>

a stream of the loaded service providers.

parseIntback to summary
public static int parseInt(String s, int offset, int length, int base) throws NumberFormatException

Parse an int from a substring. Negative numbers are not handled.

Parameters
s:String

String

offset:int

Offset within string

length:int

Length of integer or -1 for remainder of string

base:int

base of the integer

Returns:int

the parsed integer

Exceptions
NumberFormatException:
if the string cannot be parsed
parseIntback to summary
public static int parseInt(byte[] b, int offset, int length, int base) throws NumberFormatException

Parse an int from a byte array of ascii characters. Negative numbers are not handled.

Parameters
b:byte[]

byte array

offset:int

Offset within string

length:int

Length of integer or -1 for remainder of string

base:int

base of the integer

Returns:int

the parsed integer

Exceptions
NumberFormatException:
if the array cannot be parsed into an integer
serviceProviderStreamback to summary
public static <T> Stream<ServiceLoader.Provider<T>> serviceProviderStream(ServiceLoader<T> serviceLoader)

Utility to create a stream which provides the same functionality as ServiceLoader#stream(). However, this also guards the case in which Iterator#hasNext() throws. Any exceptions from the underlying iterator will be cached until the ServiceLoader.Provider#get() is called.

Parameters
<T>
the type of the service to load.
serviceLoader:ServiceLoader<T>

the ServiceLoader instance to use.

Returns:Stream<ServiceLoader.Provider<T>>

A stream that lazily loads providers for this loader's service

serviceStreamback to summary
public static <T> Stream<T> serviceStream(ServiceLoader<T> serviceLoader)

Utility method to provide a stream of the service type from a ServiceLoader. Log warnings will be given for any ServiceConfigurationErrors which occur when loading or instantiating the services.

Parameters
<T>
the type of the service to load.
serviceLoader:ServiceLoader<T>

the ServiceLoader instance to use.

Returns:Stream<T>

a stream of the service type which will not throw ServiceConfigurationError.

toClassReferenceback to summary
public static String toClassReference(Class<?> clazz)

Return the Classpath / Classloader reference for the provided class file.

Convenience method for the code

String ref = myObject.getClass().getName().replace('.','/') + ".class";
Parameters
clazz:Class<?>

the class to reference

Returns:String

the classpath reference syntax for the class file

toClassReferenceback to summary
public static String toClassReference(String className)

Return the Classpath / Classloader reference for the provided class file.

Convenience method for the code

String ref = myClassName.replace('.','/') + ".class";
Parameters
className:String

the class to reference

Returns:String

the classpath reference syntax for the class file

toHexback to summary
public static void toHex(byte b, Appendable buf)
toHexback to summary
public static void toHex(int value, Appendable buf) throws IOException
toHexback to summary
public static void toHex(long value, Appendable buf) throws IOException
toShortNameback to summary
public static String toShortName(Class<?> type)
toStringback to summary
public static String toString(byte[] bytes, int base)
truePredicateback to summary
public static <T> Predicate<T> truePredicate()

A Predicate that is always true, with optimized and/or/not methods.

Parameters
<T>
The type of the predicate test
Returns:Predicate<T>

true

valueOfback to summary
public static Object valueOf(Class<?> type, String value)

Convert String value to instance.

Parameters
type:Class<?>

The class of the instance, which may be a primitive TYPE field.

value:String

The value as a string.

Returns:Object

The value as an Object.

valueOfback to summary
public static Object valueOf(String type, String value)

Convert String value to instance.

Parameters
type:String

classname or type (eg int)

value:String

The value as a string.

Returns:Object

The value as an Object.