public static void add ( short magic , Class clazz ) { if ( magic < MIN_CUSTOM_MAGIC_NUMBER ) throw new IllegalArgumentException ( "magic ID (" + magic + ") must be >= " + MIN_CUSTOM_MAGIC_NUMBER ) ; if ( magicMapUser . containsKey ( magic ) || classMap . containsKey ( clazz ) ) alreadyInMagicMap ( magic , clazz . getName ( ) ) ; Object inst = null ; try { inst = clazz . getDeclaredConstructor ( ) . newInstance ( ) ; } catch ( Exception e ) { throw new IllegalStateException ( "failed creating instance " + clazz , e ) ; } Object val = clazz ; if ( Header . class . isAssignableFrom ( clazz ) ) { // class is a header checkSameId ( ( Header ) inst , magic ) ; val = ( ( Header ) inst ) . create ( ) ; } if ( Constructable . class . isAssignableFrom ( clazz ) ) { val = ( ( Constructable ) inst ) . create ( ) ; inst = ( ( Supplier < ? > ) val ) . get ( ) ; if ( ! inst . getClass ( ) . equals ( clazz ) ) throw new IllegalStateException ( String . format ( "%s.create() returned the wrong class: %s\n" , clazz . getSimpleName ( ) , inst . getClass ( ) . getSimpleName ( ) ) ) ; } magicMapUser . put ( magic , val ) ; classMap . put ( clazz , magic ) ; }
Method to register a user - defined header with jg - magic - map at runtime