From 5814ea195775fa24d6efc52491a61994bd0facab Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Wed, 25 Nov 2015 15:07:26 +0000 Subject: [PATCH] Store target info between compiles to support cross-project supermixins --- .../tools/obfuscation/AnnotatedMixin.java | 13 + .../tools/obfuscation/AnnotatedMixins.java | 29 +- .../tools/obfuscation/TargetMap.java | 249 ++++++++++++++++++ .../tools/obfuscation/TypeHandle.java | 12 + .../tools/obfuscation/TypeReference.java | 114 ++++++++ 5 files changed, 411 insertions(+), 6 deletions(-) create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/TypeReference.java diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 7e154ffb..5425fde1 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -225,6 +225,11 @@ class AnnotatedMixin { */ private final TypeElement mixin; + /** + * Mixin class + */ + private final TypeHandle handle; + /** * Specified targets */ @@ -265,6 +270,7 @@ class AnnotatedMixin { this.annotation = MirrorUtils.getAnnotation(type, Mixin.class); this.mixins = mixins; this.mixin = type; + this.handle = new TypeHandle(type); this.classRef = type.getQualifiedName().toString().replace('.', '/'); TypeHandle primaryTarget = this.initTargets(); @@ -358,6 +364,13 @@ class AnnotatedMixin { return this.mixin; } + /** + * Get the type handle for the mixin class + */ + public TypeHandle getHandle() { + return this.handle; + } + /** * Get the mixin's targets */ diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index 7b880e03..e8ded263 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -80,6 +80,8 @@ import net.minecraftforge.srg2source.rangeapplier.SrgContainer; */ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { + private static final String MAPID_SYSTEM_PROPERTY = "mixin.target.mapid"; + static enum CompilerEnvironment { /** * Default environment @@ -144,6 +146,11 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { */ private final Map tokenCache = new HashMap(); + /** + * Serialisable mixin target map + */ + private final TargetMap targets; + /** * SRG container for mcp->srg mappings */ @@ -167,6 +174,7 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { private AnnotatedMixins(ProcessingEnvironment processingEnv) { this.env = this.detectEnvironment(processingEnv); this.processingEnv = processingEnv; + this.targets = this.initTargetMap(); this.printMessage(Kind.NOTE, "SpongePowered Mixin Annotation Processor v" + MixinBootstrap.VERSION); @@ -182,6 +190,12 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { this.initTokenCache(this.getOption("tokens")); } + protected TargetMap initTargetMap() { + TargetMap targets = TargetMap.create(System.getProperty(AnnotatedMixins.MAPID_SYSTEM_PROPERTY)); + System.setProperty(AnnotatedMixins.MAPID_SYSTEM_PROPERTY, targets.getSessionId()); + return targets; + } + private void initTokenCache(String tokens) { if (tokens != null) { Pattern tokenPattern = Pattern.compile("^([A-Z0-9\\-_\\.]+)=([0-9]+)$"); @@ -384,6 +398,7 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { if (!this.mixins.containsKey(name)) { AnnotatedMixin mixin = new AnnotatedMixin(this, mixinType); + this.targets.registerTargets(mixin); mixin.runValidators(ValidationPass.EARLY, this.validators); this.mixins.put(name, mixin); this.mixinsForPass.add(mixin); @@ -411,12 +426,10 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { public Collection getMixinsTargeting(TypeElement targetType) { List minions = new ArrayList(); - for (AnnotatedMixin mixin : this.mixins.values()) { - for (TypeHandle mixinTarget : mixin.getTargets()) { - if (targetType.equals(mixinTarget.getElement())) { - minions.add(mixin.getMixin().asType()); - break; - } + for (TypeReference mixin : this.targets.getMixinsTargeting(targetType)) { + TypeHandle handle = mixin.getHandle(this.processingEnv); + if (handle != null) { + minions.add(handle.getType()); } } @@ -530,6 +543,10 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { * Called from each AP when a pass is completed */ public void onPassCompleted() { + if (!"true".equalsIgnoreCase(this.getOption("disableTargetExport"))) { + this.targets.write(true); + } + for (AnnotatedMixin mixin : this.mixinsForPass) { mixin.runValidators(ValidationPass.LATE, this.validators); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java b/src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java new file mode 100644 index 00000000..188d006f --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java @@ -0,0 +1,249 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.tools.obfuscation; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.lang.model.element.TypeElement; + +/** + * Serialisable map of classes to their associated mixins, used so that we can + * pass target information for supermixins from one compiler session to another + */ +public class TargetMap extends HashMap> { + + private static final long serialVersionUID = 1L; + + /** + * Session ID, used to identify the temp file + */ + private final String sessionId; + + /** + * Create a new TargetMap with a session ID based on the current system time + */ + private TargetMap() { + this(String.valueOf(System.currentTimeMillis())); + } + + /** + * Create a TargetMap with the specified session ID + * + * @param sessionId + */ + private TargetMap(String sessionId) { + this.sessionId = sessionId; + } + + /** + * Get the session ID + */ + public String getSessionId() { + return this.sessionId; + } + + /** + * Register target classes for the specified mixin + * + * @param mixin mixin to add targets for + */ + public void registerTargets(AnnotatedMixin mixin) { + this.registerTargets(mixin.getTargets(), mixin.getHandle()); + } + + /** + * Register target classes for the supplied mixin + * + * @param targets List of targets + * @param mixin Mixin class + */ + public void registerTargets(List targets, TypeHandle mixin) { + for (TypeHandle target : targets) { + this.addMixin(target, mixin); + } + } + + /** + * Register the specified mixin against the specified target + * + * @param target Target class + * @param mixin Mixin class + */ + public void addMixin(TypeHandle target, TypeHandle mixin) { + this.addMixin(target.getReference(), mixin.getReference()); + } + + /** + * Register the specified mixin against the specified target + * + * @param target Target class + * @param mixin Mixin class + */ + public void addMixin(TypeReference target, TypeReference mixin) { + Set mixins = this.getMixinsFor(target); + mixins.add(mixin); + } + + /** + * Get mixin classes which target the specified class + * + * @param target Target class + * @return Collection of mixins registered as targetting the specified class + */ + public Collection getMixinsTargeting(TypeElement target) { + return this.getMixinsTargeting(new TypeHandle(target)); + } + + /** + * Get mixin classes which target the specified class + * + * @param target Target class + * @return Collection of mixins registered as targetting the specified class + */ + public Collection getMixinsTargeting(TypeHandle target) { + return this.getMixinsTargeting(target.getReference()); + } + + /** + * Get mixin classes which target the specified class + * + * @param target Target class + * @return Collection of mixins registered as targetting the specified class + */ + public Collection getMixinsTargeting(TypeReference target) { + return Collections.unmodifiableCollection(this.getMixinsFor(target)); + } + + /** + * Get mixin set for specified class + * + * @param target Target class + * @return Set of mixins registered as targetting the specified class + */ + private Set getMixinsFor(TypeReference target) { + Set mixins = this.get(target); + if (mixins == null) { + mixins = new HashSet(); + this.put(target, mixins); + } + return mixins; + } + + /** + * Write this target map to temporary session file + * + * @param temp Set "delete on exit" for the file + */ + public void write(boolean temp) { + ObjectOutputStream oos = null; + FileOutputStream fout = null; + try { + File sessionFile = TargetMap.getSessionFile(this.sessionId); + if (temp) { + sessionFile.deleteOnExit(); + } + fout = new FileOutputStream(sessionFile, true); + oos = new ObjectOutputStream(fout); + oos.writeObject(this); + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + if (oos != null) { + try { + oos.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + } + + /** + * Attemp to deserialise a TargetMap from the specified file + * + * @param sessionFile File to read + * @return deserialised map or null if deserialisation failed + */ + private static TargetMap read(File sessionFile) { + ObjectInputStream objectinputstream = null; + FileInputStream streamIn = null; + try { + streamIn = new FileInputStream(sessionFile); + objectinputstream = new ObjectInputStream(streamIn); + TargetMap map = (TargetMap)objectinputstream.readObject(); + return map; + } catch (Exception e) { + e.printStackTrace(); + } finally { + if(objectinputstream != null){ + try { + objectinputstream .close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + return null; + } + + /** + * Create a TargetMap for the specified session id. Generate new map if + * the session id is invalid or the file cannot be read. Session ID can be + * null + * + * @param sessionId session to deserialise, can be null to create new + * session + * @return new TargetMap + */ + public static TargetMap create(String sessionId) { + if (sessionId != null) { + File sessionFile = TargetMap.getSessionFile(sessionId); + if (sessionFile.exists()) { + TargetMap map = TargetMap.read(sessionFile); + if (map != null) { + return map; + } + } + } + + return new TargetMap(); + } + + private static File getSessionFile(String sessionId) { + File tempDir = new File(System.getProperty("java.io.tmpdir")); + return new File(tempDir, String.format("mixin-targetdb-%s.tmp", sessionId)); + } +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java index 8950ae9c..90fa5725 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java @@ -63,6 +63,11 @@ public class TypeHandle { * Actual type element, this is null for inaccessible classes */ private final TypeElement element; + + /** + * Reference to this handle, for serialisation + */ + private TypeReference reference; /** * Ctor for imaginary elements, require the enclosing package and the FQ @@ -154,6 +159,13 @@ public class TypeHandle { public boolean isImaginary() { return this.element == null; } + + public TypeReference getReference() { + if (this.reference == null) { + this.reference = new TypeReference(this); + } + return this.reference; + } public String findDescriptor(MemberInfo memberInfo) { String desc = memberInfo.desc; diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TypeReference.java b/src/ap/java/org/spongepowered/tools/obfuscation/TypeReference.java new file mode 100644 index 00000000..c7d792d6 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TypeReference.java @@ -0,0 +1,114 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.tools.obfuscation; + +import java.io.Serializable; + +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.TypeElement; + +/** + * Soft wrapper for a {@link TypeHandle} so that we can serialise it + */ +public class TypeReference implements Serializable, Comparable { + + private static final long serialVersionUID = 1L; + + /** + * Class name, used to recreate the handle on the other side + */ + private final String name; + + /** + * Transient handle, not serialised, regenerated when required + */ + private transient TypeHandle handle; + + /** + * Create a new soft wrapper for the specified type handle + * + * @param handle handle to wrap + */ + public TypeReference(TypeHandle handle) { + this.name = handle.getName(); + this.handle = handle; + } + + /** + * Get the class name (internal format) + */ + public String getName() { + return this.name; + } + + /** + * Get the FQ class name (dotted format) + */ + public String getClassName() { + return this.name.replace('/', '.'); + } + + /** + * Fetch or attempt to generate the type handle + * + * @param processingEnv environment to create handle if it needs to be + * regenerated + * @return type handle + */ + public TypeHandle getHandle(ProcessingEnvironment processingEnv) { + if (this.handle == null) { + TypeElement element = processingEnv.getElementUtils().getTypeElement(getClassName()); + try { + this.handle = new TypeHandle(element); + } catch (Exception ex) { + // Class probably doesn't exist in scope :/ + ex.printStackTrace(); + } + } + + return this.handle; + } + + @Override + public String toString() { + return String.format("TypeReference[%s]", this.name); + } + + @Override + public int compareTo(TypeReference other) { + return other == null ? -1 : this.name.compareTo(other.name); + } + + @Override + public boolean equals(Object other) { + return other instanceof TypeReference && this.compareTo((TypeReference)other) == 0; + } + + @Override + public int hashCode() { + return this.name.hashCode(); + } + +}