[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
| Re: [aspectj-dev] Can I intercept before a class is load time weaved | 
Hi Choudary,
Martin is absolutely right, you need to :
- Implement your own classloader
- Use the weaving adaptor from there
- Decide which classes you want to pass to the adaptor and which you 
don't want to
- To make this check, either use BCEL/ASM, or load the class in a 
temporary classloader
This last point is what you are asking for, to use reflection instead of 
ASM/BCEL. You don't really have to, ASM/BCEL can quite easily check for 
the presence of a static field in bytecode, and it's much much faster 
than using reflection for this, but if you really want to, you can still 
load the class in a temporary classloader (you can use an 
URLClassLoader), check it there and the load it in your real classloader 
with or without passing it to the weaving adaptor. The problem is that 
playing with classloaders this way is a bit tricky;  not impossible or 
extremely difficult, just tricky, which means you could spend hours 
trying to figure out why things are not working as you expect before 
understanding what is really happening.
If you want to, I have played a bit with AspectJ LTW classloading, you 
can find some stuff here 
http://svn.apache.org/repos/asf/labs/magma/trunk/maven-magma-plugin/src/main/java/org/apache/magma/tools/classloading/ 
. What you find there is a way to pipe a number of transformations 
(AspectJ weaver -> OpenJPA enhancer -> Cobertura enhancer) and then load 
the resulting class in the final classloader (in my case a webapp 
classloader). It works in "pull mode", that is when the webapp 
classloader searches for a class, it will search for a resource 
a/b/C.class to load the bytecode. When this happen, the chain of 
transformers kick in, each pulling bytecode from the previous one, so 
that the bytecode travels from the .jar file, via aspectj, openjpa etc.. 
to the classloader requiring it.
Some of these transformers (including AspectJ) uses temporary 
classloaders in the middle. Maybe you can use that as a starting point 
and the add proper "if" statements to bypass aspectj weaver for classes 
you don't need.
The code that build the chain and uses it is found here 
http://svn.apache.org/repos/asf/labs/magma/trunk/maven-magma-plugin/src/main/java/org/apache/magma/tools/maven/MagmaJettyRun.java 
inside the execute() method.
Hope this helps,
Simone
Choudary Kothapalli wrote:
This question follows the thread on InvalidClassException while
deserializing classes without SUID. The link to that thread is
http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg02496.html .
I could not get rid of the exception and my only solution is to
exclude all those Serializable classes without SUID. I ask my users to
exclude those classes in aop.xml, but in a large legacy application,
it is a tedious process. I need to find a simpler solution.
A probable solution is to intercept just before weaving a class, check
if that class is Serializable without SUID and if so, not to weave it.
This would need two things:
1. Ability to intercept just before a class is load time weaved. In
this 'advice', the normal class should be available so that a check
can be made for SUID using java reflection.
2. Ability to proceed without weaving if I don't want to weave that class.
I don't find a way to do this currently. By the time
'staticinitialization' is intercepted, the class may be weaved
already.
Again, I don't think this is any problem with AspectJ. It is just
something I need and I am checking if you could offer any solution
with your knowledge of AspectJ implementation.
Thanks,
Choudary Kothapalli.
_______________________________________________
aspectj-dev mailing list
aspectj-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-dev
  
--
Simone Gianni            CEO Semeru s.r.l.           Apache Committer
http://www.simonegianni.it/