Skip to content

Commit

Permalink
Optimize map access in JavaMembers
Browse files Browse the repository at this point in the history
  • Loading branch information
rPraml authored and gbrail committed Sep 23, 2024
1 parent 53a7871 commit 2eccdd1
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions rhino/src/main/java/org/mozilla/javascript/JavaMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,15 @@ private void discoverAccessibleMethods(

if (isPublic(mods) || isProtected(mods) || includePrivate) {
MethodSignature sig = new MethodSignature(method);
if (!map.containsKey(sig)) {
if (includePrivate) method.trySetAccessible();
map.put(sig, method);
if (includePrivate) {
map.computeIfAbsent(
sig,
k -> {
method.trySetAccessible();
return method;
});
} else {
map.putIfAbsent(sig, method);
}
}
}
Expand All @@ -346,7 +352,7 @@ private void discoverAccessibleMethods(
Method[] methods = clazz.getMethods();
for (Method method : methods) {
MethodSignature sig = new MethodSignature(method);
if (!map.containsKey(sig)) map.put(sig, method);
map.putIfAbsent(sig, method);
}
break; // getMethods gets superclass methods, no
// need to loop any more
Expand Down Expand Up @@ -387,9 +393,7 @@ void discoverPublicMethods(Class<?> clazz, Map<MethodSignature, Method> map) {
static void registerMethod(Map<MethodSignature, Method> map, Method method) {
MethodSignature sig = new MethodSignature(method);
// Array may contain methods with same signature but different return value!
if (!map.containsKey(sig)) {
map.put(sig, method);
}
map.putIfAbsent(sig, method);
}

static final class MethodSignature {
Expand Down Expand Up @@ -595,23 +599,21 @@ private void reflect(
NativeJavaMethod setters = null;
String setterName = "set".concat(nameComponent);

if (ht.containsKey(setterName)) {
// Is this value a method?
Object member = ht.get(setterName);
if (member instanceof NativeJavaMethod) {
NativeJavaMethod njmSet = (NativeJavaMethod) member;
if (getter != null) {
// We have a getter. Now, do we have a matching
// setter?
Class<?> type = getter.method().getReturnType();
setter = extractSetMethod(type, njmSet.methods, isStatic);
} else {
// No getter, find any set method
setter = extractSetMethod(njmSet.methods, isStatic);
}
if (njmSet.methods.length > 1) {
setters = njmSet;
}
// Is this value a method?
Object member = ht.get(setterName);
if (member instanceof NativeJavaMethod) {
NativeJavaMethod njmSet = (NativeJavaMethod) member;
if (getter != null) {
// We have a getter. Now, do we have a matching
// setter?
Class<?> type = getter.method().getReturnType();
setter = extractSetMethod(type, njmSet.methods, isStatic);
} else {
// No getter, find any set method
setter = extractSetMethod(njmSet.methods, isStatic);
}
if (njmSet.methods.length > 1) {
setters = njmSet;
}
}
// Make the property.
Expand Down Expand Up @@ -687,13 +689,11 @@ private Field[] getAccessibleFields(boolean includeProtected, boolean includePri
private static MemberBox findGetter(
boolean isStatic, Map<String, Object> ht, String prefix, String propertyName) {
String getterName = prefix.concat(propertyName);
if (ht.containsKey(getterName)) {
// Check that the getter is a method.
Object member = ht.get(getterName);
if (member instanceof NativeJavaMethod) {
NativeJavaMethod njmGet = (NativeJavaMethod) member;
return extractGetMethod(njmGet.methods, isStatic);
}
// Check that the getter is a method.
Object member = ht.get(getterName);
if (member instanceof NativeJavaMethod) {
NativeJavaMethod njmGet = (NativeJavaMethod) member;
return extractGetMethod(njmGet.methods, isStatic);
}
return null;
}
Expand Down

0 comments on commit 2eccdd1

Please sign in to comment.