Update annotation processor to check declared constraints as well

This commit is contained in:
Mumfrey committed 2015-05-29 00:27:00 +01:00
1 parent f66760a5bf
commit ed5cd6f318
2 files changed
+68 -1

No files matched your search

@@ -49,6 +49,10 @@ import net.minecraftforge.srg2source.rangeapplier.MethodData;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.asm.util.ConstraintParser;
import org.spongepowered.asm.util.ConstraintParser.Constraint;
import org.spongepowered.asm.util.ConstraintViolationException;
import org.spongepowered.asm.util.InvalidConstraintException;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.IMixinValidator.ValidationPass;
@@ -410,6 +414,7 @@ class AnnotatedMixin {
public void registerOverwrite(ExecutableElement method, AnnotationMirror overwrite) {
AliasedElementName name = new AliasedElementName(method, overwrite);
this.validateTargetMethod(method, overwrite, name, "@Overwrite");
this.checkConstraints(method, overwrite);
if (!this.remap || !this.validateSingleTarget("@Overwrite", method)) {
return;
@@ -601,6 +606,26 @@ class AnnotatedMixin {
String mapping = String.format("MD: %s %s %s %s", this.classRef + "/" + mcpName, mcpSignature, this.classRef + "/" + obfName, obfSignature);
this.methodMappings.add(mapping);
}
/**
* Check constraints for the specified annotation based on token values in
* the current environment
*
* @param method Annotated method
* @param annotation Annotation to check constraints
*/
private void checkConstraints(ExecutableElement method, AnnotationMirror annotation) {
try {
Constraint constraint = ConstraintParser.parse(MirrorUtils.<String>getAnnotationValue(annotation, "constraints"));
try {
constraint.check(this.mixins);
} catch (ConstraintViolationException ex) {
this.mixins.printMessage(Kind.ERROR, ex.getMessage(), method, annotation);
}
} catch (InvalidConstraintException ex) {
this.mixins.printMessage(Kind.WARNING, ex.getMessage(), method, annotation);
}
}
/**
* Checks whether the specified method exists in all targets and raises
@@ -36,6 +36,8 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
@@ -60,6 +62,7 @@ import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper;
import org.spongepowered.asm.util.ITokenProvider;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.IMixinValidator.ValidationPass;
import org.spongepowered.tools.obfuscation.validation.ParentValidator;
@@ -75,7 +78,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 implements Messager {
class AnnotatedMixins implements Messager, ITokenProvider {
static enum CompilerEnvironment {
/**
@@ -136,6 +139,11 @@ class AnnotatedMixins implements Messager {
*/
private final List<IMixinValidator> validators;
/**
* Resolved tokens for constraint validation
*/
private final Map<String, Integer> tokenCache = new HashMap<String, Integer>();
/**
* SRG container for mcp->srg mappings
*/
@@ -170,6 +178,40 @@ class AnnotatedMixins implements Messager {
new ParentValidator(processingEnv, this),
new TargetValidator(processingEnv, this)
);
this.initTokenCache(this.getOption("tokens"));
}
private void initTokenCache(String tokens) {
if (tokens != null) {
Pattern tokenPattern = Pattern.compile("^([A-Z0-9\\-_\\.]+)=([0-9]+)$");
String[] tokenValues = tokens.replaceAll("\\s", "").toUpperCase().split("[;,]");
for (String tokenValue : tokenValues) {
Matcher tokenMatcher = tokenPattern.matcher(tokenValue);
if (tokenMatcher.matches()) {
this.tokenCache.put(tokenMatcher.group(1), Integer.parseInt(tokenMatcher.group(2)));
}
}
}
}
@Override
public Integer getToken(String token) {
if (this.tokenCache.containsKey(token)) {
return this.tokenCache.get(token);
}
String option = this.getOption(token);
Integer value = null;
try {
value = Integer.valueOf(Integer.parseInt(option));
} catch (Exception ex) {
// npe or number format exception
}
this.tokenCache.put(token, value);
return value;
}
private String getOption(String option) {