17
17
import org .graalvm .nativeimage .hosted .RuntimeReflection ;
18
18
import org .graalvm .nativeimage .hosted .RuntimeSerialization ;
19
19
20
- import java .io .IOException ;
21
-
22
20
import java .lang .annotation .Annotation ;
23
21
import java .lang .reflect .Constructor ;
24
22
import java .lang .reflect .Method ;
25
23
26
- import java .nio .file .Files ;
27
24
import java .nio .file .Path ;
28
- import java .nio .file .Paths ;
29
-
30
- import java .util .Comparator ;
31
25
32
26
import java .util .List ;
33
27
import java .util .Set ;
36
30
37
31
import java .util .function .BiConsumer ;
38
32
import java .util .function .Consumer ;
39
- import java .util .stream .Collectors ;
40
33
41
34
/**
42
35
* A base class for GraalVM native {@link Feature} implementations.
@@ -48,8 +41,8 @@ public abstract class AbstractNativeImageFeature
48
41
@ Override
49
42
public void beforeAnalysis (BeforeAnalysisAccess access )
50
43
{
51
- ClassLoader imageClassLoader = access .getApplicationClassLoader ();
52
- List <Path > classPath = access .getApplicationClassPath ();
44
+ ClassLoader imageClassLoader = access .getApplicationClassLoader ();
45
+ List <Path > classPath = access .getApplicationClassPath ();
53
46
54
47
// Register a reachability handler for all the serializable types
55
48
// which will register any subtypes for serialization
@@ -62,20 +55,37 @@ public void beforeAnalysis(BeforeAnalysisAccess access)
62
55
access .registerSubtypeReachabilityHandler (SerializableReachableTypeHandler .INSTANCE , clazz );
63
56
}
64
57
58
+ Set <String > setPackage = getLoadAllClassesFromPackages ();
59
+ System .out .println ("Oracle Coherence: Registering all classes from packages: " + setPackage );
60
+
65
61
// Find any subtypes of serializable classes on the class path
66
62
// and register them
67
63
scan (imageClassLoader , classPath , classInfo ->
68
64
{
69
65
try
70
66
{
71
- var clazz = Class .forName (classInfo .getName (), false , imageClassLoader );
67
+ var clazz = Class .forName (classInfo .getName (), false , imageClassLoader );
68
+ boolean fRegistered = false ;
69
+
70
+ for (String packageName : setPackage )
71
+ {
72
+ if (classInfo .getPackageName ().startsWith (packageName ))
73
+ {
74
+ registerAllElements (clazz );
75
+ fRegistered = true ;
76
+ break ;
77
+ }
78
+ }
79
+
72
80
for (Class <?> serializableType : getSerializableTypes ())
73
81
{
74
82
if (serializableType .isAssignableFrom (clazz ))
75
83
{
76
- logRegistration (serializableType , clazz );
77
84
RuntimeSerialization .register (clazz );
78
- registerAllElements (clazz );
85
+ if (!fRegistered )
86
+ {
87
+ registerAllElements (clazz );
88
+ }
79
89
break ;
80
90
}
81
91
}
@@ -106,7 +116,6 @@ public void afterRegistration(AfterRegistrationAccess access)
106
116
{
107
117
if (clazz .getAnnotation (annotation ) != null )
108
118
{
109
- logRegistration (annotation , clazz );
110
119
registerAllElements (clazz );
111
120
registered = true ;
112
121
break ;
@@ -117,9 +126,8 @@ public void afterRegistration(AfterRegistrationAccess access)
117
126
{
118
127
for (Class <?> handledSuperType : getSupertypes ())
119
128
{
120
- if (! handledSuperType .isAssignableFrom (clazz ))
129
+ if (handledSuperType .isAssignableFrom (clazz ))
121
130
{
122
- logRegistration (handledSuperType , clazz );
123
131
registerAllElements (clazz );
124
132
break ;
125
133
}
@@ -133,18 +141,15 @@ public void afterRegistration(AfterRegistrationAccess access)
133
141
// ignore: due to incomplete classpath
134
142
}
135
143
});
136
-
137
- /* Dump processed elements into Json */
138
- if (getProcessedElementsPath () != null )
139
- {
140
- writeToFile (getProcessedElementsPath (), processedTypes .stream ()
141
- .sorted (Comparator .comparing (c -> c .type .getTypeName ()))
142
- .map (c -> "{ \" reason\" : \" " + c .reason + "\" , \" type\" : \" " + c .type .getTypeName () + "\" }" )
143
- .collect (Collectors .joining (",\n " , "[\n " , "\n ]" ))
144
- );
145
- }
146
144
}
147
145
146
+ /**
147
+ * Return the set of package names to register all classes from.
148
+ *
149
+ * @return the set of package names to register all classes from
150
+ */
151
+ protected abstract Set <String > getLoadAllClassesFromPackages ();
152
+
148
153
/**
149
154
* Return the set of supertypes to register for serialization with all subtypes of these types.
150
155
*
@@ -216,11 +221,6 @@ protected static void registerAllElements(Class<?> clazz)
216
221
RuntimeReflection .register (clazz .getDeclaredFields ());
217
222
}
218
223
219
- protected static String getProcessedElementsPath ()
220
- {
221
- return System .getProperty ("com.oracle.coherence.graal.processedElementsPath" );
222
- }
223
-
224
224
protected static void registerClass (Class <?> clazz )
225
225
{
226
226
/* Register all members: a new API is coming where this is one line */
@@ -259,37 +259,6 @@ protected static void registerClass(Class<?> clazz)
259
259
}
260
260
}
261
261
262
- protected static void writeToFile (String filePath , String content )
263
- {
264
- try
265
- {
266
- Path path = Paths .get (filePath );
267
- if (path .getParent () != null )
268
- {
269
- Files .createDirectories (path .getParent ());
270
- }
271
- Files .writeString (path , content );
272
- }
273
- catch (IOException e )
274
- {
275
- throw new RuntimeException (e );
276
- }
277
- }
278
-
279
- protected void logRegistration (Class <?> reason , Class <?> clazz )
280
- {
281
- if (getProcessedElementsPath () != null )
282
- {
283
- processedTypes .add (new ReasonClass (reason , clazz ));
284
- }
285
- }
286
-
287
- // ----- inner class: ReasonClass ---------------------------------------
288
-
289
- protected record ReasonClass (Class <?> reason , Class <?> type )
290
- {
291
- }
292
-
293
262
// ----- inner class: SerializableReachableTypeHandler ------------------
294
263
295
264
/**
@@ -319,11 +288,4 @@ public void accept(DuringAnalysisAccess access, Class<?> clazz)
319
288
*/
320
289
protected static final Set <Class <?>> processed = ConcurrentHashMap .newKeySet ();
321
290
}
322
-
323
- // ----- data members ---------------------------------------------------
324
-
325
- /**
326
- * The types already processed by the feature.
327
- */
328
- private final Set <ReasonClass > processedTypes = ConcurrentHashMap .newKeySet ();
329
291
}
0 commit comments