Class SInvocation


  • public class SInvocation
    extends java.lang.Object
    Factory for providing fast reflective access to methods.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.util.WeakHashMap<java.lang.Class<?>,​WeakKeyValueMap<java.lang.Class<?>,​java.lang.Class<?>>> ACCESSOR_CLASSES
      Cached accessor classes.
      static boolean DEFAULT_ACCESS
      Flag if default / protected access via ASM is available.
      protected static java.util.WeakHashMap<java.lang.Class<?>,​WeakKeyValueMap<java.lang.Class<?>,​java.lang.Class<?>>> EXTRACTOR_CLASSES
      Cached extractor classes.
      protected static java.util.WeakHashMap<java.lang.reflect.Method,​java.lang.Class<IMethodInvoker>> INVOKER_CLASSES
      Cached invoker classes, the invoker class does not prevent GC (tested).
      static java.util.concurrent.atomic.AtomicLong NAME_SUFFIX_COUNTER
      Class name suffix counter.
      static boolean PRIVATE_ACCESS
      Flag if private access via ASM is available.
    • Constructor Summary

      Constructors 
      Constructor Description
      SInvocation()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.lang.Class<T> createAccessorClass​(IByteCodeClassLoader cl, java.lang.Class<T> iface, java.lang.Class<?> clazz)
      Generates an accessor class based on an interface.
      protected static SInvocation.ExtendedClassWriter createClass​(java.lang.Class<?> targetclass, java.lang.String classname, int accesslevel, java.lang.Class<?>... interfaces)
      Creates the initial setup for a new class in ASM.
      static java.lang.Class<IExtractor> createExtractorClass​(IByteCodeClassLoader cl, java.lang.Class<?> clazz, java.lang.String[] propnames, java.lang.reflect.Member[] accessormember)
      Creates extractor class.
      protected static <T> T createFallbackAccessor​(java.lang.Class<T> iface, java.lang.Class<?> clazz, java.lang.Object obj)
      Implements an accessor based on a dynamic proxy.
      protected static java.lang.Class<IMethodInvoker> createInvokerClass​(IByteCodeClassLoader cl, java.lang.reflect.Method method)
      Creates the invoker class.
      protected static int determineAccessLevel​(int currentlevel, int modifiers)
      Determines the necessary access level based on the current access level.
      protected static void enableEnhancedAccess()
      Tries to enable enhanced direct access.
      static <T> java.lang.Class<T> getAccessorClass​(java.lang.Class<T> iface, java.lang.Class<?> targetclazz)
      Gets class for an interface-based accessor.
      static java.lang.Class<IMethodInvoker> getInvokerClass​(java.lang.reflect.Method method)
      Gets an invoker class.
      static java.lang.Object invoke​(java.lang.Object obj, java.lang.Class<?> clazz, java.lang.String methodname, java.lang.Object... args)
      Directly invokes a method based on the method name and arguments.
      static java.lang.Object invoke​(java.lang.Object obj, java.lang.String methodname, java.lang.Object... args)
      Directly invokes a method based on the method name and arguments.
      static <T> T newAccessor​(java.lang.Class<T> iface, java.lang.Class<?> targetclass, java.lang.Object delegate)
      Creates a new accessor from an accessor class.
      static IExtractor newExtractor​(java.lang.Class<IExtractor> extractorclass)  
      protected static IMethodInvoker newInvoker​(java.lang.Class<?> invokerclass)
      Instantiate a new method invoker from the invoker class.
      static IMethodInvoker newInvoker​(java.lang.reflect.Method method)
      Creates a new invoker for a method.
      static IMethodInvoker newInvoker​(java.lang.reflect.Method method, IByteCodeClassLoader cl)
      Creates a new invoker for a method.
      protected static void prepareParameters​(org.objectweb.asm.MethodVisitor mv, java.lang.Class<?>[] parameters)
      Readies the parameters for delegated method invocation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • NAME_SUFFIX_COUNTER

        public static java.util.concurrent.atomic.AtomicLong NAME_SUFFIX_COUNTER
        Class name suffix counter.
      • DEFAULT_ACCESS

        public static boolean DEFAULT_ACCESS
        Flag if default / protected access via ASM is available. Cannot be final due to bootstrapping.
      • PRIVATE_ACCESS

        public static boolean PRIVATE_ACCESS
        Flag if private access via ASM is available. Cannot be final due to bootstrapping.
      • INVOKER_CLASSES

        protected static volatile java.util.WeakHashMap<java.lang.reflect.Method,​java.lang.Class<IMethodInvoker>> INVOKER_CLASSES
        Cached invoker classes, the invoker class does not prevent GC (tested).
      • ACCESSOR_CLASSES

        protected static volatile java.util.WeakHashMap<java.lang.Class<?>,​WeakKeyValueMap<java.lang.Class<?>,​java.lang.Class<?>>> ACCESSOR_CLASSES
        Cached accessor classes.
      • EXTRACTOR_CLASSES

        protected static volatile java.util.WeakHashMap<java.lang.Class<?>,​WeakKeyValueMap<java.lang.Class<?>,​java.lang.Class<?>>> EXTRACTOR_CLASSES
        Cached extractor classes.
    • Constructor Detail

      • SInvocation

        public SInvocation()
    • Method Detail

      • invoke

        public static final java.lang.Object invoke​(java.lang.Object obj,
                                                    java.lang.String methodname,
                                                    java.lang.Object... args)
        Directly invokes a method based on the method name and arguments. Method resolution is very basic and a performance penalty is incurred, do not use if you plan on making repeated calls or need good method resolution, use getInvoker() methods instead.
        Parameters:
        obj - Object on which the method is to be called.
        methodname - Name of the methods.
        args - Invocation arguments.
        Returns:
        Return value of invocation.
      • invoke

        public static final java.lang.Object invoke​(java.lang.Object obj,
                                                    java.lang.Class<?> clazz,
                                                    java.lang.String methodname,
                                                    java.lang.Object... args)
        Directly invokes a method based on the method name and arguments. Method resolution is very basic and a performance penalty is incurred, do not use if you plan on making repeated calls or need good method resolution, use getInvoker() methods instead.
        Parameters:
        obj - Object on which the method is to be called.
        clazz - Class definition for static calls, can be null if obj is defined.
        methodname - Name of the method.
        args - Invocation arguments.
        Returns:
        Return value of invocation.
      • newInvoker

        public static final IMethodInvoker newInvoker​(java.lang.reflect.Method method)
        Creates a new invoker for a method.
        Parameters:
        method - The method.
        Returns:
        Instantiated invoker.
      • newInvoker

        public static final IMethodInvoker newInvoker​(java.lang.reflect.Method method,
                                                      IByteCodeClassLoader cl)
        Creates a new invoker for a method.
        Parameters:
        method - The method.
        cl - ClassLoader to use.
        Returns:
        Instantiated invoker.
      • newInvoker

        protected static final IMethodInvoker newInvoker​(java.lang.Class<?> invokerclass)
        Instantiate a new method invoker from the invoker class.
        Parameters:
        invokerclass - The invoker class.
        Returns:
        Instantiated invoker.
      • getInvokerClass

        public static final java.lang.Class<IMethodInvoker> getInvokerClass​(java.lang.reflect.Method method)
        Gets an invoker class.
        Parameters:
        clazz - The class used to map methods.
        methodidmap - The method ID map to store the mapping.
        Returns:
        The generated invoker.
      • createInvokerClass

        protected static final java.lang.Class<IMethodInvoker> createInvokerClass​(IByteCodeClassLoader cl,
                                                                                  java.lang.reflect.Method method)
        Creates the invoker class.
        Parameters:
        cl - ClassLoader to use for generated class.
        clazz - The class used to map methods.
        Returns:
        The generated invoker.
      • newAccessor

        public static final <T> T newAccessor​(java.lang.Class<T> iface,
                                              java.lang.Class<?> targetclass,
                                              java.lang.Object delegate)
        Creates a new accessor from an accessor class.
        Parameters:
        accessorclass - The accessor class.
        delegate - The delegation object / accessor target.
        Returns:
        Instantiated accessor.
      • getAccessorClass

        public static final <T> java.lang.Class<T> getAccessorClass​(java.lang.Class<T> iface,
                                                                    java.lang.Class<?> targetclazz)
        Gets class for an interface-based accessor.
        Parameters:
        iface - The accessor interface.
        targetclazz - The target class.
        Returns:
        The accessor class.
      • createAccessorClass

        public static final <T> java.lang.Class<T> createAccessorClass​(IByteCodeClassLoader cl,
                                                                       java.lang.Class<T> iface,
                                                                       java.lang.Class<?> clazz)
        Generates an accessor class based on an interface. Methods between the interface and the target class are matched.
        Parameters:
        cl - The ClassLoader used to load the generated byte code.
        iface - The accessor interface.
        clazz - The target class of the accessor.
        Returns:
        The generated class.
      • newExtractor

        public static final IExtractor newExtractor​(java.lang.Class<IExtractor> extractorclass)
      • createExtractorClass

        public static final java.lang.Class<IExtractor> createExtractorClass​(IByteCodeClassLoader cl,
                                                                             java.lang.Class<?> clazz,
                                                                             java.lang.String[] propnames,
                                                                             java.lang.reflect.Member[] accessormember)
        Creates extractor class.
        Parameters:
        cl - ClassLoader to use for generated class.
        clazz - The class used to map methods.
        Returns:
        The generated invoker.
      • createClass

        protected static final SInvocation.ExtendedClassWriter createClass​(java.lang.Class<?> targetclass,
                                                                           java.lang.String classname,
                                                                           int accesslevel,
                                                                           java.lang.Class<?>... interfaces)
        Creates the initial setup for a new class in ASM.
        Parameters:
        classname - Simple name of the class.
        privileged - If the class should be "privileged" to allow access to non-publics.
        Returns:
        Preinitialized class writer.
      • prepareParameters

        protected static final void prepareParameters​(org.objectweb.asm.MethodVisitor mv,
                                                      java.lang.Class<?>[] parameters)
        Readies the parameters for delegated method invocation. Converts primitives as appropriate.
        Parameters:
        mv - The MethodVisitor being used.
        parameters - The parameters of the method.
      • determineAccessLevel

        protected static final int determineAccessLevel​(int currentlevel,
                                                        int modifiers)
        Determines the necessary access level based on the current access level.
        Parameters:
        currentlevel - The current level.
        modifiers - Modifiers of the target
        Returns:
        Needed access level.
      • createFallbackAccessor

        protected static final <T> T createFallbackAccessor​(java.lang.Class<T> iface,
                                                            java.lang.Class<?> clazz,
                                                            java.lang.Object obj)
        Implements an accessor based on a dynamic proxy.
        Parameters:
        cl - ClassLoader to use.
        iface - The interface to implement.
        clazz - The target class.
        obj - The target class or null if all static.
        Returns:
        Accessor.
      • enableEnhancedAccess

        protected static final void enableEnhancedAccess()
        Tries to enable enhanced direct access.