|
27 | 27 | package org.jruby; |
28 | 28 |
|
29 | 29 | import org.jruby.anno.JRubyClass; |
| 30 | +import org.jruby.anno.JRubyMethod; |
| 31 | +import org.jruby.ast.util.ArgsUtil; |
30 | 32 | import org.jruby.exceptions.RaiseException; |
31 | 33 | import org.jruby.exceptions.FrozenError; |
| 34 | +import org.jruby.runtime.ThreadContext; |
| 35 | +import org.jruby.runtime.Visibility; |
| 36 | +import org.jruby.runtime.builtin.IRubyObject; |
32 | 37 |
|
33 | 38 | /** |
34 | 39 | * The Java representation of a Ruby FrozenError. |
|
37 | 42 | */ |
38 | 43 | @JRubyClass(name="FrozenError", parent="RuntimeError") |
39 | 44 | public class RubyFrozenError extends RubyRuntimeError { |
| 45 | + private IRubyObject receiver; |
| 46 | + |
40 | 47 | protected RubyFrozenError(Ruby runtime, RubyClass exceptionClass) { |
41 | 48 | super(runtime, exceptionClass); |
42 | 49 | } |
43 | 50 |
|
44 | 51 | static RubyClass define(Ruby runtime, RubyClass exceptionClass) { |
45 | 52 | RubyClass frozenErrorClass = runtime.defineClass("FrozenError", exceptionClass, RubyFrozenError::new); |
46 | 53 |
|
| 54 | + frozenErrorClass.defineAnnotatedMethods(RubyFrozenError.class); |
| 55 | + |
47 | 56 | return frozenErrorClass; |
48 | 57 | } |
49 | 58 |
|
| 59 | + public static RubyFrozenError newFrozenError(ThreadContext context, IRubyObject message, IRubyObject receiver) { |
| 60 | + Ruby runtime = context.runtime; |
| 61 | + |
| 62 | + RubyFrozenError rfe = new RubyFrozenError(runtime, runtime.getFrozenError()); |
| 63 | + |
| 64 | + rfe.initializeCommon(context, message, receiver); |
| 65 | + |
| 66 | + return rfe; |
| 67 | + } |
| 68 | + |
50 | 69 | protected RaiseException constructThrowable(String message) { |
51 | 70 | return new FrozenError(message, this); |
52 | 71 | } |
| 72 | + |
| 73 | + @JRubyMethod |
| 74 | + public IRubyObject initialize(ThreadContext context) { |
| 75 | + return context.nil; |
| 76 | + } |
| 77 | + |
| 78 | + @JRubyMethod |
| 79 | + public IRubyObject initialize(ThreadContext context, IRubyObject messageOrKwargs) { |
| 80 | + IRubyObject receiver = ArgsUtil.extractKeywordArg(context, messageOrKwargs, "receiver"); |
| 81 | + |
| 82 | + if (receiver == null) return initialize(context, messageOrKwargs); |
| 83 | + |
| 84 | + return super.initialize(context, null, message); |
| 85 | + } |
| 86 | + |
| 87 | + @JRubyMethod |
| 88 | + public IRubyObject initialize(ThreadContext context, IRubyObject message, IRubyObject kwargs) { |
| 89 | + IRubyObject receiver = ArgsUtil.extractKeywordArg(context, kwargs, "receiver"); |
| 90 | + |
| 91 | + return initializeCommon(context, message, receiver); |
| 92 | + } |
| 93 | + |
| 94 | + IRubyObject initializeCommon(ThreadContext context, IRubyObject message, IRubyObject receiver) { |
| 95 | + this.receiver = receiver; |
| 96 | + |
| 97 | + if (message == null) { |
| 98 | + return super.initialize(context); |
| 99 | + } |
| 100 | + |
| 101 | + return super.initialize(context, message); |
| 102 | + } |
| 103 | + |
| 104 | + @JRubyMethod |
| 105 | + public IRubyObject receiver(ThreadContext context) { |
| 106 | + IRubyObject receiver = this.receiver; |
| 107 | + |
| 108 | + if (receiver == null) return context.nil; |
| 109 | + |
| 110 | + return receiver; |
| 111 | + } |
53 | 112 | } |
0 commit comments