problem in compiling already existing class

View: New views
2 Messages — Rating Filter:   Alert me  

problem in compiling already existing class

by Andrea Arcuri :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

I recently started to use Janino. It has worked well so far, but I get
problems when I try to compile a new class with an already existing
name. The compilation (seems to) works, but when I try to load the new
class I get a reference to the old one!

Is there any easy way to solve this problem, or do I have to put hand on
the Janino source code for changing it (unless of course there are some
JVM issues for which what I want to do is infeasible)?


at the bottom of the email there is a short program that shows what I am
talking about.

what I get is

#--
A:A
B:class A print: A

Compiling A...

A:A
B:class A print: A

Compiling C...

C:C
#--

whereas what I would expect is:

#--
A:A
B:class A print: A

Compiling A...

A:NEW VALUE THAT IS NOT DISPLAYED!
B:class A print: NEW VALUE THAT IS NOT DISPLAYED!

Compiling C...

C:C
#--


many thanks


Andrea


//---------------------------------------

import org.codehaus.janino.*;

import java.io.*;

public class Test
{
        public static void main(String[] args)
        {
                Test t = new Test();

                t.exe();
        }

        public void exe()
        {
                try{
               
                A a = new A();
                B b = new B();

                System.out.println("A:"+a+"\nB:"+b);

                //--------------------------
       
                System.out.println("\nCompiling A...\n");
               
                String source_a = "public class A{public String toString(){return
\"NEW VALUE THAT IS NOT DISPLAYED!\";}}";

                SimpleCompiler sc = new SimpleCompiler("A", (new StringReader(source_a)));
                       
                ClassLoader cl = sc.getClassLoader();
                       
                Object obj_a = cl.loadClass("A").newInstance();

                System.out.println("A:"+obj_a);

                Object obj_b = cl.loadClass("B").newInstance();

                System.out.println("B:"+obj_b);

                //------------------------

                System.out.println("\nCompiling C...\n");

                String source_c = "public class C{public String toString(){return
\"C\";}}";

                sc = new SimpleCompiler("C", (new StringReader(source_c)));
                       
                cl = sc.getClassLoader();
                       
                Object obj_c = cl.loadClass("C").newInstance();

                System.out.println("C:"+obj_c);
                       
                }
                catch(Exception e)
                {
                        System.out.println(e);
                }
        }
}

class A
{
        public String toString()
        {
                        return "A";
        }
}

class B
{
        public String toString()
        {
                return "class A print: "+(new A()).toString();
        }
}


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: problem in compiling already existing class

by Arno Unkrig :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Andrea,

there is NO way to replace a class that has been loaded into a Java
ClassLoader with another class with the same name. This limitation is
inherent to Java's ClassLoader concept. Since your classes A and B are
on the JVM's classpath, they are loaded into the system classloader, and
the SimpleCompiler class loader cannot replace them.

I'm not exactly sure what you're trying to achieve, but... if you
compile some classes (through ExpressionEvaluator, ScriptEvaluator,
ClassBodyEvaluator, SimpleCompiler, JavaSourceClassLoader), and at some
later time decide that you want to compile them anew, then you just give
up on the EE/SE/CBE/SC/JSCL and create a new one.


Hope that helps!


CU

Arno

Andrea Arcuri schrieb:

> Hi!
>
> I recently started to use Janino. It has worked well so far, but I get
> problems when I try to compile a new class with an already existing
> name. The compilation (seems to) works, but when I try to load the new
> class I get a reference to the old one!
>
> Is there any easy way to solve this problem, or do I have to put hand on
> the Janino source code for changing it (unless of course there are some
> JVM issues for which what I want to do is infeasible)?
>
>
> at the bottom of the email there is a short program that shows what I am
> talking about.
>
> what I get is
>
> #--
> A:A
> B:class A print: A
>
> Compiling A...
>
> A:A
> B:class A print: A
>
> Compiling C...
>
> C:C
> #--
>
> whereas what I would expect is:
>
> #--
> A:A
> B:class A print: A
>
> Compiling A...
>
> A:NEW VALUE THAT IS NOT DISPLAYED!
> B:class A print: NEW VALUE THAT IS NOT DISPLAYED!
>
> Compiling C...
>
> C:C
> #--
>
>
> many thanks
>
>
> Andrea
>
>
> //---------------------------------------
>
> import org.codehaus.janino.*;
>
> import java.io.*;
>
> public class Test
> {
>     public static void main(String[] args)
>     {
>         Test t = new Test();
>
>         t.exe();  
>     }
>
>     public void exe()
>     {
>         try{
>        
>         A a = new A();
>         B b = new B();
>
>         System.out.println("A:"+a+"\nB:"+b);
>
>         //--------------------------
>    
>         System.out.println("\nCompiling A...\n");
>        
>         String source_a = "public class A{public String toString(){return
> \"NEW VALUE THAT IS NOT DISPLAYED!\";}}";
>
>         SimpleCompiler sc = new SimpleCompiler("A", (new
> StringReader(source_a)));
>            
>         ClassLoader cl = sc.getClassLoader();
>            
>         Object obj_a = cl.loadClass("A").newInstance();
>
>         System.out.println("A:"+obj_a);
>
>         Object obj_b = cl.loadClass("B").newInstance();
>
>         System.out.println("B:"+obj_b);          
>
>         //------------------------
>
>         System.out.println("\nCompiling C...\n");
>
>         String source_c = "public class C{public String toString(){return
> \"C\";}}";
>
>         sc = new SimpleCompiler("C", (new StringReader(source_c)));
>            
>         cl = sc.getClassLoader();
>            
>         Object obj_c = cl.loadClass("C").newInstance();
>
>         System.out.println("C:"+obj_c);
>            
>         }
>         catch(Exception e)
>         {
>             System.out.println(e);
>         }
>     }  
> }
>
> class A
> {
>     public String toString()
>     {
>             return "A";  
>     }
> }
>
> class B
> {
>     public String toString()
>     {
>         return "class A print: "+(new A()).toString();  
>     }
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email