Further AP refactor, support injectors and shadows in multitarget mixins

This commit is contained in:
Mumfrey committed 2016-10-04 21:34:22 +01:00
1 parent 8d87440faf
commit ea44243240
17 files changed
+587 -144

No files matched your search

@@ -98,15 +98,10 @@ class AnnotatedMixin {
*/
private final List<TypeHandle> targets = new ArrayList<TypeHandle>();
/**
* Target "reference" (bytecode name)
*/
private final String targetRef;
/**
* Target type (for single-target mixins)
*/
private final TypeHandle targetType;
private final TypeHandle primaryTarget;
/**
* Mixin class "reference" (bytecode name)
@@ -142,16 +137,7 @@ class AnnotatedMixin {
this.mixin = type;
this.handle = new TypeHandle(type);
this.classRef = type.getQualifiedName().toString().replace('.', '/');
TypeHandle primaryTarget = this.initTargets();
if (primaryTarget != null) {
this.targetRef = primaryTarget.getName();
this.targetType = primaryTarget;
} else {
this.targetRef = null;
this.targetType = null;
}
this.primaryTarget = this.initTargets();
this.remap = AnnotatedMixins.getRemapValue(this.annotation) && this.targets.size() > 0;
this.overwrites = new AnnotatedMixinOverwriteHandler(ap, this);
@@ -279,18 +265,12 @@ class AnnotatedMixin {
return this.mixin.getKind() == ElementKind.INTERFACE;
}
/**
* Get the <em>primary</em> target class reference
*/
public String getPrimaryTargetRef() {
return this.targetRef;
}
/**
* Get the <em>primary</em> target
*/
@Deprecated
public TypeHandle getPrimaryTarget() {
return this.targetType;
return this.primaryTarget;
}
/**
@@ -300,6 +280,13 @@ class AnnotatedMixin {
return this.targets;
}
/**
* Get whether this is a multi-target mixin
*/
public boolean isMultiTarget() {
return this.targets.size() > 1;
}
/**
* Get whether to remap annotations in this mixin
*/
@@ -43,8 +43,10 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
import org.spongepowered.asm.util.throwables.ConstraintViolationException;
import org.spongepowered.asm.util.throwables.InvalidConstraintException;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.ReferenceManager.ReferenceConflictException;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager;
import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer;
/**
* Base class for module for {@link AnnotatedMixin} which handle different
@@ -249,10 +251,9 @@ abstract class AnnotatedMixinElementHandler {
*/
protected final IMixinAnnotationProcessor ap;
/**
* Manager
*/
protected final IObfuscationManager obf;
private IMappingConsumer mappings;
AnnotatedMixinElementHandler(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) {
this.ap = ap;
@@ -261,15 +262,28 @@ abstract class AnnotatedMixinElementHandler {
this.obf = ap.getObfuscationManager();
}
protected final boolean remapReference(String key, String target, Element element, AnnotationMirror inject, AnnotationMirror at) {
if (target == null) {
private IMappingConsumer getMappings() {
if (this.mappings == null) {
IMappingConsumer mappingConsumer = this.mixin.getMappings();
if (mappingConsumer instanceof Mappings) {
this.mappings = ((Mappings)mappingConsumer).asUnique();
} else {
this.mappings = mappingConsumer;
}
}
return this.mappings;
}
protected final boolean remapReference(String key, String reference, Element element, AnnotationMirror inject, AnnotationMirror at) {
if (reference == null) {
return false;
}
MemberInfo targetMember = MemberInfo.parse(target);
String annotation = "@At(" + key + ")";
MemberInfo targetMember = MemberInfo.parse(reference);
if (!targetMember.isFullyQualified()) {
String missing = "missing " + (targetMember.owner == null ? (targetMember.desc == null ? "owner and signature" : "owner") : "signature");
this.ap.printMessage(Kind.ERROR, "@At(" + key + ") is not fully qualified, " + missing, element, inject);
this.ap.printMessage(Kind.ERROR, annotation + " is not fully qualified, " + missing, element, inject);
return false;
}
@@ -279,25 +293,42 @@ abstract class AnnotatedMixinElementHandler {
this.ap.printMessage(Kind.ERROR, ex.getMessage(), element, inject);
}
if (targetMember.isField()) {
ObfuscationData<MappingField> obfFieldData = this.obf.getDataProvider().getObfFieldRecursive(targetMember);
if (obfFieldData.isEmpty()) {
this.ap.printMessage(Kind.WARNING, "Cannot find field mapping for @At(" + key + ") '" + target + "'", element, inject);
return false;
}
this.obf.getReferenceManager().addFieldMapping(this.classRef, target, targetMember, obfFieldData);
} else {
ObfuscationData<MappingMethod> obfMethodData = this.obf.getDataProvider().getObfMethodRecursive(targetMember);
if (obfMethodData.isEmpty()) {
if (targetMember.owner == null || !targetMember.owner.startsWith("java/lang/")) {
this.ap.printMessage(Kind.WARNING, "Cannot find method mapping for @At(" + key + ") '" + target + "'", element, inject);
try {
if (targetMember.isField()) {
ObfuscationData<MappingField> obfFieldData = this.obf.getDataProvider().getObfFieldRecursive(targetMember);
if (obfFieldData.isEmpty()) {
this.ap.printMessage(Kind.WARNING, "Cannot find field mapping for " + annotation + " '" + reference + "'", element, inject);
return false;
}
this.obf.getReferenceManager().addFieldMapping(this.classRef, reference, targetMember, obfFieldData);
} else {
ObfuscationData<MappingMethod> obfMethodData = this.obf.getDataProvider().getObfMethodRecursive(targetMember);
if (obfMethodData.isEmpty()) {
if (targetMember.owner == null || !targetMember.owner.startsWith("java/lang/")) {
this.ap.printMessage(Kind.WARNING, "Cannot find method mapping for " + annotation + " '" + reference + "'", element, inject);
return false;
}
}
this.obf.getReferenceManager().addMethodMapping(this.classRef, reference, targetMember, obfMethodData);
}
this.obf.getReferenceManager().addMethodMapping(this.classRef, target, targetMember, obfMethodData);
} catch (ReferenceConflictException ex) {
// Since references are fully-qualified, it shouldn't be possible for there to be multiple mappings, however
// we catch and log the error in case something weird happens in the mapping provider
this.ap.printMessage(Kind.ERROR, "Unexpected reference conflict for " + annotation + ": " + reference + " -> "
+ ex.getNew() + " previously defined as " + ex.getOld(), element, inject);
return false;
}
return true;
}
protected final void addFieldMappings(String mcpName, String mcpSignature, ObfuscationData<MappingField> obfData) {
for (ObfuscationType type : obfData) {
MappingField obfField = obfData.get(type);
this.addFieldMapping(type, mcpName, obfField.getSimpleName(), mcpSignature, obfField.getDesc());
}
}
/**
* Add a field mapping to the local table
*/
@@ -311,7 +342,14 @@ abstract class AnnotatedMixinElementHandler {
protected final void addFieldMapping(ObfuscationType type, String mcpName, String obfName, String mcpSignature, String obfSignature) {
MappingField from = new MappingField(this.classRef, mcpName, mcpSignature);
MappingField to = new MappingField(this.classRef, obfName, obfSignature);
this.mixin.getMappings().addFieldMapping(type, from, to);
this.getMappings().addFieldMapping(type, from, to);
}
protected final void addMethodMappings(String mcpName, String mcpSignature, ObfuscationData<MappingMethod> obfData) {
for (ObfuscationType type : obfData) {
MappingMethod obfMethod = obfData.get(type);
this.addMethodMapping(type, mcpName, obfMethod.getSimpleName(), mcpSignature, obfMethod.getDesc());
}
}
/**
@@ -327,24 +365,9 @@ abstract class AnnotatedMixinElementHandler {
protected final void addMethodMapping(ObfuscationType type, String mcpName, String obfName, String mcpSignature, String obfSignature) {
MappingMethod from = new MappingMethod(this.classRef, mcpName, mcpSignature);
MappingMethod to = new MappingMethod(this.classRef, obfName, obfSignature);
this.mixin.getMappings().addMethodMapping(type, from, to);
this.getMappings().addMethodMapping(type, from, to);
}
/**
* Validate method for {@link org.spongepowered.asm.mixin.Shadow} and
* {@link org.spongepowered.asm.mixin.Overwrite} registrations to check that
* only a single target is registered. Mixins containing annotated methods
* with these annotations cannot be multi-targetted.
*/
protected final boolean validateSingleTarget(String annotation, Element element) {
if (this.mixin.getPrimaryTargetRef() == null || this.mixin.getTargets().size() > 1) {
this.ap.printMessage(Kind.ERROR, "Mixin with " + annotation + " members must have exactly one target.", element);
this.ap.printMessage(Kind.ERROR, "Mixin with " + annotation + " members must have exactly one target.", this.mixin.getMixin());
return false;
}
return true;
}
/**
* Check constraints for the specified annotation based on token values in
* the current environment
@@ -416,7 +439,7 @@ abstract class AnnotatedMixinElementHandler {
* Checks whether the specified field exists in all targets and raises
* warnings where appropriate
*/
protected final void validateTargetField(VariableElement field, AnnotationMirror shadow, AliasedElementName name, String type) {
protected final void validateTargetField(VariableElement field, AnnotationMirror annotation, AliasedElementName name, String type) {
String fieldType = field.asType().toString();
for (TypeHandle target : this.mixin.getTargets()) {
@@ -439,7 +462,7 @@ abstract class AnnotatedMixinElementHandler {
}
if (targetField == null) {
this.ap.printMessage(Kind.WARNING, "Cannot find target for " + type + " field in " + target, field, shadow);
this.ap.printMessage(Kind.WARNING, "Cannot find target for " + type + " field in " + target, field, annotation);
}
}
}
@@ -38,6 +38,7 @@ import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.ReferenceManager.ReferenceConflictException;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import org.spongepowered.tools.obfuscation.struct.Message;
@@ -62,6 +63,11 @@ class AnnotatedMixinInjectorHandler extends AnnotatedMixinElementHandler {
return this.shouldRemap;
}
@Override
public String toString() {
return "@" + this.getAnnotation().getAnnotationType().asElement().getSimpleName();
}
}
/**
@@ -91,8 +97,8 @@ class AnnotatedMixinInjectorHandler extends AnnotatedMixinElementHandler {
this.ap.printMessage(Kind.ERROR, "Injector in interface is unsupported", elem.getElement());
}
String originalReference = MirrorUtils.<String>getAnnotationValue(elem.getAnnotation(), "method");
MemberInfo targetMember = MemberInfo.parse(originalReference);
String reference = MirrorUtils.<String>getAnnotationValue(elem.getAnnotation(), "method");
MemberInfo targetMember = MemberInfo.parse(reference);
if (targetMember.name == null) {
return null;
}
@@ -103,38 +109,69 @@ class AnnotatedMixinInjectorHandler extends AnnotatedMixinElementHandler {
this.ap.printMessage(Kind.ERROR, ex.getMessage(), elem.getElement(), elem.getAnnotation());
}
String type = "@" + elem.getAnnotation().getAnnotationType().asElement().getSimpleName() + "";
if (targetMember.desc != null) {
this.validateReferencedTarget(elem.getElement(), elem.getAnnotation(), targetMember, type);
this.validateReferencedTarget(elem.getElement(), elem.getAnnotation(), targetMember, elem.toString());
}
if (!elem.shouldRemap() || !this.validateSingleTarget(type, elem.getElement())) {
if (!elem.shouldRemap()) {
return null;
}
String desc = this.mixin.getPrimaryTarget().findDescriptor(targetMember);
for (TypeHandle target : this.mixin.getTargets()) {
Message message = this.registerInjector(elem, reference, targetMember, target);
if (message != null) {
return message;
}
}
return null;
}
private Message registerInjector(AnnotatedElementInjector elem, String reference, MemberInfo targetMember, TypeHandle target) {
String desc = target.findDescriptor(targetMember);
if (desc == null) {
if (this.mixin.getPrimaryTarget().isImaginary()) {
this.ap.printMessage(Kind.WARNING, type + " target requires method signature because enclosing type information is unavailable",
elem.getElement(), elem.getAnnotation());
Kind error = this.mixin.isMultiTarget() ? Kind.ERROR : Kind.WARNING;
if (target.isImaginary()) {
this.ap.printMessage(error, elem + " target requires method signature because enclosing type information for "
+ target + " is unavailable", elem.getElement(), elem.getAnnotation());
} else if (!Constants.CTOR.equals(targetMember.name)) {
this.ap.printMessage(Kind.WARNING, "Unable to determine signature for " + type + " target method",
this.ap.printMessage(error, "Unable to determine signature for " + elem + " target method",
elem.getElement(), elem.getAnnotation());
}
return null;
}
}
MappingMethod targetMethod = new MappingMethod(this.mixin.getPrimaryTargetRef(), targetMember.name, desc);
String targetName = elem + " target " + targetMember.name;
MappingMethod targetMethod = new MappingMethod(target.getName(), targetMember.name, desc);
ObfuscationData<MappingMethod> obfData = this.obf.getDataProvider().getObfMethod(targetMethod);
if (obfData.isEmpty()) {
Kind error = Constants.CTOR.equals(targetMember.name) ? Kind.WARNING : Kind.ERROR;
return new Message(error, "No obfuscation mapping for " + type + " target " + targetMember.name, elem.getElement(), elem.getAnnotation());
return new Message(error, "No obfuscation mapping for " + targetName, elem.getElement(), elem.getAnnotation());
}
try {
// If the original owner is unspecified, and the mixin is multi-target, we strip the owner from the obf mappings
if (targetMember.owner == null && this.mixin.isMultiTarget()) {
obfData = AnnotatedMixinInjectorHandler.stripOwnerData(obfData);
}
this.obf.getReferenceManager().addMethodMapping(this.classRef, reference, obfData);
} catch (ReferenceConflictException ex) {
String conflictType = this.mixin.isMultiTarget() ? "Multi-target" : "Target";
this.ap.printMessage(Kind.ERROR, conflictType + " reference conflict for " + targetName + ": " + reference + " -> "
+ ex.getNew() + " previously defined as " + ex.getOld(), elem.getElement(), elem.getAnnotation());
}
this.obf.getReferenceManager().addMethodMapping(this.classRef, originalReference, obfData);
return null;
}
private static ObfuscationData<MappingMethod> stripOwnerData(ObfuscationData<MappingMethod> data) {
ObfuscationData<MappingMethod> stripped = new ObfuscationData<MappingMethod>();
for (ObfuscationType type : data) {
MappingMethod mappingMethod = data.get(type);
stripped.add(type, mappingMethod.move(null));
}
return stripped;
}
/**
* Register a {@link org.spongepowered.asm.mixin.injection.At} annotation
* and process the references
@@ -32,6 +32,7 @@ import javax.tools.Diagnostic.Kind;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.Mappings.MappingConflictException;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
/**
@@ -43,11 +44,22 @@ class AnnotatedMixinOverwriteHandler extends AnnotatedMixinElementHandler {
* Overwrite element
*/
static class AnnotatedElementOverwrite extends AnnotatedElement<ExecutableElement> {
private final String desc;
public AnnotatedElementOverwrite(ExecutableElement element, AnnotationMirror annotation) {
super(element, annotation);
this.desc = MirrorUtils.getDescriptor(element);
}
public String getSimpleName() {
return this.getElement().getSimpleName().toString();
}
public String getDesc() {
return this.desc;
}
}
AnnotatedMixinOverwriteHandler(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) {
@@ -59,35 +71,14 @@ class AnnotatedMixinOverwriteHandler extends AnnotatedMixinElementHandler {
this.validateTargetMethod(elem.getElement(), elem.getAnnotation(), name, "@Overwrite");
this.checkConstraints(elem.getElement(), elem.getAnnotation());
if (!this.mixin.remap() || !this.validateSingleTarget("@Overwrite", elem.getElement())) {
if (!this.mixin.remap()) {
return;
}
String mcpName = elem.getElement().getSimpleName().toString();
String mcpSignature = MirrorUtils.generateSignature(elem.getElement());
MappingMethod targetMethod = new MappingMethod(this.mixin.getPrimaryTargetRef(), mcpName, mcpSignature);
ObfuscationData<MappingMethod> obfData = this.obf.getDataProvider().getObfMethod(targetMethod);
if (obfData.isEmpty()) {
Kind error = Kind.ERROR;
try {
// Try to access isStatic from com.sun.tools.javac.code.Symbol
Method md = elem.getElement().getClass().getMethod("isStatic");
if (((Boolean)md.invoke(elem.getElement())).booleanValue()) {
error = Kind.WARNING;
}
} catch (Exception ex) {
// well, we tried
for (TypeHandle target : this.mixin.getTargets()) {
if (!this.registerOverwriteForTarget(elem, target)) {
return;
}
this.ap.printMessage(error, "No obfuscation mapping for @Overwrite method", elem.getElement());
return;
}
for (ObfuscationType type : obfData) {
MappingMethod obfMethod = obfData.get(type);
this.addMethodMapping(type, mcpName, obfMethod.getSimpleName(), mcpSignature, obfMethod.getDesc());
}
if (!"true".equalsIgnoreCase(this.ap.getOption(SupportedOptions.DISABLE_OVERWRITE_CHECKER))) {
@@ -110,4 +101,35 @@ class AnnotatedMixinOverwriteHandler extends AnnotatedMixinElementHandler {
}
}
private boolean registerOverwriteForTarget(AnnotatedElementOverwrite elem, TypeHandle target) {
MappingMethod targetMethod = new MappingMethod(target.getName(), elem.getSimpleName(), elem.getDesc());
ObfuscationData<MappingMethod> obfData = this.obf.getDataProvider().getObfMethod(targetMethod);
if (obfData.isEmpty()) {
Kind error = Kind.ERROR;
try {
// Try to access isStatic from com.sun.tools.javac.code.Symbol
Method md = elem.getElement().getClass().getMethod("isStatic");
if (((Boolean)md.invoke(elem.getElement())).booleanValue()) {
error = Kind.WARNING;
}
} catch (Exception ex) {
// well, we tried
}
this.ap.printMessage(error, "No obfuscation mapping for @Overwrite method", elem.getElement());
return false;
}
try {
this.addMethodMappings(elem.getSimpleName(), elem.getDesc(), obfData);
} catch (MappingConflictException ex) {
this.ap.printMessage(Kind.ERROR, "Mapping conflict for @Overwrite method: " + ex.getNew().getSimpleName() + " for target " + target
+ " conflicts with existing mapping " + ex.getOld().getSimpleName(), elem.getElement(), elem.getAnnotation());
return false;
}
return true;
}
}
@@ -35,6 +35,7 @@ import org.spongepowered.asm.obfuscation.mapping.IMapping.Type;
import org.spongepowered.asm.obfuscation.mapping.common.MappingField;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.Mappings.MappingConflictException;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import org.spongepowered.tools.obfuscation.interfaces.IObfuscationDataProvider;
@@ -89,7 +90,7 @@ class AnnotatedMixinShadowHandler extends AnnotatedMixinElementHandler {
}
public ShadowElementName setObfuscatedName(IMapping<?> name) {
return this.setObfuscatedName(name.getName());
return this.setObfuscatedName(name.getSimpleName());
}
public ShadowElementName setObfuscatedName(String name) {
@@ -99,7 +100,7 @@ class AnnotatedMixinShadowHandler extends AnnotatedMixinElementHandler {
public ObfuscationData<M> getObfuscationData(IObfuscationDataProvider provider, String owner) {
return provider.getObfEntry(this.getMapping(owner, this.getName().toString(), this.getDesc()));
}
public abstract M getMapping(String owner, String name, String desc);
public abstract void addMapping(ObfuscationType type, IMapping<?> remapped);
@@ -124,6 +125,7 @@ class AnnotatedMixinShadowHandler extends AnnotatedMixinElementHandler {
public void addMapping(ObfuscationType type, IMapping<?> remapped) {
AnnotatedMixinShadowHandler.this.addFieldMapping(type, this.setObfuscatedName(remapped), this.getDesc(), remapped.getDesc());
}
}
/**
@@ -152,24 +154,37 @@ class AnnotatedMixinShadowHandler extends AnnotatedMixinElementHandler {
}
/**
* Register a {@link org.spongepowered.asm.mixin.Shadow} field
* Register a {@link org.spongepowered.asm.mixin.Shadow} field or method
*/
public void registerShadow(AnnotatedElementShadow<?, ?> elem) {
this.validateTarget(elem.getElement(), elem.getAnnotation(), elem.getName(), "@Shadow");
if (!elem.shouldRemap() || !this.validateSingleTarget("@Shadow", elem.getElement())) {
if (!elem.shouldRemap()) {
return;
}
ObfuscationData<? extends IMapping<?>> obfData = elem.getObfuscationData(this.obf.getDataProvider(), this.mixin.getPrimaryTargetRef());
for (TypeHandle target : this.mixin.getTargets()) {
this.registerShadowForTarget(elem, target);
}
}
private void registerShadowForTarget(AnnotatedElementShadow<?, ?> elem, TypeHandle target) {
ObfuscationData<? extends IMapping<?>> obfData = elem.getObfuscationData(this.obf.getDataProvider(), target.getName());
if (obfData.isEmpty()) {
this.ap.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow " + elem, elem.getElement(), elem.getAnnotation());
String info = this.mixin.isMultiTarget() ? " in target " + target : "";
this.ap.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Shadow " + elem,
elem.getElement(), elem.getAnnotation());
return;
}
for (ObfuscationType type : obfData) {
elem.addMapping(type, obfData.get(type));
try {
elem.addMapping(type, obfData.get(type));
} catch (MappingConflictException ex) {
this.ap.printMessage(Kind.ERROR, "Mapping conflict for @Shadow " + elem + ": " + ex.getNew().getSimpleName() + " for target "
+ target + " conflicts with existing mapping " + ex.getOld().getSimpleName(), elem.getElement(), elem.getAnnotation());
}
}
}
@@ -27,6 +27,7 @@ package org.spongepowered.tools.obfuscation;
import java.util.HashMap;
import java.util.Map;
import org.spongepowered.asm.obfuscation.mapping.IMapping;
import org.spongepowered.asm.obfuscation.mapping.common.MappingField;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer;
@@ -37,6 +38,102 @@ import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer.MappingSet.P
*/
class Mappings implements IMappingConsumer {
/**
* Exception thrown by {@link UniqueMappings} when a conflict occurs
*/
public static class MappingConflictException extends RuntimeException {
private static final long serialVersionUID = 1L;
private final IMapping<?> oldMapping, newMapping;
public MappingConflictException(IMapping<?> oldMapping, IMapping<?> newMapping) {
this.oldMapping = oldMapping;
this.newMapping = newMapping;
}
public IMapping<?> getOld() {
return this.oldMapping;
}
public IMapping<?> getNew() {
return this.newMapping;
}
}
/**
* A wrapper for mapping consumer which ensures all mappings added are
* unique
*/
static class UniqueMappings implements IMappingConsumer {
private final IMappingConsumer mappings;
private final Map<ObfuscationType, Map<MappingField, MappingField>> fields =
new HashMap<ObfuscationType, Map<MappingField, MappingField>>();
private final Map<ObfuscationType, Map<MappingMethod, MappingMethod>> methods =
new HashMap<ObfuscationType, Map<MappingMethod, MappingMethod>>();
public UniqueMappings(IMappingConsumer mappings) {
this.mappings = mappings;
}
@Override
public void clear() {
this.clearMaps();
this.mappings.clear();
}
protected void clearMaps() {
this.fields.clear();
this.methods.clear();
}
@Override
public void addFieldMapping(ObfuscationType type, MappingField from, MappingField to) {
if (!this.<MappingField>checkForExistingMapping(type, from, to, this.fields)) {
this.mappings.addFieldMapping(type, from, to);
}
}
@Override
public void addMethodMapping(ObfuscationType type, MappingMethod from, MappingMethod to) {
if (!this.<MappingMethod>checkForExistingMapping(type, from, to, this.methods)) {
this.mappings.addMethodMapping(type, from, to);
}
}
private <TMapping extends IMapping<TMapping>> boolean checkForExistingMapping(ObfuscationType type, TMapping from, TMapping to,
Map<ObfuscationType, Map<TMapping, TMapping>> mappings) throws MappingConflictException {
Map<TMapping, TMapping> existingMappings = mappings.get(type);
if (existingMappings == null) {
existingMappings = new HashMap<TMapping, TMapping>();
mappings.put(type, existingMappings);
}
TMapping existing = existingMappings.get(from);
if (existing != null) {
if (existing.equals(to)) {
return true;
}
throw new MappingConflictException(existing, to);
}
existingMappings.put(from, to);
return false;
}
@Override
public MappingSet<MappingField> getFieldMappings(ObfuscationType type) {
return this.mappings.getFieldMappings(type);
}
@Override
public MappingSet<MappingMethod> getMethodMappings(ObfuscationType type) {
return this.mappings.getMethodMappings(type);
}
}
/**
* Stored (ordered) field mappings
*/
@@ -49,6 +146,7 @@ class Mappings implements IMappingConsumer {
private final Map<ObfuscationType, MappingSet<MappingMethod>> methodMappings =
new HashMap<ObfuscationType, MappingSet<MappingMethod>>();
private UniqueMappings unique;
public Mappings() {
this.init();
@@ -61,6 +159,13 @@ class Mappings implements IMappingConsumer {
}
}
public IMappingConsumer asUnique() {
if (this.unique == null) {
this.unique = new UniqueMappings(this);
}
return this.unique;
}
/**
* Get stored field mappings
*/
@@ -86,6 +191,9 @@ class Mappings implements IMappingConsumer {
public void clear() {
this.fieldMappings.clear();
this.methodMappings.clear();
if (this.unique != null) {
this.unique.clearMaps();
}
this.init();
}
@@ -113,9 +113,9 @@ public class ObfuscationDataProvider implements IObfuscationDataProvider {
public <T> ObfuscationData<T> getObfEntry(IMapping<T> mapping) {
if (mapping != null) {
if (mapping.getType() == Type.FIELD) {
return (ObfuscationData<T>) this.getObfField((MappingField)mapping);
return (ObfuscationData<T>)this.getObfField((MappingField)mapping);
} else if (mapping.getType() == Type.METHOD) {
return (ObfuscationData<T>) this.getObfMethod((MappingMethod)mapping);
return (ObfuscationData<T>)this.getObfMethod((MappingMethod)mapping);
}
}
@@ -45,16 +45,18 @@ import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer;
import org.spongepowered.tools.obfuscation.mapping.IMappingProvider;
import org.spongepowered.tools.obfuscation.mapping.IMappingWriter;
import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer.MappingSet;
import org.spongepowered.tools.obfuscation.interfaces.IObfuscationEnvironment;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
/**
* Stores information relevant to a particular obfuscation environment.
* Provides access to information relevant to a particular obfuscation
* environment.
*
* <p>We classify different types of possible obfuscation (eg. "searge",
* "notch") as <em>obfuscation environments</em> and store related information
* such as the input mappings, generated refmap and generated mappings here.</p>
* such as the input mappings here.</p>
*/
public abstract class ObfuscationEnvironment {
public abstract class ObfuscationEnvironment implements IObfuscationEnvironment {
/**
* Remapping proxy for remapping descriptors
@@ -178,6 +180,7 @@ public abstract class ObfuscationEnvironment {
/**
* Get an obfuscation mapping for a method
*/
@Override
public MappingMethod getObfMethod(MemberInfo method) {
MappingMethod obfd = this.getObfMethod(method.asMethodMapping());
if (obfd != null || !method.isFullyQualified()) {
@@ -204,6 +207,7 @@ public abstract class ObfuscationEnvironment {
/**
* Get an obfuscation mapping for a method
*/
@Override
public MappingMethod getObfMethod(MappingMethod method) {
return this.getObfMethod(method, true);
}
@@ -211,6 +215,7 @@ public abstract class ObfuscationEnvironment {
/**
* Get an obfuscation mapping for a method
*/
@Override
public MappingMethod getObfMethod(MappingMethod method, boolean lazyRemap) {
if (this.initMappings()) {
boolean remapped = true;
@@ -243,6 +248,7 @@ public abstract class ObfuscationEnvironment {
* @param method method to remap
* @return remapped method or null if no remapping occurred
*/
@Override
public MemberInfo remapDescriptor(MemberInfo method) {
boolean transformed = false;
@@ -274,6 +280,7 @@ public abstract class ObfuscationEnvironment {
* @return remapped descriptor, may return the original descriptor if no
* remapping occurred
*/
@Override
public String remapDescriptor(String desc) {
return ObfuscationUtil.mapDescriptor(desc, this.remapper);
}
@@ -281,6 +288,7 @@ public abstract class ObfuscationEnvironment {
/**
* Get an obfuscation mapping for a field
*/
@Override
public MappingField getObfField(MemberInfo field) {
return this.getObfField(field.asFieldMapping(), true);
}
@@ -288,6 +296,7 @@ public abstract class ObfuscationEnvironment {
/**
* Get an obfuscation mapping for a field
*/
@Override
public MappingField getObfField(MappingField field) {
return this.getObfField(field, true);
}
@@ -295,6 +304,7 @@ public abstract class ObfuscationEnvironment {
/**
* Get an obfuscation mapping for a field
*/
@Override
public MappingField getObfField(MappingField field, boolean lazyRemap) {
if (!this.initMappings()) {
return null;
@@ -318,6 +328,7 @@ public abstract class ObfuscationEnvironment {
/**
* Get an obfuscation mapping for a class
*/
@Override
public String getObfClass(String className) {
if (!this.initMappings()) {
return null;
@@ -328,6 +339,7 @@ public abstract class ObfuscationEnvironment {
/**
* Write out generated mappings
*/
@Override
public void writeMappings(Collection<IMappingConsumer> consumers) {
MappingSet<MappingField> fields = new MappingSet<MappingField>();
MappingSet<MappingMethod> methods = new MappingSet<MappingMethod>();
@@ -45,6 +45,30 @@ import org.spongepowered.tools.obfuscation.interfaces.IReferenceManager;
*/
public class ReferenceManager implements IReferenceManager {
/**
* Exception thrown when a reference conflict occurs
*/
public static class ReferenceConflictException extends RuntimeException {
private static final long serialVersionUID = 1L;
private final String oldReference, newReference;
public ReferenceConflictException(String oldReference, String newReference) {
this.oldReference = oldReference;
this.newReference = newReference;
}
public String getOld() {
return this.oldReference;
}
public String getNew() {
return this.newReference;
}
}
/**
* Annotation processor
*/
@@ -189,7 +213,11 @@ public class ReferenceManager implements IReferenceManager {
}
protected void addMapping(ObfuscationType type, String className, String reference, String newReference) {
this.refMapper.addMapping(type.getKey(), className, reference, newReference);
String oldReference = this.refMapper.addMapping(type.getKey(), className, reference, newReference);
if (oldReference != null && !oldReference.equals(newReference)) {
throw new ReferenceConflictException(oldReference, newReference);
}
if (type.isDefault()) {
this.refMapper.addMapping(null, className, reference, newReference);
}
@@ -0,0 +1,140 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://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.tools.obfuscation.interfaces;
import java.util.Collection;
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
import org.spongepowered.asm.obfuscation.mapping.common.MappingField;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer;
/**
* An obfuscation environment provides facilities to fetch obfuscation mappings
* of a particular type and also manage writing generated mappings of that type
* to disk.
*/
public interface IObfuscationEnvironment {
/**
* Get an obfuscation mapping for a method, returns null if no mapping
* exists for the specified method in this environment.
*
* @param method method to locate a mapping for
* @return remapped method or null if no mapping exists
*/
public abstract MappingMethod getObfMethod(MemberInfo method);
/**
* Get an obfuscation mapping for a method, returns null if no mapping
* exists for the specified method in this environment.
*
* @param method method to locate a mapping for
* @return remapped method or null if no mapping exists
*/
public abstract MappingMethod getObfMethod(MappingMethod method);
/**
* Get an obfuscation mapping for a method, returns null if no mapping
* exists for the specified method in this environment.
*
* @param method method to locate a mapping for
* @param lazyRemap in general, if a mapping is not located, an attempt will
* be made to remap the owner and descriptor of the method to account
* for the fact that classes appearing in the descriptor may need to be
* remapped even if the method name is not. Setting <tt>lazyRemap</tt>
* to <tt>true</tt> disables this behaviour and simply fails fast if no
* mapping is located
* @return remapped method or null if no mapping exists
*/
public abstract MappingMethod getObfMethod(MappingMethod method, boolean lazyRemap);
/**
* Get an obfuscation mapping for a field, returns null if no mapping
* exists for the specified field in this environment.
*
* @param field field to locate a mapping for
* @return remapped field or null if no mapping exists
*/
public abstract MappingField getObfField(MemberInfo field);
/**
* Get an obfuscation mapping for a field, returns null if no mapping
* exists for the specified field in this environment.
*
* @param field field to locate a mapping for
* @return remapped field or null if no mapping exists
*/
public abstract MappingField getObfField(MappingField field);
/**
* Get an obfuscation mapping for a field, returns null if no mapping
* exists for the specified field in this environment.
*
* @param field field to locate a mapping for
* @param lazyRemap in general, if a mapping is not located, an attempt will
* be made to remap the owner and descriptor of the field to account
* for the fact that classes appearing in the descriptor may need to be
* remapped even if the field name is not. Setting <tt>lazyRemap</tt>
* to <tt>true</tt> disables this behaviour and simply fails fast if no
* mapping is located
* @return remapped field or null if no mapping exists
*/
public abstract MappingField getObfField(MappingField field, boolean lazyRemap);
/**
* Get an obfuscation mapping for a class, returns null if no mapping exists
* for the specified class in this environment.
*
* @param className Name of the class to remap (binary format)
* @return remapped class name
*/
public abstract String getObfClass(String className);
/**
* Remap only the owner and descriptor of the specified method
*
* @param method method to remap
* @return remapped method or null if no remapping occurred
*/
public abstract MemberInfo remapDescriptor(MemberInfo method);
/**
* Remap a single descriptor in the context of this environment
*
* @param desc descriptor to remap
* @return remapped descriptor, may return the original descriptor if no
* remapping occurred
*/
public abstract String remapDescriptor(String desc);
/**
* Write out accumulated mappings
*
* @param consumers all consumers accumulated during the AP pass
*/
public abstract void writeMappings(Collection<IMappingConsumer> consumers);
}
@@ -67,6 +67,6 @@ public interface IObfuscationManager {
/**
* Write out generated refmap
*/
void writeReferences();
public abstract void writeReferences();
}
@@ -31,7 +31,11 @@ import org.spongepowered.asm.obfuscation.mapping.common.MappingField;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
/**
* A mapping provider supplies mapping information to the AP
* A mapping provider stores raw mapping information for use by the AP, access
* is delegated via the environment which provides enhanced lookup functionality
* such as resolving members recursively in super classes. The mapping provider
* is not required to provide such functionality and merely facilitates raw
* mapping lookups from a particular source.
*/
public interface IMappingProvider {