jData.ml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. (*
  2. * This file is part of JavaLib
  3. * Copyright (c)2004-2012 Nicolas Cannasse and Caue Waneck
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. *)
  19. type jpath = (string list) * string
  20. type jversion = int * int (* minor + major *)
  21. (** unqualified names cannot have the characters '.', ';', '[' or '/' *)
  22. type unqualified_name = string
  23. type jwildcard =
  24. | WExtends (* + *)
  25. | WSuper (* - *)
  26. | WNone
  27. type jtype_argument =
  28. | TType of jwildcard * jsignature
  29. | TAny (* * *)
  30. and jsignature =
  31. | TByte (* B *)
  32. | TChar (* C *)
  33. | TDouble (* D *)
  34. | TFloat (* F *)
  35. | TInt (* I *)
  36. | TLong (* J *)
  37. | TShort (* S *)
  38. | TBool (* Z *)
  39. | TObject of jpath * jtype_argument list (* L Classname *)
  40. | TObjectInner of (string list) * (string * jtype_argument list) list (* L Classname ClassTypeSignatureSuffix *)
  41. | TArray of jsignature * int option (* [ *)
  42. | TMethod of jmethod_signature (* ( *)
  43. | TTypeParameter of string (* T *)
  44. (* ( jsignature list ) ReturnDescriptor (| V | jsignature) *)
  45. and jmethod_signature = jsignature list * jsignature option
  46. (* InvokeDynamic-specific: Method handle *)
  47. type reference_type =
  48. | RGetField (* constant must be ConstField *)
  49. | RGetStatic (* constant must be ConstField *)
  50. | RPutField (* constant must be ConstField *)
  51. | RPutStatic (* constant must be ConstField *)
  52. | RInvokeVirtual (* constant must be Method *)
  53. | RInvokeStatic (* constant must be Method *)
  54. | RInvokeSpecial (* constant must be Method *)
  55. | RNewInvokeSpecial (* constant must be Method with name <init> *)
  56. | RInvokeInterface (* constant must be InterfaceMethod *)
  57. (* TODO *)
  58. type bootstrap_method = int
  59. type jconstant =
  60. (** references a class or an interface - jpath must be encoded as StringUtf8 *)
  61. | ConstClass of jpath (* tag = 7 *)
  62. (** field reference *)
  63. | ConstField of (jpath * unqualified_name * jsignature) (* tag = 9 *)
  64. (** method reference; string can be special "<init>" and "<clinit>" values *)
  65. | ConstMethod of (jpath * unqualified_name * jmethod_signature) (* tag = 10 *)
  66. (** interface method reference *)
  67. | ConstInterfaceMethod of (jpath * unqualified_name * jmethod_signature) (* tag = 11 *)
  68. (** constant values *)
  69. | ConstString of string (* tag = 8 *)
  70. | ConstInt of int32 (* tag = 3 *)
  71. | ConstFloat of float (* tag = 4 *)
  72. | ConstLong of int64 (* tag = 5 *)
  73. | ConstDouble of float (* tag = 6 *)
  74. (** name and type: used to represent a field or method, without indicating which class it belongs to *)
  75. | ConstNameAndType of unqualified_name * jsignature
  76. (** UTF8 encoded strings. Note that when reading/writing, take into account Utf8 modifications of java *)
  77. (* (http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.7) *)
  78. | ConstUtf8 of string
  79. (** invokeDynamic-specific *)
  80. | ConstMethodHandle of (reference_type * jconstant) (* tag = 15 *)
  81. | ConstMethodType of jmethod_signature (* tag = 16 *)
  82. | ConstInvokeDynamic of (bootstrap_method * unqualified_name * jsignature) (* tag = 18 *)
  83. | ConstUnusable
  84. type jaccess_flag =
  85. | JPublic (* 0x0001 *)
  86. | JPrivate (* 0x0002 *)
  87. | JProtected (* 0x0004 *)
  88. | JStatic (* 0x0008 *)
  89. | JFinal (* 0x0010 *)
  90. | JSynchronized (* 0x0020 *)
  91. | JVolatile (* 0x0040 *)
  92. | JTransient (* 0x0080 *)
  93. (** added if created by the compiler *)
  94. | JSynthetic (* 0x1000 *)
  95. | JEnum (* 0x4000 *)
  96. | JUnusable (* should not be present *)
  97. (** class flags *)
  98. | JSuper (* 0x0020 *)
  99. | JInterface (* 0x0200 *)
  100. | JAbstract (* 0x0400 *)
  101. | JAnnotation (* 0x2000 *)
  102. (** method flags *)
  103. | JBridge (* 0x0040 *)
  104. | JVarArgs (* 0x0080 *)
  105. | JNative (* 0x0100 *)
  106. | JStrict (* 0x0800 *)
  107. type jaccess = jaccess_flag list
  108. (* type parameter name, extends signature, implements signatures *)
  109. type jtypes = (string * jsignature option * jsignature list) list
  110. type jannotation = {
  111. ann_type : jsignature;
  112. ann_elements : (string * jannotation_value) list;
  113. }
  114. and jannotation_value =
  115. | ValConst of jsignature * jconstant (* B, C, D, E, F, I, J, S, Z, s *)
  116. | ValEnum of jsignature * string (* e *)
  117. | ValClass of jsignature (* c *) (* V -> Void *)
  118. | ValAnnotation of jannotation (* @ *)
  119. | ValArray of jannotation_value list (* [ *)
  120. type jlocal = {
  121. ld_start_pc : int;
  122. ld_length : int;
  123. ld_name : string;
  124. ld_descriptor : string;
  125. ld_index : int;
  126. }
  127. type jattribute =
  128. | AttrDeprecated
  129. | AttrVisibleAnnotations of jannotation list
  130. | AttrInvisibleAnnotations of jannotation list
  131. | AttrLocalVariableTable of jlocal list
  132. | AttrUnknown of string * string
  133. type jcode = jattribute list (* TODO *)
  134. type jfield_kind =
  135. | JKField
  136. | JKMethod
  137. type jfield = {
  138. jf_name : string;
  139. jf_kind : jfield_kind;
  140. (* signature, as used by the vm *)
  141. jf_vmsignature : jsignature;
  142. (* actual signature, as used in java code *)
  143. jf_signature : jsignature;
  144. jf_throws : jsignature list;
  145. jf_types : jtypes;
  146. jf_flags : jaccess;
  147. jf_attributes : jattribute list;
  148. jf_constant : jconstant option;
  149. jf_code : jcode option;
  150. }
  151. type jclass = {
  152. cversion : jversion;
  153. cpath : jpath;
  154. csuper : jsignature;
  155. cflags : jaccess;
  156. cinterfaces : jsignature list;
  157. cfields : jfield list;
  158. cmethods : jfield list;
  159. cattributes : jattribute list;
  160. cinner_types : (jpath * jpath option * string option * jaccess) list;
  161. ctypes : jtypes;
  162. }
  163. (* reading/writing *)
  164. type utf8ref = int
  165. type classref = int
  166. type nametyperef = int
  167. type dynref = int
  168. type bootstrapref = int
  169. type jconstant_raw =
  170. | KClass of utf8ref (* 7 *)
  171. | KFieldRef of (classref * nametyperef) (* 9 *)
  172. | KMethodRef of (classref * nametyperef) (* 10 *)
  173. | KInterfaceMethodRef of (classref * nametyperef) (* 11 *)
  174. | KString of utf8ref (* 8 *)
  175. | KInt of int32 (* 3 *)
  176. | KFloat of float (* 4 *)
  177. | KLong of int64 (* 5 *)
  178. | KDouble of float (* 6 *)
  179. | KNameAndType of (utf8ref * utf8ref) (* 12 *)
  180. | KUtf8String of string (* 1 *)
  181. | KMethodHandle of (reference_type * dynref) (* 15 *)
  182. | KMethodType of utf8ref (* 16 *)
  183. | KInvokeDynamic of (bootstrapref * nametyperef) (* 18 *)
  184. | KUnusable
  185. (* jData debugging *)
  186. let is_override_attrib = (function
  187. (* TODO: pass anotations as @:meta *)
  188. | AttrVisibleAnnotations ann ->
  189. List.exists (function
  190. | { ann_type = TObject( (["java";"lang"], "Override"), [] ) } ->
  191. true
  192. | _ -> false
  193. ) ann
  194. | _ -> false
  195. )
  196. let is_override field =
  197. List.exists is_override_attrib field.jf_attributes
  198. let path_s = function
  199. | (pack,name) -> String.concat "." (pack @ [name])
  200. let rec s_sig = function
  201. | TByte (* B *) -> "byte"
  202. | TChar (* C *) -> "char"
  203. | TDouble (* D *) -> "double"
  204. | TFloat (* F *) -> "float"
  205. | TInt (* I *) -> "int"
  206. | TLong (* J *) -> "long"
  207. | TShort (* S *) -> "short"
  208. | TBool (* Z *) -> "bool"
  209. | TObject(path,args) -> path_s path ^ s_args args
  210. | TObjectInner (sl, sjargl) -> String.concat "." sl ^ "." ^ (String.concat "." (List.map (fun (s,arg) -> s ^ s_args arg) sjargl))
  211. | TArray (s,i) -> s_sig s ^ "[" ^ (match i with | None -> "" | Some i -> string_of_int i) ^ "]"
  212. | TMethod (sigs, sopt) -> (match sopt with | None -> "" | Some s -> s_sig s ^ " ") ^ "(" ^ String.concat ", " (List.map s_sig sigs) ^ ")"
  213. | TTypeParameter s -> s
  214. and s_args = function
  215. | [] -> ""
  216. | args -> "<" ^ String.concat ", " (List.map (fun t ->
  217. match t with
  218. | TAny -> "*"
  219. | TType (wc, s) ->
  220. (match wc with
  221. | WNone -> ""
  222. | WExtends -> "+"
  223. | WSuper -> "-") ^
  224. (s_sig s))
  225. args) ^ ">"
  226. let s_field f = (if is_override f then "override " else "") ^ s_sig f.jf_signature ^ " " ^ f.jf_name
  227. let s_fields fs = "{ \n\t" ^ String.concat "\n\t" (List.map s_field fs) ^ "\n}"