From ed5cd6f318b5668407bbca6d61b3f2e676230884 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Fri, 29 May 2015 00:27:00 +0100 Subject: [PATCH] Update annotation processor to check declared constraints as well --- .../tools/obfuscation/AnnotatedMixin.java | 25 +++++++++++ .../tools/obfuscation/AnnotatedMixins.java | 44 ++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 659d30a4..656a8ff0 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -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.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 diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index bf52847b..47608f89 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -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 validators; + /** + * Resolved tokens for constraint validation + */ + private final Map tokenCache = new HashMap(); + /** * 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) {