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