Skip to content

Commit

Permalink
Merge pull request #598 from pxb1988/apply-misc-fixes
Browse files Browse the repository at this point in the history
Apply miscellaneous fixes
  • Loading branch information
pxb1988 authored Sep 1, 2023
2 parents ee63470 + a3f93f5 commit d90f4ef
Show file tree
Hide file tree
Showing 26 changed files with 283 additions and 330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
Expand Down Expand Up @@ -103,7 +102,6 @@ public static FileSystem openZip(Path in) throws IOException {
throw new IOException("cant find zipfs support");
}

@SuppressWarnings("serial")
protected static class HelpException extends RuntimeException {

private static final long serialVersionUID = 5538069795297477488L;
Expand All @@ -120,7 +118,7 @@ public HelpException(String message) {

@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.FIELD })
static public @interface Opt {
public @interface Opt {
String argName() default "";

String description() default "";
Expand Down Expand Up @@ -190,7 +188,7 @@ public String getOptAndLongOpt() {

@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE })
static public @interface Syntax {
public @interface Syntax {

String cmd();

Expand All @@ -212,8 +210,8 @@ public String getOptAndLongOpt() {
@Opt(opt = "h", longOpt = "help", hasArg = false, description = "Print this help message")
private boolean printHelp = false;

protected String remainingArgs[];
protected String orginalArgs[];
protected String[] remainingArgs;
protected String[] originalArgs;

public BaseCmd() {
}
Expand All @@ -235,8 +233,8 @@ public BaseCmd(String cmdName, String cmdSyntax, String header) {
this.desc = header;
}

private Set<Option> collectRequriedOptions(Map<String, Option> optMap) {
Set<Option> options = new HashSet<Option>();
private Set<Option> collectRequiredOptions(Map<String, Option> optMap) {
Set<Option> options = new HashSet<>();
for (Map.Entry<String, Option> e : optMap.entrySet()) {
Option option = e.getValue();
if (option.required) {
Expand Down Expand Up @@ -275,14 +273,12 @@ protected Object convert(String value, Class type) {
try {
type.asSubclass(Enum.class);
return Enum.valueOf(type, value);
} catch (Exception e) {
} catch (Exception ignored) {
}

throw new RuntimeException("can't convert [" + value + "] to type " + type);
}

;

protected abstract void doCommandLine() throws Exception;

public void doMain(String... args) {
Expand Down Expand Up @@ -418,7 +414,7 @@ public static void main(String... args) throws Exception {
return;
}
Class<?> clz = Class.forName(args[0]);
String newArgs[] = new String[args.length - 1];
String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
if (BaseCmd.class.isAssignableFrom(clz)) {
BaseCmd baseCmd = (BaseCmd) clz.newInstance();
Expand All @@ -431,15 +427,15 @@ public static void main(String... args) throws Exception {
}

protected void parseSetArgs(String... args) throws IllegalArgumentException, IllegalAccessException {
this.orginalArgs = args;
List<String> remainsOptions = new ArrayList<String>();
Set<Option> requiredOpts = collectRequriedOptions(optMap);
this.originalArgs = args;
List<String> remainsOptions = new ArrayList<>();
Set<Option> requiredOpts = collectRequiredOptions(optMap);
Option needArgOpt = null;
for (String s : args) {
if (needArgOpt != null) {
needArgOpt.field.set(this, convert(s, needArgOpt.field.getType()));
needArgOpt = null;
} else if (s.startsWith("-")) {// its a short or long option
} else if (s.startsWith("-")) {// it's a short or long option
Option opt = optMap.get(s);
requiredOpts.remove(opt);
if (opt == null) {
Expand All @@ -461,7 +457,7 @@ protected void parseSetArgs(String... args) throws IllegalArgumentException, Ill
System.err.println("ERROR: Option " + needArgOpt.getOptAndLongOpt() + " need an argument value");
throw new HelpException();
}
this.remainingArgs = remainsOptions.toArray(new String[remainsOptions.size()]);
this.remainingArgs = remainsOptions.toArray(new String[0]);
if (this.printHelp) {
throw new HelpException();
}
Expand All @@ -478,7 +474,7 @@ protected void parseSetArgs(String... args) throws IllegalArgumentException, Ill
sb.append(option.getOptAndLongOpt());
}
sb.append(" is required");
System.err.println(sb.toString());
System.err.println(sb);
throw new HelpException();
}

Expand All @@ -498,7 +494,7 @@ protected void usage() {
// .-a,--aa.<arg>...desc1
// .................desc2
// .-b,--bb
TreeSet<Option> options = new TreeSet<Option>(this.optMap.values());
TreeSet<Option> options = new TreeSet<>(this.optMap.values());
int palength = -1;
for (Option option : options) {
int pa = 4 + option.getOptAndLongOpt().length();
Expand Down Expand Up @@ -542,7 +538,7 @@ protected void usage() {
nextStart = desc.length();
sb.setLength(0);
} else {
sb.append(desc.substring(nextStart, nextStart + pblength));
sb.append(desc, nextStart, nextStart + pblength);
out.println(sb);
nextStart += pblength;
sb.setLength(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.objectweb.asm.tree.ClassNode;

import java.io.*;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.FileSystem;
import java.nio.file.*;
Expand Down Expand Up @@ -78,7 +77,7 @@ protected void doCommandLine() throws Exception {
}
}

private void disassemble0(Path in, final Path output) throws IOException, URISyntaxException {
private void disassemble0(Path in, final Path output) throws IOException {
if (Files.isDirectory(in)) { // a dir
travelFileTree(in, output);
} else if (in.toString().endsWith(".class")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@
import org.objectweb.asm.tree.ClassNode;

import java.io.*;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

@BaseCmd.Syntax(cmd = "d2j-jasmin2jar", syntax = "[options] <jar>", desc = "Assemble .j files to .class file", onlineHelp = "https://sourceforge.net/p/dex2jar/wiki/Jasmin")
public class Jasmin2JarCmd extends BaseCmd implements Opcodes {
private static int versions[] = { 0, V1_1, V1_2, V1_3, V1_4, V1_5, V1_6, V1_7, 52 // V1_8 ?
, 53 // V1_9 ?
};
private static final int[] versions = { 0, V1_1, V1_2, V1_3, V1_4, V1_5, V1_6, V1_7, V1_8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18 };
@Opt(opt = "g", longOpt = "autogenerate-linenumbers", hasArg = false, description = "autogenerate-linenumbers")
boolean autogenLines = false;
@Opt(opt = "f", longOpt = "force", hasArg = false, description = "force overwrite")
Expand All @@ -52,8 +49,11 @@ public class Jasmin2JarCmd extends BaseCmd implements Opcodes {
@Opt( longOpt = "no-compute-max", description = "", hasArg = false)
private boolean noComputeMax;

@Opt(opt = "cv", longOpt = "class-version", description = "default .class version, [1~9], default 6 for JAVA6")
private int classVersion = 6;
@Opt(opt = "cv", longOpt = "class-version", description = "default .class version, [1~9], default 8 for JAVA8")
private int classVersion = 8;

public Jasmin2JarCmd() {
}

public static void main(String... args) throws ClassNotFoundException, SecurityException {
new Jasmin2JarCmd().doMain(args);
Expand All @@ -65,8 +65,8 @@ protected void doCommandLine() throws Exception {
usage();
return;
}
if (classVersion < 1 || classVersion > 9) {
throw new HelpException("-cv,--class-version out of range, 1-9 is supported.");
if (classVersion < 1 || classVersion > 18) {
throw new HelpException("-cv,--class-version out of range, 1-18 is supported.");
}

Path jar = new File(remainingArgs[0]).toPath().toAbsolutePath();
Expand Down Expand Up @@ -97,7 +97,7 @@ protected void doCommandLine() throws Exception {
}
}

private void assemble0(Path in, Path output) throws IOException, URISyntaxException {
private void assemble0(Path in, Path output) throws IOException {
if (Files.isDirectory(in)) { // a dir
travelFileTree(in, output);
} else if (in.toString().endsWith(".j")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
* @author Eric Bruneton
*/
public class JasminDumper implements Opcodes {
private static Set<String> ACCESS_KWS = new HashSet<String>(Arrays
private static final Set<String> ACCESS_KWS = new HashSet<>(Arrays
.asList("abstract", "private", "protected", "public", "enum", "final", "interface", "static", "strictfp", "native", "super"));

public JasminDumper(PrintWriter pw) {
Expand Down Expand Up @@ -187,10 +187,7 @@ public void dump(ClassNode cn) {
}

for (FieldNode fn : cn.fields) {
boolean annotations = false;
if (fn.visibleAnnotations != null && fn.visibleAnnotations.size() > 0) {
annotations = true;
}
boolean annotations = fn.visibleAnnotations != null && fn.visibleAnnotations.size() > 0;
if (fn.invisibleAnnotations != null && fn.invisibleAnnotations.size() > 0) {
annotations = true;
}
Expand Down Expand Up @@ -388,13 +385,18 @@ public void visitMethodInsn(int opcode, String owner, String name, String desc,
pw.print('/');
pw.print(name);
pw.print(desc);
if (opcode == Opcodes.INVOKEINTERFACE) {
if (isInterface) {
pw.print(' ');
pw.print((Type.getArgumentsAndReturnSizes(desc) >> 2) - 1);
}
pw.println();
}

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
visitMethodInsn(opcode, owner, name, desc, opcode == INVOKEINTERFACE);
}

@Override
public void visitJumpInsn(int opcode, Label label) {
print(opcode);
Expand Down Expand Up @@ -766,15 +768,15 @@ protected void printAnnotationValue(final Object value) {
pw.print("[F = ");
float[] v = (float[]) value;
for (float element : v) {
print(new Float(element));
print(element);
pw.print(' ');
}
pw.println();
} else if (value instanceof double[]) {
pw.print("[D = ");
double[] v = (double[]) value;
for (double element : v) {
print(new Double(element));
print(element);
pw.print(' ');
}
pw.println();
Expand Down Expand Up @@ -829,10 +831,10 @@ protected void printAnnotationValue(final Object value) {
pw.println(((Byte) value).intValue());
} else if (value instanceof Boolean) {
pw.print("Z = ");
pw.println(((Boolean) value).booleanValue() ? 1 : 0);
pw.println((Boolean) value ? 1 : 0);
} else if (value instanceof Character) {
pw.print("C = ");
pw.println(new Integer(((Character) value).charValue()));
pw.println(new Integer((Character) value));
} else if (value instanceof Short) {
pw.print("S = ");
pw.println(((Short) value).intValue());
Expand Down Expand Up @@ -870,9 +872,9 @@ protected void printAnnotationArrayValue(final Object value) {
} else if (value instanceof Byte) {
pw.print(((Byte) value).intValue());
} else if (value instanceof Boolean) {
pw.print(((Boolean) value).booleanValue() ? 1 : 0);
pw.print((Boolean) value ? 1 : 0);
} else if (value instanceof Character) {
pw.print(new Integer(((Character) value).charValue()));
pw.print(new Integer((Character) value));
} else if (value instanceof Short) {
pw.print(((Short) value).intValue());
} else if (value instanceof Type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public static ClassNode parse(String fileName, Reader bufferedReader) throws IOE
}

public static ClassNode parse(String fileName, InputStream is) throws IOException, RecognitionException {
return parse(fileName, new InputStreamReader(is, "UTF-8"));
return parse(fileName, new InputStreamReader(is, StandardCharsets.UTF_8));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static void acceptMethod(SmaliParser.SMethodContext ctx, String className
private static class M {
int locals;
String[] paramNames;
int map[];
int[] map;
public int total;

void setNameByIdx(int index, String name) {
Expand Down Expand Up @@ -116,7 +116,7 @@ int pareReg(String str) {
M(Method method, int totals, int ins, boolean isStatic) {
this.locals = totals - ins;
this.total = totals;
String paramTypes[] = method.getParameterTypes();
String[] paramTypes = method.getParameterTypes();
paramNames = new String[paramTypes.length];
map = new int[ins];
int start = 0;
Expand Down Expand Up @@ -242,8 +242,8 @@ public Object visitSLabel(SmaliParser.SLabelContext ctx) {
public Object visitFspareswitch(SmaliParser.FspareswitchContext ctx) {
List<TerminalNode> ints = ctx.INT();
List<TerminalNode> ts = ctx.LABEL();
int cases[] = new int[ts.size()];
DexLabel labels[] = new DexLabel[ts.size()];
int[] cases = new int[ts.size()];
DexLabel[] labels = new DexLabel[ts.size()];
for (int i = 0; i < ts.size(); i++) {
cases[i] = parseInt(ints.get(i).getSymbol().getText());
labels[i] = getLabel(ts.get(i).getSymbol().getText());
Expand Down Expand Up @@ -410,7 +410,7 @@ public Object visitFt5c(SmaliParser.Ft5cContext ctx) {
Op op = getOp(ctx.op);

List<TerminalNode> ts = ctx.REGISTER();
int rs[] = new int[ts.size()];
int[] rs = new int[ts.size()];
for (int i = 0; i < ts.size(); i++) {
rs[i] = m.pareReg(ts.get(i).getSymbol().getText());
}
Expand All @@ -423,7 +423,7 @@ public Object visitFm5c(SmaliParser.Fm5cContext ctx) {
Op op = getOp(ctx.op);

List<TerminalNode> ts = ctx.REGISTER();
int rs[] = new int[ts.size()];
int[] rs = new int[ts.size()];
for (int i = 0; i < ts.size(); i++) {
rs[i] = m.pareReg(ts.get(i).getSymbol().getText());
}
Expand Down Expand Up @@ -496,7 +496,7 @@ public Object visitFmrc(SmaliParser.FmrcContext ctx) {
int start = m.pareReg(ctx.rstart.getText());
int end = m.pareReg(ctx.rend.getText());
int size = end - start + 1;
int rs[] = new int[size];
int[] rs = new int[size];
for (int i = 0; i < size; i++) {
rs[i] = start + i;
}
Expand All @@ -513,7 +513,7 @@ public Object visitFtrc(SmaliParser.FtrcContext ctx) {
int start = m.pareReg(ctx.rstart.getText());
int end = m.pareReg(ctx.rend.getText());
int size = end - start + 1;
int rs[] = new int[size];
int[] rs = new int[size];
for (int i = 0; i < size; i++) {
rs[i] = start + i;
}
Expand Down Expand Up @@ -552,7 +552,7 @@ public Object visitF2sb(SmaliParser.F2sbContext ctx) {
public Object visitFpackageswitch(SmaliParser.FpackageswitchContext ctx) {
int start = parseInt(ctx.start.getText());
List<TerminalNode> ts = ctx.LABEL();
DexLabel labels[] = new DexLabel[ts.size()];
DexLabel[] labels = new DexLabel[ts.size()];
for (int i = 0; i < ts.size(); i++) {
labels[i] = getLabel(ts.get(i).getSymbol().getText());
}
Expand Down
Loading

0 comments on commit d90f4ef

Please sign in to comment.