@@ -115,36 +115,41 @@ private class PulseTimer : AnimationTimer() {
115
115
}
116
116
}
117
117
118
- internal fun initPlatform (): Boolean {
119
- /*
120
- * Try to instantiate JavaFx platform in a way which works
121
- * both on Java 8 and Java 11 and does not produce "illegal reflective access":
122
- *
123
- * 1) Try to invoke javafx.application.Platform.startup if this class is
124
- * present in a classpath.
125
- * 2) If it is not successful and does not because it is already started,
126
- * fallback to PlatformImpl.
127
- *
128
- * Ignore exception anyway in case of unexpected changes in API, in that case
129
- * user will have to instantiate it manually.
130
- */
131
- val runnable = Runnable {}
132
- return runCatching {
133
- // Invoke public API if it is present
134
- Class .forName(" javafx.application.Platform" )
135
- .getMethod(" startup" , java.lang.Runnable ::class .java)
136
- .invoke(null , runnable)
137
- }.recoverCatching { exception ->
138
- // Recover -> check re-initialization
139
- val cause = exception.cause
140
- if (exception is InvocationTargetException && cause is IllegalStateException
141
- && " Toolkit already initialized" == cause.message) {
142
- // Toolkit is already initialized -> success, return
143
- Unit
144
- } else { // Fallback to Java 8 API
145
- Class .forName(" com.sun.javafx.application.PlatformImpl" )
118
+ internal fun initPlatform (): Boolean = PlatformInitializer .success
119
+
120
+ // Lazily try to initialize JavaFx platform just once
121
+ private object PlatformInitializer {
122
+ val success = run {
123
+ /*
124
+ * Try to instantiate JavaFx platform in a way which works
125
+ * both on Java 8 and Java 11 and does not produce "illegal reflective access":
126
+ *
127
+ * 1) Try to invoke javafx.application.Platform.startup if this class is
128
+ * present in a classpath.
129
+ * 2) If it is not successful and does not because it is already started,
130
+ * fallback to PlatformImpl.
131
+ *
132
+ * Ignore exception anyway in case of unexpected changes in API, in that case
133
+ * user will have to instantiate it manually.
134
+ */
135
+ val runnable = Runnable {}
136
+ runCatching {
137
+ // Invoke public API if it is present
138
+ Class .forName(" javafx.application.Platform" )
146
139
.getMethod(" startup" , java.lang.Runnable ::class .java)
147
140
.invoke(null , runnable)
148
- }
149
- }.isSuccess
141
+ }.recoverCatching { exception ->
142
+ // Recover -> check re-initialization
143
+ val cause = exception.cause
144
+ if (exception is InvocationTargetException && cause is IllegalStateException
145
+ && " Toolkit already initialized" == cause.message) {
146
+ // Toolkit is already initialized -> success, return
147
+ Unit
148
+ } else { // Fallback to Java 8 API
149
+ Class .forName(" com.sun.javafx.application.PlatformImpl" )
150
+ .getMethod(" startup" , java.lang.Runnable ::class .java)
151
+ .invoke(null , runnable)
152
+ }
153
+ }.isSuccess
154
+ }
150
155
}
0 commit comments