Main.hx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import haxe.io.BytesInput;
  2. import sys.io.File;
  3. import sys.io.FileInput;
  4. import format.jvm.Data;
  5. using StringTools;
  6. using Lambda;
  7. typedef Annotations = {
  8. var ?runtimeVisible:Array<Annotation>;
  9. var ?runtimeInvisible:Array<Annotation>;
  10. var ?runtimeVisibleParameter:Array<Array<Annotation>>;
  11. var ?runtimeInvisibleParameter:Array<Array<Annotation>>;
  12. }
  13. function getAnnotations(attributes:Array<Attribute>) {
  14. var annotations:Annotations = {};
  15. for (attribute in attributes) {
  16. switch (attribute) {
  17. case RuntimeVisibleAnnotations(a):
  18. annotations.runtimeVisible = a;
  19. case RuntimeInvisibleAnnotations(a):
  20. annotations.runtimeInvisible = a;
  21. case RuntimeVisibleParameterAnnotations(a):
  22. annotations.runtimeVisibleParameter = a;
  23. case RuntimeInvisibleParameterAnnotations(a):
  24. annotations.runtimeInvisibleParameter = a;
  25. case _:
  26. }
  27. }
  28. return annotations;
  29. }
  30. function hasAnnotation(annotations:Array<Annotation>, name:String) {
  31. return annotations.exists(ann -> ann.type == name);
  32. }
  33. function reportPresence(source:String, annotations:Array<Array<Annotation>>, name:String) {
  34. Sys.println('Presence of $name on $source');
  35. for (key => annotations in annotations) {
  36. Sys.println(' $key: ${hasAnnotation(annotations, name)}');
  37. }
  38. }
  39. function main() {
  40. var input = File.read("annotationLib.jar");
  41. var zip = new format.zip.Reader(input);
  42. var data = zip.read();
  43. for (entry in data) {
  44. if (!entry.fileName.endsWith("AnnotationLib.class")) {
  45. continue;
  46. }
  47. var input = new BytesInput(entry.data);
  48. var reader = new format.jvm.Reader(input);
  49. var jc = reader.read();
  50. var annotations = getAnnotations(jc.attributes);
  51. reportPresence(jc.thisClass, [annotations.runtimeVisible, annotations.runtimeInvisible], "Lhaxe/root/MyVisibleAnnotation;");
  52. reportPresence(jc.thisClass, [annotations.runtimeVisible, annotations.runtimeInvisible], "Lhaxe/root/MyInvisibleAnnotation;");
  53. for (method in jc.methods) {
  54. if (method.name != "test") {
  55. continue;
  56. }
  57. var annotations = getAnnotations(method.attributes);
  58. reportPresence(method.name, [annotations.runtimeVisible, annotations.runtimeInvisible], "Lhaxe/root/MyVisibleAnnotation;");
  59. reportPresence(method.name, [annotations.runtimeVisible, annotations.runtimeInvisible], "Lhaxe/root/MyInvisibleAnnotation;");
  60. for (i in 0...2) {
  61. var name = '${method.name} (arg $i)';
  62. reportPresence(name, [annotations.runtimeVisibleParameter[i], annotations.runtimeInvisibleParameter[i]], "Lhaxe/root/MyVisibleAnnotation;");
  63. reportPresence(name, [annotations.runtimeVisibleParameter[i], annotations.runtimeInvisibleParameter[i]], "Lhaxe/root/MyInvisibleAnnotation;");
  64. }
  65. }
  66. var ann = annotations.runtimeVisible.find(ann -> ann.type == "Lhaxe/root/MyVisibleArrayAnnotation;");
  67. Sys.println(ann.elementValuePairs[0].elementValue.value);
  68. var ann = annotations.runtimeVisible.find(ann -> ann.type == "Lhaxe/root/MyVisibleArrayArrayAnnotation;");
  69. Sys.println(ann.elementValuePairs[0].elementValue.value);
  70. }
  71. }