Getting null pointer exception when getting class name from Target object in AspectJ

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting null pointer exception when getting class name from Target object in AspectJ

sridhar thiyagarajan
Hello,

I am using AspectJ 1.8.6. PFB the advise method code snippet which am using for performance logging. I am getting NULL POINTER EXCEPTION in the line invocation.getTarget().getClass().getSimpleName().

I am getting method name from the line invocation.getSignature().getName() but not getting the class name from   seeing the method from invocation.getTarget().getClass().getSimpleName().

Have anyone faced this concern ? Please help me.

public Object invoke(final ProceedingJoinPoint invocation) throws Throwable {              
    StopWatch watch = new StopWatch();
    watch.start();
    Object result = null;
    try {
        result =  invocation.proceed();
    } finally {
        watch.stop();
        StringBuilder sb = new StringBuilder();
        sb.append("Method: ")
        .append(invocation.getTarget().getClass().getSimpleName())
        .append(".")
        .append(invocation.getSignature().getName())
        .append("() finished in: ")
        .append(watch.getTime());
        log.log(sb.toString());
    }      
    return result;
}


Many thanks.

Sridhar Thiyagarajan


_______________________________________________
aspectj-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-dev
Reply | Threaded
Open this post in threaded view
|

Re: Getting null pointer exception when getting class name from Target object in AspectJ

sridhar thiyagarajan
Hi All,

Just to add more to the below email. I am using percflow aspect association and the pointcut is defined on a static method of a class. So, is that the reason why the target object is null. How to get the class name in this scenairo.


Thanks.

Sridhar Thiyagarajan


On Mon, Jun 15, 2015 at 4:47 PM, sridhar thiyagarajan <[hidden email]> wrote:
Hello,

I am using AspectJ 1.8.6. PFB the advise method code snippet which am using for performance logging. I am getting NULL POINTER EXCEPTION in the line invocation.getTarget().getClass().getSimpleName().

I am getting method name from the line invocation.getSignature().getName() but not getting the class name from   seeing the method from invocation.getTarget().getClass().getSimpleName().

Have anyone faced this concern ? Please help me.

public Object invoke(final ProceedingJoinPoint invocation) throws Throwable {              
    StopWatch watch = new StopWatch();
    watch.start();
    Object result = null;
    try {
        result =  invocation.proceed();
    } finally {
        watch.stop();
        StringBuilder sb = new StringBuilder();
        sb.append("Method: ")
        .append(invocation.getTarget().getClass().getSimpleName())
        .append(".")
        .append(invocation.getSignature().getName())
        .append("() finished in: ")
        .append(watch.getTime());
        log.log(sb.toString());
    }      
    return result;
}


Many thanks.

Sridhar Thiyagarajan



_______________________________________________
aspectj-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-dev
Reply | Threaded
Open this post in threaded view
|

Re: Getting null pointer exception when getting class name from Target object in AspectJ

Andy Clement
Which bit is null? Are you sure the joinpoint has a target? Not all of them do. 

I’d have thought the classname was visible from the signature too, is that not giving you the right answer?

I don’t use annotation style much myself but in code style I’d be using thisJoinPointStaticPart to access that information (but of course that may be giving me the static type info and not the dynamic type at runtime).

cheers,
Andy


On Jun 15, 2015, at 5:11 AM, sridhar thiyagarajan <[hidden email]> wrote:

Hi All,

Just to add more to the below email. I am using percflow aspect association and the pointcut is defined on a static method of a class. So, is that the reason why the target object is null. How to get the class name in this scenairo.


Thanks.

Sridhar Thiyagarajan


On Mon, Jun 15, 2015 at 4:47 PM, sridhar thiyagarajan <[hidden email]> wrote:
Hello,

I am using AspectJ 1.8.6. PFB the advise method code snippet which am using for performance logging. I am getting NULL POINTER EXCEPTION in the line invocation.getTarget().getClass().getSimpleName().

I am getting method name from the line invocation.getSignature().getName() but not getting the class name from   seeing the method from invocation.getTarget().getClass().getSimpleName().

Have anyone faced this concern ? Please help me.

public Object invoke(final ProceedingJoinPoint invocation) throws Throwable {              
    StopWatch watch = new StopWatch();
    watch.start();
    Object result = null;
    try {
        result =  invocation.proceed();
    } finally {
        watch.stop();
        StringBuilder sb = new StringBuilder();
        sb.append("Method: ")
        .append(invocation.getTarget().getClass().getSimpleName())
        .append(".")
        .append(invocation.getSignature().getName())
        .append("() finished in: ")
        .append(watch.getTime());
        log.log(sb.toString());
    }      
    return result;
}


Many thanks.

Sridhar Thiyagarajan


_______________________________________________
aspectj-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-dev


_______________________________________________
aspectj-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-dev
Reply | Threaded
Open this post in threaded view
|

Re: Getting null pointer exception when getting class name from Target object in AspectJ

sridhar thiyagarajan
Many thanks for the response .. Andy. Target is null and now, I could get the class name (for instance and static) fine by using the signature as mentioned in the below line of code.

ProceedingJoinPoint.getSignature().getDeclaringType().getSimpleName()


Thanks.

Sridhar Thiyagarajan


On Tue, Jun 16, 2015 at 1:57 AM, Andy Clement <[hidden email]> wrote:
Which bit is null? Are you sure the joinpoint has a target? Not all of them do. 

I’d have thought the classname was visible from the signature too, is that not giving you the right answer?

I don’t use annotation style much myself but in code style I’d be using thisJoinPointStaticPart to access that information (but of course that may be giving me the static type info and not the dynamic type at runtime).

cheers,
Andy


On Jun 15, 2015, at 5:11 AM, sridhar thiyagarajan <[hidden email]> wrote:

Hi All,

Just to add more to the below email. I am using percflow aspect association and the pointcut is defined on a static method of a class. So, is that the reason why the target object is null. How to get the class name in this scenairo.


Thanks.

Sridhar Thiyagarajan


On Mon, Jun 15, 2015 at 4:47 PM, sridhar thiyagarajan <[hidden email]> wrote:
Hello,

I am using AspectJ 1.8.6. PFB the advise method code snippet which am using for performance logging. I am getting NULL POINTER EXCEPTION in the line invocation.getTarget().getClass().getSimpleName().

I am getting method name from the line invocation.getSignature().getName() but not getting the class name from   seeing the method from invocation.getTarget().getClass().getSimpleName().

Have anyone faced this concern ? Please help me.

public Object invoke(final ProceedingJoinPoint invocation) throws Throwable {              
    StopWatch watch = new StopWatch();
    watch.start();
    Object result = null;
    try {
        result =  invocation.proceed();
    } finally {
        watch.stop();
        StringBuilder sb = new StringBuilder();
        sb.append("Method: ")
        .append(invocation.getTarget().getClass().getSimpleName())
        .append(".")
        .append(invocation.getSignature().getName())
        .append("() finished in: ")
        .append(watch.getTime());
        log.log(sb.toString());
    }      
    return result;
}


Many thanks.

Sridhar Thiyagarajan


_______________________________________________
aspectj-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-dev


_______________________________________________
aspectj-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-dev


_______________________________________________
aspectj-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-dev