src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/VersionPropsPlugin.java
author mr
Tue, 29 Oct 2019 08:26:55 -0700
changeset 58842 6c255334120d
permissions -rw-r--r--
8232080: jlink plugins for vendor information and run-time options Reviewed-by: ihse, alanb, kvn, bobv, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58842
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
     1
/*
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
     4
 *
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    10
 *
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    15
 * accompanied this code).
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    16
 *
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    20
 *
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    23
 * questions.
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    24
 */
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    25
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    26
package jdk.tools.jlink.internal.plugins;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    27
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    28
import java.io.*;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    29
import java.nio.charset.*;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    30
import java.util.*;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    31
import java.util.function.*;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    32
import java.util.stream.*;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    33
import jdk.tools.jlink.plugin.*;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    34
import jdk.internal.org.objectweb.asm.*;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    35
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    36
import static java.lang.System.out;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    37
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    38
/**
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    39
 * Base plugin to update a static field in java.lang.VersionProps
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    40
 */
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    41
abstract class VersionPropsPlugin implements Plugin {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    42
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    43
    private static final String VERSION_PROPS_CLASS
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    44
        = "/java.base/java/lang/VersionProps.class";
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    45
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    46
    private final String name;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    47
    private final String field;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    48
    private String value;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    49
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    50
    /**
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    51
     * @param field The name of the java.lang.VersionProps field to be redefined
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    52
     * @param option The option name
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    53
     */
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    54
    protected VersionPropsPlugin(String field, String option) {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    55
        this.field = field;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    56
        this.name = option;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    57
    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    58
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    59
    /**
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    60
     * Shorthand constructor for when the option name can be derived from the
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    61
     * name of the field.
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    62
     *
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    63
     * @param field The name of the java.lang.VersionProps field to be redefined
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    64
     */
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    65
    protected VersionPropsPlugin(String field) {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    66
        this(field, field.toLowerCase().replace('_', '-'));
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    67
    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    68
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    69
    @Override
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    70
    public String getName() {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    71
        return name;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    72
    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    73
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    74
    @Override
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    75
    public String getDescription() {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    76
        return PluginsResourceBundle.getDescription(name);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    77
    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    78
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    79
    @Override
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    80
    public Category getType() {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    81
        return Category.TRANSFORMER;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    82
    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    83
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    84
    @Override
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    85
    public boolean hasArguments() {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    86
        return true;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    87
    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    88
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    89
    @Override
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    90
    public boolean hasRawArgument() {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    91
        return true;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    92
    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    93
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    94
    @Override
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    95
    public String getArgumentsDescription() {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    96
       return PluginsResourceBundle.getArgument(name);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    97
    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    98
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
    99
    @Override
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   100
    public void configure(Map<String, String> config) {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   101
        var v = config.get(name);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   102
        if (v == null)
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   103
            throw new AssertionError();
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   104
        value = v;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   105
    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   106
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   107
    private boolean redefined = false;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   108
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   109
    private byte[] redefine(byte[] classFile) {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   110
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   111
        var cr = new ClassReader(classFile);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   112
        var cw = new ClassWriter(0);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   113
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   114
        cr.accept(new ClassVisitor(Opcodes.ASM7, cw) {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   115
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   116
                public MethodVisitor visitMethod(int access,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   117
                                                 String name,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   118
                                                 String desc,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   119
                                                 String sig,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   120
                                                 String[] xs)
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   121
                {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   122
                    if (name.equals("<clinit>"))
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   123
                        return new MethodVisitor(Opcodes.ASM7,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   124
                                                 super.visitMethod(access,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   125
                                                                   name,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   126
                                                                   desc,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   127
                                                                   sig,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   128
                                                                   xs))
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   129
                            {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   130
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   131
                                public void visitFieldInsn(int opcode,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   132
                                                           String owner,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   133
                                                           String name,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   134
                                                           String desc)
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   135
                                {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   136
                                    if (opcode == Opcodes.PUTSTATIC
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   137
                                        && name.equals(field))
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   138
                                    {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   139
                                        // Discard the original value
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   140
                                        super.visitInsn(Opcodes.POP);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   141
                                        // Load the value that we want
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   142
                                        super.visitLdcInsn(value);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   143
                                        redefined = true;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   144
                                    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   145
                                    super.visitFieldInsn(opcode, owner,
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   146
                                                         name, desc);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   147
                                }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   148
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   149
                        };
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   150
                    else
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   151
                        return super.visitMethod(access, name, desc, sig, xs);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   152
                }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   153
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   154
            }, 0);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   155
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   156
        return cw.toByteArray();
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   157
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   158
    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   159
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   160
    @Override
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   161
    public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   162
        in.transformAndCopy(res -> {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   163
                if (res.type().equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE)) {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   164
                    if (res.path().equals(VERSION_PROPS_CLASS)) {
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   165
                        return res.copyWithContent(redefine(res.contentBytes()));
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   166
                    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   167
                }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   168
                return res;
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   169
            }, out);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   170
        if (!redefined)
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   171
            throw new AssertionError(field);
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   172
        return out.build();
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   173
    }
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   174
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents:
diff changeset
   175
}