Explain the Commons Lang code inside of synchronized()?

I'm trying to to rewrite a piece of code from JakartaCommons-lang since we have a package that needs to have as few dependencies as possible

static void unregister(Object value) {
    Set registry = getRegistry();
    if (registry != null) {
        registry.remove(new IDKey(value));
        synchronized(class$org$apache$commons$lang$builder$HashCodeBuilder == null ? (class$org$apache$commons$lang$builder$HashCodeBuilder = class$("org.apache.commons.lang.builder.HashCodeBuilder")) : class$org$apache$commons$lang$builder$HashCodeBuilder) {
            registry = getRegistry();
            if (registry != null && registry.isEmpty()) {
                REGISTRY.set((Object)null);
            }
        }
    }
}

Can someone explain the code inside of synchronized()? Specifically all the '$'

1 answer

  • answered 2020-01-14 02:00 Andreas

    $ is a valid identifier character, so it's just part of the name.

    Note that use of $ is discouraged. The Java Language Specification, section 3.8. Identifiers says:

    The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII dollar sign ($, or \u0024) and underscore (_, or \u005f). The dollar sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems. The underscore may be used in identifiers formed of two or more characters, but it cannot be used as a one-character identifier due to being a keyword.

    Would it be more readable to you if it has used _ instead?

    synchronized (class_org_apache_commons_lang_builder_HashCodeBuilder == null ? (class_org_apache_commons_lang_builder_HashCodeBuilder = class_("org.apache.commons.lang.builder.HashCodeBuilder")) : class_org_apache_commons_lang_builder_HashCodeBuilder) {
        ...
    }
    

    or a shorter name?

    synchronized (builderClass == null ? (builderClass = getClass("org.apache.commons.lang.builder.HashCodeBuilder")) : builderClass) {
        ...
    }