Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Runtime weaving without agent

Le 05/04/2013 11:15, Alexander Kriegisch a écrit :
> Check out http://www.eclipse.org/aspectj/doc/next/weaver-api/org/aspectj/weaver/loadtime/WeavingURLClassLoader.html.
>
> Here is a little old code snippet I found in one of my projects when I implemented a plug-in concept in which I had the aspect code in my main project, but wanted to advise plugin JARs which were loaded on demand. Feel free to modify it according to your needs:
>
> 	private void loadPlugin(String pluginJar, String pluginClassName) throws Exception {
> 		echo("Trying to load plugin JAR " + pluginJar);
> 		ClassLoader loader = Thread.currentThread().getContextClassLoader();
> 		WeavingURLClassLoader weaver = new WeavingURLClassLoader(
> 			new URL[] { new File(pluginJar).toURI().toURL() },
> 			new URL[] { loader.getResource(".") },
> 			loader
> 		);
> 		Class<?> pluginClass = weaver.loadClass(pluginClassName);
> 		Plugin plugin = (Plugin) pluginClass.newInstance();
> 		plugin.init(this);
> 		plugins.add(plugin);
> 		echo("Successfully loaded and initialised plugin '" + plugin.getName() + "'");
> 	}
Hi, and thanks for the answer.

But i had already seen this code and it doesn't fits our needs
In fact:
- two annotated aspects are present in the project, but not in separate jars
- aspect can't dynamically added at runtime depending on user configuration
  (cache, log, time aspects).

I succeeded tweaking some portion of the code and using ugly introspective
code to bypass aspectj final or private modifier (aspectj uses a little bit of
defensive code).

The code currently looks like:

AspectJUrlClassLoader loader = new AspectJUrlClassLoader(urls, parentLoader);
thread.setContextClassLoader(loader);
if (parameters.getUseTrace()) {
     loader.deploy(TraceAspect.class);
}
if (parameters.getUseCache()) {
     loader.deploy(CacheAspect.class);
}
if (parameters.getUseTime()) {
     loader.deploy(TimeAspect.class);
}
loader.prepareForWeave();
thread.start();

Then every class loaded from this new class loader is automatically weaved
if needed depending of registered aspects.

I have committed my current code here:
http://forge.codelutin.com/projects/isis-fish/repository/revisions/3822/show/branches/4.0.1/src/main/java/fr/ifremer/isisfish/aspect

Is it an approach that could be introduced into aspectj ?

-- 
Éric Chatellier - Code Lutin
Tel: 02.40.50.29.28 - http://www.codelutin.com



Back to the top