From f34c9dc60d20f8f561ed746a5c724e520832ae46 Mon Sep 17 00:00:00 2001 From: Kiooeht Date: Thu, 11 Oct 2018 16:55:18 -0700 Subject: [PATCH] Copy annotations from SpireFields --- CHANGELOG.md | 1 + .../modthespire/patcher/ClassPatchInfo.java | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a27140b2..d2128335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Changelog ## #### dev #### * Fix crash if a mod doesn't have an ID +* Copy annotations from SpireFields #### v3.2.0 #### * SpireOverride: Allow overriding private methods from superclasses diff --git a/src/main/java/com/evacipated/cardcrawl/modthespire/patcher/ClassPatchInfo.java b/src/main/java/com/evacipated/cardcrawl/modthespire/patcher/ClassPatchInfo.java index 512aca87..42ae9ece 100644 --- a/src/main/java/com/evacipated/cardcrawl/modthespire/patcher/ClassPatchInfo.java +++ b/src/main/java/com/evacipated/cardcrawl/modthespire/patcher/ClassPatchInfo.java @@ -4,7 +4,12 @@ import com.evacipated.cardcrawl.modthespire.lib.SpireField; import com.evacipated.cardcrawl.modthespire.lib.StaticSpireField; import javassist.*; +import javassist.bytecode.AnnotationsAttribute; +import javassist.bytecode.ConstPool; +import javassist.bytecode.annotation.Annotation; +import javassist.bytecode.annotation.AnnotationImpl; +import java.lang.reflect.Proxy; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -71,6 +76,27 @@ public void doPatch() throws PatchingException System.out.println(" - Adding Field: " + str); } CtField new_f = CtField.make(str, ctClassToPatch); + + // Copy annotations + ConstPool constPool = ctClassToPatch.getClassFile().getConstPool(); + AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); + for (Object a : f.getAvailableAnnotations()) { + if (Proxy.getInvocationHandler(a) instanceof AnnotationImpl) { + if (Loader.DEBUG) { + System.out.println(" - Copying annotation: " + a); + } + AnnotationImpl impl = (AnnotationImpl) Proxy.getInvocationHandler(a); + Annotation annotation = new Annotation(impl.getTypeName(), constPool); + if (impl.getAnnotation().getMemberNames() != null) { + for (Object memberName : impl.getAnnotation().getMemberNames()) { + annotation.addMemberValue((String) memberName, impl.getAnnotation().getMemberValue((String) memberName)); + } + } + attr.addAnnotation(annotation); + } + } + new_f.getFieldInfo().addAttribute(attr); + String expr = String.format("(%s) %s.%s.getDefaultValue()", fieldType, ctPatchClass.getName(), f.getName()); ctClassToPatch.addField(new_f, CtField.Initializer.byExpr(expr));