Bug 47214 - Inner classes that are explicitly referenced - should not be anonymous
Summary: Inner classes that are explicitly referenced - should not be anonymous
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 8
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 8.0.x-trunk
Hardware: All All
: P2 enhancement with 1 vote (vote)
Target Milestone: ----
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-05-17 18:32 UTC by Konstantin Kolinko
Modified: 2018-01-08 11:07 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Konstantin Kolinko 2009-05-17 18:32:20 UTC
Classes
 o.a.catalina.security.SecurityClassLoad
 o.a.catalina.jasper.SecurityClassLoad
perform preloading of certain classes of Tomcat.

My concern is that some of those preloaded classes are anonymous ones, e.g.

        loader.loadClass
            (basePackage + "session.StandardSession");
        loader.loadClass
            (basePackage + "session.StandardSession$1");

Referencing anonymous classes by their numbers is too fragile. It can be broken by any code modification that will cause their renumbering. Also, it is hard to review patches to SecurityClassLoad when they contain such references. See e.g. r721704

I suggest that all such classes were explicitly named. See classes referenced in o.a.catalina.security.SecurityClassLoad for an example (e.g.: o.a.c.core.ApplicationDispatcher with inner classes PrivilegedForward, PrivilegedInclude).
Comment 1 Abdessamed MANSOURI 2016-04-27 14:18:18 UTC
What do you suggest that classes be named?
Comment 2 Christopher Schultz 2016-04-28 02:56:41 UTC
I think any reasonably descriptive name will do.

If you're considering preparing a patch, please grab the latest trunk (currently Tomcat 9.0.x) and write your initial patch for that version. Once it's been accepted, that patch can be back-ported to older versions of Tomcat.

Try to make your patches as small as possible: don't fix 3 bugs at once and change all the whitespace around. If you need to re-factor some things, submit two patches: the refactoring in the first patch with no functional changes, and then the actual change in a second patch.
Comment 3 Abdessamed MANSOURI 2016-04-28 09:05:20 UTC
Thank you for your advices and your time :), the problem is i'm not native english speaker and english is my fourth language, so i didn't find good names, but i will try my best, thank you again for your time :)
Comment 4 Christopher Schultz 2016-04-28 14:58:46 UTC
Your English is great. If there is confusion, we will ask for clarification.

Don't feel bad if we ask you to re-do your patch(es) several times. The goal is to get the best patch possible, and make you into a successful patch-writer ;)
Comment 5 Mark Thomas 2017-04-06 22:06:34 UTC
Fixed in 9.0.x for 9.0.0.M20 onwards. I do not propose to back-port it.
Comment 6 Konstantin Kolinko 2017-12-12 10:32:29 UTC
See "Re:r1817800" thread for Tomcat 7 (fix for bug 61886)
http://tomcat.markmail.org/thread/y5yyhse2rsrgg35b

Java 6 generates helper anonymous classes when the code uses switch(enum).

E.g. in Tomcat 7
org.apache.coyote.http11.AbstractHttp11Processor$1 is

static class AbstractHttp11Processor$1 {
    static final int $SwitchMap$org$apache$coyote$ActionCode[];
    static final int $SwitchMap$org$apache$tomcat$util$log$UserDataHelper$Mode[];

    static 
    {
    // the int arrays are initialized with Enum.ordinal() -> some integer value mapping
    }
}

Essentially, it is a holder class that wraps a static field that is initialized lazily.
https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom

There is no way to assign a name to such class. As such, I went with an alternative implementation that uses a for(int++) loop to load all available classes starting with $1 - r1817901.

This will be in 7.0.84 onwards.
Comment 7 Konstantin Kolinko 2017-12-12 10:33:30 UTC
Reassigning to Tomcat 8 to track forward-porting of r1817901.
Comment 8 Remy Maucherat 2017-12-12 11:11:49 UTC
Nice fix. Since Tomcat 8 doesn't support the old JDKs, then it probably doesn't need the clever hack (IMO). The improvement should still be to stop using anonymous inner classes when they need preload.
Comment 9 Konstantin Kolinko 2017-12-12 13:46:48 UTC
(In reply to Remy Maucherat from comment #8)
> Nice fix. Since Tomcat 8 doesn't support the old JDKs, then it probably
> doesn't need the clever hack (IMO). The improvement should still be to stop
> using anonymous inner classes when they need preload.

Http11Processor$1.class is present in Tomcat 8.5.24/tomcat-coyote.jar

Decompiling it, it is the same $SwitchMap$ support class.
Comment 10 Konstantin Kolinko 2018-01-08 11:07:35 UTC
(In reply to Konstantin Kolinko from comment #7)
> Reassigning to Tomcat 8 to track forward-porting of r1817901.
+ r1820543

Applied to Tomcat 8.5 in r1820546
Applied to Tomcat 8.0 in r1820550

SecurityClassLoad classes in Tomcat 9.0 do not load any anonymous inner classes. Thus there is no need for this fix in Tomcat 9.