Skip to content

Commit 7d11616

Browse files
authored
autodetect unsafe allocations metadata (#2883)
1 parent d15e32e commit 7d11616

4 files changed

Lines changed: 438 additions & 5 deletions

File tree

jme3-core/src/main/java/com/jme3/util/PreserveReflection.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,11 @@
4343
@Retention(RetentionPolicy.RUNTIME)
4444
@Target(ElementType.TYPE)
4545
public @interface PreserveReflection {
46+
/**
47+
* When true, metadata generators should also allow this type to be allocated
48+
* without invoking a constructor.
49+
*
50+
* @return true to include unsafe allocation metadata
51+
*/
52+
boolean allowUnsafeAllocation() default true;
4653
}

jme3-nativeimage-plugin/README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ public class MyNiftyController {
7777
```
7878

7979
Classes annotated with `@PreserveReflection` are included by default when this
80-
plugin generates metadata.
80+
plugin generates metadata. They are also marked as unsafe-allocatable by default.
81+
Disable that when a reflected type must always be constructed normally:
82+
83+
```java
84+
@PreserveReflection(allowUnsafeAllocation = false)
85+
public class ConstructorOnlyEntryPoint {
86+
}
87+
```
8188

8289

8390
## Default Resource Settings
@@ -134,6 +141,32 @@ jmeNativeImage {
134141
}
135142
```
136143

144+
### Unsafe Allocation
145+
146+
The plugin automatically detects direct construction of known unsafe allocation
147+
container types, such as `SafeArrayList(SomeType.class)`, in compiled classes and
148+
adds unsafe allocation metadata for the corresponding `SomeType[]` array storage.
149+
By default, the container list contains `com.jme3.util.SafeArrayList`.
150+
151+
You can also add explicit unsafe allocation entries:
152+
153+
```groovy
154+
jmeNativeImage {
155+
unsafeAllocatedType 'org.example.Foo'
156+
unsafeAllocatedType 'org.example.Bar[]'
157+
unsafeAllocatedType '[Lorg.example.Baz;'
158+
}
159+
```
160+
161+
If a project has another container type with the same `Class`-first constructor
162+
pattern, add it to the detector:
163+
164+
```groovy
165+
jmeNativeImage {
166+
unsafeAllocationContainerType 'org.example.MyArrayStore'
167+
}
168+
```
169+
137170
### Proxy Interfaces
138171

139172
If your application uses dynamic proxies, configure the interface set:

jme3-nativeimage-plugin/src/main/groovy/org/jmonkeyengine/gradle/nativeimage/JmeNativeImageExtension.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class JmeNativeImageExtension {
1414
final ListProperty<String> additionalTargetTypes
1515
final ListProperty<String> additionalTargetAnnotations
1616
final ListProperty<List<String>> additionalProxyInterfaceSets
17+
final ListProperty<String> additionalUnsafeAllocatedTypes
18+
final ListProperty<String> additionalUnsafeAllocationContainerTypes
1719
final ListProperty<String> additionalResourceGlobs
1820
final Property<String> includeResourcesPattern
1921
final Property<String> excludeResourcesPattern
@@ -24,6 +26,8 @@ class JmeNativeImageExtension {
2426
additionalTargetTypes = objects.listProperty(String).convention([])
2527
additionalTargetAnnotations = objects.listProperty(String).convention([])
2628
additionalProxyInterfaceSets = objects.listProperty(List).convention([])
29+
additionalUnsafeAllocatedTypes = objects.listProperty(String).convention([])
30+
additionalUnsafeAllocationContainerTypes = objects.listProperty(String).convention([])
2731
additionalResourceGlobs = objects.listProperty(String).convention([])
2832
includeResourcesPattern = objects.property(String)
2933
excludeResourcesPattern = objects.property(String)
@@ -42,6 +46,14 @@ class JmeNativeImageExtension {
4246
additionalProxyInterfaceSets.add(interfaceClassNames.toList())
4347
}
4448

49+
void unsafeAllocatedType(String className) {
50+
additionalUnsafeAllocatedTypes.add(className)
51+
}
52+
53+
void unsafeAllocationContainerType(String className) {
54+
additionalUnsafeAllocationContainerTypes.add(className)
55+
}
56+
4557
void resourceGlob(String glob) {
4658
additionalResourceGlobs.add(glob)
4759
}

0 commit comments

Comments
 (0)