private static < T > T instantiateObject ( String flagName , String objectType , Class < T > clazz , PluginLoader loader , String instanceClassName ) { try { return loader . loadPlugin ( instanceClassName ) . asSubclass ( clazz ) . getConstructor ( ) . newInstance ( ) ; } catch ( ClassCastException cce ) { throw new CommandLineError ( String . format ( "%s \"%s\" is not a subclass of %s. Classes passed to %s should be %ss. " + "Did you pass it to the wrong flag?" , objectType , instanceClassName , clazz . getSimpleName ( ) , flagName , clazz . getSimpleName ( ) ) , cce ) ; } catch ( ReflectiveOperationException e ) { throw new CommandLineError ( String . format ( "Cannot instantiate %s \"%s\" registered with flag %s. Please make " + "sure that the %s exists and is on the compiler classpath and has a public " + "zero arguments constructor." , objectType , instanceClassName , flagName , objectType ) , e ) ; } catch ( ExceptionInInitializerError e ) { throw new CommandLineError ( String . format ( "Cannot instantiate %s \"%s\" registered with flag %s. An error was thrown while " + "loading the class. There is a bug in the implementation." , objectType , instanceClassName , flagName ) , e ) ; } catch ( SecurityException e ) { throw new CommandLineError ( String . format ( "Cannot instantiate %s \"%s\" registered with flag %s. A security manager is " + "preventing instantiation." , objectType , instanceClassName , flagName ) , e ) ; } }
Private helper for creating objects from flags .