Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34671 > unrolled thread

How to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes?

Started bykant kodali <kanth909@gmail.com>
First post2016-06-20 00:42 -0700
Last post2016-07-10 20:12 -0400
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes? kant kodali <kanth909@gmail.com> - 2016-06-20 00:42 -0700
    Re: How to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes? Lew <lewbloch@gmail.com> - 2016-06-20 11:04 -0700
    Re: How to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes? Arne Vajhøj <arne@vajhoej.dk> - 2016-07-10 20:12 -0400

#34671 — How to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes?

Fromkant kodali <kanth909@gmail.com>
Date2016-06-20 00:42 -0700
SubjectHow to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes?
Message-ID<ee393144-64b8-4f19-8597-e139f401db85@googlegroups.com>
    public class MyClassLoader extends URLClassLoader {
    
        public MyClassLoader() throws Exception{
             super(new URL[]{new URL("file:///home/ubuntu/java/hello.jar")});
        }
    
        @Override
        public Class loadClass(String name) throws ClassNotFoundException {
            if (name.equals("hello.example.org.Foo") || name.equals("hello.example.org.Bar")) 
            {
                System.out.println("Foo or Bar is loaded");
            }
            return super.loadClass(name);
    
        }

}

I just want to execute a block of code say a print statement prior to loading certain classes and the above code doesn't quite work and I am not sure where the error is?

[toc] | [next] | [standalone]


#34676

FromLew <lewbloch@gmail.com>
Date2016-06-20 11:04 -0700
Message-ID<cdb2d366-a6a6-446b-abf2-db98908bafe5@googlegroups.com>
In reply to#34671
On Monday, June 20, 2016 at 12:42:25 AM UTC-7, kant kodali wrote:
> public class MyClassLoader extends URLClassLoader {
>     
>         public MyClassLoader() throws Exception{
>              super(new URL[]{new URL("file:///home/ubuntu/java/hello.jar")});
>         }
>     
>         @Override
>         public Class loadClass(String name) throws ClassNotFoundException {
>             if (name.equals("hello.example.org.Foo") || name.equals("hello.example.org.Bar")) 
>             {
>                 System.out.println("Foo or Bar is loaded");
>             }

'System.out' is not the channel for log messages.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#34845

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-07-10 20:12 -0400
Message-ID<nluo9d$1qjc$1@gioia.aioe.org>
In reply to#34671
On 6/20/2016 3:42 AM, kant kodali wrote:
>     public class MyClassLoader extends URLClassLoader {
>
>         public MyClassLoader() throws Exception{
>              super(new URL[]{new URL("file:///home/ubuntu/java/hello.jar")});
>         }
>
>         @Override
>         public Class loadClass(String name) throws ClassNotFoundException {
>             if (name.equals("hello.example.org.Foo") || name.equals("hello.example.org.Bar"))
>             {
>                 System.out.println("Foo or Bar is loaded");
>             }
>             return super.loadClass(name);
>
>         }
>
> }
>
> I just want to execute a block of code say a print statement prior
> toloading certain classes and the above code doesn't quite work and
 > I am not sure where the error is?

The following works.

Target class:

public class Dyn {
     private static int v = 1;
     {
         v++;
         System.out.println("Dyn static init: " + v);
     }
     public Dyn() {
         v++;
         System.out.println("Dyn ctor: " + v);
     }
     public static void special() {
         v++;
         System.out.println("Dyn special: " + v);
     }
}

Classloader and test:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class MyClassLoader extends URLClassLoader {
	public MyClassLoader(URL[] urls) {
		super(urls);
	}
	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		Class<?> res = super.findClass(name);
		try {
			Method m = res.getMethod("special", new Class[0]);
			m.invoke(null, new Object[0]);
		} catch (NoSuchMethodException | SecurityException | 
IllegalAccessException | IllegalArgumentException | 
InvocationTargetException e) {
			e.printStackTrace();
		}
		return res;
	}
	public static void main(String[] args) throws Exception {
		ClassLoader cl = new MyClassLoader(new URL[] { new 
URL("file:///C:/work/") });
		Object o = Class.forName("Dyn", true, cl).newInstance();
	}
}

Output:

Dyn special: 2
Dyn static init: 3
Dyn ctor: 4

Obviously looking up the method by name is not optimal. You should
probably use annotations, but that is another story.

And I would be very careful about what I do in that special method - it
gets called very early.

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web