Skip to content

Commit

Permalink
Fixes #5179. On eval of Time#strftime read from file: Java::JavaLang:…
Browse files Browse the repository at this point in the history
…:Error: Unknown special char: Y

Weirdest bug I have fixed in a while.  Java enum is lazily stood up upon first
access.  The enum constructor we had is also setting each element made into a
static array.  Most encodings will go down a code path which references this
enum (Format) and thus initialize itself and populate the static array. USASCII
and ASCII-8BIT will not.  So the static array will have no values and
we fail.

This really seems like java compiler should have seen this side-effect
assignment and stood up the enum at same general point during static
initialization.
  • Loading branch information
enebo committed May 21, 2018
1 parent e46e465 commit 9d6f877
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions core/src/main/java/org/jruby/util/RubyDateFormatter.java
Expand Up @@ -50,7 +50,6 @@
import org.joda.time.DateTime;
import org.joda.time.chrono.GJChronology;
import org.joda.time.chrono.JulianChronology;
import org.jruby.RubyEncoding;
import org.jruby.RubyString;
import org.jruby.RubyTime;
import org.jruby.lexer.StrftimeLexer;
Expand All @@ -66,7 +65,7 @@ public class RubyDateFormatter {
private ThreadContext context;
private StrftimeLexer lexer;

static enum Format {
enum Format {
/** encoding to give to output */
FORMAT_ENCODING,
/** raw string, no formatting */
Expand Down Expand Up @@ -228,6 +227,10 @@ public String toString() {
public RubyDateFormatter(ThreadContext context) {
super();
this.context = context;
// FIXME: Java does not seem to initialize the enum values until the first one is accessed but during
// construction of enum values we set the enum values into a static array. We look at that static array
// before we formally reference a Format. The following line will work around this by forcing enum loading...someone change this code :P
Format staticInitializerHack = Format.FORMAT_SPECIAL;
lexer = new StrftimeLexer((Reader) null);
}

Expand Down

0 comments on commit 9d6f877

Please sign in to comment.