Support private mixin targets, AP improvements & minor fixes, closes #7

This commit is contained in:
Mumfrey committed 2015-01-18 00:05:51 +00:00
1 parent 2ef7cad8a9
commit 0f27299597
17 files changed
+566 -113

No files matched your search

@@ -167,30 +167,19 @@ public abstract class MirrorUtils {
public static String getInternalName(TypeMirror type) {
switch (type.getKind()) {
case ARRAY: return "[" + MirrorUtils.getInternalName(((ArrayType)type).getComponentType());
case BOOLEAN:
return "Z";
case BYTE:
return "B";
case CHAR:
return "C";
case DOUBLE:
return "D";
case FLOAT:
return "F";
case INT:
return "I";
case LONG:
return "J";
case SHORT:
return "S";
case DECLARED:
return "L" + MirrorUtils.getInternalName((DeclaredType)type) + ";";
case ERROR:
// TODO figure out a better way to not crash when we get here
return "Ljava/lang/Object;";
case VOID:
return "V";
case ARRAY: return "[" + MirrorUtils.getInternalName(((ArrayType)type).getComponentType());
case DECLARED: return "L" + MirrorUtils.getInternalName((DeclaredType)type) + ";";
case BOOLEAN: return "Z";
case BYTE: return "B";
case CHAR: return "C";
case DOUBLE: return "D";
case FLOAT: return "F";
case INT: return "I";
case LONG: return "J";
case SHORT: return "S";
case VOID: return "V";
// TODO figure out a better way to not crash when we get here
case ERROR: return "Ljava/lang/Object;";
default:
}
@@ -26,6 +26,8 @@ package org.spongepowered.tools.obfuscation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -37,6 +39,7 @@ import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
@@ -48,6 +51,7 @@ import net.minecraftforge.srg2source.rangeapplier.MethodData;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.validation.IMixinValidator;
/**
@@ -115,6 +119,11 @@ class AnnotatedMixin {
private static final String CTOR = "<init>";
/**
* Mixin annotation
*/
private final AnnotationMirror annotation;
/**
* Manager
*/
private final AnnotatedMixins mixins;
@@ -127,7 +136,7 @@ class AnnotatedMixin {
/**
* Specified targets
*/
private final List<String> targets = new ArrayList<String>();
private final List<TypeElement> targets = new ArrayList<TypeElement>();
/**
* Target "reference" (bytecode name)
@@ -158,34 +167,88 @@ class AnnotatedMixin {
* Stored (ordered) method mappings
*/
private final Set<String> methodMappings = new LinkedHashSet<String>();
public AnnotatedMixin(AnnotatedMixins mixins, TypeElement type) {
AnnotationMirror annotation = MirrorUtils.getAnnotation(type, Mixin.class);
public AnnotatedMixin(AnnotatedMixins mixins, TypeElement type, Collection<IMixinValidator> validators) {
this.annotation = MirrorUtils.getAnnotation(type, Mixin.class);
this.mixins = mixins;
this.mixin = type;
this.classRef = type.getQualifiedName().toString().replace('.', '/');
this.remap = AnnotatedMixins.getRemapValue(annotation);
String targetRef = null;
TypeElement targetType = null;
TypeMirror primaryTarget = this.initTargets();
if (primaryTarget != null) {
this.targetRef = MirrorUtils.getInternalName((DeclaredType)primaryTarget);
this.targetType = (TypeElement)((DeclaredType)primaryTarget).asElement();
} else {
this.targetRef = null;
this.targetType = null;
}
this.remap = AnnotatedMixins.getRemapValue(this.annotation) && this.targets.size() > 0;
for (IMixinValidator validator : validators) {
if (!validator.validate(type, this.annotation, this.targets)) {
return;
}
}
}
private TypeMirror initTargets() {
TypeMirror primaryTarget = null;
// Public targets, referenced by class
try {
List<AnnotationValue> targetList = MirrorUtils.<List<AnnotationValue>>getAnnotationValue(annotation);
for (TypeMirror target : MirrorUtils.<TypeMirror>unfold(targetList)) {
Element element = ((DeclaredType)target).asElement();
String targetName = ((TypeElement)element).getQualifiedName().toString();
this.targets.add(targetName);
if (targetRef == null) {
targetRef = MirrorUtils.getInternalName((DeclaredType)target);
targetType = (TypeElement) ((DeclaredType)target).asElement();
List<AnnotationValue> publiTargets = MirrorUtils.<List<AnnotationValue>>getAnnotationValue(this.annotation, "value",
Collections.<AnnotationValue>emptyList());
for (TypeMirror target : MirrorUtils.<TypeMirror>unfold(publiTargets)) {
TypeElement element = (TypeElement)((DeclaredType)target).asElement();
if (this.targets.contains(element)) {
continue;
}
this.targets.add(element);
if (primaryTarget == null) {
primaryTarget = target;
}
}
} catch (Exception ex) {
this.mixins.printMessage(Kind.WARNING, "Error processing target list: " + ex.getClass().getSimpleName() + ": "+ ex.getMessage(), type);
this.mixins.printMessage(Kind.WARNING, "Error processing public targets: " + ex.getClass().getName() + ": " + ex.getMessage(), this);
}
this.targetRef = targetRef;
this.targetType = targetType;
// Private targets, referenced by name
try {
List<AnnotationValue> privateTargets = MirrorUtils.<List<AnnotationValue>>getAnnotationValue(this.annotation, "targets",
Collections.<AnnotationValue>emptyList());
for (String privateTarget : MirrorUtils.<String>unfold(privateTargets)) {
TypeElement element = this.mixins.getTypeElement(privateTarget);
if (this.targets.contains(element)) {
continue;
}
if (element == null) {
this.mixins.printMessage(Kind.ERROR, "Mixin target " + privateTarget + " could not be found", this);
return null;
} else if (element.getModifiers().contains(Modifier.PUBLIC)) {
this.mixins.printMessage(Kind.ERROR, "Mixin target " + privateTarget + " is public and must be specified in value", this);
return null;
}
TypeMirror target = element.asType();
this.targets.add(element);
if (primaryTarget == null) {
primaryTarget = target;
}
}
} catch (Exception ex) {
this.mixins.printMessage(Kind.WARNING, "Error processing private targets: " + ex.getClass().getName() + ": " + ex.getMessage(), this);
}
if (primaryTarget == null) {
this.mixins.printMessage(Kind.ERROR, "Mixin has no targets", this);
}
return primaryTarget;
}
public AnnotationMirror getAnnotation() {
return this.annotation;
}
/**
@@ -198,7 +261,7 @@ class AnnotatedMixin {
/**
* Get the mixin's targets
*/
public List<String> getTargets() {
public List<TypeElement> getTargets() {
return this.targets;
}
@@ -236,8 +299,9 @@ class AnnotatedMixin {
* only a single target is registered. Mixins containing annotated methods with these annotations cannot be multi-targetted.
*/
private boolean validateSingleTarget(String annotation, Element element) {
if (this.targetRef == null) {
this.mixins.printMessage(Kind.ERROR, "Mixin has multiple targets. " + annotation + " is not supported!", element);
if (this.targetRef == null || this.targets.size() > 1) {
this.mixins.printMessage(Kind.ERROR, "Mixin with " + annotation + " members must have exactly one target.", element);
this.mixins.printMessage(Kind.ERROR, "Mixin with " + annotation + " members must have exactly one target.", this.mixin);
return false;
}
return true;
@@ -287,7 +351,7 @@ class AnnotatedMixin {
String obfField = this.mixins.getObfField(this.targetRef + "/" + name);
if (obfField == null) {
this.mixins.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow field", field);
this.mixins.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow field", field, shadow);
return;
}
@@ -307,7 +371,7 @@ class AnnotatedMixin {
MethodData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + name, mcpSignature));
if (obfMethod == null) {
this.mixins.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow method", method);
this.mixins.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow method", method, shadow);
return;
}
@@ -330,14 +394,14 @@ class AnnotatedMixin {
String desc = this.findDescriptor(this.targetType, targetMember);
if (desc == null) {
this.mixins.printMessage(Kind.WARNING, "Unable to determine signature for @Inject target method", method);
this.mixins.printMessage(Kind.WARNING, "Unable to determine signature for @Inject target method", method, inject);
return;
}
MethodData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + targetMember.name, desc));
if (obfMethod == null) {
Kind error = AnnotatedMixin.CTOR.equals(targetMember.name) ? Kind.WARNING : Kind.ERROR;
this.mixins.printMessage(error, "No obfuscation mapping for @Inject target " + targetMember.name, method);
this.mixins.printMessage(error, "No obfuscation mapping for @Inject target " + targetMember.name, method, inject);
return;
}
@@ -350,10 +414,10 @@ class AnnotatedMixin {
/**
* Register a {@link org.spongepowered.asm.mixin.injection.At} annotation and process the references
*/
public void registerInjectionPoint(AnnotationMirror at, Element element) {
public void registerInjectionPoint(Element element, AnnotationMirror inject, AnnotationMirror at) {
String type = MirrorUtils.<String>getAnnotationValue(at, "value");
String target = MirrorUtils.<String>getAnnotationValue(at, "target");
this.remapReference(type + ".<target>", target, element);
this.remapReference(type + ".<target>", target, element, inject, at);
// Pattern for replacing references in args, not used yet
// if ("SOMETYPE".equals(type)) {
@@ -362,7 +426,7 @@ class AnnotatedMixin {
// }
}
private void remapReference(String key, String target, Element element) {
private void remapReference(String key, String target, Element element, AnnotationMirror inject, AnnotationMirror at) {
if (target == null) {
return;
}
@@ -370,7 +434,7 @@ class AnnotatedMixin {
MemberInfo targetMember = MemberInfo.parse(target);
if (!targetMember.isFullyQualified()) {
String missing = "missing " + (targetMember.owner == null ? (targetMember.desc == null ? "owner and signature" : "owner") : "signature");
this.mixins.printMessage(Kind.ERROR, "@At(" + key + ") is not fully qualified, " + missing, element);
this.mixins.printMessage(Kind.ERROR, "@At(" + key + ") is not fully qualified, " + missing, element, inject);
return;
}
@@ -378,14 +442,14 @@ class AnnotatedMixin {
if (targetMember.isField()) {
String obfField = this.mixins.getObfField(targetMember.toSrg());
if (obfField == null) {
this.mixins.printMessage(Kind.WARNING, "Cannot find field mapping for @At(" + key + ") '" + target, element);
this.mixins.printMessage(Kind.WARNING, "Cannot find field mapping for @At(" + key + ") '" + target, element, inject);
return;
}
remappedReference = MemberInfo.fromSrgField(obfField, targetMember.desc);
} else {
MethodData obfMethod = this.mixins.getObfMethod(targetMember.asMethodData());
if (obfMethod == null) {
this.mixins.printMessage(Kind.WARNING, "Cannot find method mapping for @At(" + key + ") '" + target, element);
this.mixins.printMessage(Kind.WARNING, "Cannot find method mapping for @At(" + key + ") '" + target, element, inject);
return;
}
remappedReference = MemberInfo.fromSrgMethod(obfMethod);
@@ -34,6 +34,7 @@ import java.util.Map;
import java.util.Set;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
@@ -48,6 +49,11 @@ import javax.tools.StandardLocation;
import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.validation.IMixinValidator;
import org.spongepowered.tools.obfuscation.validation.ParentValidator;
import org.spongepowered.tools.obfuscation.validation.TargetValidator;
import com.google.common.collect.ImmutableList;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
import net.minecraftforge.srg2source.rangeapplier.SrgContainer;
@@ -56,7 +62,7 @@ import net.minecraftforge.srg2source.rangeapplier.SrgContainer;
/**
* Mixin info manager, stores all of the mixin info during processing and also manages access to the srgs
*/
class AnnotatedMixins {
class AnnotatedMixins implements Messager {
static enum CompilerEnvironment {
/**
@@ -100,13 +106,18 @@ class AnnotatedMixins {
/**
* File containing the reobfd srgs
*/
private final File reobfSrgFile;
private final String reobfSrgFileName;
/**
* Reference mapper for reference mapping
*/
private final ReferenceMapper refMapper = new ReferenceMapper();
/**
* Rule validators
*/
private final List<IMixinValidator> validators;
/**
* SRG container for mcp->srg mappings
*/
@@ -124,18 +135,18 @@ class AnnotatedMixins {
this.env = this.detectEnvironment(processingEnv);
this.processingEnv = processingEnv;
String outSrgFileName = processingEnv.getOptions().get("outSrgFile");
this.outSrgFileName = (outSrgFileName != null) ? outSrgFileName : "mixins.srg";
this.reobfSrgFileName = processingEnv.getOptions().get("reobfSrgFile");
this.outSrgFileName = processingEnv.getOptions().get("outSrgFile");
this.outRefMapFileName = processingEnv.getOptions().get("outRefMapFile");
String outRefMapFileName = processingEnv.getOptions().get("outRefMapFile");
this.outRefMapFileName = (outRefMapFileName != null) ? outRefMapFileName : ReferenceMapper.DEFAULT_RESOURCE;
String reobfSrgFileName = processingEnv.getOptions().get("reobfSrgFile");
if (reobfSrgFileName == null) {
this.printMessage(Kind.ERROR, "The reobfSrgFile argument was not supplied, processing cannot continue");
if (this.reobfSrgFileName == null) {
this.printMessage(Kind.ERROR, "The reobfSrgFile argument was not supplied, obfuscation processing will not occur");
}
this.reobfSrgFile = new File(reobfSrgFileName);
this.validators = ImmutableList.<IMixinValidator>of(
new ParentValidator(processingEnv),
new TargetValidator(processingEnv)
);
}
private CompilerEnvironment detectEnvironment(ProcessingEnvironment processingEnv) {
@@ -153,9 +164,14 @@ class AnnotatedMixins {
if (!this.initDone) {
this.initDone = true;
if (this.reobfSrgFileName == null) {
return false;
}
try {
this.printMessage(Kind.NOTE, "Loading SRGs from " + this.reobfSrgFile.getAbsolutePath());
this.srgs = new SrgContainer().readSrg(this.reobfSrgFile);
File reobfSrgFile = new File(this.reobfSrgFileName);
this.printMessage(Kind.NOTE, "Loading SRGs from " + reobfSrgFile.getAbsolutePath());
this.srgs = new SrgContainer().readSrg(reobfSrgFile);
} catch (Exception ex) {
ex.printStackTrace();
this.printMessage(Kind.ERROR, "The specified SRG file could not be read, processing cannot continue");
@@ -170,6 +186,10 @@ class AnnotatedMixins {
* Write out generated srgs
*/
public void writeSrgs() {
if (this.outSrgFileName == null) {
return;
}
Set<String> fieldMappings = new LinkedHashSet<String>();
Set<String> methodMappings = new LinkedHashSet<String>();
@@ -181,7 +201,7 @@ class AnnotatedMixins {
PrintWriter writer = null;
try {
writer = this.openSrgFileWriter();
writer = this.openFileWriter(this.outSrgFileName, "output SRGs");
for (String fieldMapping : fieldMappings) {
writer.println(fieldMapping);
}
@@ -202,19 +222,19 @@ class AnnotatedMixins {
}
/**
* Open a writer for the output SRG file
* Open a writer for an output file
*/
private PrintWriter openSrgFileWriter() throws IOException {
if (this.outSrgFileName.matches("^.*[\\\\/:].*$")) {
File outSrgFile = new File(this.outSrgFileName);
private PrintWriter openFileWriter(String fileName, String description) throws IOException {
if (fileName.matches("^.*[\\\\/:].*$")) {
File outSrgFile = new File(fileName);
outSrgFile.getParentFile().mkdirs();
this.printMessage(Kind.NOTE, "Writing output SRGs to " + outSrgFile.getAbsolutePath());
this.printMessage(Kind.NOTE, "Writing " + description + " to " + outSrgFile.getAbsolutePath());
return new PrintWriter(outSrgFile);
}
Filer filer = this.processingEnv.getFiler();
FileObject outSrg = filer.createResource(StandardLocation.CLASS_OUTPUT, "", this.outSrgFileName);
this.printMessage(Kind.NOTE, "Writing output SRGs to " + new File(outSrg.toUri()).getAbsolutePath());
FileObject outSrg = filer.createResource(StandardLocation.CLASS_OUTPUT, "", fileName);
this.printMessage(Kind.NOTE, "Writing " + description + " to " + new File(outSrg.toUri()).getAbsolutePath());
return new PrintWriter(outSrg.openWriter());
}
@@ -222,12 +242,14 @@ class AnnotatedMixins {
* Write out stored mappings
*/
public void writeRefs() {
if (this.outRefMapFileName == null) {
return;
}
PrintWriter writer = null;
try {
Filer filer = this.processingEnv.getFiler();
FileObject outSrg = filer.createResource(StandardLocation.CLASS_OUTPUT, "", this.outRefMapFileName);
writer = new PrintWriter(outSrg.openWriter());
writer = this.openFileWriter(this.outRefMapFileName, "refmap");
this.refMapper.write(writer);
} catch (IOException ex) {
ex.printStackTrace();
@@ -264,7 +286,7 @@ class AnnotatedMixins {
String name = mixinType.getQualifiedName().toString();
if (!this.mixins.containsKey(name)) {
this.mixins.put(name, new AnnotatedMixin(this, mixinType));
this.mixins.put(name, new AnnotatedMixin(this, mixinType, this.validators));
}
}
@@ -365,18 +387,18 @@ class AnnotatedMixins {
at = ((AnnotationValue)at).getValue();
}
if (at instanceof AnnotationMirror) {
mixinClass.registerInjectionPoint((AnnotationMirror)at, method);
mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)at);
} else {
this.printMessage(Kind.WARNING, "No annotation mirror on " + at.getClass().getName());
}
}
} else if (ats instanceof AnnotationMirror) {
mixinClass.registerInjectionPoint((AnnotationMirror)ats, method);
mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)ats);
} else if (ats instanceof AnnotationValue) {
// Fix for JDT
Object mirror = ((AnnotationValue)ats).getValue();
if (mirror instanceof AnnotationMirror) {
mixinClass.registerInjectionPoint((AnnotationMirror)mirror, method);
mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)mirror);
}
}
}
@@ -396,8 +418,9 @@ class AnnotatedMixins {
/**
* Print a message to the AP messager
*/
@Override
public void printMessage(Diagnostic.Kind kind, CharSequence msg) {
if (this.env == CompilerEnvironment.JAVAC && kind != Kind.NOTE) {
if (this.env == CompilerEnvironment.JAVAC || kind != Kind.NOTE) {
this.processingEnv.getMessager().printMessage(kind, msg);
}
}
@@ -405,9 +428,40 @@ class AnnotatedMixins {
/**
* Print a message to the AP messager
*/
@Override
public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element element) {
this.processingEnv.getMessager().printMessage(kind, msg, element);
}
/**
* Print a message to the AP messager
*/
@Override
public void printMessage(Kind kind, CharSequence msg, Element element, AnnotationMirror annotation) {
this.processingEnv.getMessager().printMessage(kind, msg, element, annotation);
}
/**
* Print a message to the AP messager
*/
@Override
public void printMessage(Kind kind, CharSequence msg, Element element, AnnotationMirror annotation, AnnotationValue value) {
this.processingEnv.getMessager().printMessage(kind, msg, element, annotation, value);
}
/**
* Print a message to the AP messager
*/
public void printMessage(Kind kind, CharSequence msg, AnnotatedMixin mixin) {
this.processingEnv.getMessager().printMessage(kind, msg, mixin.getMixin(), mixin.getAnnotation());
}
/**
* Get a TypeElement representing another type in the current processing environment
*/
public TypeElement getTypeElement(String name) {
return this.processingEnv.getElementUtils().getTypeElement(name);
}
/**
* Get an obfuscation mapping for a method
@@ -54,7 +54,7 @@ import org.spongepowered.tools.MirrorUtils;
"org.spongepowered.asm.mixin.injection.Redirect",
"org.spongepowered.asm.mixin.injection.At"
})
@SupportedOptions({ "reobfSrgFile", "outSrgFile" })
@SupportedOptions({ "reobfSrgFile", "outSrgFile", "outRefMapFile" })
public class InjectionObfuscationProcessor extends MixinProcessor {
/* (non-Javadoc)
@@ -48,7 +48,7 @@ import org.spongepowered.tools.MirrorUtils;
*/
@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes({ "org.spongepowered.asm.mixin.Mixin", "org.spongepowered.asm.mixin.Shadow", "org.spongepowered.asm.mixin.Overwrite" })
@SupportedOptions({ "reobfSrgFile", "outSrgFile" })
@SupportedOptions({ "reobfSrgFile", "outSrgFile", "outRefMapFile" })
public class TargetObfuscationProcessor extends MixinProcessor {
/* (non-Javadoc)
@@ -0,0 +1,36 @@
/*
* 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.tools.obfuscation.validation;
import java.util.Collection;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.TypeElement;
public interface IMixinValidator {
public abstract boolean validate(TypeElement mixin, AnnotationMirror annotation, Collection<TypeElement> targets);
}
@@ -0,0 +1,51 @@
/*
* 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.tools.obfuscation.validation;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.tools.Diagnostic.Kind;
public abstract class MixinValidator implements IMixinValidator {
protected final ProcessingEnvironment processingEnv;
public MixinValidator(ProcessingEnvironment processingEnv) {
this.processingEnv = processingEnv;
}
protected final void note(String note, Element element) {
this.processingEnv.getMessager().printMessage(Kind.NOTE, note, element);
}
protected final void warning(String warning, Element element) {
this.processingEnv.getMessager().printMessage(Kind.WARNING, warning, element);
}
protected final void error(String error, Element element) {
this.processingEnv.getMessager().printMessage(Kind.ERROR, error, element);
}
}
@@ -0,0 +1,50 @@
/*
* 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.tools.obfuscation.validation;
import java.util.Collection;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
public class ParentValidator extends MixinValidator {
public ParentValidator(ProcessingEnvironment processingEnv) {
super(processingEnv);
}
@Override
public boolean validate(TypeElement mixin, AnnotationMirror annotation, Collection<TypeElement> targets) {
if (mixin.getEnclosingElement().getKind() != ElementKind.PACKAGE && !mixin.getModifiers().contains(Modifier.STATIC)) {
this.error("Inner class mixin must be declared static", mixin);
}
return true;
}
}
@@ -0,0 +1,54 @@
/*
* 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.tools.obfuscation.validation;
import java.util.Collection;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
public class TargetValidator extends MixinValidator {
public TargetValidator(ProcessingEnvironment processingEnv) {
super(processingEnv);
}
@Override
public boolean validate(TypeElement mixin, AnnotationMirror annotation, Collection<TypeElement> targets) {
TypeMirror superClass = mixin.getSuperclass();
for (TypeElement target : targets) {
TypeMirror targetType = target.asType();
if (!this.processingEnv.getTypeUtils().isAssignable(targetType, superClass)) {
this.error("Superclass " + superClass + " of " + mixin + " was not found in the hierarchy of target class " + targetType, mixin);
}
}
return true;
}
}