summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornickreid <nickreid@google.com>2024-02-29 19:37:37 -0800
committerJavac Team <javac-team+copybara@google.com>2024-02-29 19:38:11 -0800
commit17b3a0484826f6873c5a971faec89bee8574af80 (patch)
tree252b26783bd7d57f3b030ab1c0461ac32ddc854d
parent09d0de80308cb925ee206446827bd0c4266a0e10 (diff)
downloadjarjar-upstream-master.tar.gz
Delete MainUtil and the unnecessary reflection thereinupstream-master
PiperOrigin-RevId: 611677491
-rw-r--r--src/main/com/tonicsystems/jarjar/Main.java92
-rw-r--r--src/main/com/tonicsystems/jarjar/MainUtil.java90
-rw-r--r--src/main/com/tonicsystems/jarjar/util/IoUtil.java10
3 files changed, 59 insertions, 133 deletions
diff --git a/src/main/com/tonicsystems/jarjar/Main.java b/src/main/com/tonicsystems/jarjar/Main.java
index a5f1248..9427be7 100644
--- a/src/main/com/tonicsystems/jarjar/Main.java
+++ b/src/main/com/tonicsystems/jarjar/Main.java
@@ -18,76 +18,84 @@ package com.tonicsystems.jarjar;
import static java.nio.charset.StandardCharsets.UTF_8;
-import com.tonicsystems.jarjar.util.RuntimeIOException;
+import com.tonicsystems.jarjar.util.IoUtil;
import com.tonicsystems.jarjar.util.StandaloneJarProcessor;
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
import java.io.PrintWriter;
+import java.util.Arrays;
import java.util.List;
import java.util.Locale;
+/** Main class for Jarjar CLI. */
public class Main {
- private static final String LINE_SEPARATOR = System.getProperty("line.separator");
- private static final String HELP;
-
- static {
- try {
- HELP = readIntoString(Main.class.getResourceAsStream("help.txt"));
- } catch (IOException e) {
- throw new RuntimeIOException(e);
+ public static void main(String[] argv) throws Exception {
+ List<String> args = Arrays.asList(argv);
+ if (args.isEmpty()) {
+ help();
+ return;
}
- }
- private static String readIntoString(InputStream in) throws IOException {
- StringBuilder sb = new StringBuilder();
- BufferedReader r = new BufferedReader(new InputStreamReader(in, UTF_8));
- String line = null;
- while ((line = r.readLine()) != null) {
- sb.append(line).append(LINE_SEPARATOR);
+ List<String> commandArgs = args.subList(1, args.size());
+ switch (args.get(0)) {
+ case "strings":
+ strings(commandArgs);
+ return;
+ case "find":
+ find(commandArgs);
+ return;
+ case "process":
+ process(commandArgs);
+ return;
+ default:
+ help();
+ return;
}
- return sb.toString();
}
- public static void main(String[] args) throws Exception {
- MainUtil.runMain(new Main(), args, "help");
- }
-
- public void help() {
- System.err.print(HELP);
+ private static void help() throws IOException {
+ try (InputStream helpStream = Main.class.getResourceAsStream("help.txt")) {
+ String helpText =
+ new String(helpStream.readAllBytes(), UTF_8).replace("\n", System.lineSeparator());
+ System.err.print(helpText);
+ }
}
- public void strings(String cp) throws IOException {
- if (cp == null) {
+ private static void strings(List<String> args) throws Exception {
+ if (args.isEmpty()) {
throw new IllegalArgumentException("cp is required");
}
- new StringDumper()
- .run(cp, new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
+ String cp = args.get(0);
+
+ PrintWriter stdout = IoUtil.bufferedPrintWriter(System.out, UTF_8);
+ new StringDumper().run(cp, stdout);
+ stdout.flush();
}
- public void find(String level, String cp1, String cp2) throws IOException {
- if (level == null || cp1 == null) {
+ private static void find(List<String> args) throws IOException {
+ if (args.size() < 3) {
throw new IllegalArgumentException("level and cp1 are required");
}
- if (cp2 == null) {
- cp2 = cp1;
- }
- DepHandler.Level levelFlag = DepHandler.Level.valueOf(level.toUpperCase(Locale.ROOT));
- PrintWriter w = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8)));
- DepHandler handler = new TextDepHandler(w, levelFlag);
+ DepHandler.Level level = DepHandler.Level.valueOf(args.get(0).toUpperCase(Locale.ROOT));
+ String cp1 = args.get(1);
+ String cp2 = (args.size() == 2) ? cp1 : args.get(2);
+
+ PrintWriter stdout = IoUtil.bufferedPrintWriter(System.out, UTF_8);
+ DepHandler handler = new TextDepHandler(stdout, level);
new DepFind().run(cp1, cp2, handler);
- w.flush();
+ stdout.flush();
}
- public void process(File rulesFile, File inJar, File outJar) throws IOException {
- if (rulesFile == null || inJar == null || outJar == null) {
+ private static void process(List<String> args) throws IOException {
+ if (args.size() < 3) {
throw new IllegalArgumentException("rulesFile, inJar, and outJar are required");
}
+ File rulesFile = new File(args.get(0));
+ File inJar = new File(args.get(1));
+ File outJar = new File(args.get(2));
+
List<PatternElement> rules = RulesFileParser.parse(rulesFile);
boolean verbose = Boolean.getBoolean("verbose");
boolean skipManifest = Boolean.getBoolean("skipManifest");
diff --git a/src/main/com/tonicsystems/jarjar/MainUtil.java b/src/main/com/tonicsystems/jarjar/MainUtil.java
deleted file mode 100644
index e1a03c2..0000000
--- a/src/main/com/tonicsystems/jarjar/MainUtil.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright 2007 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.tonicsystems.jarjar;
-
-import static java.lang.Math.max;
-
-import java.io.File;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
-
-final class MainUtil {
- public static void runMain(Object main, String[] args, String defCommand) throws Exception {
- if (args.length > 0) {
- String command = args[0];
- Method[] methods = main.getClass().getMethods();
- for (int i = 0; i < methods.length; i++) {
- Method method = methods[i];
- if (method.getName().equals(command)) {
- String[] remaining = new String[args.length - 1];
- System.arraycopy(args, 1, remaining, 0, remaining.length);
- try {
- method.invoke(main, bindParameters(method, remaining));
- } catch (InvocationTargetException e) {
- Throwable cause = e.getCause();
- if (cause instanceof IllegalArgumentException) {
- System.err.println("Syntax error: " + cause.getMessage());
- } else if (cause instanceof Exception) {
- throw (Exception) cause;
- } else {
- throw e;
- }
- }
- return;
- }
- }
- }
- if (defCommand != null) {
- runMain(main, new String[] {defCommand}, null);
- }
- }
-
- private static Object[] bindParameters(Method method, String[] args) {
- List<Object> parameters = new ArrayList<>();
- Class<?>[] parameterTypes = method.getParameterTypes();
- for (int i = 0, len = parameterTypes.length; i < len; i++) {
- Class<?> type = parameterTypes[i];
- int remaining = max(0, args.length - i);
- if (type.equals(String[].class)) {
- String[] rest = new String[remaining];
- System.arraycopy(args, 1, rest, 0, remaining);
- parameters.add(rest);
- } else if (remaining > 0) {
- parameters.add(convertParameter(args[i], parameterTypes[i]));
- } else {
- parameters.add(null);
- }
- }
- return parameters.toArray();
- }
-
- private static Object convertParameter(String arg, Class<?> type) {
- if (type.equals(String.class)) {
- return arg;
- } else if (type.equals(Integer.class)) {
- return Integer.valueOf(arg, 10);
- } else if (type.equals(File.class)) {
- return new File(arg);
- } else {
- throw new UnsupportedOperationException("Unknown type " + type);
- }
- }
-
- private MainUtil() {}
-}
diff --git a/src/main/com/tonicsystems/jarjar/util/IoUtil.java b/src/main/com/tonicsystems/jarjar/util/IoUtil.java
index d74a087..3906c7b 100644
--- a/src/main/com/tonicsystems/jarjar/util/IoUtil.java
+++ b/src/main/com/tonicsystems/jarjar/util/IoUtil.java
@@ -20,10 +20,14 @@ import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.nio.charset.Charset;
import java.util.zip.ZipOutputStream;
/** Utils for IO. */
-class IoUtil {
+public final class IoUtil {
/**
* Create a ZipOutputStream with buffering.
@@ -35,5 +39,9 @@ class IoUtil {
return new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
}
+ public static PrintWriter bufferedPrintWriter(OutputStream stream, Charset charset) {
+ return new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(stream), charset));
+ }
+
private IoUtil() {}
}