Add transformer modules and create interface checker module

This commit is contained in:
Mumfrey committed 2015-04-08 03:41:49 +01:00
1 parent fdfd93c12f
commit e5deb1cf91
5 files changed
+328 -17

No files matched your search

@@ -0,0 +1,55 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered.org <http://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.asm.mixin.transformer;
import java.util.SortedSet;
import org.objectweb.asm.tree.ClassNode;
/**
* Mixin Transformer Module interface for pre- and post-processors
*/
public interface IMixinTransformerModule {
/**
* Called before the mixins are applied
*
* @param transformedName Target class name
* @param targetClass Target class tree
* @param mixins Mixins to be applied
*/
public abstract void preTransform(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins);
/**
* Called after the mixins are applied
*
* @param transformedName Target class name
* @param targetClass Target class tree
* @param mixins Mixins which were applied
*/
public abstract void postTransform(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins);
}
@@ -43,7 +43,6 @@ import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
@@ -55,7 +54,6 @@ import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LineNumberNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.util.CheckClassAdapter;
import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.MixinApplyError;
import org.spongepowered.asm.mixin.MixinEnvironment;
@@ -168,6 +166,11 @@ public class MixinTransformer extends TreeTransformer {
* Mixin configuration bundle
*/
private final List<MixinConfig> configs = new ArrayList<MixinConfig>();
/**
* Transformer modules
*/
private final List<IMixinTransformerModule> modules = new ArrayList<IMixinTransformerModule>();
/**
* True once initialisation is done. All mixin configs needs to be
@@ -195,16 +198,22 @@ public class MixinTransformer extends TreeTransformer {
* ctor
*/
public MixinTransformer() {
// Go via blackboard to create FORWARD compatibility if Mixins get pulled into FML
Object globalMixinTransformer = MixinEnvironment.getCurrentEnvironment().getActiveTransformer();
MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment();
Object globalMixinTransformer = environment.getActiveTransformer();
if (globalMixinTransformer instanceof IClassTransformer) {
throw new RuntimeException("Terminating MixinTransformer instance " + this);
}
// I am a leaf on the wind
MixinEnvironment.getCurrentEnvironment().setActiveTransformer(this);
environment.setActiveTransformer(this);
List<String> configs = MixinEnvironment.getCurrentEnvironment().getMixinConfigs();
this.initConfigs(environment);
this.initModules(environment);
}
private void initConfigs(MixinEnvironment environment) {
List<String> configs = environment.getMixinConfigs();
if (configs != null) {
for (String configFile : configs) {
@@ -222,6 +231,18 @@ public class MixinTransformer extends TreeTransformer {
Collections.sort(this.configs);
}
private void initModules(MixinEnvironment environment) {
// Run CheckClassAdapter on the mixin bytecode if debug option is enabled
if (environment.getOption(Option.DEBUG_VERIFY)) {
this.modules.add(new MixinTransformerModuleCheckClass());
}
// Run implementation checker if option is enabled
if (environment.getOption(Option.CHECK_IMPLEMENTS)) {
this.modules.add(new MixinTransformerModuleInterfaceChecker());
}
}
/* (non-Javadoc)
* @see net.minecraft.launchwrapper.IClassTransformer
* #transform(java.lang.String, java.lang.String, byte[])
@@ -393,19 +414,15 @@ public class MixinTransformer extends TreeTransformer {
// Tree for target class
ClassNode targetClass = this.readClass(basicClass, true);
this.preTransform(transformedName, targetClass, mixins);
for (MixinInfo mixin : mixins) {
this.logger.log(mixin.getLoggingLevel(), "Mixing {} into {}", mixin.getName(), transformedName);
this.applyMixin(transformedName, targetClass, mixin.createContextFor(targetClass));
}
// Extension point
this.postTransform(transformedName, targetClass, mixins);
// Run CheckClassAdapter on the mixin bytecode if debug option is enabled
if (MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_VERIFY)) {
targetClass.accept(new CheckClassAdapter(new ClassWriter(ClassWriter.COMPUTE_FRAMES)));
}
return this.writeClass(transformedName, targetClass);
}
@@ -424,14 +441,18 @@ public class MixinTransformer extends TreeTransformer {
return bytes;
}
/**
* Process tasks before transform
*
* @param transformedName Target class transformed name
* @param targetClass Target class
* @param mixins Mixin which were just applied
*/
protected void postTransform(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
// Stub for subclasses
private void preTransform(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
for (IMixinTransformerModule module : this.modules) {
module.preTransform(transformedName, targetClass, mixins);
}
}
/**
@@ -458,6 +479,19 @@ public class MixinTransformer extends TreeTransformer {
}
}
/**
* Process tasks after transform
*
* @param transformedName Target class transformed name
* @param targetClass Target class
* @param mixins Mixin which were just applied
*/
private void postTransform(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
for (IMixinTransformerModule module : this.modules) {
module.postTransform(transformedName, targetClass, mixins);
}
}
/**
* Mixin interfaces implemented by the mixin class onto the target class
*
@@ -0,0 +1,59 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered.org <http://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.asm.mixin.transformer;
import java.util.SortedSet;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.util.CheckClassAdapter;
/**
* Mixin transformer module which runs CheckClassAdapter on the post-mixin
* bytecode
*/
public class MixinTransformerModuleCheckClass implements IMixinTransformerModule {
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule
* #preTransform(java.lang.String, org.objectweb.asm.tree.ClassNode,
* java.util.SortedSet)
*/
@Override
public void preTransform(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule
* #postTransform(java.lang.String, org.objectweb.asm.tree.ClassNode,
* java.util.SortedSet)
*/
@Override
public void postTransform(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
targetClass.accept(new CheckClassAdapter(new ClassWriter(ClassWriter.COMPUTE_FRAMES)));
}
}
@@ -0,0 +1,158 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered.org <http://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.asm.mixin.transformer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SortedSet;
import org.apache.commons.io.IOUtils;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Traversal;
import org.spongepowered.asm.util.PrettyPrinter;
import org.spongepowered.asm.util.SignaturePrinter;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
/**
* Checks whether interfaces declared on mixin target classes are actually fully
* implemented and generates reports to the console and to files on disk
*/
public class MixinTransformerModuleInterfaceChecker implements IMixinTransformerModule {
/**
* CSV Report file
*/
private final File csv;
/**
* Text Report file
*/
private final File report;
public MixinTransformerModuleInterfaceChecker() {
this.csv = new File("mixin_implementation_report.csv");
this.report = new File("mixin_implementation_report.txt");
try {
Files.write("Class,Method,Signature,Interface\n", this.csv, Charsets.ISO_8859_1);
} catch (IOException ex) {
// well this sucks
}
try {
String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
Files.write("Mixin Implementation Report generated on " + dateTime + "\n", this.report, Charsets.ISO_8859_1);
} catch (IOException ex) {
// hmm :(
}
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule
* #preTransform(java.lang.String, org.objectweb.asm.tree.ClassNode,
* java.util.SortedSet)
*/
@Override
public void preTransform(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule
* #postTransform(java.lang.String, org.objectweb.asm.tree.ClassNode,
* java.util.SortedSet)
*/
@Override
public void postTransform(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
ClassInfo targetClassInfo = ClassInfo.forName(targetClass.name);
if (targetClassInfo.isAbstract()) {
return;
}
String className = targetClassInfo.getName().replace('/', '.');
int missingMethodCount = 0;
PrettyPrinter printer = new PrettyPrinter();
printer.add("Class: %s", className).hr();
printer.add("%-32s %-47s %s", "Return Type", "Missing Method", "From Interface").hr();
for (Method method : targetClassInfo.getInterfaceMethods()) {
Method found = targetClassInfo.findMethodInHierarchy(method.getName(), method.getDesc(), true, Traversal.ALL);
if (found != null) {
continue;
}
if (missingMethodCount > 0) {
printer.add();
}
SignaturePrinter signaturePrinter = new SignaturePrinter(method.getName(), method.getDesc()).setModifiers("");
String iface = method.getOwner().getName().replace('/', '.');
missingMethodCount++;
printer.add("%-32s%s", signaturePrinter.getReturnType(), signaturePrinter);
printer.add("%-80s %s", "", iface);
this.appendToCSVReport(className, method, iface);
}
if (missingMethodCount > 0) {
printer.hr().add("%82s%s: %d", "", "Total unimplemented", missingMethodCount);
printer.print(System.err);
this.appendToTextReport(printer);
}
}
private void appendToCSVReport(String className, Method method, String iface) {
try {
Files.append(String.format("%s,%s,%s,%s\n", className, method.getName(), method.getDesc(), iface), this.csv, Charsets.ISO_8859_1);
} catch (IOException ex) {
// Not the end of the world
}
}
private void appendToTextReport(PrettyPrinter printer) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(this.report, true);
PrintStream stream = new PrintStream(fos);
stream.print("\n");
printer.print(stream);
} catch (Exception ex) {
// never mind
} finally {
IOUtils.closeQuietly(fos);
}
}
}
@@ -109,6 +109,10 @@ public class SignaturePrinter {
this.argTypes = argTypes;
this.argNames = argNames;
}
public String getReturnType() {
return SignaturePrinter.getTypeName(this.returnType, false);
}
public void setModifiers(MethodNode method) {
String returnType = SignaturePrinter.getTypeName(Type.getReturnType(method.desc), false);
@@ -123,8 +127,9 @@ public class SignaturePrinter {
}
}
public void setModifiers(String modifiers) {
this.modifiers = modifiers;
public SignaturePrinter setModifiers(String modifiers) {
this.modifiers = modifiers.replace("${returnType}", this.getReturnType());
return this;
}
@Override