Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getDeclaredMethods() should search super-interfaces recursively. #55

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 44 additions & 59 deletions src/main/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -2117,76 +2117,61 @@ public static List getDeclaredMethods(Class targetClass, String propertyName, bo
if ((propertyCache == null) || ((result = (List) propertyCache.get(propertyName)) == null)) {

String baseName = capitalizeBeanPropertyName(propertyName);
result = new ArrayList();
collectAccessors(targetClass, baseName, result, findSets);

for (Class c = targetClass; c != null; c = c.getSuperclass()) {
Method[] methods = c.getDeclaredMethods();
if (propertyCache == null) {
cache.put(targetClass, propertyCache = new HashMap(101));
}
propertyCache.put(propertyName, result.isEmpty() ? NotFoundList : result);

for (int i = 0; i < methods.length; i++) {
return result.isEmpty() ? null : result;
}
}
}
return (result == NotFoundList) ? null : result;
}

if (!isMethodCallable(methods[i]))
continue;
private static void collectAccessors(Class c, String baseName, List result, boolean findSets)
{
final Method[] methods = c.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (c.isInterface()) {
if (isDefaultMethod(methods[i])) {
addIfAccessor(result, methods[i], baseName, findSets);
}
continue;
}

String ms = methods[i].getName();
if (!isMethodCallable(methods[i]))
continue;

if (ms.endsWith(baseName)) {
boolean isSet = false, isIs = false;
addIfAccessor(result, methods[i], baseName, findSets);
}

if ((isSet = ms.startsWith(SET_PREFIX)) || ms.startsWith(GET_PREFIX)
|| (isIs = ms.startsWith(IS_PREFIX))) {
int prefixLength = (isIs ? 2 : 3);
final Class superclass = c.getSuperclass();
if (superclass != null)
collectAccessors(superclass, baseName, result, findSets);

if (isSet == findSets) {
if (baseName.length() == (ms.length() - prefixLength)) {
if (result == null) {
result = new ArrayList();
}
result.add(methods[i]);
}
}
}
}
}
}
// Add default methods on interface
for (Class c = targetClass; c != null; c = c.getSuperclass()) {
for(Class intf : c.getInterfaces()){
Method[] methods = intf.getDeclaredMethods();

for (int i = 0; i < methods.length; i++) {
if (Modifier.isAbstract(methods[i].getModifiers()))
continue;

String ms = methods[i].getName();

if (ms.endsWith(baseName)) {
boolean isSet = false, isIs = false;

if ((isSet = ms.startsWith(SET_PREFIX)) || ms.startsWith(GET_PREFIX)
|| (isIs = ms.startsWith(IS_PREFIX))) {
int prefixLength = (isIs ? 2 : 3);

if (isSet == findSets) {
if (baseName.length() == (ms.length() - prefixLength)) {
if (result == null) {
result = new ArrayList();
}
result.add(methods[i]);
}
}
}
}
}
}
}
if (propertyCache == null) {
cache.put(targetClass, propertyCache = new HashMap(101));
}
for (final Class iface : c.getInterfaces())
collectAccessors(iface, baseName, result, findSets);
}

propertyCache.put(propertyName, (result == null) ? NotFoundList : result);
private static void addIfAccessor(List result, Method method, String baseName, boolean findSets)
{
final String ms = method.getName();
if (ms.endsWith(baseName)) {
boolean isSet = false, isIs = false;
if ((isSet = ms.startsWith(SET_PREFIX)) || ms.startsWith(GET_PREFIX)
|| (isIs = ms.startsWith(IS_PREFIX))) {
int prefixLength = (isIs ? 2 : 3);
if (isSet == findSets) {
if (baseName.length() == (ms.length() - prefixLength)) {
result.add(method);
}
}
}
}
return (result == NotFoundList) ? null : result;
}

/**
Expand Down