Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Some Reporting tricks with class-transformations

DZone's Guide to

Some Reporting tricks with class-transformations

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

Every time I used to see some duplication of code, I used to move that code to a new method. With Tapestry, you begin to think differently. Now every time I see duplication, my first thought is “Can I create a worker for it”.

In my current project, I am using a few new ones. So I thought why not share them with you.

@ReportException

My onSuccess methods usually are of the form

@InjectComponent
private Form form

@OnEvent(EventConstants.SUCCESS)
void success()
{
   try
   {
      do_my_stuff();
   }catch(MyException ex)
   {
      form.recordError(ex.getMessage());
   }
}

Here is how I wanted my code to look.

@ReportException
@OnEvent(EventConstants.SUCCESS)
void success()
{
   do_my_stuff();
}

So for implementation, we first need an annotation

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ReportException
{
}

and then a worker

public class ReportExceptionWorker implements ComponentClassTransformWorker2
{
    private Environment environment;

    public ReportExceptionWorker(Environment environment)
    {
        this.environment = environment;
    }

    @Override
    public void transform(PlasticClass plasticClass, TransformationSupport support,
        MutableComponentModel model)
    {
        for(PlasticMethod method : plasticClass.getMethodsWithAnnotation(ReportException.class))
        {
            method.addAdvice(new ReportExceptionAdvice());
        }
    }

    private class ReportExceptionAdvice implements MethodAdvice
    {
        @Override
        public void advise(MethodInvocation invocation)
        {
            ValidationTracker tracker = environment.peek(ValidationTracker.class);

            if(tracker != null)
            {
                try
                {
                    invocation.proceed();
                } catch(MyException ex)
                {
                    tracker.recordError(ex.getMessage());
                    ex.printStackTrace();
                }
            }
        }
    }
}

Here the advice gets the ValidationTracker from the Environment service and does the exception handling and reporting for us.

Finally the contribution in Module.

public static void contributeComponentClassTransformWorker(
        OrderedConfiguration<ComponentClassTransformWorker2> configuration)
    {
        configuration.addInstance("ReportExceptionWorker",
            ReportExceptionWorker.class);
    }

@ReportExceptionAsAlert

Another close use case is when you have an ajax call which may result in an exception but you just want to show the user a simple alert with the message.

The annotation

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ReportExceptionAsAlert
{
}


The Worker

public class ReportExceptionAsAlertWorker implements ComponentClassTransformWorker2
{

    private final AjaxResponseRenderer ajaxResponseRenderer;

    private final Request request;

    public ReportExceptionAsAlertWorker(
        AjaxResponseRenderer ajaxResponseRenderer,
        Request request)
    {
        this.ajaxResponseRenderer = ajaxResponseRenderer;
        this.request = request;
    }

    @Override
    public void transform(PlasticClass plasticClass, TransformationSupport support,
        MutableComponentModel model)
    {
        for(PlasticMethod method : plasticClass.getMethodsWithAnnotation(
            ReportExceptionAsAlert.class))
        {
            method.addAdvice(new ReportExceptionAsAlertAdvice());
        }
    }

    private class ReportExceptionAsAlertAdvice implements MethodAdvice
    {
        @Override
        public void advise(MethodInvocation invocation)
        {

            if(request.isXHR())
            {
                try
                {
                    invocation.proceed();
                } catch(final MyException ex)
                {
                    ajaxResponseRenderer.addCallback(new JavaScriptCallback()
                    {

                        @Override
                        public void run(JavaScriptSupport javascriptSupport)
                        {
                            javascriptSupport.addScript("alert('%s');", ex.getMessage());
                        }

                    });
                    ex.printStackTrace();
                }
            }
        }
    }
}

 

From http://tawus.wordpress.com/2012/02/03/some-reporting-tricks-with-class-transformations/

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}