From 8e0a4debad927905f963bdf8990bf903df561378 Mon Sep 17 00:00:00 2001 From: Agaricus Date: Mon, 21 Jan 2013 19:18:54 -0800 Subject: [PATCH] Move srg writing and symbol comparison into JarMapping --- .../net/md_5/specialsource/JarMapping.java | 95 +++++++++++++++++++ .../net/md_5/specialsource/JarRemapper.java | 71 ++------------ .../net/md_5/specialsource/SpecialSource.java | 11 ++- 3 files changed, 111 insertions(+), 66 deletions(-) create mode 100644 src/main/java/net/md_5/specialsource/JarMapping.java diff --git a/src/main/java/net/md_5/specialsource/JarMapping.java b/src/main/java/net/md_5/specialsource/JarMapping.java new file mode 100644 index 0000000..bb1d8d4 --- /dev/null +++ b/src/main/java/net/md_5/specialsource/JarMapping.java @@ -0,0 +1,95 @@ +/** + * Copyright (c) 2012, md_5. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * The name of the author may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package net.md_5.specialsource; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.text.MessageFormat; +import java.util.*; + +public class JarMapping { + public final Map classes = new HashMap(); + public final Map fields = new HashMap(); + public final Map methods = new HashMap(); + + private static final String HEADER = "" + + "# THESE ARE AUTOMATICALLY GENERATED MAPPINGS BETWEEN {0} and {1}\n" + + "# THEY WERE GENERATED ON {2} USING Special Source (c) md_5 2012.\n" + + "# PLEASE DO NOT REMOVE THIS HEADER!\n"; + + public JarMapping(JarComparer oldJar, JarComparer newJar, File logfile) throws IOException { + SpecialSource.validate(oldJar, newJar); + + List searge = new ArrayList(); + + for (int i = 0; i < oldJar.classes.size(); i++) { + String oldClass = oldJar.classes.get(i); + String newClass = newJar.classes.get(i); + classes.put(oldClass, newClass); + if (!Objects.equals(oldClass, newClass)) { + searge.add("CL: " + oldClass + " " + newClass); + } + } + for (int i = 0; i < oldJar.fields.size(); i++) { + Ownable oldField = oldJar.fields.get(i); + Ownable newField = newJar.fields.get(i); + fields.put(oldField.owner + "/" + oldField.name, newField.name); + if (!Objects.equals(oldField.name, newField.name)) { + searge.add("FD: " + oldField.owner + "/" + oldField.name + " " + newField.owner + "/" + newField.name); + } + } + for (int i = 0; i < oldJar.methods.size(); i++) { + Ownable oldMethod = oldJar.methods.get(i); + Ownable newMethod = newJar.methods.get(i); + methods.put(oldMethod.owner + "/" + oldMethod.name + " " + oldMethod.descriptor, newMethod.name); + + String oldDescriptor = oldMethod.descriptor; + for (Map.Entry entry : classes.entrySet()) { + oldDescriptor = oldDescriptor.replaceAll("L" + entry.getKey() + ";", "L" + entry.getValue() + ";"); + } + + if (!Objects.equals(oldMethod.name + " " + oldDescriptor, newMethod.name + " " + newMethod.descriptor)) { + searge.add("MD: " + oldMethod.owner + "/" + oldMethod.name + " " + oldMethod.descriptor + " " + newMethod.owner + "/" + newMethod.name + " " + newMethod.descriptor); + } + } + + Collections.sort(searge); + // No try with resources for us! + PrintWriter out = new PrintWriter(logfile); + try { + out.println(MessageFormat.format(HEADER, oldJar.jar.file.getName(), newJar.jar.file.getName(), new Date())); + for (String s : searge) { + out.println(s); + } + } finally { + out.close(); + } + } +} diff --git a/src/main/java/net/md_5/specialsource/JarRemapper.java b/src/main/java/net/md_5/specialsource/JarRemapper.java index ab7e4d3..1af740f 100644 --- a/src/main/java/net/md_5/specialsource/JarRemapper.java +++ b/src/main/java/net/md_5/specialsource/JarRemapper.java @@ -54,80 +54,25 @@ import org.objectweb.asm.tree.ClassNode; public class JarRemapper extends Remapper { private static final int CLASS_LEN = ".class".length(); - private static final String HEADER = "" - + "# THESE ARE AUTOMATICALLY GENERATED MAPPINGS BETWEEN {0} and {1}\n" - + "# THEY WERE GENERATED ON {2} USING Special Source (c) md_5 2012.\n" - + "# PLEASE DO NOT REMOVE THIS HEADER!\n"; - private final JarComparer oldJar; - private final JarComparer newJar; private final Jar self; - private final Map classes = new HashMap(); - private final Map fields = new HashMap(); - private final Map methods = new HashMap(); + private final JarMapping jarMapping; - private JarRemapper(JarComparer oldJar, JarComparer newJar, Jar self, File logfile) throws IOException { - SpecialSource.validate(oldJar, newJar); - this.oldJar = oldJar; - this.newJar = newJar; + private JarRemapper(JarMapping jarMapping, Jar self) throws IOException { + this.jarMapping = jarMapping; this.self = self; - - List searge = new ArrayList(); - - for (int i = 0; i < oldJar.classes.size(); i++) { - String oldClass = oldJar.classes.get(i); - String newClass = newJar.classes.get(i); - classes.put(oldClass, newClass); - if (!Objects.equals(oldClass, newClass)) { - searge.add("CL: " + oldClass + " " + newClass); - } - } - for (int i = 0; i < oldJar.fields.size(); i++) { - Ownable oldField = oldJar.fields.get(i); - Ownable newField = newJar.fields.get(i); - fields.put(oldField.owner + "/" + oldField.name, newField.name); - if (!Objects.equals(oldField.name, newField.name)) { - searge.add("FD: " + oldField.owner + "/" + oldField.name + " " + newField.owner + "/" + newField.name); - } - } - for (int i = 0; i < oldJar.methods.size(); i++) { - Ownable oldMethod = oldJar.methods.get(i); - Ownable newMethod = newJar.methods.get(i); - methods.put(oldMethod.owner + "/" + oldMethod.name + " " + oldMethod.descriptor, newMethod.name); - - String oldDescriptor = oldMethod.descriptor; - for (Map.Entry entry : classes.entrySet()) { - oldDescriptor = oldDescriptor.replaceAll("L" + entry.getKey() + ";", "L" + entry.getValue() + ";"); - } - - if (!Objects.equals(oldMethod.name + " " + oldDescriptor, newMethod.name + " " + newMethod.descriptor)) { - searge.add("MD: " + oldMethod.owner + "/" + oldMethod.name + " " + oldMethod.descriptor + " " + newMethod.owner + "/" + newMethod.name + " " + newMethod.descriptor); - } - } - - Collections.sort(searge); - // No try with resources for us! - PrintWriter out = new PrintWriter(logfile); - try { - out.println(MessageFormat.format(HEADER, oldJar.jar.file.getName(), newJar.jar.file.getName(), new Date())); - for (String s : searge) { - out.println(s); - } - } finally { - out.close(); - } } @Override public String map(String typeName) { int index = typeName.indexOf('$'); String key = (index == -1) ? typeName : typeName.substring(0, index); - String mapped = classes.get(key); + String mapped = jarMapping.classes.get(key); return mapped != null ? mapped + (index == -1 ? "" : typeName.substring(index, typeName.length())) : typeName; } @Override public String mapFieldName(String owner, String name, String desc) { - String mapped = tryClimb(fields, NodeType.FIELD, owner, name); + String mapped = tryClimb(jarMapping.fields, NodeType.FIELD, owner, name); return mapped == null ? name : mapped; } @@ -153,14 +98,14 @@ public class JarRemapper extends Remapper { @Override public String mapMethodName(String owner, String name, String desc) { - String mapped = tryClimb(methods, NodeType.METHOD, owner, name + " " + desc); + String mapped = tryClimb(jarMapping.methods, NodeType.METHOD, owner, name + " " + desc); return mapped == null ? name : mapped; } - public static void renameJar(Jar jar, File target, JarComparer oldNames, JarComparer newNames) throws IOException { + public static void renameJar(Jar jar, File target, JarMapping jarMapping) throws IOException { JarOutputStream out = new JarOutputStream(new FileOutputStream(target)); try { - JarRemapper self = new JarRemapper(oldNames, newNames, jar, new File(target.getPath() + ".srg")); + JarRemapper self = new JarRemapper(jarMapping, jar); if (jar == null) { return; } diff --git a/src/main/java/net/md_5/specialsource/SpecialSource.java b/src/main/java/net/md_5/specialsource/SpecialSource.java index 915bfde..6a8b82c 100644 --- a/src/main/java/net/md_5/specialsource/SpecialSource.java +++ b/src/main/java/net/md_5/specialsource/SpecialSource.java @@ -51,10 +51,15 @@ public class SpecialSource { JarComparer visitor2 = new JarComparer(jar2); visit(new Pair(jar1, jar2), new Pair(visitor1, visitor2), new Pair(jar1.main, jar2.main)); - Jar jar3 = (args.length == 3) ? Jar.init(args[2]) : null; + System.out.println("Writing mapping file"); + File srgOutput = new File("out.srg"); + JarMapping jarMapping = new JarMapping(visitor1, visitor2, srgOutput); - System.out.println("Renaming final jar"); - JarRemapper.renameJar(jar3, new File("out.jar"), visitor1, visitor2); + if (args.length == 3) { + System.out.println("Renaming final jar"); + Jar jar3 = Jar.init(args[2]); + JarRemapper.renameJar(jar3, new File("out.jar"), jarMapping); + } } private static void visit(Pair jars, Pair visitors, Pair classes) throws IOException {