|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +import java.io.IOException; |
| 18 | +import java.net.URI; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.PathMatcher; |
| 22 | +import java.nio.file.Paths; |
| 23 | +import java.nio.file.attribute.FileTime; |
| 24 | +import java.time.Instant; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.HashSet; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Objects; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.TreeMap; |
| 33 | +import java.util.function.Predicate; |
| 34 | +import java.util.stream.Collectors; |
| 35 | +import java.util.stream.Stream; |
| 36 | +import java.util.zip.ZipEntry; |
| 37 | +import java.util.zip.ZipOutputStream; |
| 38 | + |
| 39 | +import org.objectweb.asm.AnnotationVisitor; |
| 40 | +import org.objectweb.asm.ClassReader; |
| 41 | +import org.objectweb.asm.ClassVisitor; |
| 42 | +import org.objectweb.asm.ClassWriter; |
| 43 | +import org.objectweb.asm.FieldVisitor; |
| 44 | +import org.objectweb.asm.MethodVisitor; |
| 45 | +import org.objectweb.asm.Opcodes; |
| 46 | +import org.objectweb.asm.Type; |
| 47 | + |
| 48 | +public final class ExtractJdkApis { |
| 49 | + |
| 50 | + private static final FileTime FIXED_FILEDATE = FileTime.from(Instant.parse("2022-01-01T00:00:00Z")); |
| 51 | + |
| 52 | + private static final String PATTERN_PANAMA_FOREIGN = "java.base/java/{lang/foreign/*,nio/channels/FileChannel,util/Objects}"; |
| 53 | + private static final String PATTERN_VECTOR_INCUBATOR = "jdk.incubator.vector/jdk/incubator/vector/*"; |
| 54 | + private static final String PATTERN_VECTOR_VM_INTERNALS = "java.base/jdk/internal/vm/vector/VectorSupport{,$Vector,$VectorMask,$VectorPayload,$VectorShuffle}"; |
| 55 | + |
| 56 | + static final Map<Integer,List<String>> CLASSFILE_PATTERNS = Map.of( |
| 57 | + 19, List.of(PATTERN_PANAMA_FOREIGN), |
| 58 | + 20, List.of(PATTERN_PANAMA_FOREIGN, PATTERN_VECTOR_VM_INTERNALS, PATTERN_VECTOR_INCUBATOR) |
| 59 | + ); |
| 60 | + |
| 61 | + public static void main(String... args) throws IOException { |
| 62 | + if (args.length != 2) { |
| 63 | + throw new IllegalArgumentException("Need two parameters: java version, output file"); |
| 64 | + } |
| 65 | + Integer jdk = Integer.valueOf(args[0]); |
| 66 | + if (jdk.intValue() != Runtime.version().feature()) { |
| 67 | + throw new IllegalStateException("Incorrect java version: " + Runtime.version().feature()); |
| 68 | + } |
| 69 | + if (!CLASSFILE_PATTERNS.containsKey(jdk)) { |
| 70 | + throw new IllegalArgumentException("No support to extract stubs from java version: " + jdk); |
| 71 | + } |
| 72 | + var outputPath = Paths.get(args[1]); |
| 73 | + |
| 74 | + // create JRT filesystem and build a combined FileMatcher: |
| 75 | + var jrtPath = Paths.get(URI.create("jrt:/")).toRealPath(); |
| 76 | + var patterns = CLASSFILE_PATTERNS.get(jdk).stream() |
| 77 | + .map(pattern -> jrtPath.getFileSystem().getPathMatcher("glob:" + pattern + ".class")) |
| 78 | + .toArray(PathMatcher[]::new); |
| 79 | + PathMatcher pattern = p -> Arrays.stream(patterns).anyMatch(matcher -> matcher.matches(p)); |
| 80 | + |
| 81 | + // Collect all files to process: |
| 82 | + final List<Path> filesToExtract; |
| 83 | + try (var stream = Files.walk(jrtPath)) { |
| 84 | + filesToExtract = stream.filter(p -> pattern.matches(jrtPath.relativize(p))).collect(Collectors.toList()); |
| 85 | + } |
| 86 | + |
| 87 | + // Process all class files: |
| 88 | + try (var out = new ZipOutputStream(Files.newOutputStream(outputPath))) { |
| 89 | + process(filesToExtract, out); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static void process(List<Path> filesToExtract, ZipOutputStream out) throws IOException { |
| 94 | + var classesToInclude = new HashSet<String>(); |
| 95 | + var references = new HashMap<String, String[]>(); |
| 96 | + var processed = new TreeMap<String, byte[]>(); |
| 97 | + System.out.println("Transforming " + filesToExtract.size() + " class files..."); |
| 98 | + for (Path p : filesToExtract) { |
| 99 | + try (var in = Files.newInputStream(p)) { |
| 100 | + var reader = new ClassReader(in); |
| 101 | + var cw = new ClassWriter(0); |
| 102 | + var cleaner = new Cleaner(cw, classesToInclude, references); |
| 103 | + reader.accept(cleaner, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); |
| 104 | + processed.put(reader.getClassName(), cw.toByteArray()); |
| 105 | + } |
| 106 | + } |
| 107 | + // recursively add all superclasses / interfaces of visible classes to classesToInclude: |
| 108 | + for (Set<String> a = classesToInclude; !a.isEmpty();) { |
| 109 | + a = a.stream().map(references::get).filter(Objects::nonNull).flatMap(Arrays::stream).collect(Collectors.toSet()); |
| 110 | + classesToInclude.addAll(a); |
| 111 | + } |
| 112 | + // remove all non-visible or not referenced classes: |
| 113 | + processed.keySet().removeIf(Predicate.not(classesToInclude::contains)); |
| 114 | + System.out.println("Writing " + processed.size() + " visible classes..."); |
| 115 | + for (var cls : processed.entrySet()) { |
| 116 | + String cn = cls.getKey(); |
| 117 | + System.out.println("Writing stub for class: " + cn); |
| 118 | + out.putNextEntry(new ZipEntry(cn.concat(".class")).setLastModifiedTime(FIXED_FILEDATE)); |
| 119 | + out.write(cls.getValue()); |
| 120 | + out.closeEntry(); |
| 121 | + } |
| 122 | + classesToInclude.removeIf(processed.keySet()::contains); |
| 123 | + System.out.println("Referenced classes not included: " + classesToInclude); |
| 124 | + } |
| 125 | + |
| 126 | + static boolean isVisible(int access) { |
| 127 | + return (access & (Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC)) != 0; |
| 128 | + } |
| 129 | + |
| 130 | + static class Cleaner extends ClassVisitor { |
| 131 | + private static final String PREVIEW_ANN = "jdk/internal/javac/PreviewFeature"; |
| 132 | + private static final String PREVIEW_ANN_DESCR = Type.getObjectType(PREVIEW_ANN).getDescriptor(); |
| 133 | + |
| 134 | + private final Set<String> classesToInclude; |
| 135 | + private final Map<String, String[]> references; |
| 136 | + |
| 137 | + Cleaner(ClassWriter out, Set<String> classesToInclude, Map<String, String[]> references) { |
| 138 | + super(Opcodes.ASM9, out); |
| 139 | + this.classesToInclude = classesToInclude; |
| 140 | + this.references = references; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { |
| 145 | + super.visit(Opcodes.V11, access, name, signature, superName, interfaces); |
| 146 | + if (isVisible(access)) { |
| 147 | + classesToInclude.add(name); |
| 148 | + } |
| 149 | + references.put(name, Stream.concat(Stream.of(superName), Arrays.stream(interfaces)).toArray(String[]::new)); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) { |
| 154 | + return Objects.equals(descriptor, PREVIEW_ANN_DESCR) ? null : super.visitAnnotation(descriptor, visible); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) { |
| 159 | + if (!isVisible(access)) { |
| 160 | + return null; |
| 161 | + } |
| 162 | + return new FieldVisitor(Opcodes.ASM9, super.visitField(access, name, descriptor, signature, value)) { |
| 163 | + @Override |
| 164 | + public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) { |
| 165 | + return Objects.equals(descriptor, PREVIEW_ANN_DESCR) ? null : super.visitAnnotation(descriptor, visible); |
| 166 | + } |
| 167 | + }; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { |
| 172 | + if (!isVisible(access)) { |
| 173 | + return null; |
| 174 | + } |
| 175 | + return new MethodVisitor(Opcodes.ASM9, super.visitMethod(access, name, descriptor, signature, exceptions)) { |
| 176 | + @Override |
| 177 | + public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) { |
| 178 | + return Objects.equals(descriptor, PREVIEW_ANN_DESCR) ? null : super.visitAnnotation(descriptor, visible); |
| 179 | + } |
| 180 | + }; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void visitInnerClass(String name, String outerName, String innerName, int access) { |
| 185 | + if (!Objects.equals(outerName, PREVIEW_ANN)) { |
| 186 | + super.visitInnerClass(name, outerName, innerName, access); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public void visitPermittedSubclass(String c) { |
| 192 | + } |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments