public class InvokerHelper
extends Object
A static helper class to make bytecode generation easier and act as a facade over the Invoker
| Modifiers | Name | Description |
|---|---|---|
static Object[] |
EMPTY_ARGS |
Shared zero-length array used as the argument list for a call that takes no arguments. |
protected static Object[] |
EMPTY_ARGUMENTS |
The same array as EMPTY_ARGS. |
protected static Class[] |
EMPTY_TYPES |
Shared zero-length array of Class objects. |
static String |
MAIN_METHOD_NAME |
The method name "main". |
static MetaClassRegistry |
metaRegistry |
The MetaClassRegistry from GroovySystem.getMetaClassRegistry. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public static void |
append(Appendable out, Object object)Appends object to out by calling
FormatHelper.append(Appendable, Object). |
|
public static Object[] |
asArray(Object arguments)Converts the given object into an array; if it's an array then just cast otherwise wrap it in an array |
|
public static Iterator<Object> |
asIterator(Object o)Invokes the no-argument method iterator on o and
casts the result to Iterator. |
|
public static List |
asList(Object value)Adapts value to a List. |
|
public static Object[] |
asUnwrappedArray(Object arguments)Normalizes arguments with asArray(Object), then
replaces each PojoWrapper element with
PojoWrapper.unwrap. |
|
public static void |
assertFailed(Object expression, Object message)Throws the AssertionError returned by
createAssertError(Object, Object) for expression
and message. |
|
public static Object |
bitwiseNegate(Object value)Bitwise-negates value. |
|
public static AssertionError |
createAssertError(Object expression, Object message)
|
|
public static List |
createList(Object[] values)Returns a new ArrayList sized to values.length and
filled with those elements in order. |
|
public static Map |
createMap(Object[] values)Builds a LinkedHashMap from successive pairs in values.
|
|
public static List |
createRange(Object from, Object to, boolean exclusiveLeft, boolean exclusiveRight)Creates the range from from to to by delegating to
ScriptBytecodeAdapter.createRange. |
|
public static List |
createRange(Object from, Object to, boolean inclusive)Creates a range from from to to with the left end
included. |
|
public static Script |
createScript(Class scriptClass, Binding context)Adapts scriptClass to a Script associated with context. |
|
public static Tuple |
createTuple(Object[] array)Returns a Tuple constructed from array. |
|
public static String |
escapeBackslashes(String orig)Returns orig with backslash, newline, carriage return, tab,
and form feed written as escape sequences, by calling
FormatHelper.escapeBackslashes(String). |
|
public static Matcher |
findRegex(Object left, Object right)Find the right hand regex within the left hand string and return a matcher. |
|
protected static String |
format(Object arguments, boolean verbose)Renders arguments by calling
FormatHelper.format(Object, boolean). |
|
public static String |
format(Object arguments, boolean verbose, int maxSize)Renders arguments by calling
FormatHelper.format(Object, boolean, int). |
|
public static String |
format(Object arguments, boolean verbose, int maxSize, boolean safe)Renders arguments by calling
FormatHelper.format(Object, boolean, int, boolean).
|
|
public static Object |
getAttribute(Object object, String attribute)Reads the attribute attribute from object.
|
|
public static Object |
getGroovyObjectProperty(GroovyObject object, String property)Reads property by calling GroovyObject.getProperty
on object, with no null substitution and no registry lookup. |
|
public static MetaClass |
getMetaClass(Object object)Returns the meta-class used for object. |
|
public static MetaClass |
getMetaClass(Class cls)Returns the meta-class registered for cls. |
|
public static MetaClassRegistry |
getMetaRegistry()Returns metaRegistry. |
|
public static Closure |
getMethodPointer(Object object, String methodName)Returns a method closure for the given object and name. |
|
public static Object |
getProperty(Object object, String property)Reads the property property from object.
|
|
public static Object |
getPropertySafe(Object object, String property)Reads property from object when object is
not null. |
|
public static int |
initialCapacity(int initialEntryCnt)According to the initial entry count, calculate the initial capacity of hash map, which is power of 2 (SEE https://stackoverflow.com/questions/8352378/why-does-hashmap-require-that-the-initial-capacity-be-a-power-of-two) |
|
public static String |
inspect(Object self)Returns the inspect rendering of self produced by
FormatHelper.inspect(Object), which selects inspect mode
with escaped backslashes. |
|
public static Object |
invokeClosure(Object closure, Object arguments)Invokes doCall on closure through
invokeMethod(Object, String, Object). |
|
public static Object |
invokeConstructorOf(String klass, Object arguments)Loads klass with Class.forName(String) and invokes
a constructor of that class. |
|
public static Object |
invokeConstructorOf(Class type, Object arguments)Invokes a constructor of type through the meta-class
registered for that class. |
|
public static Object |
invokeMethod(Object object, String methodName, Object arguments)Invokes the given method on the object. |
|
public static Object |
invokeMethodSafe(Object object, String methodName, Object arguments)Invokes methodName on object when object is
not null. |
|
public static Object |
invokeNoArgumentsConstructorOf(Class type)Invokes a constructor of type with EMPTY_ARGS. |
|
public static Object |
invokeStaticMethod(String klass, String methodName, Object arguments)Loads klass with Class.forName(String) and invokes
the static method methodName on that class. |
|
public static Object |
invokeStaticMethod(Class type, String method, Object arguments)Invokes the static method method on type through the
meta-class registered for type. |
|
public static Object |
invokeStaticNoArgumentsMethod(Class type, String methodName)Invokes the static method methodName on type with
EMPTY_ARGS. |
|
public static Object |
invokeSuperMethod(Object object, String methodName, Object arguments)Invokes methodName through the meta-class of the superclass
of object's runtime class. |
|
public static boolean |
matchRegex(Object left, Object right)Find the right hand regex within the left hand string and return a matcher. |
|
public static Script |
newScript(Class<? extends Script> scriptClass, Binding context)Instantiates scriptClass and associates context.
|
|
public static void |
removeClass(Class clazz)Drops clazz from the meta-class registry, from ClassInfo,
and from the Introspector cache. |
|
public static Object |
runScript(Class scriptClass, String[] args)Runs scriptClass in a Binding whose args
variable is the supplied array. |
|
public static void |
setAttribute(Object object, String attribute, Object newValue)Writes newValue to the attribute attribute on
object. |
|
public static void |
setGroovyObjectProperty(Object newValue, GroovyObject object, String property)This is so we don't have to reorder the stack when we call this method. |
|
public static void |
setProperties(Object object, Map map)Sets the properties on the given object |
|
public static void |
setProperty(Object object, String property, Object newValue)Writes newValue to the property property on object.
|
|
public static void |
setProperty2(Object newValue, Object object, String property)This is so we don't have to reorder the stack when we call this method. |
|
public static void |
setPropertySafe2(Object newValue, Object object, String property)This is so we don't have to reorder the stack when we call this method. |
|
public static SpreadMap |
spreadMap(Object value)Builds a SpreadMap from value when value is a
Map. |
|
public static String |
toArrayString(Object[] arguments)Returns the bracketed rendering of arguments, using [
and ] boundaries, by calling
FormatHelper.toArrayString(Object[]). |
|
public static String |
toArrayString(Object[] arguments, int maxSize, boolean safe)Returns the bracketed rendering of arguments, using [
and ] boundaries, by calling
FormatHelper.toArrayString(Object[], int, boolean).
|
|
public static String |
toListString(Collection arg)Returns the bracketed rendering of arg, using [ and
] boundaries, by calling FormatHelper.toListString(Collection). |
|
public static String |
toListString(Collection arg, int maxSize)Returns the bracketed rendering of arg, using [ and
] boundaries, by calling
FormatHelper.toListString(Collection, int). |
|
public static String |
toListString(Collection arg, int maxSize, boolean safe)Returns the bracketed rendering of arg, using [ and
] boundaries, by calling
FormatHelper.toListString(Collection, int, boolean).
|
|
public static String |
toMapString(Map arg)Returns the bracketed rendering of arg, using [ and
] boundaries, by calling FormatHelper.toMapString(Map). |
|
public static String |
toMapString(Map arg, int maxSize)Returns the bracketed rendering of arg, using [ and
] boundaries, by calling FormatHelper.toMapString(Map, int).
|
|
public static String |
toString(Object arguments)Returns the string representation of arguments produced by
FormatHelper.toString(Object). |
|
public static String |
toTypeString(Object[] arguments)Returns a comma-separated list of the runtime type names in arguments, by calling FormatHelper.toTypeString(Object[]). |
|
public static String |
toTypeString(Object[] arguments, int maxSize)Returns a comma-separated list of the runtime type names in arguments, by calling FormatHelper.toTypeString(Object[], int).
|
|
public static Object |
unaryMinus(Object value)Negates value. |
|
public static Object |
unaryPlus(Object value)Applies unary plus to value. |
|
public static void |
write(Writer out, Object object)Writes object to out by calling
FormatHelper.write(Writer, Object). |
Shared zero-length array used as the argument list for a call that takes no arguments.
The same array as EMPTY_ARGS. asArray(Object)
returns it when the supplied arguments are null.
Shared zero-length array of Class objects.
The method name "main". createScript(Class, Binding)
calls a static method of this name when a class cannot be constructed
from a Binding.
The MetaClassRegistry from GroovySystem.getMetaClassRegistry. Meta-class lookups in this class go through this registry.
Appends object to out by calling
FormatHelper.append(Appendable, Object).
FormatHelper.append(Appendable, Object)out - the appendable that receives the textobject - the object to appendConverts the given object into an array; if it's an array then just cast otherwise wrap it in an array
Invokes the no-argument method iterator on o and
casts the result to Iterator.
o - the receiver of iterator Adapts value to a List. null becomes
Collections.EMPTY_LIST. A List is returned as given.
When the runtime class is an array, the value is cast to Object[]
and passed to Arrays.asList; a primitive array fails that cast.
An Enumeration is copied, in iteration order, into a new
ArrayList. Any other value becomes a one-element list.
Throws ClassCastException if value is a primitive array.
value - the value to adaptvalue Normalizes arguments with asArray(Object), then
replaces each PojoWrapper element with
PojoWrapper.unwrap. When arguments is already an
Object[], that same array is updated and returned.
arguments - the argument list, a single argument, or null Throws the AssertionError returned by
createAssertError(Object, Object) for expression
and message.
expression - the expression supplied to the errormessage - the message supplied to the error Bitwise-negates value. Integer and Long use
the ~ operator. BigInteger uses not().
A String or GString is compiled as a regular expression
by StringGroovyMethods.bitwiseNegate on value.toString().
An ArrayList yields a new list of the element-wise results.
Any other value is passed to invokeMethod(Object, String, Object)
as the no-argument method bitwiseNegate.
value - the value to negatebitwiseNegate method
Returns a new ArrayList sized to values.length and
filled with those elements in order.
values - the elements to copy into the list Builds a LinkedHashMap from successive pairs in values.
The initial capacity is initialCapacity(int) of
values.length / 2. A pair whose first element is a
SpreadMap and whose second element is a Map contributes
that map's entries. Every other pair is stored as one key and value.
A trailing element left without a pair is ignored.
values - alternating keys and values, with optional spread pairs Creates the range from from to to by delegating to
ScriptBytecodeAdapter.createRange. A RuntimeException
or Error is rethrown unchanged. Any other Throwable
is wrapped in a RuntimeException.
from - the start value passed to the delegateto - the end value passed to the delegateexclusiveLeft - passed through as the left-exclusive flagexclusiveRight - passed through as the right-exclusive flag Creates a range from from to to with the left end
included. The call is createRange(from, to, false, !inclusive),
so inclusive controls whether to is included.
from - the start value; the left end is inclusiveto - the end valueinclusive - true to include to; false to exclude it Adapts scriptClass to a Script associated with context.
A null class produces a NullScript bound to context;
its run method returns null. A class assignable to
Script is instantiated with newScript(Class, Binding).
For any other class, Binding.class.getName() is loaded from
scriptClass's class loader. A binding of that loaded type is
constructed from context.getVariables() through its declared
Map constructor, and scriptClass is constructed with
its declared constructor that takes that binding type. The instance is
wrapped in a Script created with the no-argument constructor,
so the wrapper's own binding is a new empty Binding.
The wrapper's run method invokes run on the instance
with no arguments.
If that attempt throws ClassNotFoundException,
NoSuchMethodException, or SecurityException, the result
is a Script constructed with context. Its run
method calls the static MAIN_METHOD_NAME method with one
argument: the binding property args when that value is a
String[], otherwise a new empty String[]. A missing
args property, reported as MissingPropertyException,
also selects the empty array. Before the wrapper is returned, each
entry of context.getVariables() whose key's toString()
does not start with '_' is written as a property of the
scriptClass class object. A
MissingPropertyException from that write is ignored, and so is
an IllegalArgumentException carried as the cause of an
InvokerInvocationException.
Any other Exception is wrapped in a GroovyRuntimeException
that names scriptClass.
scriptClass - the class to adapt, or nullcontext - the binding supplied to the adapted script Returns a Tuple constructed from array.
array - the elements passed to the tuple constructor Returns orig with backslash, newline, carriage return, tab,
and form feed written as escape sequences, by calling
FormatHelper.escapeBackslashes(String).
FormatHelper.escapeBackslashes(String)orig - the text to escapeFind the right hand regex within the left hand string and return a matcher.
left - string to compareright - regular expression to compare the string to Renders arguments by calling
FormatHelper.format(Object, boolean). When verbose
is true, the delegate uses inspect mode with escaped backslashes.
FormatHelper.format(Object, boolean)arguments - the object to renderverbose - true to select inspect mode with escaped backslashes Renders arguments by calling
FormatHelper.format(Object, boolean, int). When verbose
is true, the delegate uses inspect mode with escaped backslashes.
maxSize is an approximate character limit; -1 means no limit.
FormatHelper.format(Object, boolean, int)arguments - the object to renderverbose - true to select inspect mode with escaped backslashesmaxSize - the approximate character limit, or -1 for no limit Renders arguments by calling
FormatHelper.format(Object, boolean, int, boolean).
When verbose is true, the delegate uses inspect mode
with escaped backslashes. maxSize is an approximate character
limit; -1 means no limit. When safe is true,
an Exception thrown while rendering is swallowed and a fallback
representation is used.
FormatHelper.format(Object, boolean, int, boolean)arguments - the object to renderverbose - true to select inspect mode with escaped backslashesmaxSize - the approximate character limit, or -1 for no limitsafe - true to swallow a rendering Exception Reads the attribute attribute from object.
A null receiver is replaced with NullObject.getNullObject()
before the read. A Class receiver is tested before a
GroovyObject and uses the meta-class registered for that class.
A GroovyObject uses the meta-class from
GroovyObject.getMetaClass. Every other receiver uses
the meta-class registered for its runtime class. The value is then
read with getAttribute on that meta-class.
object - the receiver, or nullattribute - the attribute name Reads property by calling GroovyObject.getProperty
on object, with no null substitution and no registry lookup.
object - the Groovy receiverproperty - the property name Returns the meta-class used for object. A GroovyObject
is tested before a Class and answers with
GroovyObject.getMetaClass. A Class answers with
MetaClassRegistry.getMetaClass on
metaRegistry. Any other object answers with
MetaClassRegistryImpl.getMetaClass.
object - the receiver whose meta-class is required Returns the meta-class registered for cls.
cls - the class to look up in metaRegistryclsReturns metaRegistry.
Returns a method closure for the given object and name.
Reads the property property from object.
A null receiver is replaced with NullObject.getNullObject()
before the read. A GroovyObject is tested before a Class
and is read through GroovyObject.getProperty. A
Class receiver uses the meta-class registered for that class.
Every other receiver uses
MetaClassRegistryImpl.getMetaClass and then
getProperty on that meta-class.
object - the receiver, or nullproperty - the property name Reads property from object when object is
not null. A null receiver returns null and
is not replaced with NullObject.
object - the receiver, or nullproperty - the property namenull when object is nullAccording to the initial entry count, calculate the initial capacity of hash map, which is power of 2 (SEE https://stackoverflow.com/questions/8352378/why-does-hashmap-require-that-the-initial-capacity-be-a-power-of-two)
initialEntryCnt - the initial entry count Returns the inspect rendering of self produced by
FormatHelper.inspect(Object), which selects inspect mode
with escaped backslashes.
FormatHelper.inspect(Object)self - the object to inspect Invokes doCall on closure through
invokeMethod(Object, String, Object).
closure - the receiver of doCallarguments - the arguments passed to that invocationdoCall Loads klass with Class.forName(String) and invokes
a constructor of that class.
klass cannot be loadedklass - the class name passed to Class.forName(String)arguments - the arguments passed to invokeConstructorOf(Class, Object) Invokes a constructor of type through the meta-class
registered for that class. Arguments are normalized with
asArray(Object).
type - the class to instantiatearguments - the argument list, a single argument, or nullInvokes the given method on the object.
Invokes methodName on object when object is
not null. A null receiver returns null and
does not call invokeMethod(Object, String, Object).
object - the receiver, or nullmethodName - the method to invokearguments - the arguments accepted by invokeMethod(Object, String, Object)null when object is null Invokes a constructor of type with EMPTY_ARGS.
type - the class to instantiate Loads klass with Class.forName(String) and invokes
the static method methodName on that class.
klass cannot be loadedklass - the class name passed to Class.forName(String)methodName - the static method to invokearguments - the arguments passed to invokeStaticMethod(Class, String, Object) Invokes the static method method on type through the
meta-class registered for type. Arguments are normalized with
asArray(Object).
type - the class that receives the static callmethod - the method namearguments - the argument list, a single argument, or null Invokes the static method methodName on type with
EMPTY_ARGS.
type - the class that receives the static callmethodName - the static method to invoke Invokes methodName through the meta-class of the superclass
of object's runtime class. Arguments are normalized with
asArray(Object). A null receiver throws
NullPointerException whose message names methodName.
object - the receiver; must not be nullmethodName - the method to invokearguments - the argument list, a single argument, or nullFind the right hand regex within the left hand string and return a matcher.
left - string to compareright - regular expression to compare the string to Instantiates scriptClass and associates context.
Uses the public (Binding) constructor when scriptClass
has one. When that constructor is absent, calls the declared
no-argument constructor and then Script.setBinding.
scriptClass - the Script type to constructcontext - the binding passed to the constructor, or to setBinding Drops clazz from the meta-class registry, from ClassInfo,
and from the Introspector cache.
clazz - the class removed from those caches Runs scriptClass in a Binding whose args
variable is the supplied array. The instance comes from
createScript(Class, Binding), and run is invoked
on it with EMPTY_ARGS.
scriptClass - the class passed to createScript(Class, Binding)args - the array stored as the binding variable argsrun Writes newValue to the attribute attribute on
object. A null receiver is replaced with
NullObject.getNullObject() before the write. A Class
receiver is tested before a GroovyObject and uses the meta-class
registered for that class. A GroovyObject uses the meta-class
from GroovyObject.getMetaClass. Every other receiver uses the
meta-class registered for its runtime class. The value is then
written with setAttribute on that meta-class.
object - the receiver, or nullattribute - the attribute namenewValue - the value to assignThis is so we don't have to reorder the stack when we call this method. At some point a better name might be in order.
Writes newValue to the property property on object.
A null receiver is replaced with NullObject.getNullObject()
before the write. A GroovyObject is tested before a Class
and is written through GroovyObject.setProperty.
A Class receiver uses the meta-class registered for that class.
Every other receiver uses
MetaClassRegistryImpl.getMetaClass and then
setProperty on that meta-class.
object - the receiver, or nullproperty - the property namenewValue - the value to assignThis is so we don't have to reorder the stack when we call this method. At some point a better name might be in order.
This is so we don't have to reorder the stack when we call this method. At some point a better name might be in order.
Builds a SpreadMap from value when value is a
Map. Entries are flattened, in key-set iteration order, into
an array that alternates key then value, and that array is passed to
the SpreadMap constructor. A non-map value throws
SpreadMapEvaluatingException. null does not reach that
throw: the message calls FormatHelper.typeName(value), which
dereferences value.
value - the map to spread Returns the bracketed rendering of arguments, using [
and ] boundaries, by calling
FormatHelper.toArrayString(Object[]).
FormatHelper.toArrayString(Object[])arguments - the array to render Returns the bracketed rendering of arguments, using [
and ] boundaries, by calling
FormatHelper.toArrayString(Object[], int, boolean).
Output stops after about maxSize characters and then appends
"..."; -1 means no limit. When safe is
true, an element whose rendering throws an Exception
uses a fallback representation.
FormatHelper.toArrayString(Object[], int, boolean)arguments - the array to rendermaxSize - the approximate character limit, or -1 for no limitsafe - true to substitute a fallback for an element that fails to render Returns the bracketed rendering of arg, using [ and
] boundaries, by calling FormatHelper.toListString(Collection).
FormatHelper.toListString(Collection)arg - the collection to render Returns the bracketed rendering of arg, using [ and
] boundaries, by calling
FormatHelper.toListString(Collection, int). Output stops after
about maxSize characters and then appends "...";
-1 means no limit.
FormatHelper.toListString(Collection, int)arg - the collection to rendermaxSize - the approximate character limit, or -1 for no limit Returns the bracketed rendering of arg, using [ and
] boundaries, by calling
FormatHelper.toListString(Collection, int, boolean).
Output stops after about maxSize characters and then appends
"..."; -1 means no limit. When safe is
true, an item whose rendering throws an Exception
uses a fallback representation.
FormatHelper.toListString(Collection, int, boolean)arg - the collection to rendermaxSize - the approximate character limit, or -1 for no limitsafe - true to substitute a fallback for an item that fails to render Returns the bracketed rendering of arg, using [ and
] boundaries, by calling FormatHelper.toMapString(Map).
FormatHelper.toMapString(Map)arg - the map to render Returns the bracketed rendering of arg, using [ and
] boundaries, by calling FormatHelper.toMapString(Map, int).
Output stops after about maxSize characters and then appends
"..."; -1 means no limit.
FormatHelper.toMapString(Map, int)arg - the map to rendermaxSize - the approximate character limit, or -1 for no limit Returns the string representation of arguments produced by
FormatHelper.toString(Object).
FormatHelper.toString(Object)arguments - the object to render Returns a comma-separated list of the runtime type names in
arguments, by calling FormatHelper.toTypeString(Object[]).
FormatHelper.toTypeString(Object[])arguments - the values whose types are rendered"null" when arguments is null Returns a comma-separated list of the runtime type names in
arguments, by calling FormatHelper.toTypeString(Object[], int).
Output stops after about maxSize characters and then appends
"..."; -1 means no limit.
FormatHelper.toTypeString(Object[], int)arguments - the values whose types are renderedmaxSize - the approximate character limit, or -1 for no limit Negates value. Integer, Long, Double,
and Float use arithmetic negation. Short and
Byte are negated and cast back to short and
byte. BigInteger and BigDecimal use
negate(). An ArrayList yields a new list of the
element-wise results. Any other value is passed to
invokeMethod(Object, String, Object) as the no-argument
method negative.
value - the value to negate Applies unary plus to value. Integer, Long,
BigInteger, BigDecimal, Double, Float,
Short, and Byte are returned unchanged. An
ArrayList yields a new list of the element-wise results.
Any other value is passed to invokeMethod(Object, String, Object)
as the no-argument method positive.
value - the value to which unary plus is applied Writes object to out by calling
FormatHelper.write(Writer, Object).
FormatHelper.write(Writer, Object)out - the writer that receives the textobject - the object to write