Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] ClassNotFoundException in reflective type when passing null array.

Greetings.

I am using an aspect to track calls to certain functions. As identification of the 
function, I use reflection to find the actual Method and use that as key in a map 
to look up some properties.

My problem is that in some cases, the reflected parameter types were 
ClassNotFoundException instead of the array type I expected.

I created this (almost) minimal example to show the problem:
--- Main.java ---
public class Main {
    public static void main(String[] args) {
        Main obj = new Main();
        obj.test(null, null);
        obj.test(null, new Main[0]);
    }
    void test(Main dummy, Main[] dummy2) {}
}
--- end Main.java ---
and
--- MainAspect.aj ---
import org.aspectj.lang.reflect.MethodSignature;

public aspect MainAspect {
    
    pointcut testcall(): execution(* test*(..));
    
    before(): testcall() {
        MethodSignature sig = (MethodSignature) thisJoinPointStaticPart.getSignature();
    	System.out.println(sig);
        Class[] params = sig.getParameterTypes();
        for(int i=0;i<params.length;i++) {
        	Class cls = params[i];
            System.out.println(" - " + cls.getName());
        }
    }
}
--- end MainAspect.aj ---

When I run this (currently from Eclipse 3.0M8 with AJDT 1.19/Aspectj1.2rc2) I get the 
following output:
---
void Main.test(Main, ClassNotFoundException)
 - Main
 - java.lang.ClassNotFoundException
void Main.test(Main, ClassNotFoundException)
 - Main
 - java.lang.ClassNotFoundException
---

HOWEVER, if I swap the two calls to obj.test, so the one with a non-null array argument 
comes first, the output changes to:
---
void Main.test(Main, Main[])
 - Main
 - [LMain;
void Main.test(Main, Main[])
 - Main
 - [LMain;
---
i.e., what I expected.


It seems there is a problem with reflecting the type of the array. I don't have that 
problem with an Integer array (showing "Integer[]" and "[Ljava.lang.Integer;" as 
expected), so it must be only some classes, and it only happens for arrays. The Main
class is definitly available, as shown by the first argument. It seems the static part 
of the join point is only initialized the first time the aspect is triggered for a method.

In the application where I try to use this, the problem also appeared in AspectJ v1.1.1, 
so it's not new to 1.2.

If anyone recognizes the problem, and (preferably :) knows a solution, I'd be very 
interested in hearing about it.

Regards
/L
--
Lasse R. Nielsen 
 'Faith without judgement merely degrade the spirit divine'


Back to the top