approach here uses newInstance to achieve initialisation for JDK 1.1 compatability. Under JDK 1.2, both initialization and the classloader to use can be specified to Class.forName(); git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269021 13f79535-47bb-0310-9956-ffa450edef68master
@@ -159,7 +159,22 @@ public class AntClassLoader extends ClassLoader { | |||||
public void setIsolated(boolean isolated) { | public void setIsolated(boolean isolated) { | ||||
ignoreBase = isolated; | ignoreBase = isolated; | ||||
} | } | ||||
/** | |||||
* Force initialization of a class in a JDK 1.1 compatible, albeit hacky | |||||
* way | |||||
*/ | |||||
static public void initializeClass(Class theClass) { | |||||
// ***HACK*** We try to create an instance to force the VM to run the | |||||
// class' static initializer. We don't care if the instance can't | |||||
// be created - we are just interested in the side effect. | |||||
try { | |||||
theClass.newInstance(); | |||||
} | |||||
catch (Exception e) { | |||||
//ignore - our work is done | |||||
} | |||||
} | |||||
/** | /** | ||||
* Add a package root to the list of packages which must be loaded on the | * Add a package root to the list of packages which must be loaded on the | ||||
@@ -382,8 +382,8 @@ public class Main { | |||||
try { | try { | ||||
addBuildListeners(project); | addBuildListeners(project); | ||||
project.fireBuildStarted(); | |||||
project.fireBuildStarted(); | |||||
project.init(); | project.init(); | ||||
// set user-define properties | // set user-define properties | ||||
@@ -122,6 +122,7 @@ public class ExecuteJava { | |||||
AntClassLoader loader = new AntClassLoader(project, classpath, false); | AntClassLoader loader = new AntClassLoader(project, classpath, false); | ||||
loader.setIsolated(true); | loader.setIsolated(true); | ||||
target = loader.forceLoadClass(classname); | target = loader.forceLoadClass(classname); | ||||
AntClassLoader.initializeClass(target); | |||||
} | } | ||||
final Method main = target.getMethod("main", param); | final Method main = target.getMethod("main", param); | ||||
main.invoke(null, argument); | main.invoke(null, argument); | ||||
@@ -109,6 +109,7 @@ public class Taskdef extends Task { | |||||
Class taskClass = null; | Class taskClass = null; | ||||
if (loader != null) { | if (loader != null) { | ||||
taskClass = loader.loadClass(value); | taskClass = loader.loadClass(value); | ||||
AntClassLoader.initializeClass(taskClass); | |||||
} else { | } else { | ||||
taskClass = Class.forName(value); | taskClass = Class.forName(value); | ||||
} | } | ||||
@@ -265,7 +265,7 @@ public class XMLValidateTask extends Task { | |||||
AntClassLoader loader = new AntClassLoader(project, classpath, false); | AntClassLoader loader = new AntClassLoader(project, classpath, false); | ||||
loader.addSystemPackageRoot("org.xml"); // needed to avoid conflict | loader.addSystemPackageRoot("org.xml"); // needed to avoid conflict | ||||
readerClass = loader.loadClass(readerClassName); | readerClass = loader.loadClass(readerClassName); | ||||
//readerImpl = loader.loadClass( | |||||
AntClassLoader.initializeClass(readerClass); | |||||
} else | } else | ||||
readerClass = Class.forName(readerClassName); | readerClass = Class.forName(readerClassName); | ||||
@@ -54,8 +54,7 @@ | |||||
package org.apache.tools.ant.taskdefs.optional.junit; | package org.apache.tools.ant.taskdefs.optional.junit; | ||||
import org.apache.tools.ant.BuildException; | |||||
import org.apache.tools.ant.Project; | |||||
import org.apache.tools.ant.*; | |||||
import junit.framework.*; | import junit.framework.*; | ||||
import java.lang.reflect.*; | import java.lang.reflect.*; | ||||
@@ -159,6 +158,7 @@ public class JUnitTestRunner implements TestListener { | |||||
testClass = Class.forName(test.getName()); | testClass = Class.forName(test.getName()); | ||||
} else { | } else { | ||||
testClass = loader.loadClass(test.getName()); | testClass = loader.loadClass(test.getName()); | ||||
AntClassLoader.initializeClass(testClass); | |||||
} | } | ||||
Method suiteMethod = null; | Method suiteMethod = null; | ||||
@@ -203,6 +203,7 @@ public class Mapper extends DataType { | |||||
} else { | } else { | ||||
AntClassLoader al = new AntClassLoader(p, classpath); | AntClassLoader al = new AntClassLoader(p, classpath); | ||||
c = al.loadClass(classname); | c = al.loadClass(classname); | ||||
AntClassLoader.initializeClass(c); | |||||
} | } | ||||
FileNameMapper m = (FileNameMapper) c.newInstance(); | FileNameMapper m = (FileNameMapper) c.newInstance(); | ||||