@@ -157,6 +157,8 @@ private static class NativeStringTracking {
157
157
protected static final int CALCULATE_SIZE = -1 ;
158
158
static final Map <Class <?>, LayoutInfo > layoutInfo = new WeakHashMap <>();
159
159
static final Map <Class <?>, List <String >> fieldOrder = new WeakHashMap <>();
160
+ static final Map <Class <?>, List <Field >> fieldList = new WeakHashMap <>();
161
+ static final Map <Class <?>, Boolean > validationMap = new WeakHashMap <>();
160
162
161
163
// This field is accessed by native code
162
164
private Pointer memory ;
@@ -1015,22 +1017,26 @@ protected void sortFields(List<Field> fields, List<String> names) {
1015
1017
* this {@link Structure} class.
1016
1018
*/
1017
1019
protected List <Field > getFieldList () {
1018
- List <Field > flist = new ArrayList <>();
1019
- for (Class <?> cls = getClass ();
1020
- !cls .equals (Structure .class );
1021
- cls = cls .getSuperclass ()) {
1022
- List <Field > classFields = new ArrayList <>();
1023
- Field [] fields = cls .getDeclaredFields ();
1024
- for (int i =0 ;i < fields .length ;i ++) {
1025
- int modifiers = fields [i ].getModifiers ();
1026
- if (Modifier .isStatic (modifiers ) || !Modifier .isPublic (modifiers )) {
1027
- continue ;
1020
+ synchronized (fieldList ) {
1021
+ return fieldList .computeIfAbsent (getClass (), (c ) -> {
1022
+ List <Field > flist = new ArrayList <>();
1023
+ List <Field > classFields = new ArrayList <>();
1024
+ for (Class <?> cls = getClass ();
1025
+ !cls .equals (Structure .class );
1026
+ cls = cls .getSuperclass ()) {
1027
+ for (Field field : cls .getDeclaredFields ()) {
1028
+ int modifiers = field .getModifiers ();
1029
+ if (Modifier .isStatic (modifiers ) || !Modifier .isPublic (modifiers )) {
1030
+ continue ;
1031
+ }
1032
+ classFields .add (field );
1033
+ }
1034
+ flist .addAll (0 , classFields );
1035
+ classFields .clear ();
1028
1036
}
1029
- classFields .add (fields [i ]);
1030
- }
1031
- flist .addAll (0 , classFields );
1037
+ return flist ;
1038
+ });
1032
1039
}
1033
- return flist ;
1034
1040
}
1035
1041
1036
1042
/** Cache field order per-class.
@@ -1039,12 +1045,7 @@ protected List<Field> getFieldList() {
1039
1045
private List <String > fieldOrder () {
1040
1046
Class <?> clazz = getClass ();
1041
1047
synchronized (fieldOrder ) {
1042
- List <String > list = fieldOrder .get (clazz );
1043
- if (list == null ) {
1044
- list = getFieldOrder ();
1045
- fieldOrder .put (clazz , list );
1046
- }
1047
- return list ;
1048
+ return fieldOrder .computeIfAbsent (clazz , (c ) -> getFieldOrder ());
1048
1049
}
1049
1050
}
1050
1051
@@ -1250,9 +1251,13 @@ private void validateField(String name, Class<?> type) {
1250
1251
1251
1252
/** ensure all fields are of valid type. */
1252
1253
private void validateFields () {
1253
- List <Field > fields = getFieldList ();
1254
- for (Field f : fields ) {
1255
- validateField (f .getName (), f .getType ());
1254
+ synchronized (validationMap ) {
1255
+ validationMap .computeIfAbsent (getClass (), (cls ) -> {
1256
+ for (Field f : getFieldList ()) {
1257
+ validateField (f .getName (), f .getType ());
1258
+ }
1259
+ return true ;
1260
+ });
1256
1261
}
1257
1262
}
1258
1263
0 commit comments