1
1
/* Licensed under MIT 2022-2024. */
2
2
package edu .kit .kastel .mcse .ardoco .core .configuration ;
3
3
4
- import java .io .IOException ;
5
- import java .io .ObjectInputStream ;
6
- import java .io .ObjectOutputStream ;
7
- import java .io .Serial ;
8
4
import java .lang .reflect .Field ;
9
5
import java .lang .reflect .ParameterizedType ;
10
6
import java .util .ArrayList ;
14
10
import java .util .SortedMap ;
15
11
import java .util .TreeMap ;
16
12
17
- import org .apache .commons .lang3 .reflect .FieldUtils ;
18
13
import org .slf4j .Logger ;
19
14
import org .slf4j .LoggerFactory ;
20
15
21
16
import edu .kit .kastel .mcse .ardoco .core .architecture .Deterministic ;
22
17
23
18
@ Deterministic
24
19
public abstract class AbstractConfigurable implements IConfigurable {
25
- protected final Logger logger = LoggerFactory .getLogger (this .getClass ());
26
-
27
20
public static final String CLASS_ATTRIBUTE_CONNECTOR = "::" ;
28
21
public static final String KEY_VALUE_CONNECTOR = "=" ;
29
22
public static final String LIST_SEPARATOR = "," ;
30
23
24
+ @ SuppressWarnings ("java:S2065" ) // The logger is used in the subclasses that are serializable
25
+ private transient Logger logger ;
26
+
31
27
private SortedMap <String , String > lastAppliedConfiguration = new TreeMap <>();
32
28
33
29
@ Override
@@ -50,7 +46,7 @@ private void applyConfiguration(SortedMap<String, String> additionalConfiguratio
50
46
}
51
47
52
48
if (currentClassInHierarchy .getAnnotation (NoConfiguration .class ) != null ) {
53
- this .logger .debug ("Skipping configuration for class {}" , currentClassInHierarchy .getSimpleName ());
49
+ this .getLogger () .debug ("Skipping configuration for class {}" , currentClassInHierarchy .getSimpleName ());
54
50
return ;
55
51
}
56
52
@@ -101,7 +97,7 @@ private void setValue(Field field, String value) {
101
97
field .setAccessible (true );
102
98
field .set (this , parsedValue );
103
99
} catch (Exception e ) {
104
- this .logger .error (e .getMessage (), e );
100
+ this .getLogger () .error (e .getMessage (), e );
105
101
}
106
102
}
107
103
@@ -131,20 +127,10 @@ private Object parse(Field field, Class<?> fieldsClass, String value) {
131
127
throw new IllegalArgumentException ("Could not find a parse method for fields of type: " + fieldsClass );
132
128
}
133
129
134
- @ Serial
135
- private void writeObject (ObjectOutputStream objectOutputStream ) throws IOException {
136
- objectOutputStream .defaultWriteObject ();
137
- }
138
-
139
- @ Serial
140
- private void readObject (ObjectInputStream objectInputStream ) throws IOException , ClassNotFoundException {
141
- objectInputStream .defaultReadObject ();
142
- try {
143
- var loggerField = Arrays .stream (FieldUtils .getAllFields (this .getClass ())).filter (f -> f .getName ().equals ("logger" )).findFirst ().orElseThrow ();
144
- loggerField .setAccessible (true );
145
- loggerField .set (this , LoggerFactory .getLogger (this .getClass ()));
146
- } catch (IllegalAccessException e ) {
147
- throw new IllegalAccessError (e .getMessage ());
130
+ protected final Logger getLogger () {
131
+ if (this .logger == null ) {
132
+ this .logger = LoggerFactory .getLogger (this .getClass ());
148
133
}
134
+ return this .logger ;
149
135
}
150
136
}
0 commit comments