Move srg writing and symbol comparison into JarMapping

This commit is contained in:
Agaricus 2013-01-21 19:18:54 -08:00
parent b004ebf919
commit 8e0a4debad
3 changed files with 111 additions and 66 deletions

View File

@ -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<String, String> classes = new HashMap<String, String>();
public final Map<String, String> fields = new HashMap<String, String>();
public final Map<String, String> methods = new HashMap<String, String>();
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<String> searge = new ArrayList<String>();
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<String, String> 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();
}
}
}

View File

@ -54,80 +54,25 @@ import org.objectweb.asm.tree.ClassNode;
public class JarRemapper extends Remapper { public class JarRemapper extends Remapper {
private static final int CLASS_LEN = ".class".length(); 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 Jar self;
private final Map<String, String> classes = new HashMap<String, String>(); private final JarMapping jarMapping;
private final Map<String, String> fields = new HashMap<String, String>();
private final Map<String, String> methods = new HashMap<String, String>();
private JarRemapper(JarComparer oldJar, JarComparer newJar, Jar self, File logfile) throws IOException { private JarRemapper(JarMapping jarMapping, Jar self) throws IOException {
SpecialSource.validate(oldJar, newJar); this.jarMapping = jarMapping;
this.oldJar = oldJar;
this.newJar = newJar;
this.self = self; this.self = self;
List<String> searge = new ArrayList<String>();
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<String, String> 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 @Override
public String map(String typeName) { public String map(String typeName) {
int index = typeName.indexOf('$'); int index = typeName.indexOf('$');
String key = (index == -1) ? typeName : typeName.substring(0, index); 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; return mapped != null ? mapped + (index == -1 ? "" : typeName.substring(index, typeName.length())) : typeName;
} }
@Override @Override
public String mapFieldName(String owner, String name, String desc) { 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; return mapped == null ? name : mapped;
} }
@ -153,14 +98,14 @@ public class JarRemapper extends Remapper {
@Override @Override
public String mapMethodName(String owner, String name, String desc) { 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; 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)); JarOutputStream out = new JarOutputStream(new FileOutputStream(target));
try { try {
JarRemapper self = new JarRemapper(oldNames, newNames, jar, new File(target.getPath() + ".srg")); JarRemapper self = new JarRemapper(jarMapping, jar);
if (jar == null) { if (jar == null) {
return; return;
} }

View File

@ -51,10 +51,15 @@ public class SpecialSource {
JarComparer visitor2 = new JarComparer(jar2); JarComparer visitor2 = new JarComparer(jar2);
visit(new Pair<Jar>(jar1, jar2), new Pair<JarComparer>(visitor1, visitor2), new Pair<String>(jar1.main, jar2.main)); visit(new Pair<Jar>(jar1, jar2), new Pair<JarComparer>(visitor1, visitor2), new Pair<String>(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"); if (args.length == 3) {
JarRemapper.renameJar(jar3, new File("out.jar"), visitor1, visitor2); 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<Jar> jars, Pair<JarComparer> visitors, Pair<String> classes) throws IOException { private static void visit(Pair<Jar> jars, Pair<JarComparer> visitors, Pair<String> classes) throws IOException {