Skip to content

Commit 641d349

Browse files
committed
Bug 38282554 - [38277648->25.09] GraalVM Add Coherence GraalVM native feature
(merge main -> ce/main 118406) [git-p4: depot-paths = "//dev/coherence-ce/main/": change = 118409]
1 parent f0927a1 commit 641d349

File tree

14 files changed

+166
-80
lines changed

14 files changed

+166
-80
lines changed

prj/coherence-bedrock/coherence-bedrock/src/main/java/com/oracle/bedrock/runtime/coherence/graal/BedrockNativeImageFeature.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ protected Set<Class<? extends Annotation>> getAnnotations()
4444
return ANNOTATIONS;
4545
}
4646

47+
@Override
48+
protected Set<String> getLoadAllClassesFromPackages()
49+
{
50+
return Set.of("com.oracle.bedrock");
51+
}
52+
4753
// ----- data members ---------------------------------------------------
4854

4955
/**

prj/coherence-core-components/src/main/java/com/tangosol/coherence/graal/AbstractNativeImageFeature.java

Lines changed: 30 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,11 @@
1717
import org.graalvm.nativeimage.hosted.RuntimeReflection;
1818
import org.graalvm.nativeimage.hosted.RuntimeSerialization;
1919

20-
import java.io.IOException;
21-
2220
import java.lang.annotation.Annotation;
2321
import java.lang.reflect.Constructor;
2422
import java.lang.reflect.Method;
2523

26-
import java.nio.file.Files;
2724
import java.nio.file.Path;
28-
import java.nio.file.Paths;
29-
30-
import java.util.Comparator;
3125

3226
import java.util.List;
3327
import java.util.Set;
@@ -36,7 +30,6 @@
3630

3731
import java.util.function.BiConsumer;
3832
import java.util.function.Consumer;
39-
import java.util.stream.Collectors;
4033

4134
/**
4235
* A base class for GraalVM native {@link Feature} implementations.
@@ -48,8 +41,8 @@ public abstract class AbstractNativeImageFeature
4841
@Override
4942
public void beforeAnalysis(BeforeAnalysisAccess access)
5043
{
51-
ClassLoader imageClassLoader = access.getApplicationClassLoader();
52-
List<Path> classPath = access.getApplicationClassPath();
44+
ClassLoader imageClassLoader = access.getApplicationClassLoader();
45+
List<Path> classPath = access.getApplicationClassPath();
5346

5447
// Register a reachability handler for all the serializable types
5548
// which will register any subtypes for serialization
@@ -62,20 +55,37 @@ public void beforeAnalysis(BeforeAnalysisAccess access)
6255
access.registerSubtypeReachabilityHandler(SerializableReachableTypeHandler.INSTANCE, clazz);
6356
}
6457

58+
Set<String> setPackage = getLoadAllClassesFromPackages();
59+
System.out.println("Oracle Coherence: Registering all classes from packages: " + setPackage);
60+
6561
// Find any subtypes of serializable classes on the class path
6662
// and register them
6763
scan(imageClassLoader, classPath, classInfo ->
6864
{
6965
try
7066
{
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+
7280
for (Class<?> serializableType : getSerializableTypes())
7381
{
7482
if (serializableType.isAssignableFrom(clazz))
7583
{
76-
logRegistration(serializableType, clazz);
7784
RuntimeSerialization.register(clazz);
78-
registerAllElements(clazz);
85+
if (!fRegistered)
86+
{
87+
registerAllElements(clazz);
88+
}
7989
break;
8090
}
8191
}
@@ -106,7 +116,6 @@ public void afterRegistration(AfterRegistrationAccess access)
106116
{
107117
if (clazz.getAnnotation(annotation) != null)
108118
{
109-
logRegistration(annotation, clazz);
110119
registerAllElements(clazz);
111120
registered = true;
112121
break;
@@ -117,9 +126,8 @@ public void afterRegistration(AfterRegistrationAccess access)
117126
{
118127
for (Class<?> handledSuperType : getSupertypes())
119128
{
120-
if (!handledSuperType.isAssignableFrom(clazz))
129+
if (handledSuperType.isAssignableFrom(clazz))
121130
{
122-
logRegistration(handledSuperType, clazz);
123131
registerAllElements(clazz);
124132
break;
125133
}
@@ -133,18 +141,15 @@ public void afterRegistration(AfterRegistrationAccess access)
133141
// ignore: due to incomplete classpath
134142
}
135143
});
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-
}
146144
}
147145

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+
148153
/**
149154
* Return the set of supertypes to register for serialization with all subtypes of these types.
150155
*
@@ -216,11 +221,6 @@ protected static void registerAllElements(Class<?> clazz)
216221
RuntimeReflection.register(clazz.getDeclaredFields());
217222
}
218223

219-
protected static String getProcessedElementsPath()
220-
{
221-
return System.getProperty("com.oracle.coherence.graal.processedElementsPath");
222-
}
223-
224224
protected static void registerClass(Class<?> clazz)
225225
{
226226
/* Register all members: a new API is coming where this is one line */
@@ -259,37 +259,6 @@ protected static void registerClass(Class<?> clazz)
259259
}
260260
}
261261

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-
293262
// ----- inner class: SerializableReachableTypeHandler ------------------
294263

295264
/**
@@ -319,11 +288,4 @@ public void accept(DuringAnalysisAccess access, Class<?> clazz)
319288
*/
320289
protected static final Set<Class<?>> processed = ConcurrentHashMap.newKeySet();
321290
}
322-
323-
// ----- data members ---------------------------------------------------
324-
325-
/**
326-
* The types already processed by the feature.
327-
*/
328-
private final Set<ReasonClass> processedTypes = ConcurrentHashMap.newKeySet();
329291
}

0 commit comments

Comments
 (0)