Skip to content

Commit ce8f3ac

Browse files
author
Vadim Platonov
committed
Fix reflection in generate two-field time entity constructors
1 parent d7e5573 commit ce8f3ac

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
## 0.3.0
1+
## 0.2.1
22

33
### Fixed
44

5+
* [#1](https://github.com/dm3/clojure.java-time/issues/1): Reflection warnings in two-field time entity constructors
56
* [#2](https://github.com/dm3/clojure.java-time/issues/2): `Ordered` implementation for `java.time.Instant`
67

78
## 0.2.0

src/java_time/single_field.clj

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,12 @@
118118

119119
(defmacro two-field-entity [tp doc & {:keys [major-field-types major-field-ctor
120120
minor-field-ctor minor-field-default]}]
121-
(let [fname (with-meta (symbol (jt.u/dashize (str tp))) {:tag tp})
122-
arg (gensym)]
121+
(let [[major-field-ctor major-field-type] major-field-ctor
122+
[minor-field-ctor minor-field-type] minor-field-ctor
123+
fname (with-meta (symbol (jt.u/dashize (str tp))) {:tag tp})
124+
arg (gensym)
125+
tmp-major (with-meta (gensym) {:tag major-field-type})
126+
tmp-minor (with-meta (gensym) {:tag minor-field-type})]
123127
`(do
124128
(defn ^{:doc ~(str "True if `" tp "`.") } ~(symbol (str fname "?"))
125129
[o#]
@@ -128,7 +132,8 @@
128132
(defn ~fname ~doc
129133
([] (. ~tp from (jt.z/zoned-date-time)))
130134
([~arg] (cond (some (fn [x#] (instance? x# ~arg)) ~major-field-types)
131-
(. ~tp of (~major-field-ctor ~arg) ~minor-field-default)
135+
(let [~tmp-major (~major-field-ctor ~arg)]
136+
(. ~tp of ~tmp-major ~minor-field-default))
132137

133138
(instance? TemporalAccessor ~arg)
134139
(. ~tp from ~arg)
@@ -144,11 +149,14 @@
144149
(catch java.time.format.DateTimeParseException _#
145150
(. ~tp now (jt.z/zone-id ~arg))))
146151

147-
:else (. ~tp of (~major-field-ctor ~arg) ~minor-field-default)))
152+
:else (let [~tmp-major (~major-field-ctor ~arg)]
153+
(. ~tp of ~tmp-major ~minor-field-default))))
148154
([a# b#]
149155
(if (and (or (instance? DateTimeFormatter a#) (string? a#)) (string? b#))
150156
(~fname (jt.f/parse a# b#))
151-
(. ~tp of (~major-field-ctor a#) (~minor-field-ctor b#)))))
157+
(let [~tmp-major (~major-field-ctor a#)
158+
~tmp-minor (~minor-field-ctor b#)]
159+
(. ~tp of ~tmp-major ~tmp-minor)))))
152160

153161
(extend-type ~tp
154162
jt.c/Ordered
@@ -175,17 +183,17 @@
175183
(two-field-entity MonthDay
176184
"Returns the `MonthDay` for the given entity, string, clock, zone or
177185
month/day combination. Current month-day if no arguments given."
178-
:major-field-ctor month
186+
:major-field-ctor [month Month]
179187
:major-field-types [Month Number]
180-
:minor-field-ctor (comp int jt.c/value)
188+
:minor-field-ctor [(comp int jt.c/value) int]
181189
:minor-field-default 1)
182190

183191
(two-field-entity YearMonth
184192
"Returns the `YearMonth` for the given entity, string, clock, zone or
185193
month/day combination. Current year-month if no arguments given."
186-
:major-field-ctor (comp int jt.c/value)
194+
:major-field-ctor [(comp int jt.c/value) int]
187195
:major-field-types [Year Number]
188-
:minor-field-ctor month
196+
:minor-field-ctor [month Month]
189197
:minor-field-default 1)
190198

191199
;;;;;;;;;; Threeten Extra
@@ -218,7 +226,7 @@
218226
(two-field-entity YearQuarter
219227
"Returns the `YearQuarter` for the given entity, clock, zone or year with quarter.
220228
Current year quarter if no arguments given."
221-
:major-field-ctor year-to-int
229+
:major-field-ctor [year-to-int int]
222230
:major-field-types [Year Number]
223-
:minor-field-ctor quarter
231+
:minor-field-ctor [quarter Quarter]
224232
:minor-field-default 1))

test/java_time_test.clj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272

7373
(is (= (j/month-day 1)
7474
(j/month-day (j/month 1))
75-
(j/month-day 1 1)))
75+
(j/month-day 1 1)
76+
(j/month-day 1 (j/day-of-week 1))))
7677

7778
(is (j/month-day? (j/month-day 1 1)))
7879

@@ -82,7 +83,8 @@
8283

8384
(is (= (j/year-month 1)
8485
(j/year-month (j/year 1))
85-
(j/year-month 1 1)))
86+
(j/year-month 1 1)
87+
(j/year-month 1 (j/month 1))))
8688

8789
(is (j/year-month? (j/year-month 1 1))))
8890

0 commit comments

Comments
 (0)