/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javafx.application; import com.sun.javafx.application.LauncherImpl; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javafx.stage.Stage; /** * Mock JavaFX Application class, used by the FXLauncherTest sample * application and the FX mode of the Java launcher. */ public abstract class Application { private Parameters params = new Parameters(); public static void launch(Class appClass, String... args) { LauncherImpl.launchApplication(appClass, args); } public static void launch(String... args) { // Figure out the right class to call StackTraceElement[] cause = Thread.currentThread().getStackTrace(); boolean foundThisMethod = false; String callingClassName = null; for (StackTraceElement se : cause) { // Skip entries until we get to the entry for this class String className = se.getClassName(); String methodName = se.getMethodName(); if (foundThisMethod) { callingClassName = className; break; } else if (Application.class.getName().equals(className) && "launch".equals(methodName)) { foundThisMethod = true; } } if (callingClassName == null) { throw new RuntimeException("Error: unable to determine Application class"); } try { Class theClass = Class.forName(callingClassName, false, Thread.currentThread().getContextClassLoader()); if (Application.class.isAssignableFrom(theClass)) { Class appClass = theClass; LauncherImpl.launchApplication(appClass, args); } else { throw new RuntimeException("Error: " + theClass + " is not a subclass of javafx.application.Application"); } } catch (RuntimeException ex) { throw ex; } catch (Exception ex) { throw new RuntimeException(ex); } } /** * Constructs a new {@code Application} instance. */ public Application() { } public void init() throws Exception { } public abstract void start(Stage primaryStage) throws Exception; public void stop() throws Exception { } // Helper method called by LauncherImpl public void setParameters(String... args) { params.setArgs(args); } public final Parameters getParameters() { return params; } public static class Parameters { private List raw = new ArrayList<>(); void setArgs(String... args) { raw.clear(); raw.addAll(Arrays.asList(args)); } public Parameters() { } public List getRaw() { return raw; } public List getUnnamed() { return getRaw(); } } }