@@ -64,9 +64,10 @@ public void accept(Object origTarget, Field field, Object value) {
6464 throw new IllegalArgumentException (Strings .compile ("Value {} is not assignable to {}" , value , field .getName ()));
6565 }
6666 Class <?> fieldDeclaringClass = field .getDeclaringClass ();
67+ boolean isStatic = Modifier .isStatic (field .getModifiers ());
6768 long fieldOffset ;
6869 Object target ;
69- if (Modifier . isStatic ( field . getModifiers ()) ) {
70+ if (isStatic ) {
7071 fieldOffset = unsafe .staticFieldOffset (field );
7172 target = fieldDeclaringClass ;
7273 } else {
@@ -80,7 +81,10 @@ public void accept(Object origTarget, Field field, Object value) {
8081 fieldOffset = unsafe .objectFieldOffset (field );
8182
8283 }
83- Class <?> cls = field .getType ();
84+ setByUnsafe (field , value , fieldOffset , target , field .getType ());
85+ }
86+
87+ protected void setByUnsafe (Field field , Object value , long fieldOffset , Object target , Class <?> cls ) {
8488 if (!cls .isPrimitive ()) {
8589 if (!Modifier .isVolatile (field .getModifiers ())) {
8690 unsafe .putObject (target , fieldOffset , value );
@@ -140,6 +144,58 @@ public void accept(Object origTarget, Field field, Object value) {
140144
141145 }
142146
147+ public static class ForJava25 extends ForJava7 {
148+ protected SetAccessibleFunction setAccessibleFunction ;
149+ protected ThrowExceptionFunction throwExceptionFunction ;
150+
151+ public ForJava25 (Map <Object , Object > context ) {
152+ super (context );
153+ setAccessibleFunction = ObjectProvider .get (context ).getOrBuildObject (SetAccessibleFunction .class , context );
154+ throwExceptionFunction = ObjectProvider .get (context ).getOrBuildObject (ThrowExceptionFunction .class , context );
155+ }
156+
157+ @ Override
158+ public void accept (Object origTarget , Field field , Object value ) {
159+ if (value != null && !Classes .isAssignableFrom (field .getType (), value .getClass ())) {
160+ throw new IllegalArgumentException (Strings .compile ("Value {} is not assignable to {}" , value , field .getName ()));
161+ }
162+ Class <?> fieldDeclaringClass = field .getDeclaringClass ();
163+ boolean isStatic = Modifier .isStatic (field .getModifiers ());
164+ Object target ;
165+ if (isStatic ) {
166+ target = fieldDeclaringClass ;
167+ } else {
168+ if ((target = origTarget ) == null ) {
169+ throw new IllegalArgumentException ("Target object is null" );
170+ }
171+ Class <?> targetObjectClass = target .getClass ();
172+ if (!Classes .isAssignableFrom (fieldDeclaringClass , targetObjectClass )) {
173+ throw new IllegalArgumentException ("Target object class " + targetObjectClass + " is not assignable to " + fieldDeclaringClass );
174+ }
175+ }
176+ try {
177+ Long fieldOffset ;
178+ if (isStatic ) {
179+ fieldOffset = unsafe .staticFieldOffset (field );
180+ } else {
181+ fieldOffset = unsafe .objectFieldOffset (field );
182+ }
183+ setByUnsafe (field , value , fieldOffset , target , field .getType ());
184+ } catch (UnsupportedOperationException exc ) {
185+ try {
186+ setAccessibleFunction .accept (field , true );
187+ if (isStatic ) {
188+ field .set (null , value );
189+ } else {
190+ field .set (target , value );
191+ }
192+ } catch (Throwable exc2 ) {
193+ throwExceptionFunction .accept (exc2 );
194+ }
195+ }
196+ }
197+ }
198+
143199
144200 public interface Native extends SetFieldValueFunction {
145201
0 commit comments