Skip to content

Commit

Permalink
8234959: FXMLLoader does not populate ENGINE_SCOPE Bindings with FILE…
Browse files Browse the repository at this point in the history
…NAME and ARGV

Reviewed-by: kcr, aghaisas
  • Loading branch information
ronyfla authored and aghaisas committed Mar 31, 2020
1 parent d7f13f4 commit 6d098fe
Show file tree
Hide file tree
Showing 15 changed files with 912 additions and 13 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3571,6 +3571,7 @@ project(":systemTests") {
testapp4
testapp5
testapp6
testscriptapp1
}

def nonModSrcSets = [
Expand All @@ -3583,7 +3584,8 @@ project(":systemTests") {
sourceSets.testapp3,
sourceSets.testapp4,
sourceSets.testapp5,
sourceSets.testapp6
sourceSets.testapp6,
sourceSets.testscriptapp1
]

project.ext.buildModule = false
Expand Down Expand Up @@ -3683,7 +3685,7 @@ project(":systemTests") {
}
test.dependsOn(createTestApps);

def modtestapps = [ "testapp2", "testapp3", "testapp4", "testapp5", "testapp6" ]
def modtestapps = [ "testapp2", "testapp3", "testapp4", "testapp5", "testapp6", "testscriptapp1" ]
modtestapps.each { testapp ->
def testappCapital = testapp.capitalize()
def copyTestAppTask = task("copy${testappCapital}", type: Copy) {
Expand Down
24 changes: 14 additions & 10 deletions modules/javafx.fxml/src/main/java/javafx/fxml/FXMLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,8 @@ public void processEventHandlerAttributes() throws LoadException {
throw constructLoadException("Error resolving " + attribute.name + "='" + attribute.value
+ "', either the event handler is not in the Namespace or there is an error in the script.");
}

eventHandler = new ScriptEventHandler(handlerName, scriptEngine);
eventHandler = new ScriptEventHandler(handlerName, scriptEngine, location.getPath()
+ "-" + attribute.name + "_attribute_in_element_ending_at_line_" + getLineNumber());
}

// Add the handler
Expand Down Expand Up @@ -1557,6 +1557,8 @@ public void processStartElement() throws IOException {

location = new URL(FXMLLoader.this.location, source);
}
Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
engineBindings.put(engine.FILENAME, location.getPath());

InputStreamReader scriptReader = null;
try {
Expand All @@ -1582,6 +1584,9 @@ public void processEndElement() throws IOException {
if (value != null && !staticLoad) {
// Evaluate the script
try {
Bindings engineBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
engineBindings.put(scriptEngine.FILENAME, location.getPath() + "-script_starting_at_line_"
+ (getLineNumber() - (int) ((String) value).codePoints().filter(c -> c == '\n').count()));
scriptEngine.eval((String)value);
} catch (ScriptException exception) {
System.err.println(exception.getMessage());
Expand Down Expand Up @@ -1675,30 +1680,29 @@ public void handle(T event) {
private static class ScriptEventHandler implements EventHandler<Event> {
public final String script;
public final ScriptEngine scriptEngine;
public final String filename;

public ScriptEventHandler(String script, ScriptEngine scriptEngine) {
public ScriptEventHandler(String script, ScriptEngine scriptEngine, String filename) {
this.script = script;
this.scriptEngine = scriptEngine;
this.filename = filename;
}

@Override
public void handle(Event event) {
// Don't pollute the page namespace with values defined in the script
Bindings engineBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
Bindings localBindings = scriptEngine.createBindings();
localBindings.put(EVENT_KEY, event);
localBindings.putAll(engineBindings);
scriptEngine.setBindings(localBindings, ScriptContext.ENGINE_SCOPE);

localBindings.put(EVENT_KEY, event);
localBindings.put(scriptEngine.ARGV, new Object[]{event});
localBindings.put(scriptEngine.FILENAME, filename);
// Execute the script
try {
scriptEngine.eval(script);
scriptEngine.eval(script, localBindings);
} catch (ScriptException exception){
throw new RuntimeException(exception);
}

// Restore the original bindings
scriptEngine.setBindings(engineBindings, ScriptContext.ENGINE_SCOPE);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2020, 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
Expand Down Expand Up @@ -43,6 +43,8 @@ public class ModuleLauncherTest {
private static final String modulePath4 = System.getProperty("launchertest.testapp4.module.path");
private static final String modulePath5 = System.getProperty("launchertest.testapp5.module.path");
private static final String modulePath6 = System.getProperty("launchertest.testapp6.module.path");
private static final String modulePathScript1 = System.getProperty("launchertest.testscriptapp1.module.path");

private static final String moduleName = "mymod";

private final int testExitCode = ERROR_NONE;
Expand Down Expand Up @@ -274,4 +276,9 @@ public void testModuleFXMLQualOpened() throws Exception {
doTestLaunchModule(modulePath6, "myapp6.AppFXMLQualOpened");
}

@Test (timeout = 15000)
public void testFXMLScriptDeployment() throws Exception {
doTestLaunchModule(modulePathScript1, "myapp1.FXMLScriptDeployment");
}

}
34 changes: 34 additions & 0 deletions tests/system/src/testscriptapp1/java/mymod/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2020, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/

module mymod {
requires javafx.controls;
requires javafx.fxml;

requires java.scripting;
provides javax.script.ScriptEngineFactory with pseudoScriptEngine.RgfPseudoScriptEngineFactory;
exports pseudoScriptEngine;
exports myapp1;
}
37 changes: 37 additions & 0 deletions tests/system/src/testscriptapp1/java/mymod/myapp1/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2017, 2020, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 myapp1; // verbatim from myapp6

public class Constants {

public static final int SHOWTIME = 2500;

// NOTE: these constants must match those in test.launchertest.Constants
public static final int ERROR_NONE = 2;
public static final int ERROR_UNEXPECTED_EXCEPTION = 4;

public static final int ERROR_ASSERTION_FAILURE = 28;
}
Loading

0 comments on commit 6d098fe

Please sign in to comment.