Class ClassCopyProvider<T>


  • public class ClassCopyProvider<T>
    extends java.lang.Object
    Helper to create multiple copies of a class.

    The copies of the class have individual copies of the byte code. The JIT compiler optimizes the class copies individually, which can increase performance.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private ClassCopier<T> copier  
      private java.util.Map<java.lang.Object,​java.lang.Class<? extends T>> map  
      private java.lang.Class<?>[] signature  
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassCopyProvider​(java.lang.Class<? extends T> clazz, java.lang.Class<T> interfaceOfClazz, java.lang.Class<?>... constructorSignature)
      Create a ClassCopyProvider to make copy a a given class.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.Class<?>[] assumeConstructorSignature​(java.lang.Class<?> clazz)  
      private java.lang.Class<? extends T> classForKey​(java.lang.Object key)  
      private static boolean hasDefaultConstructor​(java.lang.Class<?> clazz)  
      boolean matches​(java.lang.Object... parameters)
      Returns true if the given list of objects, matches the constructor signature provided to ClassCopyProvider(Class, Class, Class[]).
      T newInstanceForKey​(java.lang.Object key, java.lang.Object... parameters)
      Returns an instance of a copy of the original class, that has been provided to the constructor.
      • Methods inherited from class java.lang.Object

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

      • map

        private final java.util.Map<java.lang.Object,​java.lang.Class<? extends T>> map
      • signature

        private final java.lang.Class<?>[] signature
    • Constructor Detail

      • ClassCopyProvider

        public ClassCopyProvider​(java.lang.Class<? extends T> clazz,
                                 java.lang.Class<T> interfaceOfClazz,
                                 java.lang.Class<?>... constructorSignature)
        Create a ClassCopyProvider to make copy a a given class.
        Parameters:
        clazz - Class to be copied.
        interfaceOfClazz - Interface that's implemented by the clazz. A class copies can only be used through this interface.
        constructorSignature - Constructor signature to be used when creating a new instance with newInstanceForKey(java.lang.Object, java.lang.Object...)
    • Method Detail

      • assumeConstructorSignature

        private static java.lang.Class<?>[] assumeConstructorSignature​(java.lang.Class<?> clazz)
      • hasDefaultConstructor

        private static boolean hasDefaultConstructor​(java.lang.Class<?> clazz)
      • classForKey

        private java.lang.Class<? extends T> classForKey​(java.lang.Object key)
      • matches

        public boolean matches​(java.lang.Object... parameters)
        Returns true if the given list of objects, matches the constructor signature provided to ClassCopyProvider(Class, Class, Class[]).
      • newInstanceForKey

        public T newInstanceForKey​(java.lang.Object key,
                                   java.lang.Object... parameters)
        Returns an instance of a copy of the original class, that has been provided to the constructor.

        Note: The returned object isn't an instance of the original class. Class.isInstance(Object) and "instanceof" will return false. And ClassCastException might occur if this is ignored.

        The returned instance has a copy of the byte code of the original class. This byte code is independently optimised by the JIT compiler. The JIT compiler will optimise the byte code, if it's only used in a specific use case. If used wisely a performance increase is the consequence.

        Parameters:
        key - Instances created with the same key, share their byte code.
        parameters - Parameters that are passed to the constructor.