Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-dev] Enums and AspectJ - getEnumConstants() returns null

Hello,

Thanks for your answer.
Sometimes Eclipse showed me that NPE.

All the best.
Cosmin V

On Mon, May 30, 2011 at 11:52 PM, Andy Clement <andrew.clement@xxxxxxxxx> wrote:
Thanks for the test program.

The reason this is occurring is because your advice is throwing an
NPE.  Due to the way in which getEnumConstants is implemented, it
catches any exceptions and returns 'null' as the value.  If we put a
try catch in your advice we can see the exception that occurs:


java.lang.NullPointerException
       at com.cvu.numsoft.Sex.values_aroundBody1$advice(Sex.java:25)
       at com.cvu.numsoft.Sex.values(Sex.java:1)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at java.lang.Class.getEnumConstantsShared(Class.java:2942)
       at java.lang.Class.getEnumConstants(Class.java:2920)
       at com.cvu.numsoft.Runner.main(Runner.java:7)

this causes the name:

 jp.getThis().getClass().getName()

It is occurring because your advice is calling getThis() and not
allowing for a null.  There is no this in a static method and so the
subsequent call you make to getClass() will raise an NPE.  values() in
the enum type happens to be a static method, and you advise it.

Solutions would be to exclude that enum type, the values method, or
just allow for a null this.

cheers
Andy

On 30 May 2011 13:08, Cosmin Nicolae Vacaroiu <cosminvacaroiu@xxxxxxxxx> wrote:
> I forgot, please uncomment from the project test-aspectj, in the
> file pom.xml
>          <!--build>
> <plugins>
> <plugin>
> <groupId>org.codehaus.mojo</groupId>
> <artifactId>aspectj-maven-plugin</artifactId>
>
> On Mon, May 30, 2011 at 10:49 PM, Cosmin Nicolae Vacaroiu
> <cosminvacaroiu@xxxxxxxxx> wrote:
>>
>> Hello,
>> I've tested again and it's the same result.
>> I've attached a zip with 2 projects. The Aspect and The Enum with a simple
>> class with a main method.
>> Ps: I'm compiling and waving with a maven plugin.
>> If you use Eclipse please use mvn eclipse:eclipse so that you can import
>> the projects.
>>
>> Thanks.
>>
>>
>>
>> On Mon, May 30, 2011 at 7:03 PM, Andy Clement <andrew.clement@xxxxxxxxx>
>> wrote:
>>>
>>> Hi,
>>>
>>> I can't seem to recreate this problem.  I probably need to know what
>>> you are weaving into what.
>>>
>>> I tried weaving at every joinpoint other than my aspect:
>>> ---
>>> enum EnumName { B, L, A }
>>>
>>> public class A {
>>>  public static void main(String[]argv) {
>>>    System.out.println(EnumName.class.isEnum());
>>>    System.out.println(EnumName.class.getEnumConstants());
>>>  }
>>> }
>>>
>>> aspect X {
>>>  before(): !within(X) {}
>>> }
>>> ---
>>> But I still don't get null from getEnumConstants().  Are you able to
>>> share the aspect with me that gives rise to this?
>>>
>>> cheers
>>> Andy
>>>
>>> On 27 May 2011 11:11, Cosmin Nicolae Vacaroiu <cosminvacaroiu@xxxxxxxxx>
>>> wrote:
>>> > Using AspectJ with enums I get the next results:
>>> >
>>> > public enum EnumName { B, L, A }
>>> >
>>> >
>>> > EnumName.class.isEnum() : true
>>> >
>>> > EnumName.class.getEnumConstants() : null
>>> >
>>> > If I don't wave (at compile time) my project, getEnumConstants() works
>>> > as
>>> > expected.
>>> >
>>> > _______________________________________________
>>> > aspectj-dev mailing list
>>> > aspectj-dev@xxxxxxxxxxx
>>> > https://dev.eclipse.org/mailman/listinfo/aspectj-dev
>>> >
>>> >
>>> _______________________________________________
>>> aspectj-dev mailing list
>>> aspectj-dev@xxxxxxxxxxx
>>> https://dev.eclipse.org/mailman/listinfo/aspectj-dev
>>
>
>
> _______________________________________________
> aspectj-dev mailing list
> aspectj-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-dev
>
>
_______________________________________________
aspectj-dev mailing list
aspectj-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-dev


Back to the top