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


Groups > comp.lang.java.programmer > #34845

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

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: How to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes?
Date 2016-07-10 20:12 -0400
Organization Aioe.org NNTP Server
Message-ID <nluo9d$1qjc$1@gioia.aioe.org> (permalink)
References <ee393144-64b8-4f19-8597-e139f401db85@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web