package com.jentfoo.classloading;

import org.junit.Test;
import org.threadly.util.Clock;

public class LoadClassesTest {
  @Test
  public void loadClasses() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    long start = Clock.accurateForwardProgressingMillis();
    for (int i = 0; i < GenerateClasses.CLASS_COUNT; i++) {
      @SuppressWarnings("unchecked")
      Class<? extends AbstractGenerated> c =
          (Class<? extends AbstractGenerated>) Class.forName(this.getClass().getPackage().getName() +
                                                               ".generated." +
                                                               GenerateClasses.CLASS_PREFIX + i);
      @SuppressWarnings("unused")
      AbstractGenerated genClass = c.newInstance();
      genClass.foo();
    }
    System.out.println("---> run time: " + (Clock.accurateForwardProgressingMillis() - start));
  }
}

@@ -360,144 +360,52 @@
     
-@SuppressWarnings(%22unchecked%22)%0A      Class%3C? extends AbstractGenerated%3E c =%0A          (Class%3C? extends AbstractGenerated%3E) Class.forName
+ClassLoader.getSystemClassLoader().loadClass
 (thi
@@ -495,26 +495,16 @@
         
-          
 %22.genera
@@ -514,212 +514,41 @@
 .%22 +
-%0A                                                               GenerateClasses.CLASS_PREFIX + i);%0A      @SuppressWarnings(%22unused%22)%0A      AbstractGenerated genClass = c.newInstance();%0A      genClass.foo(
+ GenerateClasses.CLASS_PREFIX + i
 );%0A 

Use ClassLoader rather than Class.forName