Android Open Source - AspectJ-Android-Example Exception Handling Aspect






From Project

Back to project page AspectJ-Android-Example.

License

The source code is released under:

Apache License

If you think the Android project AspectJ-Android-Example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package android.mobile.peakgames.net.aspectjandroid.aspect;
/*  w ww. j  a v  a 2 s  . c  om*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.mobile.peakgames.net.aspectjandroid.exception.AuthenticationException;
import android.util.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class ExceptionHandlingAspect {
    private static final String TAG = ExceptionHandlingAspect.class.getName();

    @Pointcut("execution(* android.mobile.peakgames.net.aspectjandroid.AspectActivity.*(..))")
    public void exceptionEntryPoint() {
    }

    @AfterThrowing(pointcut = "exceptionEntryPoint()", throwing = "throwable")
    public void exceptionMethod(JoinPoint joinPoint, Throwable throwable) {
        Log.e(TAG, "Exception caught : " + throwable + " on method : " + joinPoint.getSignature());

        if (joinPoint.getTarget() instanceof Activity) {
            if (throwable instanceof AuthenticationException) {
                new AlertDialog.Builder((Context) joinPoint.getTarget())
                        .setTitle("Authentication Error")
                        .setMessage("You are not authenticated")
                        .show();
            } else {
                new AlertDialog.Builder((Context) joinPoint.getTarget())
                        .setTitle("Error")
                        .setMessage("Error occurred at : " + joinPoint.getSignature() + " Exception : " + throwable)
                        .show();
            }
        }
    }
}




Java Source Code List

android.mobile.peakgames.net.aspectjandroid.ApplicationTest.java
android.mobile.peakgames.net.aspectjandroid.AspectActivity.java
android.mobile.peakgames.net.aspectjandroid.SecureMethod.java
android.mobile.peakgames.net.aspectjandroid.Session.java
android.mobile.peakgames.net.aspectjandroid.aspect.AuthenticationAspect.java
android.mobile.peakgames.net.aspectjandroid.aspect.CachingAspect.java
android.mobile.peakgames.net.aspectjandroid.aspect.ExceptionHandlingAspect.java
android.mobile.peakgames.net.aspectjandroid.aspect.LoggingAspect.java
android.mobile.peakgames.net.aspectjandroid.aspect.ProfilingAspect.java
android.mobile.peakgames.net.aspectjandroid.exception.AuthenticationException.java