How to exclude some codes that use refection API ?

1,216 views
Skip to first unread message

Steven

unread,
Apr 14, 2015, 10:13:02 PM4/14/15
to jac...@googlegroups.com
Hi All,
         I use reflection in code as follow:
 public static void setFormFieldsToRequest(Object vo,
      HttpServletRequest request) throws Exception {
        Field[] fields = vo.getClass().getDeclaredFields();
        Field fie = null;
        for (int i = 0; i < fields.length; i++) {
            fie = fields[i];
            if (fie.getName().equals("serialVersionUID"))
                continue;
            else {
                Object fvalue = getFieldValue(fie, vo);
                request.setAttribute(fie.getName(), fvalue);
            }
        }
    }

It throwed the follow exception:
at Caused by: java.lang.Exception: can't process the type: class [Z
at com.test.web.ap.action.APActionUtil.getFieldValue(APActionUtil.java:301)
at com.test.web.ap.action.APActionUtil.setFormFieldsToRequest(APActionUtil.java:240)
at

I viewed the section about reflection on FAQ page.
It may cause to get incorrect coverage if use  reflection in code. But the application fails to run now.
 
I know that it can exclude some packages by using the key word 'exclude='com.test.web.*'',
but if I have many those codes in different packages which may lead to miscellaneous conf.
 
I want to exclude those codes that use reflection simply, any suggestion ? Thanks.
 
 

Marc Hoffmann

unread,
Apr 15, 2015, 2:14:11 AM4/15/15
to jac...@googlegroups.com
From the FAQ (http://www.eclemma.org/jacoco/trunk/doc/faq.html):

Q: My code uses reflection. Why does it fail when I execute it with
JaCoCo?

A: To collect execution data JaCoCo instruments the classes under test
which adds two members to the classes: A private static field
$jacocoData and a private static method $jacocoInit(). Both members are
marked as synthetic. Please change your code to ignore synthetic
members. This is a good practice anyways as also the Java compiler
creates synthetic members in certain situation.


So in your particular case you will probably add the following check:

if (!fie.isSynthetic()) {
Object fvalue = getFieldValue(fie, vo);
request.setAttribute(fie.getName(), fvalue);
}


Regards,
-marc
> --
> You received this message because you are subscribed to the Google
> Groups "JaCoCo and EclEmma Users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to jacoco+un...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jacoco/92643cf7-e760-48b5-8860-97b4da69007c%40googlegroups.com
> [1].
> For more options, visit https://groups.google.com/d/optout [2].
>
>
> Links:
> ------
> [1]
> https://groups.google.com/d/msgid/jacoco/92643cf7-e760-48b5-8860-97b4da69007c%40googlegroups.com?utm_medium=email&utm_source=footer
> [2] https://groups.google.com/d/optout

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages