Enable catching of all Java exceptions through a Rhino FEATURE (disabled

by default).


git-svn-id: svn://10.0.0.236/trunk@235547 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
nboyd%atg.com
2007-09-12 16:57:09 +00:00
parent f82702bc0b
commit 127f7d994e
5 changed files with 32 additions and 9 deletions

View File

@@ -294,10 +294,16 @@ public class Context
public static final int FEATURE_WARNING_AS_ERROR = 12;
/**
* Controls whether private and protected members can be accessed
* Enables enhanced access to Java.
* Specifically, controls whether private and protected members can be
* accessed, and whether scripts can catch all Java exceptions.
* <p>
* Note that this feature should only be enabled for trusted scripts.
* <p>
* By default {@link #hasFeature(int)} returns false.
* @since 1.7 Release 1
*/
public static final int FEATURE_ACCESS_PRIVATE_MEMBERS = 13;
public static final int FEATURE_ENHANCED_JAVA_ACCESS = 13;
public static final String languageVersionProperty = "language version";
@@ -1762,7 +1768,12 @@ public class Context
}
// special handling of Error so scripts would not catch them
if (e instanceof Error) {
throw (Error)e;
Context cx = getContext();
if (cx == null ||
!cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS))
{
throw (Error)e;
}
}
if (e instanceof RhinoException) {
throw (RhinoException)e;
@@ -2149,7 +2160,7 @@ public class Context
* @see #FEATURE_LOCATION_INFORMATION_IN_ERROR
* @see #FEATURE_STRICT_MODE
* @see #FEATURE_WARNING_AS_ERROR
* @see #FEATURE_ACCESS_PRIVATE_MEMBERS
* @see #FEATURE_ENHANCED_JAVA_ACCESS
*/
public boolean hasFeature(int featureIndex)
{

View File

@@ -288,7 +288,7 @@ public class ContextFactory
case Context.FEATURE_WARNING_AS_ERROR:
return false;
case Context.FEATURE_ACCESS_PRIVATE_MEMBERS:
case Context.FEATURE_ENHANCED_JAVA_ACCESS:
return false;
}
// It is a bug to call the method with unknown featureIndex

View File

@@ -73,7 +73,7 @@ class JavaMembers
cl.getName());
}
this.includePrivate = cx.hasFeature(
Context.FEATURE_ACCESS_PRIVATE_MEMBERS);
Context.FEATURE_ENHANCED_JAVA_ACCESS);
this.members = new Hashtable(23);
this.staticMembers = new Hashtable(7);
this.cl = cl;

View File

@@ -27,7 +27,7 @@ public class JavaAcessibilityTest extends TestCase {
private ContextFactory contextFactory = new ShellContextFactory() {
protected boolean hasFeature(Context cx, int featureIndex) {
if (featureIndex == Context.FEATURE_ACCESS_PRIVATE_MEMBERS)
if (featureIndex == Context.FEATURE_ENHANCED_JAVA_ACCESS)
return true;
return super.hasFeature(cx, featureIndex);
}

View File

@@ -38,7 +38,7 @@ package org.mozilla.javascript.tests;
/**
* A class with private/protected/package private members, to test the Rhino
* feature Context.FEATURE_ACCESS_PRIVATE_MEMBERS, that allows bypassing Java
* feature Context.FEATURE_ENHANCED_JAVA_ACCESS, that allows bypassing Java
* member access restrictions.
* @author Donna Malayeri
*/
@@ -73,5 +73,17 @@ public class PrivateAccessClass
int packagePrivateMethod() { return 3; }
private int privateMethod() { return 4; }
protected int protectedMethod() { return 5; }
protected int protectedMethod() { return 5; }
/*
* Suppress warnings about unused private members.
*/
public int referenceToPrivateMembers() {
PrivateAccessClass pac = new PrivateAccessClass();
PrivateAccessClass pac2 = new PrivateAccessClass(2);
PrivateNestedClass pnc = new PrivateNestedClass();
System.out.println(privateString);
return pnc.privateInt + staticPrivateInt + staticPrivateMethod() +
pac.privateMethod();
}
}