Skip to content

Commit 2d4a9fe

Browse files
Bug fix. Releasing new version
1 parent 8baa850 commit 2d4a9fe

File tree

1 file changed

+24
-62
lines changed

1 file changed

+24
-62
lines changed

java/src/main/java/io/github/toolfactory/jvm/function/catalog/SetFieldValueFunction.java

+24-62
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,14 @@ protected void setByUnsafe(Field field, Object value, long fieldOffset, Object t
146146
}
147147

148148
public static class ForJava25 extends ForJava7 {
149-
protected SetAccessibleFunction setAccessibleFunction;
150149
protected ThrowExceptionFunction throwExceptionFunction;
150+
protected Field modifiersField;
151+
protected GetDeclaredFieldFunction getDeclaredFieldFunction;
152+
protected Supplier<GetDeclaredFieldFunction> getDeclaredFieldFunctionSupplier;
153+
protected SetAccessibleFunction setAccessibleFunction;
151154
protected Supplier<SetAccessibleFunction> setAccessibleFunctionSupplier;
152155

153-
public ForJava25(final Map<Object, Object> context) {
156+
public ForJava25(final Map<Object, Object> context) throws Throwable {
154157
super(context);
155158
throwExceptionFunction = ObjectProvider.get(context).getOrBuildObject(ThrowExceptionFunction.class, context);
156159
setAccessibleFunctionSupplier = new Supplier<SetAccessibleFunction>() {
@@ -159,6 +162,12 @@ public SetAccessibleFunction get() {
159162
return ObjectProvider.get(context).getOrBuildObject(SetAccessibleFunction.class, context);
160163
}
161164
};
165+
getDeclaredFieldFunctionSupplier = new Supplier<GetDeclaredFieldFunction>() {
166+
@Override
167+
public GetDeclaredFieldFunction get() {
168+
return ObjectProvider.get(context).getOrBuildObject(GetDeclaredFieldFunction.class, context);
169+
}
170+
};
162171
}
163172

164173
@Override
@@ -200,69 +209,16 @@ public void accept(Object origTarget, Field field, Object value) {
200209

201210
protected void setByReflection(Field field, Class<?> fieldType, boolean isStatic, Object target, Object value) throws Throwable {
202211
setAccessible(field);
203-
Integer modifiers = field.getModifiers();
212+
int modifiers = field.getModifiers();
204213
Field modifiersField = null;
205-
if ((modifiers & Modifier.FINAL) == Modifier.FINAL) {
206-
modifiersField = Field.class.getDeclaredField("modifiers");
207-
setAccessible(modifiersField);
214+
if (Modifier.isFinal(modifiers)) {
215+
modifiersField = this.modifiersField;
208216
modifiersField.setInt(field, modifiers & ~Modifier.FINAL);
209217
}
210-
if (fieldType.isPrimitive()) {
211-
if (fieldType == short.class) {
212-
if (isStatic) {
213-
field.set(null, (short)value);
214-
} else {
215-
field.set(target, (short)value);
216-
}
217-
} else if (fieldType == int.class) {
218-
if (isStatic) {
219-
field.set(null, (int)value);
220-
} else {
221-
field.set(target, (int)value);
222-
}
223-
} else if (fieldType == long.class) {
224-
if (isStatic) {
225-
field.set(null, (long)value);
226-
} else {
227-
field.set(target, (long)value);
228-
}
229-
} else if (fieldType == float.class) {
230-
if (isStatic) {
231-
field.set(null, (float)value);
232-
} else {
233-
field.set(target, (float)value);
234-
}
235-
} else if (fieldType == double.class) {
236-
if (isStatic) {
237-
field.set(null, (double)value);
238-
} else {
239-
field.set(target, (double)value);
240-
}
241-
} else if (fieldType == boolean.class) {
242-
if (isStatic) {
243-
field.set(null, (boolean)value);
244-
} else {
245-
field.set(target, (boolean)value);
246-
}
247-
} else if (fieldType == byte.class) {
248-
if (isStatic) {
249-
field.set(null, (byte)value);
250-
} else {
251-
field.set(target, (byte)value);
252-
}
253-
} else if (fieldType == char.class) {
254-
if (isStatic) {
255-
field.set(null, (char)value);
256-
} else {
257-
field.set(target, (char)value);
258-
}
259-
}
218+
if (isStatic) {
219+
field.set(null, value);
260220
} else {
261-
if (isStatic) {
262-
field.set(null, value);
263-
} else {
264-
field.set(target, value);
265-
}
221+
field.set(target, value);
266222
}
267223
if (modifiersField != null) {
268224
modifiersField.setInt(field, modifiers);
@@ -274,7 +230,13 @@ protected void setAccessible(Field field) throws Throwable {
274230
setAccessibleFunction.accept(field, true);
275231
} catch (NullPointerException exc) {
276232
if (setAccessibleFunction == null) {
277-
setAccessibleFunction = setAccessibleFunctionSupplier.get();
233+
synchronized (this) {
234+
if (setAccessibleFunction == null) {
235+
setAccessibleFunction = setAccessibleFunctionSupplier.get();
236+
getDeclaredFieldFunction = getDeclaredFieldFunctionSupplier.get();
237+
setAccessible(this.modifiersField = getDeclaredFieldFunction.apply(Field.class, "modifiers"));
238+
}
239+
}
278240
setAccessible(field);
279241
} else {
280242
throwExceptionFunction.accept(exc);

0 commit comments

Comments
 (0)