From 65aa52ad070fe79892d8aec757b3e92f1d09b290 Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Sun, 5 Apr 2026 13:15:33 -0700 Subject: [PATCH 1/3] Simplify --- .claude/settings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.claude/settings.json b/.claude/settings.json index 2626b61a..75f4599c 100644 --- a/.claude/settings.json +++ b/.claude/settings.json @@ -17,7 +17,7 @@ "Bash(ls:*)", "Bash(mkdir:*)", "Bash(mkdir:*)", - "Bash(python3 -m json.tool)" + "Bash(python3 -m json.tool)", "Bash(timeout 300 ./gradlew:*)", "Bash(wc:*)", "Edit(./**)", From 10bd1daeb540a6a2a77587068835540d071d2023 Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Sun, 5 Apr 2026 17:08:28 -0700 Subject: [PATCH 2/3] Grammar --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 9cc2f444..a381b6dd 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { alias(libs.plugins.com.gradleup.shadow) // Code formatting; defines targets "spotlessApply" and "spotlessCheck" - // which are run by "check" (which is itself run by "build"). + // which is run by "check" (which is itself run by "build"). alias(libs.plugins.com.diffplug.spotless) // Error Prone linter From 00101964a18459c0cd620427e6de36e7bf338ff7 Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Sun, 5 Apr 2026 17:17:40 -0700 Subject: [PATCH 3/3] Simplify --- .../plumelib/reflection/ReflectionPlume.java | 36 ++++++++----------- .../plumelib/reflection/SignatureRegexes.java | 2 +- .../org/plumelib/reflection/Signatures.java | 7 ++-- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/plumelib/reflection/ReflectionPlume.java b/src/main/java/org/plumelib/reflection/ReflectionPlume.java index d144051d..420f643e 100644 --- a/src/main/java/org/plumelib/reflection/ReflectionPlume.java +++ b/src/main/java/org/plumelib/reflection/ReflectionPlume.java @@ -6,11 +6,11 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayDeque; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -170,12 +170,13 @@ public static String nameWithoutPackage(Class c) { return c.getSimpleName(); } - StringBuilder result = new StringBuilder(c.getSimpleName()); - while (enclosing != null) { - result.insert(0, enclosing.getSimpleName() + "."); - enclosing = enclosing.getEnclosingClass(); + ArrayDeque parts = new ArrayDeque<>(); + Class current = c; + while (current != null) { + parts.addFirst(current.getSimpleName()); + current = current.getEnclosingClass(); } - return result.toString(); + return String.join(".", parts); } // ////////////////////////////////////////////////////////////////////// @@ -204,26 +205,20 @@ public PromiscuousLoader() {} */ public Class defineClassFromFile(@BinaryName String className, String pathname) throws FileNotFoundException, IOException { - int numbytes; byte[] classBytes; - int bytesRead; - try (InputStream fi = Files.newInputStream(Path.of(pathname))) { - numbytes = fi.available(); - classBytes = new byte[numbytes]; - bytesRead = fi.read(classBytes); - } - if (bytesRead < numbytes) { - throw new Error( - "Expected to read %d bytes from %s, got %d".formatted(numbytes, pathname, bytesRead)); + try { + classBytes = Files.readAllBytes(Path.of(pathname)); + } catch (java.nio.file.NoSuchFileException e) { + throw new FileNotFoundException(pathname); } - Class returnClass = defineClass(className, classBytes, 0, numbytes); + Class returnClass = defineClass(className, classBytes, 0, classBytes.length); resolveClass(returnClass); // link the class return returnClass; } } /** A ClassLoader that can call defineClassFromFile. */ - private static PromiscuousLoader thePromiscuousLoader = new PromiscuousLoader(); + private static final PromiscuousLoader thePromiscuousLoader = new PromiscuousLoader(); /** * Converts the bytes in a file into an instance of class Class, and resolves (links) the class. @@ -256,7 +251,6 @@ public static Class defineClassFromFile(@BinaryName String className, String public static void addToClasspath(String dir) { // If the dir isn't on CLASSPATH, add it. String pathSep = System.getProperty("path.separator"); - // what is the point of the "replace()" call? String cp = System.getProperty("java.class.path", ".").replace('\\', '/'); StringTokenizer tokenizer = new StringTokenizer(cp, pathSep, false); boolean found = false; @@ -287,7 +281,7 @@ public static String classpathToString() { * array of Class objects, one for each arg type. Example keys include: "java.lang.String, * java.lang.String, java.lang.Class[]" and "int,int". */ - static HashMap[]> args_seen = new HashMap<>(); + private static final HashMap[]> args_seen = new HashMap<>(); /** * Given a method signature, return the method. @@ -402,7 +396,6 @@ public static void setFinalField(Object o, String fieldName, @Interned Object va throws NoSuchFieldException { Class c = o.getClass(); while (c != Object.class) { // Class is interned - // System.out.printf ("Setting field %s in %s%n", fieldName, c); try { Field f = c.getDeclaredField(fieldName); @SuppressWarnings("deprecation") // No non-deprecated alternative to query accessible flag. @@ -439,7 +432,6 @@ public static void setFinalField(Object o, String fieldName, @Interned Object va throws NoSuchFieldException { Class c = o.getClass(); while (c != Object.class) { // Class is interned - // System.out.printf ("Setting field %s in %s%n", fieldName, c); try { Field f = c.getDeclaredField(fieldName); @SuppressWarnings("deprecation") // No non-deprecated alternative to query accessible flag. diff --git a/src/main/java/org/plumelib/reflection/SignatureRegexes.java b/src/main/java/org/plumelib/reflection/SignatureRegexes.java index c54112e0..c7c53064 100644 --- a/src/main/java/org/plumelib/reflection/SignatureRegexes.java +++ b/src/main/java/org/plumelib/reflection/SignatureRegexes.java @@ -273,7 +273,7 @@ private SignatureRegexes() { Pattern.compile(FieldDescriptorWithoutPackageRegex); /** An anchored regex that matches FieldDescriptorForPrimitive strings. */ - public static final @Regex String FieldDescriptorForPrimitiveRegex = ANCHORED("^[BCDFIJSZ]$"); + public static final @Regex String FieldDescriptorForPrimitiveRegex = ANCHORED("[BCDFIJSZ]"); /** An anchored pattern that matches FieldDescriptorForPrimitive strings. */ public static final Pattern FieldDescriptorForPrimitivePattern = diff --git a/src/main/java/org/plumelib/reflection/Signatures.java b/src/main/java/org/plumelib/reflection/Signatures.java index d2beccab..4d8e0550 100644 --- a/src/main/java/org/plumelib/reflection/Signatures.java +++ b/src/main/java/org/plumelib/reflection/Signatures.java @@ -54,7 +54,7 @@ private Signatures() { * Returns the element type for the given type name, which results from removing all the array * brackets. * - * @param fqBinaryName "a fully-qualified binary name" ({@code @FqBinaryNome}) + * @param fqBinaryName "a fully-qualified binary name" ({@code @FqBinaryName}) * @return the base element type of the argument, with all array brackets stripped */ @SuppressWarnings("signature") // @FqBinaryName = @ClassGetName plus optional array brackets @@ -484,7 +484,7 @@ public static ClassnameAndDimensions parseFqBinaryName(@FqBinaryName String type ) public static @FullyQualifiedName String binaryNameToFullyQualified( @BinaryName String binaryName) { - return binaryName.replaceAll("\\$", "."); + return binaryName.replace('$', '.'); } /** @@ -502,7 +502,7 @@ public static ClassnameAndDimensions parseFqBinaryName(@FqBinaryName String type } } - /** A map from field descriptor (sach as "I") to Java primitive type (such as "int"). */ + /** A map from field descriptor (such as "I") to Java primitive type (such as "int"). */ private static final Map fieldDescriptorToPrimitive = Map.of( "Z", "boolean", @@ -699,7 +699,6 @@ public static String arglistToJvm(String arglist) { for (@BinaryName String javaArg : splitJavaArglist(arglist)) { result.add(binaryNameToFieldDescriptor(javaArg)); } - // System.out.println("arglistToJvm: " + arglist + " => " + result); return result.toString(); }