Package org.apache.tomcat.util
Class IntrospectionUtils
- java.lang.Object
-
- org.apache.tomcat.util.IntrospectionUtils
-
public final class IntrospectionUtils extends java.lang.ObjectUtils for introspection and reflection
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceIntrospectionUtils.PropertySourceInterface for a source of named properties used during introspection.static interfaceIntrospectionUtils.SecurePropertySource
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.ObjectcallMethod1(java.lang.Object target, java.lang.String methodN, java.lang.Object param1, java.lang.String typeParam1, java.lang.ClassLoader cl)Call a method with a single parameter on the given target object.static java.lang.ObjectcallMethodN(java.lang.Object target, java.lang.String methodN, java.lang.Object[] params, java.lang.Class<?>[] typeParams)Call a method with multiple parameters on the given target object.static java.lang.Stringcapitalize(java.lang.String name)Reverse of Introspector.decapitalize.static voidclear()Clear the internal method cache.static java.lang.Objectconvert(java.lang.String object, java.lang.Class<?> paramType)Convert a string value to the specified type.static java.lang.Stringescape(java.lang.String s)Escapes special characters in a string for use in generated code.static java.lang.reflect.MethodfindMethod(java.lang.Class<?> c, java.lang.String name, java.lang.Class<?>[] params)Find a specific method by name and parameter types.static java.lang.reflect.Method[]findMethods(java.lang.Class<?> c)Find all public methods of the given class, using a cache for performance.static java.lang.ObjectgetProperty(java.lang.Object o, java.lang.String name)Get the value of a property from the given object.static booleanisInstance(java.lang.Class<?> clazz, java.lang.String type)Checks to see if the specified class is an instance of or assignable from the specified type.static java.lang.StringreplaceProperties(java.lang.String value, java.util.Hashtable<java.lang.Object,java.lang.Object> staticProp, IntrospectionUtils.PropertySource[] dynamicProp, java.lang.ClassLoader classLoader)Replaces ${NAME} in the value with the value of the property 'NAME'.static booleansetProperty(java.lang.Object o, java.lang.String name, java.lang.String value)Find a method with the right name If found, call the method ( if param is int or boolean we'll convert value to the right type before) - that means you can have setDebug(1).static booleansetProperty(java.lang.Object o, java.lang.String name, java.lang.String value, boolean invokeSetProperty)Set a property on the given object.static booleansetProperty(java.lang.Object o, java.lang.String name, java.lang.String value, boolean invokeSetProperty, java.lang.StringBuilder actualMethod)Set a property on the given object, tracking the actual method called.
-
-
-
Method Detail
-
setProperty
public static boolean setProperty(java.lang.Object o, java.lang.String name, java.lang.String value)Find a method with the right name If found, call the method ( if param is int or boolean we'll convert value to the right type before) - that means you can have setDebug(1).- Parameters:
o- The object to set a property onname- The property namevalue- The property value- Returns:
trueif operation was successful
-
setProperty
public static boolean setProperty(java.lang.Object o, java.lang.String name, java.lang.String value, boolean invokeSetProperty)Set a property on the given object.- Parameters:
o- the object to set the property onname- the property namevalue- the property valueinvokeSetProperty- whether to invoke setProperty as a fallback- Returns:
trueif the operation was successful
-
setProperty
public static boolean setProperty(java.lang.Object o, java.lang.String name, java.lang.String value, boolean invokeSetProperty, java.lang.StringBuilder actualMethod)Set a property on the given object, tracking the actual method called.- Parameters:
o- the object to set the property onname- the property namevalue- the property valueinvokeSetProperty- whether to invoke setProperty as a fallbackactualMethod- StringBuilder to append the actual method call to, ornull- Returns:
trueif the operation was successful
-
escape
public static java.lang.String escape(java.lang.String s)
Escapes special characters in a string for use in generated code.- Parameters:
s- the input string- Returns:
- the escaped string with double quotes, backslashes, newlines and carriage returns escaped
-
getProperty
public static java.lang.Object getProperty(java.lang.Object o, java.lang.String name)Get the value of a property from the given object.- Parameters:
o- the object to get the property fromname- the property name- Returns:
- the property value, or
nullif not found
-
replaceProperties
public static java.lang.String replaceProperties(java.lang.String value, java.util.Hashtable<java.lang.Object,java.lang.Object> staticProp, IntrospectionUtils.PropertySource[] dynamicProp, java.lang.ClassLoader classLoader)Replaces ${NAME} in the value with the value of the property 'NAME'. Replaces ${NAME:DEFAULT} with the value of the property 'NAME:DEFAULT', if the property 'NAME:DEFAULT' is not set, the expression is replaced with the value of the property 'NAME', if the property 'NAME' is not set, the expression is replaced with 'DEFAULT'. If the property is not set and there is no default the value will be returned unmodified.- Parameters:
value- The valuestaticProp- Replacement propertiesdynamicProp- Replacement propertiesclassLoader- Class loader associated with the code requesting the property- Returns:
- the replacement value
-
capitalize
public static java.lang.String capitalize(java.lang.String name)
Reverse of Introspector.decapitalize.- Parameters:
name- The name- Returns:
- the capitalized string
-
clear
public static void clear()
Clear the internal method cache.
-
findMethods
public static java.lang.reflect.Method[] findMethods(java.lang.Class<?> c)
Find all public methods of the given class, using a cache for performance.- Parameters:
c- the class- Returns:
- the array of methods
-
findMethod
public static java.lang.reflect.Method findMethod(java.lang.Class<?> c, java.lang.String name, java.lang.Class<?>[] params)Find a specific method by name and parameter types.- Parameters:
c- the classname- the method nameparams- the parameter types, ornullfor no parameters- Returns:
- the matching method, or
nullif not found
-
callMethod1
public static java.lang.Object callMethod1(java.lang.Object target, java.lang.String methodN, java.lang.Object param1, java.lang.String typeParam1, java.lang.ClassLoader cl) throws java.lang.ExceptionCall a method with a single parameter on the given target object.- Parameters:
target- the target objectmethodN- the method nameparam1- the parameter valuetypeParam1- the parameter type name, ornullto infer from the parametercl- the class loader to use for loading the parameter type- Returns:
- the result of the method call
- Throws:
java.lang.Exception- if the method cannot be found or invoked
-
callMethodN
public static java.lang.Object callMethodN(java.lang.Object target, java.lang.String methodN, java.lang.Object[] params, java.lang.Class<?>[] typeParams) throws java.lang.ExceptionCall a method with multiple parameters on the given target object.- Parameters:
target- the target objectmethodN- the method nameparams- the parameter valuestypeParams- the parameter types- Returns:
- the result of the method call, or
nullif the method cannot be found - Throws:
java.lang.Exception- if the method cannot be invoked
-
convert
public static java.lang.Object convert(java.lang.String object, java.lang.Class<?> paramType)Convert a string value to the specified type.- Parameters:
object- the string value to convertparamType- the target type- Returns:
- the converted object
- Throws:
java.lang.IllegalArgumentException- if conversion fails
-
isInstance
public static boolean isInstance(java.lang.Class<?> clazz, java.lang.String type)Checks to see if the specified class is an instance of or assignable from the specified type. The classclazz, all its superclasses, interfaces and those superinterfaces are tested for a match against the type nametype. This is similar toinstanceoforClass.isAssignableFrom(java.lang.Class<?>)except that the target type will not be resolved into a Class object, which provides some security and memory benefits.- Parameters:
clazz- The class to test for a match.type- The name of the type thatclazzmust be.- Returns:
trueif theclazztested is an instance of the specifiedtype,falseotherwise.
-
-