Bug 57113

Summary: ImportHandler not returning class properly from resolveClass()
Product: Tomcat 8 Reporter: Arthur Fiedler <artfiedler>
Component: ELAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 8.0.x-trunk   
Target Milestone: ----   
Hardware: PC   
OS: All   

Description Arthur Fiedler 2014-10-18 07:52:40 UTC
Typically the importHandler.resolveClass(String) function is used something like...
  result = ctx.getImportHandler().resolveClass("MyClass");
  if (result != null)
    return result;

However the current functionality will fail (before classes are cached) if MyClass is in package "com.test" and "org.apache" is imported after com.test is... eg.

ctx.getImportHandler().importPackage("com.test");
ctx.getImportHandler().importPackage("org.apache");

Object result = ctx.getImportHandler().resolveClass("MyClass");

At this point result is null the first time ran through, because resolveClass loops through all packages finding the className in each package before it returns the temp variable its been using... which when it comes to org.apache.MyClass it will be null, when com.test.MyClass was actually found... but method returns null. 

I would think the expected result should be to return the first matching class. To continue its current logic for multi match detection, it can be modified to save the first match and return that variable... Like so.


public java.lang.Class<?> resolveClass(String name) {
    Class<?> result = clazzes.get(name);

    if (result == null) {
        // Search the package imports - note there may be multiple matches
        // (which correctly triggers an error)
        for (String p : packages) {
            String className = p + '.' + name;
            Class<?> tmpClass = findClass(className, true);
            if (result == null && tmpClass != null) {
                result = tmpClass;
            } 
        }
    }

    return result;
}
Comment 1 Mark Thomas 2014-10-21 19:26:29 UTC
Thanks for the report. This has been fixed in 8.0.x for 8.0.15 onwards.