|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.gradle.internal.transport; |
| 11 | + |
| 12 | +import org.gradle.api.DefaultTask; |
| 13 | +import org.gradle.api.file.ConfigurableFileCollection; |
| 14 | +import org.gradle.api.file.RegularFileProperty; |
| 15 | +import org.gradle.api.tasks.CacheableTask; |
| 16 | +import org.gradle.api.tasks.Classpath; |
| 17 | +import org.gradle.api.tasks.OutputFile; |
| 18 | +import org.gradle.api.tasks.TaskAction; |
| 19 | +import org.objectweb.asm.ClassReader; |
| 20 | +import org.objectweb.asm.ClassVisitor; |
| 21 | +import org.objectweb.asm.Label; |
| 22 | +import org.objectweb.asm.MethodVisitor; |
| 23 | +import org.objectweb.asm.Opcodes; |
| 24 | +import org.objectweb.asm.tree.LdcInsnNode; |
| 25 | +import org.objectweb.asm.tree.MethodNode; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.nio.file.FileVisitResult; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
| 32 | +import java.nio.file.SimpleFileVisitor; |
| 33 | +import java.nio.file.attribute.BasicFileAttributes; |
| 34 | +import java.util.HashSet; |
| 35 | +import java.util.Set; |
| 36 | + |
| 37 | +/** |
| 38 | + * This task locates all method invocations of org.elasticsearch.TransportVersion#fromName(java.lang.String) in the |
| 39 | + * provided directory, and then records the value of string literals passed as arguments. It then records each |
| 40 | + * string on a newline along with path and line number in the provided output file. |
| 41 | + */ |
| 42 | +@CacheableTask |
| 43 | +public abstract class CollectTransportVersionReferencesTask extends DefaultTask { |
| 44 | + public static final String TRANSPORT_VERSION_SET_CLASS = "org/elasticsearch/TransportVersion"; |
| 45 | + public static final String TRANSPORT_VERSION_SET_METHOD_NAME = "fromName"; |
| 46 | + public static final String CLASS_EXTENSION = ".class"; |
| 47 | + public static final String MODULE_INFO = "module-info.class"; |
| 48 | + |
| 49 | + /** |
| 50 | + * The directory to scan for method invocations. |
| 51 | + */ |
| 52 | + @Classpath |
| 53 | + public abstract ConfigurableFileCollection getClassPath(); |
| 54 | + |
| 55 | + /** |
| 56 | + * The output file, with each newline containing the string literal argument of each method |
| 57 | + * invocation. |
| 58 | + */ |
| 59 | + @OutputFile |
| 60 | + public abstract RegularFileProperty getOutputFile(); |
| 61 | + |
| 62 | + @TaskAction |
| 63 | + public void checkTransportVersion() throws IOException { |
| 64 | + var results = new HashSet<TransportVersionUtils.TransportVersionReference>(); |
| 65 | + |
| 66 | + for (var cpElement : getClassPath()) { |
| 67 | + Path file = cpElement.toPath(); |
| 68 | + if (Files.isDirectory(file)) { |
| 69 | + addNamesFromClassesDirectory(results, file); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Path outputFile = getOutputFile().get().getAsFile().toPath(); |
| 74 | + Files.writeString(outputFile, String.join("\n", results.stream().map(Object::toString).sorted().toList())); |
| 75 | + } |
| 76 | + |
| 77 | + private void addNamesFromClassesDirectory(Set<TransportVersionUtils.TransportVersionReference> results, Path file) throws IOException { |
| 78 | + Files.walkFileTree(file, new SimpleFileVisitor<>() { |
| 79 | + @Override |
| 80 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 81 | + String filename = file.getFileName().toString(); |
| 82 | + if (filename.endsWith(CLASS_EXTENSION) && filename.endsWith(MODULE_INFO) == false) { |
| 83 | + try (var inputStream = Files.newInputStream(file)) { |
| 84 | + addNamesFromClass(results, inputStream, classname(file.toString())); |
| 85 | + } |
| 86 | + } |
| 87 | + return FileVisitResult.CONTINUE; |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + private void addNamesFromClass(Set<TransportVersionUtils.TransportVersionReference> results, InputStream classBytes, String classname) |
| 93 | + throws IOException { |
| 94 | + ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM9) { |
| 95 | + @Override |
| 96 | + public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { |
| 97 | + return new MethodNode(Opcodes.ASM9, access, name, descriptor, signature, exceptions) { |
| 98 | + int lineNumber = -1; |
| 99 | + |
| 100 | + @Override |
| 101 | + public void visitLineNumber(int line, Label start) { |
| 102 | + lineNumber = line; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) { |
| 107 | + if (owner.equals(TRANSPORT_VERSION_SET_CLASS) && name.equals(TRANSPORT_VERSION_SET_METHOD_NAME)) { |
| 108 | + var abstractInstruction = this.instructions.getLast(); |
| 109 | + String location = classname + " line " + lineNumber; |
| 110 | + if (abstractInstruction instanceof LdcInsnNode ldcInsnNode |
| 111 | + && ldcInsnNode.cst instanceof String tvName |
| 112 | + && tvName.isEmpty() == false) { |
| 113 | + results.add(new TransportVersionUtils.TransportVersionReference(tvName, location)); |
| 114 | + } else { |
| 115 | + // The instruction is not a LDC with a String constant (or an empty String), which is not allowed. |
| 116 | + throw new RuntimeException( |
| 117 | + "TransportVersion.fromName must be called with a non-empty String literal. " + "See " + location + "." |
| 118 | + ); |
| 119 | + } |
| 120 | + } |
| 121 | + super.visitMethodInsn(opcode, owner, name, descriptor, isInterface); |
| 122 | + } |
| 123 | + }; |
| 124 | + } |
| 125 | + }; |
| 126 | + ClassReader classReader = new ClassReader(classBytes); |
| 127 | + classReader.accept(classVisitor, 0); |
| 128 | + } |
| 129 | + |
| 130 | + private static String classname(String filename) { |
| 131 | + return filename.substring(0, filename.length() - CLASS_EXTENSION.length()).replaceAll("[/\\\\]", "."); |
| 132 | + } |
| 133 | +} |
0 commit comments