-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19115 from stuartwdouglas/weak-reflection
New weak reflection implementation
- Loading branch information
Showing
3 changed files
with
132 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
core/runtime/src/main/java/io/quarkus/runtime/graal/WeakReflection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.quarkus.runtime.graal; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Consumer; | ||
|
||
import org.graalvm.nativeimage.hosted.Feature; | ||
import org.graalvm.nativeimage.hosted.RuntimeReflection; | ||
import org.jboss.logging.Logger; | ||
|
||
/** | ||
* Weak reflection implementation called from generated bytecode | ||
*/ | ||
public class WeakReflection { | ||
|
||
static final Logger log = Logger.getLogger(WeakReflection.class); | ||
|
||
public static void register(Feature.BeforeAnalysisAccess analysisAccess, Class<?> clazz, boolean constructors, | ||
boolean methods, boolean fields) { | ||
analysisAccess.registerReachabilityHandler(new Callback(clazz, constructors, methods, fields), clazz); | ||
} | ||
|
||
public static class Callback implements Consumer<Feature.DuringAnalysisAccess> { | ||
final AtomicBoolean onlyOnce = new AtomicBoolean(false); | ||
final Class<?> clazz; | ||
final boolean constructors; | ||
final boolean methods; | ||
final boolean fields; | ||
|
||
public Callback(Class<?> clazz, boolean constructors, boolean methods, boolean fields) { | ||
this.clazz = clazz; | ||
this.constructors = constructors; | ||
this.methods = methods; | ||
this.fields = fields; | ||
} | ||
|
||
@Override | ||
public void accept(Feature.DuringAnalysisAccess duringAnalysisAccess) { | ||
if (!onlyOnce.compareAndSet(false, true)) { | ||
return; | ||
} | ||
log.debugf("Registering %s for reflection as it is reachable", clazz); | ||
RuntimeReflection.register(clazz); | ||
if (fields) { | ||
RuntimeReflection.register(clazz.getDeclaredFields()); | ||
} | ||
if (constructors) { | ||
RuntimeReflection.register(clazz.getDeclaredConstructors()); | ||
} | ||
if (methods) { | ||
RuntimeReflection.register(clazz.getDeclaredMethods()); | ||
} | ||
} | ||
} | ||
|
||
} |