Skip to content

Use String.intern() for Class reading [SPR-14886] #19452

Closed
@spring-projects-issues

Description

@spring-projects-issues

Renier Roth opened SPR-14886 and commented

Another place where String.intern() can be used as described in issue #19428
is in
file: org.springframework.asm.ClassReader
line: 3463

{CODE}
/**

  • Reads a class constant pool item in {@link #b b}. <i>This method is

  • intended for {@link Attribute} sub classes, and is normally not needed by

  • class generators or adapters.</i>

  • @param index

  the start index of an unsigned short value in {@link #b b},
  whose value is the index of a class constant pool item.
  • @param buf

  buffer to be used to read the item. This buffer must be
  sufficiently large. It is not automatically resized.
  • @return the String corresponding to the specified class item.
    */
    public String readClass(final int index, final char[] buf) {
    // computes the start index of the CONSTANT_Class item in b
    // and reads the CONSTANT_Utf8 item designated by
    // the first two bytes of this CONSTANT_Class item
    return readUTF8(items[readUnsignedShort(index)], buf);
    }
    {CODE}

This is the most inner class i could find that create the String. So the return of this method can use ....intern()

This method is used internally inside the class for super / interface and other classnames.
Spring uses this ClassReader via an Visitor "AnnotationMetadataReadingVisitor" more precise the extend "ClassMetadataReadingVisitor".

This visitor is returned as an implementation for the Interface "AnnotationMetadata" inside "SimpleMetadataReader" which is the only implementation for an metadata reader in the "ScannedGenericBeanDefinition" definition which will be hold inside memory.

Complicated enough, if you read backwards the ScannedGenericBeanDefinition stores the metadata as a field. This is the Visitor described above, which uses the ASM ClassReader to generate the Strings. The Strings are stored multiple times in Memory, this can be avoided to intern the String inside this chain of classes - most inner one is ClassReader (see source above).


Affects: 4.3.4

Issue Links:

Referenced from: commits 3a29daa, 7ffed85

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions