ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gurdev Parmar" <gpar...@versant.com>
Subject Re: AntClassLoader leads to ClassCastException
Date Mon, 04 Mar 2002 22:03:32 GMT
Thanks for your inputs, Stefan.

----- Original Message -----
From: "Stefan Bodewig" <bodewig@apache.org>
To: <ant-user@jakarta.apache.org>
Sent: Monday, March 04, 2002 8:05 AM
Subject: Re: AntClassLoader leads to ClassCastException


> Ok, here is the deal:
>
> Your class Driver will be loaded by an instance of AntClassLoader
> because of your taskdef.  This classloader will also load class A and
> determines what Driver is willing to accept as an A.
>
> In execute, you create a new AntClassLoader and use this to load class
> B which in turn loads A again - this class loader is different from
> the one Driver will accept, and there things break down.
>
> So you are in fact using two different instances of AntClassLoader in
> your task.
>
> Your options include:
>
> (1) Don't put any references to A or B into your task implementation.
>
> (2) Apply the patch below (the first two hunks are not that
> important), which is going to fix the problem of this particular
> exemple, but may not be applicable to your real application.
>
> Stefan
>
> diff -urb cl.orig/src_and_compile_dir/B.java cl/src_and_compile_dir/B.java
> --- cl.orig/src_and_compile_dir/B.java Fri Mar  1 21:06:54 2002
> +++ cl/src_and_compile_dir/B.java Mon Mar  4 16:44:41 2002
> @@ -2,6 +2,6 @@
>  {
>      void print()
>      {
> - System.out.println("Print called from A");
> + System.out.println("Print called from B");
>      }
>  }
> diff -urb cl.orig/src_and_compile_dir/Driver.java
cl/src_and_compile_dir/Driver.java
> --- cl.orig/src_and_compile_dir/Driver.java Fri Mar  1 21:10:02 2002
> +++ cl/src_and_compile_dir/Driver.java Mon Mar  4 17:01:52 2002
> @@ -13,7 +13,7 @@
>   this.classpath = clp;
>      }
>
> -    public void AddClasspath(Path classpath) {
> +    public void addClasspath(Path classpath) {
>          if (classpath == null) {
>              this.classpath = classpath;
>          }
> @@ -22,11 +22,10 @@
>      // The method executing the task
>      public void execute() throws BuildException
>      {
> - AntClassLoader acl = new AntClassLoader(project, classpath, true);
>   try {
>       A a = null;
>       System.out.println("1");
> -     a = (A) createDynamically("B", acl);
> +     a = (A) createDynamically("B", getClass().getClassLoader());
>       System.out.println("2");
>       a.print();
>   } catch(Exception e)
> @@ -35,7 +34,7 @@
>       }
>      }
>
> -    private Object createDynamically (String className, AntClassLoader
cl) throws Exception  {
> +    private Object createDynamically (String className, ClassLoader cl)
throws Exception  {
>          Class c = Class.forName(className, true, cl);
>   AntClassLoader.initializeClass(c);
>   Object o = c.newInstance();
>
> --
> To unsubscribe, e-mail:   <mailto:ant-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:ant-user-help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <mailto:ant-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-user-help@jakarta.apache.org>


Mime
View raw message