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 Sign up to answer
Community showcase
Published yesterday in Jira Ops

Jira Ops Early Access Program Update #1: Announcing our next feature and a new integration

Thanks for signing up for Jira Ops! I’m Matt Ryall, leader for the Jira Ops product team at Atlassian. Since this is a brand new product, we’ll be delivering improvements quickly and sharing updates...

222 views 0 6
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