Skip to content

Commit

Permalink
improve JavaMembers.java v2
Browse files Browse the repository at this point in the history
rhino_JavaMembers_lazyInit=false
rhino_JavaMembers_reflect_cache_on=true

rhino_JavaMembers_lazyInit=true
rhino_JavaMembers_reflect_cache_on=false
  • Loading branch information
qxo committed Jan 20, 2022
1 parent c7823bb commit 3f921a0
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/org/mozilla/javascript/JavaMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ 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 boolean lazyInit;
private final Scriptable javaMemberScope;

JavaMembers(Scriptable scope, Class<?> cl) {
Expand All @@ -64,7 +71,6 @@ class JavaMembers {
this.staticMembers = new HashMap<String, Object>();
this.cl = cl;
includePrivate = cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS);
lazyInit = true;
cfCache = reflect(scope, includeProtected, includePrivate);
} finally {
Context.exit();
Expand Down Expand Up @@ -128,7 +134,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 (lazyInit && member == null) {
if (LAZY_INIT && member == null) {
final Object m1 = initFieldAndMethod(name, ht, isStatic);
Map<String, String> props =
isStatic ? cfCache.staticBeanProperties : cfCache.instBeanProperties;
Expand Down Expand Up @@ -523,7 +529,7 @@ private static Map<Class, ClassReflectBean> getCache(
private ClassReflectBean createClassReflectBean(
Class<?> clazz, boolean includeProtected, boolean includePrivate) {
final Map<Class, ClassReflectBean> cache =
getCache(clazz, includeProtected, includePrivate);
CACHE_ON ? getCache(clazz, includeProtected, includePrivate) : null;
ClassReflectBean ret = null;
if (cache != null) {
ret = cache.get(clazz);
Expand Down Expand Up @@ -673,7 +679,7 @@ private ClassReflectBean reflect(
// gets in the way.
final ClassReflectBean cfCache =
createClassReflectBean(cl, includeProtected, includePrivate);
if (!lazyInit) {
if (!LAZY_INIT) {
// 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 @@ -793,7 +799,7 @@ private BeanProperty initBeanProperty(
String setterName = "set".concat(nameComponent);
// Is this value a method?
Object member = ht.get(setterName);
if (member == null && lazyInit) {
if (member == null && LAZY_INIT) {
member = initFieldAndMethod(setterName, ht, isStatic);
}
if (member instanceof NativeJavaMethod) {
Expand Down Expand Up @@ -961,7 +967,7 @@ private MemberBox findGetter(
String getterName = prefix.concat(propertyName);
// Check that the getter is a method.
Object member = ht.get(getterName);
if (member == null && lazyInit) {
if (member == null && LAZY_INIT) {
member = initFieldAndMethod(getterName, ht, isStatic);
}
if (member instanceof NativeJavaMethod) {
Expand Down Expand Up @@ -1033,6 +1039,10 @@ private static MemberBox extractSetMethod(MemberBox[] methods, boolean isStatic)
return null;
}

protected static final String getProperty(final String key, final String defaultValue) {
return System.getProperty(key, 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 3f921a0

Please sign in to comment.