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 vote

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 Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published 3 hours ago in Hipchat

Moving from Hipchat to Stride? Here’s what you’ll love

Heya, Hipchat friends! We’re so happy you’re checking out   Stride. Whether you know it or not, you have been instrumental in making Stride come to life. Every feature, design, and functionality...

14 views 0 1
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