Configured build for Ubuntu and added Stempel polish analyzer
[pylucene.git] / jcc / _jcc / java / lang / reflect / Method.cpp
1 /*
2  *   Licensed under the Apache License, Version 2.0 (the "License");
3  *   you may not use this file except in compliance with the License.
4  *   You may obtain a copy of the License at
5  *
6  *       http://www.apache.org/licenses/LICENSE-2.0
7  *
8  *   Unless required by applicable law or agreed to in writing, software
9  *   distributed under the License is distributed on an "AS IS" BASIS,
10  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  *   See the License for the specific language governing permissions and
12  *   limitations under the License.
13  */
14
15 #include <jni.h>
16 #include "JCCEnv.h"
17 #include "JArray.h"
18
19 #include "java/lang/Class.h"
20 #include "java/lang/Object.h"
21 #include "java/lang/String.h"
22 #include "java/lang/reflect/Method.h"
23 #ifdef _java_generics
24 #include "java/lang/reflect/Type.h"
25 #include "java/lang/reflect/TypeVariable.h"
26 #endif
27
28 namespace java {
29     namespace lang {
30         namespace reflect {
31
32             enum {
33                 mid_getModifiers,
34                 mid_getReturnType,
35                 mid_getName,
36                 mid_getParameterTypes,
37                 mid_getExceptionTypes,
38                 mid_getDeclaringClass,
39 #ifdef _java_generics
40                 mid_getTypeParameters,
41                 mid_getGenericExceptionTypes,
42                 mid_getGenericParameterTypes,
43                 mid_getGenericReturnType,
44 #endif
45                 max_mid
46             };
47
48             Class *Method::class$ = NULL;
49             jmethodID *Method::_mids = NULL;
50
51             jclass Method::initializeClass()
52             {
53                 if (!class$)
54                 {
55                     jclass cls = env->findClass("java/lang/reflect/Method");
56
57                     _mids = new jmethodID[max_mid];
58                     _mids[mid_getModifiers] =
59                         env->getMethodID(cls, "getModifiers",
60                                          "()I");
61                     _mids[mid_getReturnType] =
62                         env->getMethodID(cls, "getReturnType",
63                                          "()Ljava/lang/Class;");
64                     _mids[mid_getName] =
65                         env->getMethodID(cls, "getName",
66                                          "()Ljava/lang/String;");
67
68                     _mids[mid_getParameterTypes] =
69                         env->getMethodID(cls, "getParameterTypes",
70                                          "()[Ljava/lang/Class;");
71                     _mids[mid_getExceptionTypes] =
72                         env->getMethodID(cls, "getExceptionTypes",
73                                          "()[Ljava/lang/Class;");
74                     _mids[mid_getDeclaringClass] =
75                         env->getMethodID(cls, "getDeclaringClass",
76                                          "()Ljava/lang/Class;");
77 #ifdef _java_generics
78                     _mids[mid_getTypeParameters] =
79                         env->getMethodID(cls, "getTypeParameters",
80                                          "()[Ljava/lang/reflect/TypeVariable;");
81                     _mids[mid_getGenericExceptionTypes] =
82                         env->getMethodID(cls, "getGenericExceptionTypes",
83                                          "()[Ljava/lang/reflect/Type;");
84                     _mids[mid_getGenericParameterTypes] =
85                         env->getMethodID(cls, "getGenericParameterTypes",
86                                          "()[Ljava/lang/reflect/Type;");
87                     _mids[mid_getGenericReturnType] =
88                         env->getMethodID(cls, "getGenericReturnType",
89                                          "()Ljava/lang/reflect/Type;");
90 #endif
91
92                     class$ = (Class *) new JObject(cls);
93                 }
94                 
95                 return (jclass) class$->this$;
96             }
97
98             int Method::getModifiers() const
99             {
100                 return env->callIntMethod(this$, _mids[mid_getModifiers]);
101             }
102
103             Class Method::getReturnType() const
104             {
105                 return Class(env->callObjectMethod(this$, _mids[mid_getReturnType]));
106             }
107
108             String Method::getName() const
109             {
110                 return String(env->callObjectMethod(this$, _mids[mid_getName]));
111             }
112
113             JArray<Class> Method::getParameterTypes() const
114             {
115                 jobjectArray array = (jobjectArray)
116                     env->callObjectMethod(this$, _mids[mid_getParameterTypes]);
117
118                 return JArray<Class>(array);
119             }
120
121             JArray<Class> Method::getExceptionTypes() const
122             {
123                 jobjectArray array = (jobjectArray)
124                     env->callObjectMethod(this$, _mids[mid_getExceptionTypes]);
125
126                 return JArray<Class>(array);
127             }
128
129             Class Method::getDeclaringClass() const
130             {
131                 return Class(env->callObjectMethod(this$, _mids[mid_getDeclaringClass]));
132             }
133
134 #ifdef _java_generics
135             JArray<TypeVariable> Method::getTypeParameters() const
136             {
137                 return JArray<TypeVariable>(env->callObjectMethod(this$, _mids[mid_getTypeParameters]));
138             }
139
140             JArray<Type> Method::getGenericExceptionTypes() const
141             {
142                 return JArray<Type>(env->callObjectMethod(this$, _mids[mid_getGenericExceptionTypes]));
143             }
144
145             JArray<Type> Method::getGenericParameterTypes() const
146             {
147                 return JArray<Type>(env->callObjectMethod(this$, _mids[mid_getGenericParameterTypes]));
148             }
149
150             Type Method::getGenericReturnType() const
151             {
152                 return Type(env->callObjectMethod(this$, _mids[mid_getGenericReturnType]));
153             }
154 #endif
155         }
156     }
157 }
158
159
160 #include "structmember.h"
161 #include "functions.h"
162 #include "macros.h"
163
164 namespace java {
165     namespace lang {
166         namespace reflect {
167
168             static PyObject *t_Method_cast_(PyTypeObject *type, PyObject *arg);
169             static PyObject *t_Method_instance_(PyTypeObject *type, PyObject *arg);
170             static PyObject *t_Method_getModifiers(t_Method *self);
171             static PyObject *t_Method_getReturnType(t_Method *self);
172             static PyObject *t_Method_getName(t_Method *self);
173             static PyObject *t_Method_getParameterTypes(t_Method *self);
174             static PyObject *t_Method_getExceptionTypes(t_Method *self);
175             static PyObject *t_Method_getDeclaringClass(t_Method *self);
176 #ifdef _java_generics
177             static PyObject *t_Method_getTypeParameters(t_Method *self);
178             static PyObject *t_Method_getGenericExceptionTypes(t_Method *self);
179             static PyObject *t_Method_getGenericParameterTypes(t_Method *self);
180             static PyObject *t_Method_getGenericReturnType(t_Method *self);
181 #endif
182
183             static PyMethodDef t_Method__methods_[] = {
184                 DECLARE_METHOD(t_Method, cast_, METH_O | METH_CLASS),
185                 DECLARE_METHOD(t_Method, instance_, METH_O | METH_CLASS),
186                 DECLARE_METHOD(t_Method, getModifiers, METH_NOARGS),
187                 DECLARE_METHOD(t_Method, getReturnType, METH_NOARGS),
188                 DECLARE_METHOD(t_Method, getName, METH_NOARGS),
189                 DECLARE_METHOD(t_Method, getParameterTypes, METH_NOARGS),
190                 DECLARE_METHOD(t_Method, getExceptionTypes, METH_NOARGS),
191                 DECLARE_METHOD(t_Method, getDeclaringClass, METH_NOARGS),
192 #ifdef _java_generics
193                 DECLARE_METHOD(t_Method, getTypeParameters, METH_NOARGS),
194                 DECLARE_METHOD(t_Method, getGenericExceptionTypes, METH_NOARGS),
195                 DECLARE_METHOD(t_Method, getGenericParameterTypes, METH_NOARGS),
196                 DECLARE_METHOD(t_Method, getGenericReturnType, METH_NOARGS),
197 #endif
198                 { NULL, NULL, 0, NULL }
199             };
200
201             DECLARE_TYPE(Method, t_Method, Object, Method,
202                          abstract_init, 0, 0, 0, 0, 0);
203
204             static PyObject *t_Method_cast_(PyTypeObject *type, PyObject *arg)
205             {
206                 if (!(arg = castCheck(arg, Method::initializeClass, 1)))
207                     return NULL;
208                 return t_Method::wrap_Object(Method(((t_Method *) arg)->object.this$));
209             }
210
211             static PyObject *t_Method_instance_(PyTypeObject *type, PyObject *arg)
212             {
213                 if (!castCheck(arg, Method::initializeClass, 0))
214                     Py_RETURN_FALSE;
215                 Py_RETURN_TRUE;
216             }
217
218             static PyObject *t_Method_getModifiers(t_Method *self)
219             {
220                 jint modifiers;
221
222                 OBJ_CALL(modifiers = self->object.getModifiers());
223                 return PyInt_FromLong(modifiers);
224             }
225
226             static PyObject *t_Method_getReturnType(t_Method *self)
227             {
228                 Class cls((jobject) NULL);
229
230                 OBJ_CALL(cls = self->object.getReturnType());
231                 return t_Class::wrap_Object(cls);
232             }
233
234             static PyObject *t_Method_getName(t_Method *self)
235             {
236                 String name((jobject) NULL);
237
238                 OBJ_CALL(name = self->object.getName());
239                 return j2p(name);
240             }
241
242             static PyObject *t_Method_getParameterTypes(t_Method *self)
243             {
244                 JArray<Class> types((jobject) NULL);
245
246                 OBJ_CALL(types = self->object.getParameterTypes());
247                 return types.toSequence(t_Class::wrap_Object);
248             }
249
250             static PyObject *t_Method_getExceptionTypes(t_Method *self)
251             {
252                 JArray<Class> types((jobject) NULL);
253
254                 OBJ_CALL(types = self->object.getExceptionTypes());
255                 return types.toSequence(t_Class::wrap_Object);
256             }
257
258             static PyObject *t_Method_getDeclaringClass(t_Method *self)
259             {
260                 Class cls((jobject) NULL);
261
262                 OBJ_CALL(cls = self->object.getDeclaringClass());
263                 return t_Class::wrap_Object(cls);
264             }
265
266 #ifdef _java_generics
267             static PyObject *t_Method_getTypeParameters(t_Method *self)
268             {
269                 JArray<TypeVariable> result((jobject) NULL);
270                 OBJ_CALL(result = self->object.getTypeParameters());
271
272                 return result.toSequence(t_TypeVariable::wrap_Object);
273             }
274
275             static PyObject *t_Method_getGenericExceptionTypes(t_Method *self)
276             {
277                 JArray<Type> result((jobject) NULL);
278                 OBJ_CALL(result = self->object.getGenericExceptionTypes());
279
280                 return result.toSequence(t_Type::wrap_Object);
281             }
282
283             static PyObject *t_Method_getGenericParameterTypes(t_Method *self)
284             {
285                 JArray<Type> result((jobject) NULL);
286                 OBJ_CALL(result = self->object.getGenericParameterTypes());
287
288                 return result.toSequence(t_Type::wrap_Object);
289             }
290
291             static PyObject *t_Method_getGenericReturnType(t_Method *self)
292             {
293                 Type result((jobject) NULL);
294                 OBJ_CALL(result = self->object.getGenericReturnType());
295
296                 return t_Type::wrap_Object(result);
297             }
298 #endif
299         }
300     }
301 }