30
30
import java .util .Arrays ;
31
31
import java .util .HashSet ;
32
32
import java .util .Set ;
33
- import java .util .stream .Collectors ;
34
- import java .util .stream .Stream ;
35
33
36
34
import org .graalvm .nativeimage .Platform ;
37
35
import org .graalvm .nativeimage .Platforms ;
38
36
import org .graalvm .nativeimage .impl .ConfigurationCondition ;
39
37
38
+ import com .oracle .svm .core .hub .DynamicHub ;
40
39
import com .oracle .svm .core .util .VMError ;
41
40
import com .oracle .svm .util .LogUtils ;
42
41
43
42
/**
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
45
44
* <p>
46
45
* If any of the {@link #conditions} is satisfied then the whole set becomes also
47
46
* {@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)
52
51
*/
53
52
public class RuntimeConditionSet {
54
53
55
- private RuntimeCondition [] conditions ;
54
+ private Object [] conditions ;
56
55
private boolean satisfied ;
57
56
58
57
@ 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 ]);
65
60
}
66
61
67
62
@ 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 ;
70
67
}
71
68
72
69
@ Platforms (Platform .HOSTED_ONLY .class )
@@ -80,10 +77,10 @@ public synchronized void addCondition(ConfigurationCondition cnd) {
80
77
return ;
81
78
}
82
79
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 ( ));
87
84
}
88
85
89
86
@ Platforms (Platform .HOSTED_ONLY .class )
@@ -92,28 +89,19 @@ public Set<Class<?>> getTypesForEncoding() {
92
89
return Set .of ();
93
90
} else {
94
91
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 ));
97
94
}
98
95
return types ;
99
96
}
100
97
}
101
98
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 ;
108
101
}
109
102
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 );
117
105
}
118
106
119
107
/**
@@ -133,8 +121,8 @@ public boolean satisfied() {
133
121
if (localConditions == null ) {
134
122
result = true ;
135
123
} else {
136
- for (RuntimeCondition condition : localConditions ) {
137
- if (condition . isSatisfied ()) {
124
+ for (Object condition : localConditions ) {
125
+ if (isSatisfied (condition )) {
138
126
conditions = null ;
139
127
satisfied = result = true ;
140
128
break ;
@@ -158,10 +146,46 @@ public String toString() {
158
146
return conditionsString + " = " + satisfied ;
159
147
}
160
148
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
+
161
185
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 ] );
163
187
164
- private UnmodifiableRuntimeConditionSet (Set < RuntimeCondition > conditions ) {
188
+ private UnmodifiableRuntimeConditionSet (Object [] conditions ) {
165
189
super (conditions );
166
190
}
167
191
0 commit comments