Skip to content

Commit

Permalink
Copy annotations from SpireFields
Browse files Browse the repository at this point in the history
  • Loading branch information
kiooeht committed Oct 11, 2018
1 parent 0a21eeb commit f34c9dc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand Down

0 comments on commit f34c9dc

Please sign in to comment.