- java.lang.Object
-
- jdk.dynalink.beans.BeansLinker
-
- All Implemented Interfaces:
GuardingDynamicLinker
public class BeansLinker extends Object implements GuardingDynamicLinker
A linker for ordinary Java objects. Normally used as the ultimate fallback linker by theDynamicLinkerFactory
so it is given the chance to link calls to all objects that no other linker recognized. Specifically, this linker will:- expose all public methods of form
setXxx()
,getXxx()
, andisXxx()
as property setters and getters forStandardOperation.SET
andStandardOperation.GET
operations in theStandardNamespace.PROPERTY
namespace; - expose all public methods for retrieval for
StandardOperation.GET
operation in theStandardNamespace.METHOD
namespace; the methods thus retrieved can then be invoked usingStandardOperation.CALL
. - expose all public fields as properties, unless there are getters or setters for the properties of the same name;
- expose elements of native Java arrays,
List
andMap
objects asStandardOperation.GET
andStandardOperation.SET
operations in theStandardNamespace.ELEMENT
namespace; - expose removal of elements of
List
andMap
objects asStandardOperation.REMOVE
operation in theStandardNamespace.ELEMENT
namespace; - expose a virtual property named
length
on Java arrays,Collection
andMap
objects; - expose
StandardOperation.NEW
on instances ofStaticClass
as calls to constructors, including those static class objects that represent Java arrays (their constructors take a singleint
parameter representing the length of the array to create); - expose static methods, fields, and properties of classes in a similar
manner to how instance method, fields, and properties are exposed, on
StaticClass
objects. - expose a virtual property named
static
on instances ofClass
to access theirStaticClass
.
Overloaded method resolution is performed automatically for property setters, methods, and constructors. Additionally, manual overloaded method selection is supported by having a call site specify a name for a method that contains an explicit signature, e.g.
StandardOperation.GET.withNamespace(METHOD).named("parseInt(String,int)")
You can use non-qualified class names in such signatures regardless of those classes' packages, they will match any class with the same non-qualified name. You only have to use a fully qualified class name in case non-qualified class names would cause selection ambiguity (that is extremely rare). Overloaded resolution for constructors is not automatic as there is no logical place to attach that functionality to but if a language wishes to provide this functionality, it can usegetConstructorMethod(Class, String)
as a useful building block for it.Variable argument invocation is handled for both methods and constructors.
Caller sensitive methods can be linked as long as they are otherwise public and link requests have call site descriptors carrying full-strength
MethodHandles.Lookup
objects and not weakened lookups or the public lookup.The behavior for handling missing members can be customized by passing a
MissingMemberHandlerFactory
to theconstructor
.The class also exposes various methods for discovery of available property and method names on classes and class instances, as well as access to per-class linkers using the
getLinkerForClass(Class)
method.
-
-
Constructor Summary
Constructors Constructor Description BeansLinker()
Creates a new beans linker.BeansLinker(MissingMemberHandlerFactory missingMemberHandlerFactory)
Creates a new beans linker with the specified factory for creating missing member handlers.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Object
getConstructorMethod(Class<?> clazz, String signature)
Return the dynamic method of constructor of the given class and the given signature.static Set<String>
getInstanceMethodNames(Class<?> clazz)
Returns a set of names of all instance methods of a class.TypeBasedGuardingDynamicLinker
getLinkerForClass(Class<?> clazz)
Returns a bean linker for a particular single class.static Set<String>
getReadableInstancePropertyNames(Class<?> clazz)
Returns a set of names of all readable instance properties of a class.static Set<String>
getReadableStaticPropertyNames(Class<?> clazz)
Returns a set of names of all readable static properties of a class.static Set<String>
getStaticMethodNames(Class<?> clazz)
Returns a set of names of all static methods of a class.static Set<String>
getWritableInstancePropertyNames(Class<?> clazz)
Returns a set of names of all writable instance properties of a class.static Set<String>
getWritableStaticPropertyNames(Class<?> clazz)
Returns a set of names of all writable static properties of a class.static boolean
isDynamicConstructor(Object obj)
Returns true if the object is a Java constructor (obtained throughgetConstructorMethod(Class, String)
}.static boolean
isDynamicMethod(Object obj)
Returns true if the object is a Java dynamic method (e.g., one obtained through aGET:METHOD
operation on a Java object orStaticClass
or throughgetConstructorMethod(Class, String)
.-
Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods declared in interface jdk.dynalink.linker.GuardingDynamicLinker
getGuardedInvocation
-
-
-
-
Constructor Detail
-
BeansLinker
public BeansLinker()
Creates a new beans linker. Equivalent toBeansLinker(MissingMemberHandlerFactory)
withnull
passed as the missing member handler factory, resulting in the default behavior for linking and evaluating missing members.
-
BeansLinker
public BeansLinker(MissingMemberHandlerFactory missingMemberHandlerFactory)
Creates a new beans linker with the specified factory for creating missing member handlers. The passed factory can be null if the default behavior is adequate. SeeMissingMemberHandlerFactory
for details.- Parameters:
missingMemberHandlerFactory
- a factory for creating handlers for operations on missing members.
-
-
Method Detail
-
getLinkerForClass
public TypeBasedGuardingDynamicLinker getLinkerForClass(Class<?> clazz)
Returns a bean linker for a particular single class. Useful when you need to override or extend the behavior of linking for some classes in your language runtime's linker, but still want to delegate to the default behavior in some cases.- Parameters:
clazz
- the class- Returns:
- a bean linker for that class
-
isDynamicMethod
public static boolean isDynamicMethod(Object obj)
Returns true if the object is a Java dynamic method (e.g., one obtained through aGET:METHOD
operation on a Java object orStaticClass
or throughgetConstructorMethod(Class, String)
.- Parameters:
obj
- the object we want to test for being a Java dynamic method.- Returns:
- true if it is a dynamic method, false otherwise.
-
isDynamicConstructor
public static boolean isDynamicConstructor(Object obj)
Returns true if the object is a Java constructor (obtained throughgetConstructorMethod(Class, String)
}.- Parameters:
obj
- the object we want to test for being a Java constructor.- Returns:
- true if it is a constructor, false otherwise.
-
getConstructorMethod
public static Object getConstructorMethod(Class<?> clazz, String signature)
Return the dynamic method of constructor of the given class and the given signature. This method is useful for exposing a functionality for selecting an overloaded constructor based on an explicit signature, as this functionality is not otherwise exposed by Dynalink asStaticClass
objects act as overloaded constructors without explicit signature selection. Example usage would be:getConstructorMethod(java.awt.Color.class, "int, int, int")
.- Parameters:
clazz
- the classsignature
- full signature of the constructor. Note how you can use names of primitive types, array names with normal Java notation (e.g."int[]"
), and normally you can even use unqualified class names (e.g."String, List"
instead of"java.lang.String, java.util.List"
as long as they don't cause ambiguity in the specific parameter position.- Returns:
- dynamic method for the constructor or null if no constructor with the specified signature exists.
-
getReadableInstancePropertyNames
public static Set<String> getReadableInstancePropertyNames(Class<?> clazz)
Returns a set of names of all readable instance properties of a class.- Parameters:
clazz
- the class- Returns:
- a set of names of all readable instance properties of a class.
-
getWritableInstancePropertyNames
public static Set<String> getWritableInstancePropertyNames(Class<?> clazz)
Returns a set of names of all writable instance properties of a class.- Parameters:
clazz
- the class- Returns:
- a set of names of all writable instance properties of a class.
-
getInstanceMethodNames
public static Set<String> getInstanceMethodNames(Class<?> clazz)
Returns a set of names of all instance methods of a class.- Parameters:
clazz
- the class- Returns:
- a set of names of all instance methods of a class.
-
getReadableStaticPropertyNames
public static Set<String> getReadableStaticPropertyNames(Class<?> clazz)
Returns a set of names of all readable static properties of a class.- Parameters:
clazz
- the class- Returns:
- a set of names of all readable static properties of a class.
-
getWritableStaticPropertyNames
public static Set<String> getWritableStaticPropertyNames(Class<?> clazz)
Returns a set of names of all writable static properties of a class.- Parameters:
clazz
- the class- Returns:
- a set of names of all writable static properties of a class.
-
-