aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/target/jvm/type/reflection.lux
blob: 73d5c2154f129c140d06b206f00f9e1b5953ec39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(.module:
  [lux (#- int char)
   [abstract
    [equivalence (#+ Equivalence)]]
   [data
    ["." text ("#\." equivalence)
     ["%" format (#+ format)]]]
   [type
    abstract]]
  ["." // #_
   [category (#+ Void Value Return Method Primitive Object Class Array Var Parameter Declaration)]
   ["#." descriptor]
   [//
    [encoding
     ["#." name (#+ External)]]]])

(abstract: #export (Reflection category)
  Text

  (def: #export reflection
    (-> (Reflection Any) Text)
    (|>> :representation))

  (structure: #export equivalence
    (All [category] (Equivalence (Reflection category)))
    
    (def: (= parameter subject)
      (text\= (:representation parameter) (:representation subject))))

  (template [<category> <name> <reflection>]
    [(def: #export <name>
       (Reflection <category>)
       (:abstraction <reflection>))]

    [Void void "void"]
    [Primitive boolean "boolean"]
    [Primitive byte "byte"]
    [Primitive short "short"]
    [Primitive int "int"]
    [Primitive long "long"]
    [Primitive float "float"]
    [Primitive double "double"]
    [Primitive char "char"]
    )

  (def: #export class
    (-> External (Reflection Class))
    (|>> :abstraction))

  (def: #export (declaration name)
    (-> External (Reflection Declaration))
    (:transmutation (..class name)))

  (def: #export as-class
    (-> (Reflection Declaration) (Reflection Class))
    (|>> :transmutation))

  (def: #export (array element)
    (-> (Reflection Value) (Reflection Array))
    (let [element' (:representation element)
          elementR (`` (cond (text.starts-with? //descriptor.array-prefix element')
                             element'
                             
                             (~~ (template [<primitive> <descriptor>]
                                   [(\ ..equivalence = <primitive> element)
                                    (//descriptor.descriptor <descriptor>)]

                                   [..boolean //descriptor.boolean]
                                   [..byte //descriptor.byte]
                                   [..short //descriptor.short]
                                   [..int //descriptor.int]
                                   [..long //descriptor.long]
                                   [..float //descriptor.float]
                                   [..double //descriptor.double]
                                   [..char //descriptor.char]))

                             (|> element'
                                 //descriptor.class
                                 //descriptor.descriptor
                                 (text.replace-all //name.internal-separator
                                                   //name.external-separator))))]
      (|> elementR
          (format //descriptor.array-prefix)
          :abstraction)))

  (template [<name> <category>]
    [(def: #export <name>
       (Reflection <category>)
       (:transmutation
        (..class "java.lang.Object")))]

    [var Var]
    [wildcard Parameter]
    )

  (def: #export (lower reflection)
    (-> (Reflection Class) (Reflection Parameter))
    ..wildcard)

  (def: #export upper
    (-> (Reflection Class) (Reflection Parameter))
    (|>> :transmutation))
  )