Skip to content

ReflectionUtils.findMethod does not find methods for Object.class [SPR-5434] #10108

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

Closed
spring-projects-issues opened this issue Jan 27, 2009 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Daan Kets opened SPR-5434 and commented

ReflectionUtils.findMethod(Class<?> clazz, String methodName) javadoc states:

Attempt to find a Method on the supplied class with the supplied name and no parameters. Searches all superclasses up to Object.

Currently, the implementation does not find any results for methods declared on the Object class. As from the moment when the Object class is reached while searching, the search ends, and no result is returned. So, it's impossible for example to find the public getClass() method using findMethod(<anyObject>,"getClass"). This is pretty inconsistent with the behaviour of for example the 'getAllDeclaredMethods' method, which does return the getClass method as a result.


Affects: 2.5.6

@spring-projects-issues
Copy link
Collaborator Author

Daan Kets commented

// Fix proposal:

public static Method findMethod(Class clazz, String name, Class[] paramTypes) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(name, "Method name must not be null");
Class searchType = clazz;
while (searchType != null) { // CHANGED CONDITION HERE
Method[] methods = (searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods());
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (name.equals(method.getName()) &&
(paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
return method;
}
}
if (!Object.class.equals(searchType){ // ADDED CHECK HERE
return null;
}
searchType = searchType.getSuperclass();
}
return null;
}

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Good point: There's no actual reason to exclude methods on Object.class - even if those are not typically the target of such a findMethod call.

Juergen

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0 M2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants