src/java.base/share/classes/java/util/jar/Attributes.java
changeset 54275 5ee30b6991a7
parent 53630 53c72d9d962b
child 54337 5a9d780eb9dd
equal deleted inserted replaced
54274:a7358438d463 54275:5ee30b6991a7
    32 import java.util.LinkedHashMap;
    32 import java.util.LinkedHashMap;
    33 import java.util.Map;
    33 import java.util.Map;
    34 import java.util.Objects;
    34 import java.util.Objects;
    35 import java.util.Set;
    35 import java.util.Set;
    36 
    36 
       
    37 import jdk.internal.misc.VM;
       
    38 import jdk.internal.vm.annotation.Stable;
    37 import sun.util.logging.PlatformLogger;
    39 import sun.util.logging.PlatformLogger;
    38 
    40 
    39 import static java.nio.charset.StandardCharsets.UTF_8;
    41 import static java.nio.charset.StandardCharsets.UTF_8;
    40 
    42 
    41 /**
    43 /**
   452         private final int hashCode;
   454         private final int hashCode;
   453 
   455 
   454         /**
   456         /**
   455          * Avoid allocation for common Names
   457          * Avoid allocation for common Names
   456          */
   458          */
   457         private static final Map<String, Name> KNOWN_NAMES;
   459         private static @Stable Map<String, Name> KNOWN_NAMES;
   458 
   460 
   459         static final Name of(String name) {
   461         static final Name of(String name) {
   460             Name n = KNOWN_NAMES.get(name);
   462             Name n = KNOWN_NAMES.get(name);
   461             if (n != null) {
   463             if (n != null) {
   462                 return n;
   464                 return n;
   539          * manifest attribute. This attribute indicates the version number
   541          * manifest attribute. This attribute indicates the version number
   540          * of the manifest standard to which a JAR file's manifest conforms.
   542          * of the manifest standard to which a JAR file's manifest conforms.
   541          * @see <a href="{@docRoot}/../specs/jar/jar.html#jar-manifest">
   543          * @see <a href="{@docRoot}/../specs/jar/jar.html#jar-manifest">
   542          *      Manifest and Signature Specification</a>
   544          *      Manifest and Signature Specification</a>
   543          */
   545          */
   544         public static final Name MANIFEST_VERSION = new Name("Manifest-Version");
   546         public static final Name MANIFEST_VERSION;
   545 
   547 
   546         /**
   548         /**
   547          * {@code Name} object for {@code Signature-Version}
   549          * {@code Name} object for {@code Signature-Version}
   548          * manifest attribute used when signing JAR files.
   550          * manifest attribute used when signing JAR files.
   549          * @see <a href="{@docRoot}/../specs/jar/jar.html#jar-manifest">
   551          * @see <a href="{@docRoot}/../specs/jar/jar.html#jar-manifest">
   550          *      Manifest and Signature Specification</a>
   552          *      Manifest and Signature Specification</a>
   551          */
   553          */
   552         public static final Name SIGNATURE_VERSION = new Name("Signature-Version");
   554         public static final Name SIGNATURE_VERSION;
   553 
   555 
   554         /**
   556         /**
   555          * {@code Name} object for {@code Content-Type}
   557          * {@code Name} object for {@code Content-Type}
   556          * manifest attribute.
   558          * manifest attribute.
   557          */
   559          */
   558         public static final Name CONTENT_TYPE = new Name("Content-Type");
   560         public static final Name CONTENT_TYPE;
   559 
   561 
   560         /**
   562         /**
   561          * {@code Name} object for {@code Class-Path}
   563          * {@code Name} object for {@code Class-Path}
   562          * manifest attribute.
   564          * manifest attribute.
   563          * @see <a href="{@docRoot}/../specs/jar/jar.html#class-path-attribute">
   565          * @see <a href="{@docRoot}/../specs/jar/jar.html#class-path-attribute">
   564          *      JAR file specification</a>
   566          *      JAR file specification</a>
   565          */
   567          */
   566         public static final Name CLASS_PATH = new Name("Class-Path");
   568         public static final Name CLASS_PATH;
   567 
   569 
   568         /**
   570         /**
   569          * {@code Name} object for {@code Main-Class} manifest
   571          * {@code Name} object for {@code Main-Class} manifest
   570          * attribute used for launching applications packaged in JAR files.
   572          * attribute used for launching applications packaged in JAR files.
   571          * The {@code Main-Class} attribute is used in conjunction
   573          * The {@code Main-Class} attribute is used in conjunction
   572          * with the {@code -jar} command-line option of the
   574          * with the {@code -jar} command-line option of the
   573          * {@code java} application launcher.
   575          * {@code java} application launcher.
   574          */
   576          */
   575         public static final Name MAIN_CLASS = new Name("Main-Class");
   577         public static final Name MAIN_CLASS;
   576 
   578 
   577         /**
   579         /**
   578          * {@code Name} object for {@code Sealed} manifest attribute
   580          * {@code Name} object for {@code Sealed} manifest attribute
   579          * used for sealing.
   581          * used for sealing.
   580          * @see <a href="{@docRoot}/../specs/jar/jar.html#package-sealing">
   582          * @see <a href="{@docRoot}/../specs/jar/jar.html#package-sealing">
   581          *      Package Sealing</a>
   583          *      Package Sealing</a>
   582          */
   584          */
   583         public static final Name SEALED = new Name("Sealed");
   585         public static final Name SEALED;
   584 
   586 
   585         /**
   587         /**
   586          * {@code Name} object for {@code Extension-List} manifest attribute
   588          * {@code Name} object for {@code Extension-List} manifest attribute
   587          * used for the extension mechanism that is no longer supported.
   589          * used for the extension mechanism that is no longer supported.
   588          */
   590          */
   589         public static final Name EXTENSION_LIST = new Name("Extension-List");
   591         public static final Name EXTENSION_LIST;
   590 
   592 
   591         /**
   593         /**
   592          * {@code Name} object for {@code Extension-Name} manifest attribute.
   594          * {@code Name} object for {@code Extension-Name} manifest attribute.
   593          * used for the extension mechanism that is no longer supported.
   595          * used for the extension mechanism that is no longer supported.
   594          */
   596          */
   595         public static final Name EXTENSION_NAME = new Name("Extension-Name");
   597         public static final Name EXTENSION_NAME;
   596 
   598 
   597         /**
   599         /**
   598          * {@code Name} object for {@code Extension-Installation} manifest attribute.
   600          * {@code Name} object for {@code Extension-Installation} manifest attribute.
   599          *
   601          *
   600          * @deprecated Extension mechanism is no longer supported.
   602          * @deprecated Extension mechanism is no longer supported.
   601          */
   603          */
   602         @Deprecated
   604         @Deprecated
   603         public static final Name EXTENSION_INSTALLATION = new Name("Extension-Installation");
   605         public static final Name EXTENSION_INSTALLATION;
   604 
   606 
   605         /**
   607         /**
   606          * {@code Name} object for {@code Implementation-Title}
   608          * {@code Name} object for {@code Implementation-Title}
   607          * manifest attribute used for package versioning.
   609          * manifest attribute used for package versioning.
   608          */
   610          */
   609         public static final Name IMPLEMENTATION_TITLE = new Name("Implementation-Title");
   611         public static final Name IMPLEMENTATION_TITLE;
   610 
   612 
   611         /**
   613         /**
   612          * {@code Name} object for {@code Implementation-Version}
   614          * {@code Name} object for {@code Implementation-Version}
   613          * manifest attribute used for package versioning.
   615          * manifest attribute used for package versioning.
   614          */
   616          */
   615         public static final Name IMPLEMENTATION_VERSION = new Name("Implementation-Version");
   617         public static final Name IMPLEMENTATION_VERSION;
   616 
   618 
   617         /**
   619         /**
   618          * {@code Name} object for {@code Implementation-Vendor}
   620          * {@code Name} object for {@code Implementation-Vendor}
   619          * manifest attribute used for package versioning.
   621          * manifest attribute used for package versioning.
   620          */
   622          */
   621         public static final Name IMPLEMENTATION_VENDOR = new Name("Implementation-Vendor");
   623         public static final Name IMPLEMENTATION_VENDOR;
   622 
   624 
   623         /**
   625         /**
   624          * {@code Name} object for {@code Implementation-Vendor-Id}
   626          * {@code Name} object for {@code Implementation-Vendor-Id}
   625          * manifest attribute.
   627          * manifest attribute.
   626          *
   628          *
   627          * @deprecated Extension mechanism is no longer supported.
   629          * @deprecated Extension mechanism is no longer supported.
   628          */
   630          */
   629         @Deprecated
   631         @Deprecated
   630         public static final Name IMPLEMENTATION_VENDOR_ID = new Name("Implementation-Vendor-Id");
   632         public static final Name IMPLEMENTATION_VENDOR_ID;
   631 
   633 
   632         /**
   634         /**
   633          * {@code Name} object for {@code Implementation-URL}
   635          * {@code Name} object for {@code Implementation-URL}
   634          * manifest attribute.
   636          * manifest attribute.
   635          *
   637          *
   636          * @deprecated Extension mechanism is no longer supported.
   638          * @deprecated Extension mechanism is no longer supported.
   637          */
   639          */
   638         @Deprecated
   640         @Deprecated
   639         public static final Name IMPLEMENTATION_URL = new Name("Implementation-URL");
   641         public static final Name IMPLEMENTATION_URL;
   640 
   642 
   641         /**
   643         /**
   642          * {@code Name} object for {@code Specification-Title}
   644          * {@code Name} object for {@code Specification-Title}
   643          * manifest attribute used for package versioning.
   645          * manifest attribute used for package versioning.
   644          */
   646          */
   645         public static final Name SPECIFICATION_TITLE = new Name("Specification-Title");
   647         public static final Name SPECIFICATION_TITLE;
   646 
   648 
   647         /**
   649         /**
   648          * {@code Name} object for {@code Specification-Version}
   650          * {@code Name} object for {@code Specification-Version}
   649          * manifest attribute used for package versioning.
   651          * manifest attribute used for package versioning.
   650          */
   652          */
   651         public static final Name SPECIFICATION_VERSION = new Name("Specification-Version");
   653         public static final Name SPECIFICATION_VERSION;
   652 
   654 
   653         /**
   655         /**
   654          * {@code Name} object for {@code Specification-Vendor}
   656          * {@code Name} object for {@code Specification-Vendor}
   655          * manifest attribute used for package versioning.
   657          * manifest attribute used for package versioning.
   656          */
   658          */
   657         public static final Name SPECIFICATION_VENDOR = new Name("Specification-Vendor");
   659         public static final Name SPECIFICATION_VENDOR;
   658 
   660 
   659         /**
   661         /**
   660          * {@code Name} object for {@code Multi-Release}
   662          * {@code Name} object for {@code Multi-Release}
   661          * manifest attribute that indicates this is a multi-release JAR file.
   663          * manifest attribute that indicates this is a multi-release JAR file.
   662          *
   664          *
   663          * @since   9
   665          * @since   9
   664          */
   666          */
   665         public static final Name MULTI_RELEASE = new Name("Multi-Release");
   667         public static final Name MULTI_RELEASE;
   666 
   668 
   667         private static void addName(Map<String, Name> names, Name name) {
   669         private static void addName(Map<String, Name> names, Name name) {
   668             names.put(name.name, name);
   670             names.put(name.name, name);
   669         }
   671         }
   670 
   672 
   671         static {
   673         static {
   672             var names = new HashMap<String, Name>(64);
   674 
   673             addName(names, MANIFEST_VERSION);
   675             VM.initializeFromArchive(Attributes.Name.class);
   674             addName(names, SIGNATURE_VERSION);
   676 
   675             addName(names, CONTENT_TYPE);
   677             if (KNOWN_NAMES == null) {
   676             addName(names, CLASS_PATH);
   678                 MANIFEST_VERSION = new Name("Manifest-Version");
   677             addName(names, MAIN_CLASS);
   679                 SIGNATURE_VERSION = new Name("Signature-Version");
   678             addName(names, SEALED);
   680                 CONTENT_TYPE = new Name("Content-Type");
   679             addName(names, EXTENSION_LIST);
   681                 CLASS_PATH = new Name("Class-Path");
   680             addName(names, EXTENSION_NAME);
   682                 MAIN_CLASS = new Name("Main-Class");
   681             addName(names, IMPLEMENTATION_TITLE);
   683                 SEALED = new Name("Sealed");
   682             addName(names, IMPLEMENTATION_VERSION);
   684                 EXTENSION_LIST = new Name("Extension-List");
   683             addName(names, IMPLEMENTATION_VENDOR);
   685                 EXTENSION_NAME = new Name("Extension-Name");
   684             addName(names, SPECIFICATION_TITLE);
   686                 EXTENSION_INSTALLATION = new Name("Extension-Installation");
   685             addName(names, SPECIFICATION_VERSION);
   687                 IMPLEMENTATION_TITLE = new Name("Implementation-Title");
   686             addName(names, SPECIFICATION_VENDOR);
   688                 IMPLEMENTATION_VERSION = new Name("Implementation-Version");
   687             addName(names, MULTI_RELEASE);
   689                 IMPLEMENTATION_VENDOR = new Name("Implementation-Vendor");
   688 
   690                 IMPLEMENTATION_VENDOR_ID = new Name("Implementation-Vendor-Id");
   689             // Common attributes used in MANIFEST.MF et.al; adding these has a
   691                 IMPLEMENTATION_URL = new Name("Implementation-URL");
   690             // small footprint cost, but is likely to be quickly paid for by
   692                 SPECIFICATION_TITLE = new Name("Specification-Title");
   691             // reducing allocation when reading and parsing typical manifests
   693                 SPECIFICATION_VERSION = new Name("Specification-Version");
   692             addName(names, new Name("Add-Exports"));
   694                 SPECIFICATION_VENDOR = new Name("Specification-Vendor");
   693             addName(names, new Name("Add-Opens"));
   695                 MULTI_RELEASE = new Name("Multi-Release");
   694             addName(names, new Name("Ant-Version"));
   696 
   695             addName(names, new Name("Archiver-Version"));
   697                 var names = new HashMap<String, Name>(64);
   696             addName(names, new Name("Build-Jdk"));
   698                 addName(names, MANIFEST_VERSION);
   697             addName(names, new Name("Built-By"));
   699                 addName(names, SIGNATURE_VERSION);
   698             addName(names, new Name("Bnd-LastModified"));
   700                 addName(names, CONTENT_TYPE);
   699             addName(names, new Name("Bundle-Description"));
   701                 addName(names, CLASS_PATH);
   700             addName(names, new Name("Bundle-DocURL"));
   702                 addName(names, MAIN_CLASS);
   701             addName(names, new Name("Bundle-License"));
   703                 addName(names, SEALED);
   702             addName(names, new Name("Bundle-ManifestVersion"));
   704                 addName(names, EXTENSION_LIST);
   703             addName(names, new Name("Bundle-Name"));
   705                 addName(names, EXTENSION_NAME);
   704             addName(names, new Name("Bundle-Vendor"));
   706                 addName(names, IMPLEMENTATION_TITLE);
   705             addName(names, new Name("Bundle-Version"));
   707                 addName(names, IMPLEMENTATION_VERSION);
   706             addName(names, new Name("Bundle-SymbolicName"));
   708                 addName(names, IMPLEMENTATION_VENDOR);
   707             addName(names, new Name("Created-By"));
   709                 addName(names, SPECIFICATION_TITLE);
   708             addName(names, new Name("Export-Package"));
   710                 addName(names, SPECIFICATION_VERSION);
   709             addName(names, new Name("Import-Package"));
   711                 addName(names, SPECIFICATION_VENDOR);
   710             addName(names, new Name("Name"));
   712                 addName(names, MULTI_RELEASE);
   711             addName(names, new Name("SHA1-Digest"));
   713 
   712             addName(names, new Name("X-Compile-Source-JDK"));
   714                 // Common attributes used in MANIFEST.MF et.al; adding these has a
   713             addName(names, new Name("X-Compile-Target-JDK"));
   715                 // small footprint cost, but is likely to be quickly paid for by
   714             KNOWN_NAMES = names;
   716                 // reducing allocation when reading and parsing typical manifests
       
   717 
       
   718                 // JDK internal attributes
       
   719                 addName(names, new Name("Add-Exports"));
       
   720                 addName(names, new Name("Add-Opens"));
       
   721                 // LauncherHelper attributes
       
   722                 addName(names, new Name("Launcher-Agent-Class"));
       
   723                 addName(names, new Name("JavaFX-Application-Class"));
       
   724                 // jarsigner attributes
       
   725                 addName(names, new Name("Name"));
       
   726                 addName(names, new Name("Created-By"));
       
   727                 addName(names, new Name("SHA1-Digest"));
       
   728                 addName(names, new Name("SHA-256-Digest"));
       
   729                 KNOWN_NAMES = Map.copyOf(names);
       
   730             } else {
       
   731                 // Even if KNOWN_NAMES was read from archive, we still need
       
   732                 // to initialize the public constants
       
   733                 MANIFEST_VERSION = KNOWN_NAMES.get("Manifest-Version");
       
   734                 SIGNATURE_VERSION = KNOWN_NAMES.get("Signature-Version");
       
   735                 CONTENT_TYPE = KNOWN_NAMES.get("Content-Type");
       
   736                 CLASS_PATH = KNOWN_NAMES.get("Class-Path");
       
   737                 MAIN_CLASS = KNOWN_NAMES.get("Main-Class");
       
   738                 SEALED = KNOWN_NAMES.get("Sealed");
       
   739                 EXTENSION_LIST = KNOWN_NAMES.get("Extension-List");
       
   740                 EXTENSION_NAME = KNOWN_NAMES.get("Extension-Name");
       
   741                 EXTENSION_INSTALLATION = KNOWN_NAMES.get("Extension-Installation");
       
   742                 IMPLEMENTATION_TITLE = KNOWN_NAMES.get("Implementation-Title");
       
   743                 IMPLEMENTATION_VERSION = KNOWN_NAMES.get("Implementation-Version");
       
   744                 IMPLEMENTATION_VENDOR = KNOWN_NAMES.get("Implementation-Vendor");
       
   745                 IMPLEMENTATION_VENDOR_ID = KNOWN_NAMES.get("Implementation-Vendor-Id");
       
   746                 IMPLEMENTATION_URL = KNOWN_NAMES.get("Implementation-URL");
       
   747                 SPECIFICATION_TITLE = KNOWN_NAMES.get("Specification-Title");
       
   748                 SPECIFICATION_VERSION = KNOWN_NAMES.get("Specification-Version");
       
   749                 SPECIFICATION_VENDOR = KNOWN_NAMES.get("Specification-Vendor");
       
   750                 MULTI_RELEASE = KNOWN_NAMES.get("Multi-Release");
       
   751             }
   715         }
   752         }
   716     }
   753     }
   717 }
   754 }