Skip to content

Commit

Permalink
adding feature flags to Context instead of system properties
Browse files Browse the repository at this point in the history
  • Loading branch information
qxo committed Feb 15, 2022
1 parent 802724c commit fa58dbb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
18 changes: 18 additions & 0 deletions src/org/mozilla/javascript/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,24 @@ public class Context implements Closeable {
*/
public static final int FEATURE_ENABLE_JAVA_MAP_ACCESS = 21;

/**
* Configure whether JavaMembers lazy init off.
*
* <p> default is lazy init.
*
* @since 1.7 Release 15
*/
public static final int FEATURE_JAVAMEMBERS_LAZY_INIT_OFF = 22;

/**
* Configure whether JavaMembers reflect cache off.
*
* <p> default is cache on.
*
* @since 1.7 Release 15
*/
public static final int FEATURE_JAVAMEMBERS_REFLECT_CACHE_OFF = 23;

public static final String languageVersionProperty = "language version";
public static final String errorReporterProperty = "error reporter";

Expand Down
31 changes: 9 additions & 22 deletions src/org/mozilla/javascript/JavaMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessControlContext;
import java.security.AccessControlException;
import java.security.AllPermission;
import java.security.Permission;
import java.util.ArrayList;
Expand All @@ -43,18 +42,12 @@ class JavaMembers {

private static final Permission allPermission = new AllPermission();

// rhino_JavaMembers_lazyInit=true for enable
private static final boolean LAZY_INIT =
"true".equals(getProperty("rhino_JavaMembers_lazyInit", "true"));

// rhino_JavaMembers_reflect_cache_on=false for disable
private static final boolean CACHE_ON =
!"false".equals(getProperty("rhino_JavaMembers_reflect_cache_on", "true"));

// private final boolean includeProtected;
private final boolean includePrivate;
private final ClassReflectBean cfCache;
private final Scriptable javaMemberScope;
private final boolean lazyInit;
private final boolean reflectCacheOn;

JavaMembers(Scriptable scope, Class<?> cl) {
this(scope, cl, false);
Expand All @@ -72,6 +65,8 @@ class JavaMembers {
this.staticMembers = new HashMap<String, Object>();
this.cl = cl;
includePrivate = cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS);
lazyInit = !cx.hasFeature(Context.FEATURE_JAVAMEMBERS_LAZY_INIT_OFF);
reflectCacheOn = !cx.hasFeature(Context.FEATURE_JAVAMEMBERS_REFLECT_CACHE_OFF);
cfCache = reflect(scope, includeProtected, includePrivate);
} finally {
Context.exit();
Expand Down Expand Up @@ -135,7 +130,7 @@ private final Object getMember(
final Scriptable scope, final String name, final boolean isStatic) {
final Map<String, Object> ht = isStatic ? staticMembers : members;
Object member = ht.get(name);
if (LAZY_INIT && member == null) {
if (lazyInit && member == null) {
final Object m1 = initFieldAndMethod(name, ht, isStatic);
Map<String, String> props =
isStatic ? cfCache.staticBeanProperties : cfCache.instBeanProperties;
Expand Down Expand Up @@ -530,7 +525,7 @@ private static Map<Class, ClassReflectBean> getCache(
private ClassReflectBean createClassReflectBean(
Class<?> clazz, boolean includeProtected, boolean includePrivate) {
final Map<Class, ClassReflectBean> cache =
CACHE_ON ? getCache(clazz, includeProtected, includePrivate) : null;
reflectCacheOn ? getCache(clazz, includeProtected, includePrivate) : null;
ClassReflectBean ret = null;
if (cache != null) {
ret = cache.get(clazz);
Expand Down Expand Up @@ -680,7 +675,7 @@ private ClassReflectBean reflect(
// gets in the way.
final ClassReflectBean cfCache =
createClassReflectBean(cl, includeProtected, includePrivate);
if (!LAZY_INIT) {
if (!lazyInit) {
// replace Method instances by wrapped NativeJavaMethod objects
// first in staticMembers and then in members
for (int tableCursor = 0; tableCursor != 2; ++tableCursor) {
Expand Down Expand Up @@ -800,7 +795,7 @@ private BeanProperty initBeanProperty(
String setterName = "set".concat(nameComponent);
// Is this value a method?
Object member = ht.get(setterName);
if (member == null && LAZY_INIT) {
if (member == null && lazyInit) {
member = initFieldAndMethod(setterName, ht, isStatic);
}
if (member instanceof NativeJavaMethod) {
Expand Down Expand Up @@ -968,7 +963,7 @@ private MemberBox findGetter(
String getterName = prefix.concat(propertyName);
// Check that the getter is a method.
Object member = ht.get(getterName);
if (member == null && LAZY_INIT) {
if (member == null && lazyInit) {
member = initFieldAndMethod(getterName, ht, isStatic);
}
if (member instanceof NativeJavaMethod) {
Expand Down Expand Up @@ -1040,14 +1035,6 @@ private static MemberBox extractSetMethod(MemberBox[] methods, boolean isStatic)
return null;
}

protected static final String getProperty(final String key, final String defaultValue) {
try {
return System.getProperty(key, defaultValue);
} catch (AccessControlException ex) {
return defaultValue;
}
}

Map<String, FieldAndMethods> getFieldAndMethodsObjects(
Scriptable scope, Object javaObject, boolean isStatic) {
Set<String> names = isStatic ? cfCache.staticFieldAndMethods : cfCache.instFieldAndMethods;
Expand Down

0 comments on commit fa58dbb

Please sign in to comment.