IllegalAccessException when running clover test

I am getting the below error when running the clover test. Same test runs fine during the unit test.

This is happening when i am trying to copy an object using reflection.

Do we have any work around for this issue. 

Clover Version:4.0.2

Error:

Caused by: java.lang.IllegalAccessException: Can not set static final com_atlassian_clover.TestNameSniffer field <<XXX Class>>.__CLR4_0_2_TEST_NAME_SNIFFER to com_atlassian_clover.TestNameSniffer$1
at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:76)
at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:80)
at sun.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl.set(UnsafeQualifiedStaticObjectFieldAccessorImpl.java:77)
at java.lang.reflect.Field.set(Field.java:764)

 

Code that copies the object:

Class<? extends Object> copy1 = entity.getClass();

 Class<? extends Object> copy2 = entity2.getClass();

Field[] fromFields = copy1.getDeclaredFields();
Field[] toFields = copy2.getDeclaredFields();
Object value = null;
for (Field field : fromFields) {
Field field1 = copy2.getDeclaredField(field.getName());
value = field.get(entity);
  field1.set(entity2, value);

}

 

2 answers

0 votes

As you can see in the exception message, you're trying to write to a static final field.

Change your copying method to skip fields of type TestNameSniffer.

Thanks Marek. This worked

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

675 views 4 16
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot