@Test public void testCreateClass() throws Exception { System.out.println( "createClass" ); String pkg = DefinitionBasedMigrator.class.getPackage().getName(); String fullName = pkg + ".FooMigrator"; Class cls = MigratorSubclassMaker.createClass( fullName ); assertTrue( DefinitionBasedMigrator.class.isAssignableFrom( cls ) ); assertEquals( fullName, cls.getName() ); assertEquals( pkg, cls.getPackage().getName() ); Class<? extends DefinitionBasedMigrator> migCls = cls; }
static Class<? extends DefinitionBasedMigrator> createClass( String fullName ) throws NotFoundException, CannotCompileException, MigrationException { log.debug("Creating class " + fullName); ClassPool pool = ClassPool.getDefault(); URL cls = pool.find( fullName ); if( cls != null ){ throw new MigrationException("Class already exists: " + fullName + "\n Maybe you already have a migrator of that name?"); } try { Class.forName( fullName ); throw new MigrationException("Class already exists: " + fullName + "\n Maybe you already have a migrator of that name?"); } catch ( ClassNotFoundException ex ){ } CtClass subClass = pool.makeClass( fullName ); final CtClass superClass = pool.get( DefinitionBasedMigrator.class.getName() ); subClass.setSuperclass( superClass ); subClass.setModifiers( Modifier.PUBLIC ); CtClass[] params = new CtClass[]{ pool.get( MigratorDefinition.class.getName() ), pool.get( GlobalConfiguration.class.getName()) }; final CtConstructor ctor = CtNewConstructor.make( params, null, CtNewConstructor.PASS_PARAMS, null, null, subClass ); subClass.addConstructor( ctor ); return subClass.toClass(); }
MigratorSubclassMaker { static Class<? extends DefinitionBasedMigrator> createClass( String fullName ) throws NotFoundException, CannotCompileException, MigrationException { log.debug("Creating class " + fullName); ClassPool pool = ClassPool.getDefault(); URL cls = pool.find( fullName ); if( cls != null ){ throw new MigrationException("Class already exists: " + fullName + "\n Maybe you already have a migrator of that name?"); } try { Class.forName( fullName ); throw new MigrationException("Class already exists: " + fullName + "\n Maybe you already have a migrator of that name?"); } catch ( ClassNotFoundException ex ){ } CtClass subClass = pool.makeClass( fullName ); final CtClass superClass = pool.get( DefinitionBasedMigrator.class.getName() ); subClass.setSuperclass( superClass ); subClass.setModifiers( Modifier.PUBLIC ); CtClass[] params = new CtClass[]{ pool.get( MigratorDefinition.class.getName() ), pool.get( GlobalConfiguration.class.getName()) }; final CtConstructor ctor = CtNewConstructor.make( params, null, CtNewConstructor.PASS_PARAMS, null, null, subClass ); subClass.addConstructor( ctor ); return subClass.toClass(); } }
MigratorSubclassMaker { static Class<? extends DefinitionBasedMigrator> createClass( String fullName ) throws NotFoundException, CannotCompileException, MigrationException { log.debug("Creating class " + fullName); ClassPool pool = ClassPool.getDefault(); URL cls = pool.find( fullName ); if( cls != null ){ throw new MigrationException("Class already exists: " + fullName + "\n Maybe you already have a migrator of that name?"); } try { Class.forName( fullName ); throw new MigrationException("Class already exists: " + fullName + "\n Maybe you already have a migrator of that name?"); } catch ( ClassNotFoundException ex ){ } CtClass subClass = pool.makeClass( fullName ); final CtClass superClass = pool.get( DefinitionBasedMigrator.class.getName() ); subClass.setSuperclass( superClass ); subClass.setModifiers( Modifier.PUBLIC ); CtClass[] params = new CtClass[]{ pool.get( MigratorDefinition.class.getName() ), pool.get( GlobalConfiguration.class.getName()) }; final CtConstructor ctor = CtNewConstructor.make( params, null, CtNewConstructor.PASS_PARAMS, null, null, subClass ); subClass.addConstructor( ctor ); return subClass.toClass(); }  }
MigratorSubclassMaker { static Class<? extends DefinitionBasedMigrator> createClass( String fullName ) throws NotFoundException, CannotCompileException, MigrationException { log.debug("Creating class " + fullName); ClassPool pool = ClassPool.getDefault(); URL cls = pool.find( fullName ); if( cls != null ){ throw new MigrationException("Class already exists: " + fullName + "\n Maybe you already have a migrator of that name?"); } try { Class.forName( fullName ); throw new MigrationException("Class already exists: " + fullName + "\n Maybe you already have a migrator of that name?"); } catch ( ClassNotFoundException ex ){ } CtClass subClass = pool.makeClass( fullName ); final CtClass superClass = pool.get( DefinitionBasedMigrator.class.getName() ); subClass.setSuperclass( superClass ); subClass.setModifiers( Modifier.PUBLIC ); CtClass[] params = new CtClass[]{ pool.get( MigratorDefinition.class.getName() ), pool.get( GlobalConfiguration.class.getName()) }; final CtConstructor ctor = CtNewConstructor.make( params, null, CtNewConstructor.PASS_PARAMS, null, null, subClass ); subClass.addConstructor( ctor ); return subClass.toClass(); }   }
MigratorSubclassMaker { static Class<? extends DefinitionBasedMigrator> createClass( String fullName ) throws NotFoundException, CannotCompileException, MigrationException { log.debug("Creating class " + fullName); ClassPool pool = ClassPool.getDefault(); URL cls = pool.find( fullName ); if( cls != null ){ throw new MigrationException("Class already exists: " + fullName + "\n Maybe you already have a migrator of that name?"); } try { Class.forName( fullName ); throw new MigrationException("Class already exists: " + fullName + "\n Maybe you already have a migrator of that name?"); } catch ( ClassNotFoundException ex ){ } CtClass subClass = pool.makeClass( fullName ); final CtClass superClass = pool.get( DefinitionBasedMigrator.class.getName() ); subClass.setSuperclass( superClass ); subClass.setModifiers( Modifier.PUBLIC ); CtClass[] params = new CtClass[]{ pool.get( MigratorDefinition.class.getName() ), pool.get( GlobalConfiguration.class.getName()) }; final CtConstructor ctor = CtNewConstructor.make( params, null, CtNewConstructor.PASS_PARAMS, null, null, subClass ); subClass.addConstructor( ctor ); return subClass.toClass(); }    }