Skip to content

Commit bae3b4e

Browse files
Revert "Remove deprecated methods from JodaCompatibleZonedDateTime which are called by scripts (#3346) (#3348)" (#3393)
This reverts commit a764fde. Signed-off-by: Sarat Vemulapalli <[email protected]>
1 parent c4fbe7c commit bae3b4e

File tree

3 files changed

+304
-20
lines changed

3 files changed

+304
-20
lines changed

modules/lang-painless/src/main/resources/org/opensearch/painless/spi/org.opensearch.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,29 @@ class org.opensearch.script.JodaCompatibleZonedDateTime {
124124
ZonedDateTime withYear(int)
125125
ZonedDateTime withZoneSameLocal(ZoneId)
126126
ZonedDateTime withZoneSameInstant(ZoneId)
127+
128+
#### Joda time methods
129+
long getMillis()
130+
int getCenturyOfEra()
131+
int getEra()
132+
int getHourOfDay()
133+
int getMillisOfDay()
134+
int getMillisOfSecond()
135+
int getMinuteOfDay()
136+
int getMinuteOfHour()
137+
int getMonthOfYear()
138+
int getSecondOfDay()
139+
int getSecondOfMinute()
140+
int getWeekOfWeekyear()
141+
int getWeekyear()
142+
int getYearOfCentury()
143+
int getYearOfEra()
144+
String toString(String)
145+
String toString(String,Locale)
146+
147+
# conflicting methods
127148
DayOfWeek getDayOfWeekEnum()
149+
int getDayOfWeek()
128150
}
129151

130152
class org.opensearch.index.fielddata.ScriptDocValues$Dates {

server/src/main/java/org/opensearch/script/JodaCompatibleZonedDateTime.java

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@
3232

3333
package org.opensearch.script;
3434

35+
import org.joda.time.DateTime;
3536
import org.opensearch.common.SuppressForbidden;
37+
import org.opensearch.common.SuppressLoggerChecks;
38+
import org.opensearch.common.logging.DeprecationLogger;
3639
import org.opensearch.common.time.DateFormatter;
40+
import org.opensearch.common.time.DateFormatters;
41+
import org.opensearch.common.time.DateUtils;
3742

43+
import java.security.AccessController;
44+
import java.security.PrivilegedAction;
3845
import java.time.DayOfWeek;
3946
import java.time.Instant;
4047
import java.time.LocalDate;
@@ -48,6 +55,7 @@
4855
import java.time.chrono.ChronoZonedDateTime;
4956
import java.time.chrono.Chronology;
5057
import java.time.format.DateTimeFormatter;
58+
import java.time.temporal.ChronoField;
5159
import java.time.temporal.Temporal;
5260
import java.time.temporal.TemporalAccessor;
5361
import java.time.temporal.TemporalAdjuster;
@@ -56,6 +64,7 @@
5664
import java.time.temporal.TemporalQuery;
5765
import java.time.temporal.TemporalUnit;
5866
import java.time.temporal.ValueRange;
67+
import java.util.Locale;
5968
import java.util.Objects;
6069

6170
/**
@@ -71,6 +80,23 @@ public class JodaCompatibleZonedDateTime
7180
TemporalAccessor {
7281

7382
private static final DateFormatter DATE_FORMATTER = DateFormatter.forPattern("strict_date_time");
83+
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(JodaCompatibleZonedDateTime.class);
84+
85+
private static void logDeprecated(String key, String message, Object... params) {
86+
AccessController.doPrivileged(new PrivilegedAction<Void>() {
87+
@SuppressLoggerChecks(reason = "safely delegates to logger")
88+
@Override
89+
public Void run() {
90+
deprecationLogger.deprecate(key, message, params);
91+
return null;
92+
}
93+
});
94+
}
95+
96+
private static void logDeprecatedMethod(String oldMethod, String newMethod) {
97+
logDeprecated(oldMethod, "Use of the joda time method [{}] is deprecated. Use [{}] instead.", oldMethod, newMethod);
98+
}
99+
74100
private ZonedDateTime dt;
75101

76102
public JodaCompatibleZonedDateTime(Instant instant, ZoneId zone) {
@@ -401,7 +427,120 @@ public ZonedDateTime withZoneSameInstant(ZoneId zone) {
401427
return dt.withZoneSameInstant(zone);
402428
}
403429

430+
@Deprecated
431+
public long getMillis() {
432+
logDeprecatedMethod("getMillis()", "toInstant().toEpochMilli()");
433+
return dt.toInstant().toEpochMilli();
434+
}
435+
436+
@Deprecated
437+
public int getCenturyOfEra() {
438+
logDeprecatedMethod("getCenturyOfEra()", "get(ChronoField.YEAR_OF_ERA) / 100");
439+
return dt.get(ChronoField.YEAR_OF_ERA) / 100;
440+
}
441+
442+
@Deprecated
443+
public int getEra() {
444+
logDeprecatedMethod("getEra()", "get(ChronoField.ERA)");
445+
return dt.get(ChronoField.ERA);
446+
}
447+
448+
@Deprecated
449+
public int getHourOfDay() {
450+
logDeprecatedMethod("getHourOfDay()", "getHour()");
451+
return dt.getHour();
452+
}
453+
454+
@Deprecated
455+
public int getMillisOfDay() {
456+
logDeprecatedMethod("getMillisOfDay()", "get(ChronoField.MILLI_OF_DAY)");
457+
return dt.get(ChronoField.MILLI_OF_DAY);
458+
}
459+
460+
@Deprecated
461+
public int getMillisOfSecond() {
462+
logDeprecatedMethod("getMillisOfSecond()", "get(ChronoField.MILLI_OF_SECOND)");
463+
return dt.get(ChronoField.MILLI_OF_SECOND);
464+
}
465+
466+
@Deprecated
467+
public int getMinuteOfDay() {
468+
logDeprecatedMethod("getMinuteOfDay()", "get(ChronoField.MINUTE_OF_DAY)");
469+
return dt.get(ChronoField.MINUTE_OF_DAY);
470+
}
471+
472+
@Deprecated
473+
public int getMinuteOfHour() {
474+
logDeprecatedMethod("getMinuteOfHour()", "getMinute()");
475+
return dt.getMinute();
476+
}
477+
478+
@Deprecated
479+
public int getMonthOfYear() {
480+
logDeprecatedMethod("getMonthOfYear()", "getMonthValue()");
481+
return dt.getMonthValue();
482+
}
483+
484+
@Deprecated
485+
public int getSecondOfDay() {
486+
logDeprecatedMethod("getSecondOfDay()", "get(ChronoField.SECOND_OF_DAY)");
487+
return dt.get(ChronoField.SECOND_OF_DAY);
488+
}
489+
490+
@Deprecated
491+
public int getSecondOfMinute() {
492+
logDeprecatedMethod("getSecondOfMinute()", "getSecond()");
493+
return dt.getSecond();
494+
}
495+
496+
@Deprecated
497+
public int getWeekOfWeekyear() {
498+
logDeprecatedMethod("getWeekOfWeekyear()", "get(DateFormatters.WEEK_FIELDS_ROOT.weekOfWeekBasedYear())");
499+
return dt.get(DateFormatters.WEEK_FIELDS_ROOT.weekOfWeekBasedYear());
500+
}
501+
502+
@Deprecated
503+
public int getWeekyear() {
504+
logDeprecatedMethod("getWeekyear()", "get(DateFormatters.WEEK_FIELDS_ROOT.weekBasedYear())");
505+
return dt.get(DateFormatters.WEEK_FIELDS_ROOT.weekBasedYear());
506+
}
507+
508+
@Deprecated
509+
public int getYearOfCentury() {
510+
logDeprecatedMethod("getYearOfCentury()", "get(ChronoField.YEAR_OF_ERA) % 100");
511+
return dt.get(ChronoField.YEAR_OF_ERA) % 100;
512+
}
513+
514+
@Deprecated
515+
public int getYearOfEra() {
516+
logDeprecatedMethod("getYearOfEra()", "get(ChronoField.YEAR_OF_ERA)");
517+
return dt.get(ChronoField.YEAR_OF_ERA);
518+
}
519+
520+
@Deprecated
521+
public String toString(String format) {
522+
logDeprecatedMethod("toString(String)", "a DateTimeFormatter");
523+
// TODO: replace with bwc formatter
524+
return new DateTime(dt.toInstant().toEpochMilli(), DateUtils.zoneIdToDateTimeZone(dt.getZone())).toString(format);
525+
}
526+
527+
@Deprecated
528+
public String toString(String format, Locale locale) {
529+
logDeprecatedMethod("toString(String,Locale)", "a DateTimeFormatter");
530+
// TODO: replace with bwc formatter
531+
return new DateTime(dt.toInstant().toEpochMilli(), DateUtils.zoneIdToDateTimeZone(dt.getZone())).toString(format, locale);
532+
}
533+
404534
public DayOfWeek getDayOfWeekEnum() {
405535
return dt.getDayOfWeek();
406536
}
537+
538+
@Deprecated
539+
public int getDayOfWeek() {
540+
logDeprecated(
541+
"getDayOfWeek()",
542+
"The return type of [getDayOfWeek()] will change to an enum in 7.0. Use getDayOfWeekEnum().getValue()."
543+
);
544+
return dt.getDayOfWeek().getValue();
545+
}
407546
}

0 commit comments

Comments
 (0)