From 36a3809394f8981fcd3945562e4f88602ce28658 Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Fri, 29 Mar 2024 10:00:15 +0100 Subject: [PATCH] use try-with-resources for the context at more places --- src/org/mozilla/javascript/Interpreter.java | 5 +--- src/org/mozilla/javascript/JavaMembers.java | 5 +--- .../javascript/tests/Bug448816Test.java | 22 ++++++++--------- .../javascript/tests/DynamicScopeTest.java | 6 +---- .../javascript/tests/XMLSecureParserTest.java | 24 +++++++++++-------- 5 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/org/mozilla/javascript/Interpreter.java b/src/org/mozilla/javascript/Interpreter.java index af904e2686..e4e872af62 100644 --- a/src/org/mozilla/javascript/Interpreter.java +++ b/src/org/mozilla/javascript/Interpreter.java @@ -222,8 +222,7 @@ public boolean equals(Object other) { // one. It is required as some objects within fully initialized // global scopes (notably, XMLLibImpl) need to have a top scope // in order to evaluate their attributes. - final Context cx = Context.enter(); - try { + try (Context cx = Context.enter()) { if (ScriptRuntime.hasTopCall(cx)) { return equalsInTopScope(other).booleanValue(); } @@ -237,8 +236,6 @@ public boolean equals(Object other) { ScriptRuntime.emptyArgs, isStrictTopFrame())) .booleanValue(); - } finally { - Context.exit(); } } return false; diff --git a/src/org/mozilla/javascript/JavaMembers.java b/src/org/mozilla/javascript/JavaMembers.java index e4d5854024..f9c457b042 100644 --- a/src/org/mozilla/javascript/JavaMembers.java +++ b/src/org/mozilla/javascript/JavaMembers.java @@ -41,8 +41,7 @@ class JavaMembers { } JavaMembers(Scriptable scope, Class cl, boolean includeProtected) { - try { - Context cx = ContextFactory.getGlobal().enterContext(); + try (Context cx = ContextFactory.getGlobal().enterContext()) { ClassShutter shutter = cx.getClassShutter(); if (shutter != null && !shutter.visibleToScripts(cl.getName())) { throw Context.reportRuntimeErrorById("msg.access.prohibited", cl.getName()); @@ -52,8 +51,6 @@ class JavaMembers { this.cl = cl; boolean includePrivate = cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS); reflect(cx, scope, includeProtected, includePrivate); - } finally { - Context.exit(); } } diff --git a/testsrc/org/mozilla/javascript/tests/Bug448816Test.java b/testsrc/org/mozilla/javascript/tests/Bug448816Test.java index 386a8926e2..93d87fc977 100644 --- a/testsrc/org/mozilla/javascript/tests/Bug448816Test.java +++ b/testsrc/org/mozilla/javascript/tests/Bug448816Test.java @@ -36,17 +36,17 @@ public void setUp() { reference.put("c", new HashMap()); reference.put(Integer.valueOf(1), Integer.valueOf(42)); // get a js object as map - Context context = Context.enter(); - ScriptableObject scope = context.initStandardObjects(); - map = - (Map) - context.evaluateString( - scope, - "({ a: 'a', b: true, c: new java.util.HashMap(), 1: 42});", - "testsrc", - 1, - null); - Context.exit(); + try (Context context = Context.enter()) { + ScriptableObject scope = context.initStandardObjects(); + map = + (Map) + context.evaluateString( + scope, + "({ a: 'a', b: true, c: new java.util.HashMap(), 1: 42});", + "testsrc", + 1, + null); + } } @Test diff --git a/testsrc/org/mozilla/javascript/tests/DynamicScopeTest.java b/testsrc/org/mozilla/javascript/tests/DynamicScopeTest.java index b48c0d3240..ca2a0352e5 100644 --- a/testsrc/org/mozilla/javascript/tests/DynamicScopeTest.java +++ b/testsrc/org/mozilla/javascript/tests/DynamicScopeTest.java @@ -88,8 +88,7 @@ public void initStandardObjectsSealed() { public void standardMethodObjectCreate() { ContextFactory contextFactory = new DynamicScopeContextFactory(); - final Context cx = contextFactory.enterContext(); - try { + try (Context cx = contextFactory.enterContext()) { // Used to fail with org.mozilla.javascript.EvaluatorException: Cannot modify a property // of a sealed object: iterator. @@ -122,9 +121,6 @@ public void standardMethodObjectCreate() { assertSame(subScope.getPrototype(), someScope); assertSame(someObj.getParentScope(), someScope); assertSame(subObj.getParentScope(), subScope); - - } finally { - Context.exit(); } } } diff --git a/testsrc/org/mozilla/javascript/tests/XMLSecureParserTest.java b/testsrc/org/mozilla/javascript/tests/XMLSecureParserTest.java index 8214b0d013..322e0f4aa9 100644 --- a/testsrc/org/mozilla/javascript/tests/XMLSecureParserTest.java +++ b/testsrc/org/mozilla/javascript/tests/XMLSecureParserTest.java @@ -35,7 +35,9 @@ public void xmlSecureConfiguration() { CALLED_BY_XML_PARSER = false; // run with defaults for this JRE - executeXML(ContextFactory.getGlobal().enterContext()); + try (Context cx = ContextFactory.getGlobal().enterContext()) { + executeXML(cx); + } assertFalse(CALLED_BY_XML_PARSER); // store the original setting for xml, if any @@ -44,7 +46,9 @@ public void xmlSecureConfiguration() { // inject our own xml parser System.setProperty(XML_PROPERTY, DBF_CLASSNAME); // run with our injected parser - executeXML(ContextFactory.getGlobal().enterContext()); + try (Context cx = ContextFactory.getGlobal().enterContext()) { + executeXML(cx); + } } catch (RuntimeException e) { // Our parser immediately throws a ParserConfigurationException on creating a // documentbuilder, @@ -78,7 +82,9 @@ public void xmlInsecureConfiguration() { CALLED_BY_XML_PARSER = false; // run with defaults for this JRE - executeXML(new InsecureContextFactory().enterContext()); + try (Context cx = new InsecureContextFactory().enterContext()) { + executeXML(cx); + } assertFalse(CALLED_BY_XML_PARSER); // store the original setting for xml, if any @@ -87,7 +93,9 @@ public void xmlInsecureConfiguration() { // inject our own xml parser System.setProperty(XML_PROPERTY, DBF_CLASSNAME); // run with our injected parser - executeXML(new InsecureContextFactory().enterContext()); + try (Context cx = new InsecureContextFactory().enterContext()) { + executeXML(cx); + } } catch (RuntimeException e) { // Our parser immediately throws a ParserConfigurationException on creating a // documentbuilder, @@ -113,12 +121,8 @@ public void xmlInsecureConfiguration() { } private void executeXML(Context cx) { - try { - Scriptable scope = cx.initStandardObjects(); - cx.evaluateString(scope, "new XML('').toXMLString();", "source", 1, null); - } finally { - Context.exit(); - } + Scriptable scope = cx.initStandardObjects(); + cx.evaluateString(scope, "new XML('').toXMLString();", "source", 1, null); } class InsecureContextFactory extends ContextFactory {