Fix for JRUBY-2882. Handle error messages related to constructors better
[jruby.git] / src / org / jruby / javasupport / JavaCallable.java
blob8d4be85761f79ddbf3977942e61f05b28395e021
1 /***** BEGIN LICENSE BLOCK *****
2 * Version: CPL 1.0/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Common Public
5 * License Version 1.0 (the "License"); you may not use this file
6 * except in compliance with the License. You may obtain a copy of
7 * the License at http://www.eclipse.org/legal/cpl-v10.html
9 * Software distributed under the License is distributed on an "AS
10 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11 * implied. See the License for the specific language governing
12 * rights and limitations under the License.
14 * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
15 * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
16 * Copyright (C) 2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
17 * Copyright (C) 2004 David Corbin <dcorbin@users.sourceforge.net>
19 * Alternatively, the contents of this file may be used under the terms of
20 * either of the GNU General Public License Version 2 or later (the "GPL"),
21 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22 * in which case the provisions of the GPL or the LGPL are applicable instead
23 * of those above. If you wish to allow use of your version of this file only
24 * under the terms of either the GPL or the LGPL, and not to allow others to
25 * use your version of this file under the terms of the CPL, indicate your
26 * decision by deleting the provisions above and replace them with the notice
27 * and other provisions required by the GPL or the LGPL. If you do not delete
28 * the provisions above, a recipient may use your version of this file under
29 * the terms of any one of the CPL, the GPL or the LGPL.
30 ***** END LICENSE BLOCK *****/
31 package org.jruby.javasupport;
33 import java.lang.annotation.Annotation;
34 import java.lang.reflect.Modifier;
35 import java.lang.reflect.Type;
37 import org.jruby.Ruby;
38 import org.jruby.RubyArray;
39 import org.jruby.RubyBoolean;
40 import org.jruby.RubyClass;
41 import org.jruby.RubyFixnum;
42 import org.jruby.RubyString;
43 import org.jruby.anno.JRubyMethod;
44 import org.jruby.runtime.builtin.IRubyObject;
46 public abstract class JavaCallable extends JavaAccessibleObject implements ParameterTypes {
48 public JavaCallable(Ruby runtime, RubyClass rubyClass) {
49 super(runtime, rubyClass);
52 public static void registerRubyMethods(Ruby runtime, RubyClass result) {
53 result.defineAnnotatedMethods(JavaCallable.class);
56 public abstract int getArity();
57 public abstract int getModifiers();
58 public abstract Class<?>[] getParameterTypes();
59 public abstract Class<?>[] getExceptionTypes();
60 public abstract Type[] getGenericExceptionTypes();
61 public abstract Type[] getGenericParameterTypes();
62 public abstract Annotation[][] getParameterAnnotations();
63 public abstract boolean isVarArgs();
64 public abstract String toGenericString();
65 /**
66 * @return the name used in the head of the string returned from inspect()
68 protected abstract String nameOnInspection();
70 @JRubyMethod
71 public final RubyFixnum arity() {
72 return getRuntime().newFixnum(getArity());
75 @JRubyMethod
76 public final RubyArray argument_types() {
77 return JavaClass.getRubyArray(getRuntime(), getParameterTypes());
80 // same as argument_types, but matches name in java.lang.reflect.Constructor/Method
81 @JRubyMethod
82 public IRubyObject parameter_types() {
83 return JavaClass.getRubyArray(getRuntime(), getParameterTypes());
86 @JRubyMethod
87 public IRubyObject exception_types() {
88 return JavaClass.getRubyArray(getRuntime(), getExceptionTypes());
91 @JRubyMethod
92 public IRubyObject generic_parameter_types() {
93 return Java.getInstance(getRuntime(), getGenericParameterTypes());
96 @JRubyMethod
97 public IRubyObject generic_exception_types() {
98 return Java.getInstance(getRuntime(), getGenericExceptionTypes());
101 @JRubyMethod
102 public IRubyObject parameter_annotations() {
103 return Java.getInstance(getRuntime(), getParameterAnnotations());
106 @JRubyMethod(name = "varargs?")
107 public RubyBoolean varargs_p() {
108 return getRuntime().newBoolean(isVarArgs());
111 @JRubyMethod
112 public RubyString to_generic_string() {
113 return getRuntime().newString(toGenericString());
116 @JRubyMethod
117 public IRubyObject inspect() {
118 StringBuilder result = new StringBuilder();
119 result.append(nameOnInspection());
120 Class<?>[] parameterTypes = getParameterTypes();
121 for (int i = 0; i < parameterTypes.length; i++) {
122 result.append(parameterTypes[i].getName());
123 if (i < parameterTypes.length - 1) {
124 result.append(',');
127 result.append(")>");
128 return getRuntime().newString(result.toString());
132 @JRubyMethod(name = "public?")
133 public RubyBoolean public_p() {
134 return RubyBoolean.newBoolean(getRuntime(), Modifier.isPublic(getModifiers()));