Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] WeavingAdapter - how to use it?

Hi,

when you say this is an alternative way, what do you exactly mean? An alternative way to the code I posted or an alternative way to -Xbootclasspath, which, BTW, does not work. Crashes the JVM.

Cheers,
	Silviu
On Nov 21, 2009, at 11:10 PM, Andy Clement wrote:

> Glad to see you got it working.
> 
> There is an alternative way too. It involves defining a correct
> weaving context that names the aspects and your options:
> 
> static class SimpleWeavingContext extends DefaultWeavingContext {
> 
> 		public SimpleWeavingContext(ClassLoader loader) {
> 			super(loader);
> 		}
> 
> 		@Override
> 		public List getDefinitions(ClassLoader loader, WeavingAdaptor adaptor) {
> 			List definitions = new ArrayList();
> 			Definition d = new Definition();
> 			d.getAspectClassNames().add("MonitorAspect");
> 			d.appendWeaverOptions("-Xjoinpoints:synchronization -debug");
> 			definitions.add(d);
> 			return definitions;
> 		}
> 
> 	}
> 
> 
> and then using the ClassLoaderWeavingAdaptor:
> 
> 		byte[] bytes = readEntireInputStream(new FileInputStream("bin/Test.class"));
> 		URL[] aspects = getWeaversURL();
> 		URL[] classpath = getClasspathURL();
> 		URL[] realClasspath = new URL[aspects.length + classpath.length];
> 		System.arraycopy(aspects, 0, realClasspath, 0, aspects.length);
> 		System.arraycopy(classpath, 0, realClasspath, aspects.length,
> classpath.length);
> 		URLClassLoader myClassLoader = new URLClassLoader(realClasspath,
> ClassLoader.getSystemClassLoader());
> 
> 		ClassLoaderWeavingAdaptor clwa = new ClassLoaderWeavingAdaptor();
> 
> 		clwa.initialize(myClassLoader, new SimpleWeavingContext(myClassLoader));
> 
> 		byte[] newBytes = clwa.weaveClass("test.Test", bytes, true);
> 
> However, it will only work with AspectJ 1.6.7 dev builds after today
> because I needed to open up initialize() so it was public rather than
> protected. And I've only just committed that change.
> 
> Andy
> 
> 
> 2009/11/21 Andrica Silviu <silviu.andrica@xxxxxxx>:
>> Hi,
>> Indeed I forgot :) And I did create a wrapper class around the
>> WeavingAdaptor that sets the right permissions. The code is the following:
>> Please find attached the code.
>> It works brilliant as long as I don't instrument already loaded classes,
>> through java.lang.Instrumentation.instrumentation.retransformClasses(...);
>> or java.* classes, I don't exactly know. In this case, Java complains about
>> not finding the aspect. I am currently exploring the use of
>> -Xbootclassptah/p:<path_to_aspect>.
>> 
>> Thanks for the replies.
>> Best regards,
>> Silviu
>> _______________________________________________
>> aspectj-users mailing list
>> aspectj-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>> 
>> 
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users



Back to the top