src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/GenerateJLIClassesPlugin.java
changeset 48071 a5cadeee8763
parent 47753 a2008587c13f
child 49206 e991d0be4cc9
equal deleted inserted replaced
48070:a4b6dd7ba4b8 48071:a5cadeee8763
    52  * Plugin to generate java.lang.invoke classes.
    52  * Plugin to generate java.lang.invoke classes.
    53  */
    53  */
    54 public final class GenerateJLIClassesPlugin implements Plugin {
    54 public final class GenerateJLIClassesPlugin implements Plugin {
    55 
    55 
    56     private static final String NAME = "generate-jli-classes";
    56     private static final String NAME = "generate-jli-classes";
    57     private static final String IGNORE_VERSION = "ignore-version";
       
    58 
    57 
    59     private static final String DESCRIPTION = PluginsResourceBundle.getDescription(NAME);
    58     private static final String DESCRIPTION = PluginsResourceBundle.getDescription(NAME);
    60     private static final String IGNORE_VERSION_WARNING = NAME + ".ignore.version.warn";
       
    61     private static final String VERSION_MISMATCH_WARNING = NAME + ".version.mismatch.warn";
       
    62 
    59 
    63     private static final String DEFAULT_TRACE_FILE = "default_jli_trace.txt";
    60     private static final String DEFAULT_TRACE_FILE = "default_jli_trace.txt";
    64 
    61 
    65     private static final String DIRECT_HOLDER = "java/lang/invoke/DirectMethodHandle$Holder";
    62     private static final String DIRECT_HOLDER = "java/lang/invoke/DirectMethodHandle$Holder";
    66     private static final String DMH_INVOKE_VIRTUAL = "invokeVirtual";
    63     private static final String DMH_INVOKE_VIRTUAL = "invokeVirtual";
    82     Set<String> invokerTypes = Set.of();
    79     Set<String> invokerTypes = Set.of();
    83 
    80 
    84     Map<String, Set<String>> dmhMethods = Map.of();
    81     Map<String, Set<String>> dmhMethods = Map.of();
    85 
    82 
    86     String mainArgument;
    83     String mainArgument;
    87 
       
    88     boolean ignoreVersion;
       
    89 
    84 
    90     public GenerateJLIClassesPlugin() {
    85     public GenerateJLIClassesPlugin() {
    91     }
    86     }
    92 
    87 
    93     @Override
    88     @Override
   168             );
   163             );
   169 
   164 
   170     @Override
   165     @Override
   171     public void configure(Map<String, String> config) {
   166     public void configure(Map<String, String> config) {
   172         mainArgument = config.get(NAME);
   167         mainArgument = config.get(NAME);
   173         ignoreVersion = Boolean.parseBoolean(config.get(IGNORE_VERSION));
       
   174     }
   168     }
   175 
   169 
   176     public void initialize(ResourcePool in) {
   170     public void initialize(ResourcePool in) {
   177         // Start with the default configuration
   171         // Start with the default configuration
   178         speciesTypes = defaultSpecies().stream()
   172         speciesTypes = defaultSpecies().stream()
   204             File file = new File(mainArgument.substring(1));
   198             File file = new File(mainArgument.substring(1));
   205             if (file.exists()) {
   199             if (file.exists()) {
   206                 readTraceConfig(fileLines(file));
   200                 readTraceConfig(fileLines(file));
   207             }
   201             }
   208         }
   202         }
   209     }
       
   210 
       
   211     private boolean checkVersion(Runtime.Version linkedVersion) {
       
   212         Runtime.Version baseVersion = Runtime.version();
       
   213         if (baseVersion.major() != linkedVersion.major() ||
       
   214                 baseVersion.minor() != linkedVersion.minor()) {
       
   215             return false;
       
   216         }
       
   217         return true;
       
   218     }
       
   219 
       
   220     private Runtime.Version getLinkedVersion(ResourcePool in) {
       
   221         ModuleDescriptor.Version version = in.moduleView()
       
   222                 .findModule("java.base")
       
   223                 .get()
       
   224                 .descriptor()
       
   225                 .version()
       
   226                 .orElseThrow(() -> new PluginException("No version defined in "
       
   227                         + "the java.base being linked"));
       
   228          return Runtime.Version.parse(version.toString());
       
   229     }
   203     }
   230 
   204 
   231     private void readTraceConfig(Stream<String> lines) {
   205     private void readTraceConfig(Stream<String> lines) {
   232         // Use TreeSet/TreeMap to keep things sorted in a deterministic
   206         // Use TreeSet/TreeMap to keep things sorted in a deterministic
   233         // order to avoid scrambling the layout on small changes and to
   207         // order to avoid scrambling the layout on small changes and to
   313         }
   287         }
   314     }
   288     }
   315 
   289 
   316     @Override
   290     @Override
   317     public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
   291     public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
   318         if (ignoreVersion) {
       
   319             System.out.println(
       
   320                     PluginsResourceBundle
       
   321                             .getMessage(IGNORE_VERSION_WARNING));
       
   322         } else if (!checkVersion(getLinkedVersion(in))) {
       
   323             // The linked images are not version compatible
       
   324             if (mainArgument != null) {
       
   325                 // Log a mismatch warning if an argument was specified
       
   326                 System.out.println(
       
   327                         PluginsResourceBundle
       
   328                                 .getMessage(VERSION_MISMATCH_WARNING,
       
   329                                             getLinkedVersion(in),
       
   330                                             Runtime.version()));
       
   331             }
       
   332             in.transformAndCopy(entry -> entry, out);
       
   333             return out.build();
       
   334         }
       
   335 
       
   336         initialize(in);
   292         initialize(in);
   337         // Copy all but DMH_ENTRY to out
   293         // Copy all but DMH_ENTRY to out
   338         in.transformAndCopy(entry -> {
   294         in.transformAndCopy(entry -> {
   339                 // filter out placeholder entries
   295                 // filter out placeholder entries
   340                 if (entry.path().equals(DIRECT_METHOD_HOLDER_ENTRY) ||
   296                 if (entry.path().equals(DIRECT_METHOD_HOLDER_ENTRY) ||