Skip to content

Commit 83c5901

Browse files
committed
refactoring of make accessible mechanism
1 parent 8be5d70 commit 83c5901

File tree

5 files changed

+32
-34
lines changed

5 files changed

+32
-34
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/io/AbstractMappedClassFieldObserver.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.igormaznitsa.jbbp.utils.DslBinCustom;
2828
import com.igormaznitsa.jbbp.utils.JBBPUtils;
2929
import com.igormaznitsa.jbbp.utils.ReflectUtils;
30-
3130
import java.lang.reflect.Array;
3231
import java.lang.reflect.Field;
3332
import java.lang.reflect.Modifier;
@@ -117,7 +116,9 @@ protected void processObject(final Object obj, Field field, final Object customF
117116
final Bin clazzAnno = clazzToProcess.getAnnotation(Bin.class);
118117

119118
for (Field f : clazzToProcess.getDeclaredFields()) {
120-
f = ReflectUtils.makeAccessible(f);
119+
if (!ReflectUtils.isPotentiallyAccessibleField(f)) {
120+
f = ReflectUtils.makeAccessible(f);
121+
}
121122

122123
final int modifiers = f.getModifiers();
123124
if (Modifier.isTransient(modifiers) || Modifier.isStatic(modifiers) || f.getName().indexOf('$') >= 0) {
@@ -146,7 +147,9 @@ protected void processObject(final Object obj, Field field, final Object customF
146147
}
147148
}
148149

149-
field = ReflectUtils.makeAccessible(field);
150+
if (field != null && !ReflectUtils.isPotentiallyAccessibleField(field)) {
151+
field = ReflectUtils.makeAccessible(field);
152+
}
150153

151154
final Bin clazzAnno = obj.getClass().getAnnotation(Bin.class);
152155
final DslBinCustom clazzCustomAnno = obj.getClass().getAnnotation(DslBinCustom.class);
@@ -472,7 +475,7 @@ protected void processObjectField(final Object obj, final Field field, final Bin
472475
this.onArrayStart(obj, field, annotation, len);
473476
for (int i = 0; i < len; i++) {
474477
final Object value = Array.get(array, i);
475-
String nullableStrValue = value == null ? null : String.valueOf (value);
478+
String nullableStrValue = value == null ? null : String.valueOf(value);
476479
if (nullableStrValue != null && reverseBits) {
477480
nullableStrValue = JBBPFieldString.reverseBits(nullableStrValue);
478481
}

jbbp/src/main/java/com/igormaznitsa/jbbp/mapper/JBBPMapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import com.igormaznitsa.jbbp.utils.Function;
3939
import com.igormaznitsa.jbbp.utils.JBBPUtils;
4040
import com.igormaznitsa.jbbp.utils.ReflectUtils;
41-
4241
import java.lang.reflect.Array;
4342
import java.lang.reflect.Field;
4443
import java.lang.reflect.InvocationTargetException;
@@ -314,7 +313,9 @@ public static <T> T map(final JBBPFieldStruct rootStructure, final T instance, f
314313
continue;
315314
}
316315

317-
mappingField = ReflectUtils.makeAccessible(mappingField);
316+
if (!ReflectUtils.isPotentiallyAccessibleField(mappingField)) {
317+
mappingField = ReflectUtils.makeAccessible(mappingField);
318+
}
318319

319320
final Bin fieldAnno = mappingField.getAnnotation(Bin.class);
320321
final Bin mappedAnno;

jbbp/src/main/java/com/igormaznitsa/jbbp/utils/JBBPTextWriterExtraAdapter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.igormaznitsa.jbbp.exceptions.JBBPException;
2020
import com.igormaznitsa.jbbp.mapper.Bin;
21-
2221
import java.io.IOException;
2322
import java.lang.reflect.Field;
2423

@@ -97,7 +96,11 @@ public void onReachedMaxValueNumberForLine(final JBBPTextWriter context) throws
9796
public Object extractFieldValue(final Object instance, final Field field) {
9897
JBBPUtils.assertNotNull(field, "Field must not be null");
9998
try {
100-
return ReflectUtils.makeAccessible(field).get(instance);
99+
if (ReflectUtils.isPotentiallyAccessibleField(field)) {
100+
return field.get(instance);
101+
} else {
102+
return ReflectUtils.makeAccessible(field).get(instance);
103+
}
101104
} catch (Exception ex) {
102105
throw new JBBPException("Can't extract value from field for exception", ex);
103106
}

jbbp/src/main/java/com/igormaznitsa/jbbp/utils/ReflectUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.igormaznitsa.jbbp.utils;
22

33
import java.lang.reflect.AccessibleObject;
4+
import java.lang.reflect.Field;
5+
import java.lang.reflect.Modifier;
46
import java.security.AccessController;
57
import java.security.PrivilegedAction;
68
import java.util.Queue;
@@ -21,6 +23,19 @@ public final class ReflectUtils {
2123
private ReflectUtils() {
2224
}
2325

26+
public static boolean isPotentiallyAccessibleClass(final Class<?> klazz) {
27+
if (klazz.isLocalClass() && !isPotentiallyAccessibleClass(klazz.getEnclosingClass())) {
28+
return false;
29+
}
30+
return Modifier.isPublic(klazz.getModifiers());
31+
}
32+
33+
public static boolean isPotentiallyAccessibleField(final Field field) {
34+
return isPotentiallyAccessibleClass(field.getDeclaringClass())
35+
&& Modifier.isPublic(field.getModifiers())
36+
&& !Modifier.isFinal(field.getModifiers());
37+
}
38+
2439
/**
2540
* Make accessible an accessible object, AccessController.doPrivileged will be
2641
* called.

jbbp/src/test/java/com/igormaznitsa/jbbp/TestUtils.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616

1717
package com.igormaznitsa.jbbp;
1818

19-
import java.lang.reflect.Field;
20-
import java.lang.reflect.Modifier;
21-
import java.util.Locale;
22-
import java.util.zip.CRC32;
23-
2419
import static org.junit.jupiter.api.Assertions.assertEquals;
2520

21+
22+
import java.util.Locale;
2623
import org.apache.commons.codec.digest.PureJavaCrc32;
2724

2825
/**
@@ -36,27 +33,6 @@ public enum TestUtils {
3633
*/
3734
public static final float FLOAT_DELTA = Float.MIN_VALUE;
3835

39-
40-
/**
41-
* Inject new value into final field
42-
*
43-
* @param klazz a class which field must be injected, must not be null
44-
* @param instance the instance of the class, it can be null for static fields
45-
* @param fieldName the field name, must not be null
46-
* @param value the value to be injected
47-
* @throws Exception it will be thrown for any error
48-
*/
49-
public static void injectDeclaredFinalFieldValue(final Class<?> klazz, final Object instance, final String fieldName, final Object value) throws Exception {
50-
final Field field = klazz.getDeclaredField(fieldName);
51-
field.setAccessible(true);
52-
53-
final Field modifiersField = Field.class.getDeclaredField("modifiers");
54-
modifiersField.setAccessible(true);
55-
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
56-
57-
field.set(instance, value);
58-
}
59-
6036
/**
6137
* Read field value, also allows to provide dot-separated chain of fields
6238
*

0 commit comments

Comments
 (0)