001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2006 Anders Bengtsson <ndrsbngtssn@yahoo.se>
015: *
016: * Alternatively, the contents of this file may be used under the terms of
017: * either of the GNU General Public License Version 2 or later (the "GPL"),
018: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
019: * in which case the provisions of the GPL or the LGPL are applicable instead
020: * of those above. If you wish to allow use of your version of this file only
021: * under the terms of either the GPL or the LGPL, and not to allow others to
022: * use your version of this file under the terms of the CPL, indicate your
023: * decision by deleting the provisions above and replace them with the notice
024: * and other provisions required by the GPL or the LGPL. If you do not delete
025: * the provisions above, a recipient may use your version of this file under
026: * the terms of any one of the CPL, the GPL or the LGPL.
027: ***** END LICENSE BLOCK *****/package org.jruby;
028:
029: import org.jruby.runtime.Block;
030: import org.jruby.runtime.ObjectAllocator;
031: import org.jruby.runtime.builtin.IRubyObject;
032: import org.jruby.runtime.CallbackFactory;
033:
034: /**
035: * @author Anders Bengtsson
036: */
037: public class RubyNameError extends RubyException {
038: private IRubyObject name;
039:
040: private static ObjectAllocator NAMEERROR_ALLOCATOR = new ObjectAllocator() {
041: public IRubyObject allocate(Ruby runtime, RubyClass klass) {
042: return new RubyNameError(runtime, klass);
043: }
044: };
045:
046: public static RubyClass createNameErrorClass(Ruby runtime,
047: RubyClass standardErrorClass) {
048: RubyClass nameErrorClass = runtime.defineClass("NameError",
049: standardErrorClass, NAMEERROR_ALLOCATOR);
050: CallbackFactory callbackFactory = runtime
051: .callbackFactory(RubyNameError.class);
052:
053: nameErrorClass.getMetaClass().defineFastMethod(
054: "exception",
055: callbackFactory
056: .getFastOptSingletonMethod("newRubyNameError"));
057:
058: nameErrorClass.defineMethod("initialize", callbackFactory
059: .getOptMethod("initialize"));
060: nameErrorClass.defineFastMethod("to_s", callbackFactory
061: .getFastMethod("to_s"));
062: nameErrorClass.defineFastMethod("name", callbackFactory
063: .getFastMethod("name"));
064:
065: return nameErrorClass;
066: }
067:
068: protected RubyNameError(Ruby runtime, RubyClass exceptionClass) {
069: this (runtime, exceptionClass, exceptionClass.getName()
070: .toString());
071: }
072:
073: public RubyNameError(Ruby runtime, RubyClass exceptionClass,
074: String message) {
075: this (runtime, exceptionClass, message, null);
076: }
077:
078: public RubyNameError(Ruby runtime, RubyClass exceptionClass,
079: String message, String name) {
080: super (runtime, exceptionClass, message);
081: this .name = name == null ? runtime.getNil() : runtime
082: .newString(name);
083: }
084:
085: public static RubyException newRubyNameError(IRubyObject recv,
086: IRubyObject[] args) {
087: RubyClass klass = (RubyClass) recv;
088:
089: RubyException newError = (RubyException) klass.allocate();
090:
091: newError.callInit(args, Block.NULL_BLOCK);
092:
093: return newError;
094: }
095:
096: public IRubyObject initialize(IRubyObject[] args, Block block) {
097: if (args.length > 1) {
098: name = args[args.length - 1];
099: int newLength = args.length > 2 ? args.length - 2
100: : args.length - 1;
101:
102: IRubyObject[] tmpArgs = new IRubyObject[newLength];
103: System.arraycopy(args, 0, tmpArgs, 0, newLength);
104: args = tmpArgs;
105: } else {
106: name = getRuntime().getNil();
107: }
108:
109: super .initialize(args, block);
110: return this ;
111: }
112:
113: public IRubyObject to_s() {
114: if (message.isNil())
115: return getRuntime().newString(
116: message.getMetaClass().getName());
117: RubyString str = message.convertToString();
118: if (str != message)
119: message = str;
120: if (isTaint())
121: message.setTaint(true);
122: return message;
123: }
124:
125: public IRubyObject name() {
126: return name;
127: }
128: }
|