You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Lenny Primak <lp...@hope.nyc.ny.us> on 2011/09/19 18:50:55 UTC

Tapestry 5.3 - MethodAdvice - How to read from instances' fields?

I have a need to check in the MethodAdvice whether any of the persistent fields are not null, something like below...
How would I go about doing that?
-------------------------------
    @Override
    public void transform(final PlasticClass plasticClass, TransformationSupport support, final MutableComponentModel model)
    {
        final List<PlasticField> fields = plasticClass.getFieldsWithAnnotation(Persist.class);
        for(final PlasticMethod method : plasticClass.getMethodsWithAnnotation(AJAX.class))
        {
            final AJAX annotation = method.getAnnotation(AJAX.class);
            if(method.isVoid() == false)
            {
                method.addAdvice(new MethodAdvice() 
                {
                    @Override
                    public void advise(MethodInvocation invocation)
                    {
                        boolean isNull = true;
                        for(PlasticField field : fields)
                        {
                            Object value = field.readValue();  // THIS IS WHAT I WANT
                            if(value != null)
                            {
                                isNull = false;
                                break;
                            }
                        }
                        if(request.isXHR() && isNull)
                        {
                            // do not invoke on bad sessions
                        }
                        else
                        {
                            invocation.proceed();
                        }
                    }
                });
            }
            else
            {
                throw new RuntimeException(
                "@AJAX can be applied to non-void event handlers only"); 
            }
        }