3030import java .util .Arrays ;
3131import java .util .HashSet ;
3232import java .util .Set ;
33- import java .util .stream .Collectors ;
34- import java .util .stream .Stream ;
3533
3634import org .graalvm .nativeimage .Platform ;
3735import org .graalvm .nativeimage .Platforms ;
3836import org .graalvm .nativeimage .impl .ConfigurationCondition ;
3937
38+ import com .oracle .svm .core .hub .DynamicHub ;
4039import com .oracle .svm .core .util .VMError ;
4140import com .oracle .svm .util .LogUtils ;
4241
4342/**
44- * Represents a group of {@link RuntimeCondition}s that guard a value.
43+ * Represents a group of {@link #conditions} that guard a value. The conditions are encoded
4544 * <p>
4645 * If any of the {@link #conditions} is satisfied then the whole set becomes also
4746 * {@link #satisfied}. {@link RuntimeConditionSet}s can be created at build time
48- * {@link #createHosted(ConfigurationCondition... )} and stored to the image heap, or it can be
49- * encoded ({@link #getTypesForEncoding()} and later decoded at run time
50- * ({@link #createRuntime(Set)}. The current implementation does not cache {@link #conditions},
51- * although this will be implemented in the future (GR-49526)
47+ * {@link #createHosted(ConfigurationCondition)} and stored to the image heap, or it can be encoded
48+ * ({@link #getTypesForEncoding()} and later decoded at run time ({@link #createDecoded(Object[])}.
49+ * The current implementation does not cache {@link #conditions}, although this will be implemented
50+ * in the future (GR-49526)
5251 */
5352public class RuntimeConditionSet {
5453
55- private RuntimeCondition [] conditions ;
54+ private Object [] conditions ;
5655 private boolean satisfied ;
5756
5857 @ Platforms (Platform .HOSTED_ONLY .class )
59- public static RuntimeConditionSet createHosted (ConfigurationCondition ... conditions ) {
60- var conditionSet = new RuntimeConditionSet (Set .of ());
61- for (ConfigurationCondition condition : conditions ) {
62- conditionSet .addCondition (condition );
63- }
64- return conditionSet ;
58+ public static RuntimeConditionSet emptySet () {
59+ return new RuntimeConditionSet (new Object [0 ]);
6560 }
6661
6762 @ Platforms (Platform .HOSTED_ONLY .class )
68- public static RuntimeConditionSet unmodifiableEmptySet () {
69- return UnmodifiableRuntimeConditionSet .UNMODIFIABLE_EMPTY_SET ;
63+ public static RuntimeConditionSet createHosted (ConfigurationCondition condition ) {
64+ var conditionSet = new RuntimeConditionSet (new Object [0 ]);
65+ conditionSet .addCondition (condition );
66+ return conditionSet ;
7067 }
7168
7269 @ Platforms (Platform .HOSTED_ONLY .class )
@@ -80,10 +77,10 @@ public synchronized void addCondition(ConfigurationCondition cnd) {
8077 return ;
8178 }
8279
83- RuntimeCondition newRuntimeCondition = RuntimeCondition . create (cnd );
84- Stream < RuntimeCondition > existingConditions = conditions == null ? Stream . empty () : Arrays .stream (conditions );
85- setConditions ( Stream . concat ( existingConditions , Stream . of (newRuntimeCondition ))
86- . collect ( Collectors . toSet () ));
80+ Object newRuntimeCondition = createRuntimeCondition (cnd );
81+ Set < Object > existingConditions = conditions == null ? new HashSet <> () : new HashSet <>( Arrays .asList (conditions ) );
82+ existingConditions . add (newRuntimeCondition );
83+ setConditions ( existingConditions . toArray ( ));
8784 }
8885
8986 @ Platforms (Platform .HOSTED_ONLY .class )
@@ -92,28 +89,19 @@ public Set<Class<?>> getTypesForEncoding() {
9289 return Set .of ();
9390 } else {
9491 Set <Class <?>> types = new HashSet <>();
95- for (RuntimeCondition condition : conditions ) {
96- types .addAll (condition . getTypesForEncoding ());
92+ for (Object condition : conditions ) {
93+ types .addAll (getTypesForEncoding (condition ));
9794 }
9895 return types ;
9996 }
10097 }
10198
102- public static RuntimeConditionSet createRuntime (Set <RuntimeCondition > conditions ) {
103- return new RuntimeConditionSet (conditions );
104- }
105-
106- private RuntimeConditionSet (Set <RuntimeCondition > conditions ) {
107- setConditions (conditions );
99+ public static RuntimeConditionSet unmodifiableEmptySet () {
100+ return UnmodifiableRuntimeConditionSet .UNMODIFIABLE_EMPTY_SET ;
108101 }
109102
110- private void setConditions (Set <RuntimeCondition > conditions ) {
111- if (conditions .isEmpty ()) {
112- this .conditions = null ;
113- } else {
114- this .conditions = conditions .toArray (RuntimeCondition []::new );
115- }
116- satisfied = false ;
103+ public static RuntimeConditionSet createDecoded (Object [] conditions ) {
104+ return new RuntimeConditionSet (conditions );
117105 }
118106
119107 /**
@@ -133,8 +121,8 @@ public boolean satisfied() {
133121 if (localConditions == null ) {
134122 result = true ;
135123 } else {
136- for (RuntimeCondition condition : localConditions ) {
137- if (condition . isSatisfied ()) {
124+ for (Object condition : localConditions ) {
125+ if (isSatisfied (condition )) {
138126 conditions = null ;
139127 satisfied = result = true ;
140128 break ;
@@ -158,10 +146,46 @@ public String toString() {
158146 return conditionsString + " = " + satisfied ;
159147 }
160148
149+ private RuntimeConditionSet (Object [] conditions ) {
150+ setConditions (conditions );
151+ }
152+
153+ private void setConditions (Object [] conditions ) {
154+ if (conditions .length == 0 ) {
155+ this .conditions = null ;
156+ } else {
157+ this .conditions = conditions ;
158+ }
159+ satisfied = false ;
160+ }
161+
162+ private static Object createRuntimeCondition (ConfigurationCondition cnd ) {
163+ if (cnd .isAlwaysTrue () || !cnd .isRuntimeChecked ()) {
164+ throw VMError .shouldNotReachHere ("We should never create run-time conditions from conditions that are always true at build time. Condition: " + cnd );
165+ }
166+ return cnd .getType ();
167+ }
168+
169+ private static boolean isSatisfied (Object condition ) {
170+ if (condition instanceof Class <?> typeReachedCondition ) {
171+ return DynamicHub .fromClass (typeReachedCondition ).isReached ();
172+ } else {
173+ throw VMError .shouldNotReachHere ("Only typeReached condition is supported." );
174+ }
175+ }
176+
177+ private static Set <Class <?>> getTypesForEncoding (Object condition ) {
178+ if (condition instanceof Class <?> res ) {
179+ return Set .of (res );
180+ } else {
181+ throw VMError .shouldNotReachHere ("Only typeReached condition is supported." );
182+ }
183+ }
184+
161185 public static final class UnmodifiableRuntimeConditionSet extends RuntimeConditionSet {
162- private static final RuntimeConditionSet UNMODIFIABLE_EMPTY_SET = new UnmodifiableRuntimeConditionSet (Set . of () );
186+ private static final RuntimeConditionSet UNMODIFIABLE_EMPTY_SET = new UnmodifiableRuntimeConditionSet (new Object [ 0 ] );
163187
164- private UnmodifiableRuntimeConditionSet (Set < RuntimeCondition > conditions ) {
188+ private UnmodifiableRuntimeConditionSet (Object [] conditions ) {
165189 super (conditions );
166190 }
167191
0 commit comments