|
| 1 | +/* |
| 2 | + * Copyright 2003-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.graalvm.buildtools.utils; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.PrintWriter; |
| 20 | +import java.io.Writer; |
| 21 | +import java.nio.file.FileSystem; |
| 22 | +import java.nio.file.FileSystems; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.TreeSet; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +/** |
| 30 | + * Performs scanning of a JAR file and extracts some metadata in the |
| 31 | + * form of a properties file. For now this type only extracts the list |
| 32 | + * of packages from a jar. |
| 33 | + */ |
| 34 | +public class JarScanner { |
| 35 | + /** |
| 36 | + * Scans a jar and creates a properties file with metadata about the jar contents. |
| 37 | + * @param inputJar the input jar |
| 38 | + * @param outputFile the output file |
| 39 | + * @throws IOException |
| 40 | + */ |
| 41 | + public static void scanJar(Path inputJar, Path outputFile) throws IOException { |
| 42 | + try (Writer fileWriter = Files.newBufferedWriter(outputFile); PrintWriter writer = new PrintWriter(fileWriter)) { |
| 43 | + Set<String> packageList = new TreeSet<>(); |
| 44 | + try (FileSystem jarFileSystem = FileSystems.newFileSystem(inputJar, null)) { |
| 45 | + Path root = jarFileSystem.getPath("/"); |
| 46 | + try (Stream<Path> files = Files.walk(root)) { |
| 47 | + files.forEach(path -> { |
| 48 | + if (path.toString().endsWith(".class") && !path.toString().contains("META-INF")) { |
| 49 | + Path relativePath = root.relativize(path); |
| 50 | + String className = relativePath.toString() |
| 51 | + .replace('/', '.') |
| 52 | + .replace('\\', '.') |
| 53 | + .replaceAll("[.]class$", ""); |
| 54 | + var lastDot = className.lastIndexOf("."); |
| 55 | + if (lastDot > 0) { |
| 56 | + var packageName = className.substring(0, lastDot); |
| 57 | + packageList.add(packageName); |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + } |
| 63 | + writer.println("packages=" + String.join(",", packageList)); |
| 64 | + } catch (IOException ex) { |
| 65 | + throw new RuntimeException("Unable to write JAR analysis", ex); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments