Configured build for Ubuntu and added Stempel polish analyzer
[pylucene.git] / jcc / _jcc / java / lang / Class.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
18 #include "JArray.h"
19 #include "java/lang/Object.h"
20 #include "java/lang/Class.h"
21 #include "java/lang/String.h"
22 #include "java/lang/reflect/Method.h"
23 #include "java/lang/reflect/Constructor.h"
24 #include "java/lang/reflect/Field.h"
25 #ifdef _java_generics
26 #include "java/lang/reflect/Type.h"
27 #include "java/lang/reflect/TypeVariable.h"
28 #endif
29
30 namespace java {
31     namespace lang {
32         using namespace reflect;
33
34         enum {
35             mid_forName,
36             mid_getDeclaredMethods,
37             mid_getMethods,
38             mid_getMethod,
39             mid_getDeclaredMethod,
40             mid_getDeclaredConstructors,
41             mid_getDeclaredFields,
42             mid_getDeclaredClasses,
43             mid_isArray,
44             mid_isPrimitive,
45             mid_isInterface,
46             mid_isAssignableFrom,
47             mid_getComponentType,
48             mid_getSuperclass,
49             mid_getDeclaringClass,
50             mid_getEnclosingClass,
51             mid_getInterfaces,
52             mid_getName,
53             mid_getModifiers,
54             mid_isInstance,
55 #ifdef _java_generics
56             mid_getTypeParameters,
57             mid_getGenericInterfaces,
58             mid_getGenericSuperclass,
59 #endif
60             max_mid
61         };
62
63         Class *Class::class$ = NULL;
64         jmethodID *Class::_mids = NULL;
65
66         jclass Class::initializeClass()
67         {
68             if (!class$)
69             {
70                 jclass cls = env->findClass("java/lang/Class");
71
72                 _mids = new jmethodID[max_mid];
73                 _mids[mid_forName] =
74                     env->getStaticMethodID(cls, "forName",
75                                            "(Ljava/lang/String;)Ljava/lang/Class;");
76                 _mids[mid_getDeclaredMethods] =
77                     env->getMethodID(cls, "getDeclaredMethods",
78                                      "()[Ljava/lang/reflect/Method;");
79                 _mids[mid_getMethods] =
80                     env->getMethodID(cls, "getMethods",
81                                      "()[Ljava/lang/reflect/Method;");
82                 _mids[mid_getMethod] =
83                     env->getMethodID(cls, "getMethod",
84                                      "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");
85                 _mids[mid_getDeclaredMethod] =
86                     env->getMethodID(cls, "getDeclaredMethod",
87                                      "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");
88                 _mids[mid_getDeclaredConstructors] =
89                     env->getMethodID(cls, "getDeclaredConstructors",
90                                      "()[Ljava/lang/reflect/Constructor;");
91                 _mids[mid_getDeclaredFields] =
92                     env->getMethodID(cls, "getDeclaredFields",
93                                      "()[Ljava/lang/reflect/Field;");
94                 _mids[mid_getDeclaredClasses] =
95                     env->getMethodID(cls, "getDeclaredClasses",
96                                      "()[Ljava/lang/Class;");
97                 _mids[mid_isArray] =
98                     env->getMethodID(cls, "isArray",
99                                      "()Z");
100                 _mids[mid_isPrimitive] =
101                     env->getMethodID(cls, "isPrimitive",
102                                      "()Z");
103                 _mids[mid_isInterface] =
104                     env->getMethodID(cls, "isInterface",
105                                      "()Z");
106                 _mids[mid_isAssignableFrom] =
107                     env->getMethodID(cls, "isAssignableFrom",
108                                      "(Ljava/lang/Class;)Z");
109                 _mids[mid_getComponentType] =
110                     env->getMethodID(cls, "getComponentType",
111                                      "()Ljava/lang/Class;");
112                 _mids[mid_getSuperclass] =
113                     env->getMethodID(cls, "getSuperclass",
114                                      "()Ljava/lang/Class;");
115                 _mids[mid_getDeclaringClass] =
116                     env->getMethodID(cls, "getDeclaringClass",
117                                      "()Ljava/lang/Class;");
118                 _mids[mid_getEnclosingClass] =
119                     env->getMethodID(cls, "getEnclosingClass",
120                                      "()Ljava/lang/Class;");
121                 _mids[mid_getInterfaces] =
122                     env->getMethodID(cls, "getInterfaces",
123                                      "()[Ljava/lang/Class;");
124                 _mids[mid_getName] =
125                     env->getMethodID(cls, "getName",
126                                      "()Ljava/lang/String;");
127                 _mids[mid_getModifiers] =
128                     env->getMethodID(cls, "getModifiers",
129                                      "()I");
130                 _mids[mid_isInstance] =
131                     env->getMethodID(cls, "isInstance",
132                                      "(Ljava/lang/Object;)Z");
133 #ifdef _java_generics
134                 _mids[mid_getTypeParameters] =
135                     env->getMethodID(cls, "getTypeParameters",
136                                      "()[Ljava/lang/reflect/TypeVariable;");
137                 _mids[mid_getGenericInterfaces] =
138                     env->getMethodID(cls, "getGenericInterfaces",
139                                      "()[Ljava/lang/reflect/Type;");
140                 _mids[mid_getGenericSuperclass] =
141                     env->getMethodID(cls, "getGenericSuperclass",
142                                      "()Ljava/lang/reflect/Type;");
143 #endif
144                 class$ = (Class *) new JObject(cls);
145             }
146
147             return (jclass) class$->this$;
148         }
149
150
151         Class Class::forName(const String& className)
152         {
153             jclass cls = initializeClass();
154             jobject obj = env->callStaticObjectMethod(cls, _mids[mid_forName], className.this$);
155
156             return Class((jclass) obj);
157         }
158
159         JArray<Method> Class::getDeclaredMethods() const
160         {
161             jobjectArray array = (jobjectArray)
162                 env->callObjectMethod(this$, _mids[mid_getDeclaredMethods]);
163
164             return JArray<Method>(array);
165         }
166
167         JArray<Method> Class::getMethods() const
168         {
169             jobjectArray array = (jobjectArray)
170                 env->callObjectMethod(this$, _mids[mid_getMethods]);
171
172             return JArray<Method>(array);
173         }
174
175         Method Class::getMethod(const String& name, const JArray<Class>& params) const
176         {
177             return Method(env->callObjectMethod(this$, _mids[mid_getMethod], name.this$, params.this$));
178         }
179
180         Method Class::getDeclaredMethod(const String& name, const JArray<Class>& params) const
181         {
182             return Method(env->callObjectMethod(this$, _mids[mid_getDeclaredMethod], name.this$, params.this$));
183         }
184
185         JArray<Constructor> Class::getDeclaredConstructors() const
186         {
187             jobjectArray array = (jobjectArray)
188                 env->callObjectMethod(this$, _mids[mid_getDeclaredConstructors]);
189
190             return JArray<Constructor>(array);
191         }
192
193         JArray<Field> Class::getDeclaredFields() const
194         {
195             jobjectArray array = (jobjectArray)
196                 env->callObjectMethod(this$, _mids[mid_getDeclaredFields]);
197
198             return JArray<Field>(array);
199         }
200
201         JArray<Class> Class::getDeclaredClasses() const
202         {
203             jobjectArray array = (jobjectArray)
204                 env->callObjectMethod(this$, _mids[mid_getDeclaredClasses]);
205
206             return JArray<Class>(array);
207         }
208
209         int Class::isArray() const
210         {
211             return (int) env->callBooleanMethod(this$, _mids[mid_isArray]);
212         }
213
214         int Class::isPrimitive() const
215         {
216             return (int) env->callBooleanMethod(this$, _mids[mid_isPrimitive]);
217         }
218
219         int Class::isInterface() const
220         {
221             return (int) env->callBooleanMethod(this$, _mids[mid_isInterface]);
222         }
223
224         int Class::isAssignableFrom(const Class& obj) const
225         {
226             return (int) env->callBooleanMethod(this$, _mids[mid_isAssignableFrom], obj.this$);
227         }
228
229         Class Class::getComponentType() const
230         {
231             return Class(env->callObjectMethod(this$, _mids[mid_getComponentType]));
232         }
233
234         Class Class::getSuperclass() const
235         {
236             return Class(env->callObjectMethod(this$, _mids[mid_getSuperclass]));
237         }
238
239         Class Class::getDeclaringClass() const
240         {
241             return Class(env->callObjectMethod(this$, _mids[mid_getDeclaringClass]));
242         }
243
244         Class Class::getEnclosingClass() const
245         {
246             return Class(env->callObjectMethod(this$, _mids[mid_getEnclosingClass]));
247         }
248
249         JArray<Class> Class::getInterfaces() const
250         {
251             jobjectArray array = (jobjectArray)
252                 env->callObjectMethod(this$, _mids[mid_getInterfaces]);
253
254             return JArray<Class>(array);
255         }
256
257         String Class::getName() const
258         {
259             return String(env->callObjectMethod(this$, _mids[mid_getName]));
260         }
261
262         int Class::getModifiers() const
263         {
264             return env->callIntMethod(this$, _mids[mid_getModifiers]);
265         }
266
267         int Class::isInstance(const Object &obj) const
268         {
269             return env->callBooleanMethod(this$, _mids[mid_isInstance],
270                                           obj.this$);
271         }
272
273 #ifdef _java_generics
274         JArray<TypeVariable> Class::getTypeParameters() const
275         {
276             return JArray<TypeVariable>(env->callObjectMethod(this$, _mids[mid_getTypeParameters]));
277         }
278
279         JArray<Type> Class::getGenericInterfaces() const
280         {
281             return JArray<Type>(env->callObjectMethod(this$, _mids[mid_getGenericInterfaces]));
282         }
283
284         Type Class::getGenericSuperclass() const
285         {
286             return Type(env->callObjectMethod(this$, _mids[mid_getGenericSuperclass]));
287         }
288 #endif
289     }
290 }
291
292
293 #include "structmember.h"
294 #include "functions.h"
295 #include "macros.h"
296
297 namespace java {
298     namespace lang {
299         using namespace reflect;
300
301         static PyObject *t_Class_cast_(PyTypeObject *type, PyObject *arg);
302         static PyObject *t_Class_instance_(PyTypeObject *type, PyObject *arg);
303         static PyObject *t_Class_forName(PyTypeObject *type, PyObject *arg);
304         static PyObject *t_Class_getDeclaredConstructors(t_Class *self);
305         static PyObject *t_Class_getDeclaredMethods(t_Class *self);
306         static PyObject *t_Class_getMethods(t_Class *self);
307         static PyObject *t_Class_getMethod(t_Class *self, PyObject *args);
308         static PyObject *t_Class_getDeclaredMethod(t_Class *self, PyObject *args);
309         static PyObject *t_Class_getDeclaredFields(t_Class *self);
310         static PyObject *t_Class_getDeclaredClasses(t_Class *self);
311         static PyObject *t_Class_isArray(t_Class *self);
312         static PyObject *t_Class_isPrimitive(t_Class *self);
313         static PyObject *t_Class_isInterface(t_Class *self);
314         static PyObject *t_Class_isAssignableFrom(t_Class *self, PyObject *arg);
315         static PyObject *t_Class_getComponentType(t_Class *self);
316         static PyObject *t_Class_getSuperclass(t_Class *self);
317         static PyObject *t_Class_getDeclaringClass(t_Class *self);
318         static PyObject *t_Class_getEnclosingClass(t_Class *self);
319         static PyObject *t_Class_getInterfaces(t_Class *self);
320         static PyObject *t_Class_getName(t_Class *self);
321         static PyObject *t_Class_getModifiers(t_Class *self);
322 #ifdef _java_generics
323         static PyObject *t_Class_getTypeParameters(t_Class *self);
324         static PyObject *t_Class_getGenericInterfaces(t_Class *self);
325         static PyObject *t_Class_getGenericSuperclass(t_Class *self);
326         static PyObject *t_Class_get__parameters_(t_Class *self, void *data);
327
328         static PyGetSetDef t_Class__fields_[] = {
329             DECLARE_GET_FIELD(t_Class, parameters_),
330             { NULL, NULL, NULL, NULL, NULL }
331         };
332 #else
333         static PyGetSetDef t_Class__fields_[] = {
334             { NULL, NULL, NULL, NULL, NULL }
335         };
336 #endif
337
338         static PyMethodDef t_Class__methods_[] = {
339             DECLARE_METHOD(t_Class, cast_, METH_O | METH_CLASS),
340             DECLARE_METHOD(t_Class, instance_, METH_O | METH_CLASS),
341             DECLARE_METHOD(t_Class, forName, METH_O | METH_CLASS),
342             DECLARE_METHOD(t_Class, getDeclaredConstructors, METH_NOARGS),
343             DECLARE_METHOD(t_Class, getDeclaredMethods, METH_NOARGS),
344             DECLARE_METHOD(t_Class, getMethods, METH_NOARGS),
345             DECLARE_METHOD(t_Class, getMethod, METH_VARARGS),
346             DECLARE_METHOD(t_Class, getDeclaredMethod, METH_VARARGS),
347             DECLARE_METHOD(t_Class, getDeclaredFields, METH_NOARGS),
348             DECLARE_METHOD(t_Class, getDeclaredClasses, METH_NOARGS),
349             DECLARE_METHOD(t_Class, isArray, METH_NOARGS),
350             DECLARE_METHOD(t_Class, isPrimitive, METH_NOARGS),
351             DECLARE_METHOD(t_Class, isInterface, METH_NOARGS),
352             DECLARE_METHOD(t_Class, isAssignableFrom, METH_O),
353             DECLARE_METHOD(t_Class, getComponentType, METH_NOARGS),
354             DECLARE_METHOD(t_Class, getSuperclass, METH_NOARGS),
355             DECLARE_METHOD(t_Class, getDeclaringClass, METH_NOARGS),
356             DECLARE_METHOD(t_Class, getEnclosingClass, METH_NOARGS),
357             DECLARE_METHOD(t_Class, getInterfaces, METH_NOARGS),
358             DECLARE_METHOD(t_Class, getName, METH_NOARGS),
359             DECLARE_METHOD(t_Class, getModifiers, METH_NOARGS),
360 #ifdef _java_generics
361             DECLARE_METHOD(t_Class, getTypeParameters, METH_NOARGS),
362             DECLARE_METHOD(t_Class, getGenericInterfaces, METH_NOARGS),
363             DECLARE_METHOD(t_Class, getGenericSuperclass, METH_NOARGS),
364 #endif
365             { NULL, NULL, 0, NULL }
366         };
367
368         DECLARE_TYPE(Class, t_Class, Object, java::lang::Class,
369                      abstract_init, 0, 0, t_Class__fields_, 0, 0);
370
371 #ifdef _java_generics
372         PyObject *t_Class::wrap_Object(const Class& object, PyTypeObject *T)
373         {
374             PyObject *obj = t_Class::wrap_Object(object);
375             if (obj != Py_None)
376             {
377                 t_Class *self = (t_Class *) obj;
378                 self->parameters[0] = T;
379             }
380             return obj;
381         }
382 #endif
383         static PyObject *t_Class_cast_(PyTypeObject *type, PyObject *arg)
384         {
385             if (!(arg = castCheck(arg, Class::initializeClass, 1)))
386                 return NULL;
387             return t_Class::wrap_Object(Class(((t_Class *) arg)->object.this$));
388         }
389         static PyObject *t_Class_instance_(PyTypeObject *type, PyObject *arg)
390         {
391             if (!castCheck(arg, Class::initializeClass, 0))
392                 Py_RETURN_FALSE;
393             Py_RETURN_TRUE;
394         }
395
396         static PyObject *t_Class_forName(PyTypeObject *type, PyObject *arg)
397         {
398             if (!PyString_Check(arg))
399             {
400                 PyErr_SetObject(PyExc_TypeError, arg);
401                 return NULL;
402             }
403
404             try {
405                 char *className = PyString_AsString(arg);
406                 String name = String(env->fromUTF(className));
407
408                 return t_Class::wrap_Object(Class::forName(name));
409             } catch (int e) {
410                 switch (e) {
411                   case _EXC_JAVA:
412                     return PyErr_SetJavaError();
413                   default:
414                     throw;
415                 }
416             }
417         }
418
419         static PyObject *t_Class_getDeclaredConstructors(t_Class *self)
420         {
421             JArray<Constructor> constructors((jobject) NULL);
422
423             OBJ_CALL(constructors = self->object.getDeclaredConstructors());
424             return constructors.toSequence(t_Constructor::wrap_Object);
425         }
426
427         static PyObject *t_Class_getDeclaredMethods(t_Class *self)
428         {
429             JArray<Method> methods((jobject) NULL);
430
431             OBJ_CALL(methods = self->object.getDeclaredMethods());
432             return methods.toSequence(t_Method::wrap_Object);
433         }
434
435         static PyObject *t_Class_getMethods(t_Class *self)
436         {
437             JArray<Method> methods((jobject) NULL);
438
439             OBJ_CALL(methods = self->object.getMethods());
440             return methods.toSequence(t_Method::wrap_Object);
441         }
442
443         static PyObject *t_Class_getMethod(t_Class *self, PyObject *args)
444         {
445             String name((jobject) NULL);
446             JArray<Class> params((jobject) NULL);
447             Method method((jobject) NULL);
448
449             if (!parseArgs(args, "s[j", Class::class$, &name, &params))
450             {
451                 OBJ_CALL(method = self->object.getMethod(name, params));
452                 return t_Method::wrap_Object(method);
453             }
454
455             return PyErr_SetArgsError((PyObject *) self, "getMethod", args);
456         }
457
458         static PyObject *t_Class_getDeclaredMethod(t_Class *self, PyObject *args)
459         {
460             String name((jobject) NULL);
461             JArray<Class> params((jobject) NULL);
462             Method method((jobject) NULL);
463
464             if (!parseArgs(args, "s[j", Class::class$, &name, &params))
465             {
466                 OBJ_CALL(method = self->object.getDeclaredMethod(name, params));
467                 return t_Method::wrap_Object(method);
468             }
469
470             return PyErr_SetArgsError((PyObject *) self, "getMethod", args);
471         }
472
473         static PyObject *t_Class_getDeclaredFields(t_Class *self)
474         {
475             JArray<Field> fields((jobject) NULL);
476
477             OBJ_CALL(fields = self->object.getDeclaredFields());
478             return fields.toSequence(t_Field::wrap_Object);
479         }
480
481         static PyObject *t_Class_getDeclaredClasses(t_Class *self)
482         {
483             JArray<Class> array((jobject) NULL);
484
485             OBJ_CALL(array = self->object.getDeclaredClasses());
486             return array.toSequence(t_Class::wrap_Object);
487         }
488
489         static PyObject *t_Class_isArray(t_Class *self)
490         {
491             int isArray;
492
493             OBJ_CALL(isArray = self->object.isArray());
494             Py_RETURN_BOOL(isArray);
495         }
496
497         static PyObject *t_Class_isPrimitive(t_Class *self)
498         {
499             int isPrimitive;
500
501             OBJ_CALL(isPrimitive = self->object.isPrimitive());
502             Py_RETURN_BOOL(isPrimitive);
503         }
504
505         static PyObject *t_Class_isInterface(t_Class *self)
506         {
507             int isInterface;
508
509             OBJ_CALL(isInterface = self->object.isInterface());
510             Py_RETURN_BOOL(isInterface);
511         }
512
513         static PyObject *t_Class_isAssignableFrom(t_Class *self, PyObject *arg)
514         {
515             if (!PyObject_TypeCheck(arg, &PY_TYPE(Class)))
516             {
517                 PyErr_SetObject(PyExc_TypeError, arg);
518                 return NULL;
519             }
520
521             try {
522                 Class cls = ((t_Class *) arg)->object;
523                 int isAssignableFrom = self->object.isAssignableFrom(cls);
524
525                 Py_RETURN_BOOL(isAssignableFrom);
526             } catch (int e) {
527                 switch (e) {
528                   case _EXC_JAVA:
529                     return PyErr_SetJavaError();
530                   default:
531                     throw;
532                 }
533             }
534         }
535
536         static PyObject *t_Class_getComponentType(t_Class *self)
537         {
538             Class cls((jobject) NULL);
539
540             OBJ_CALL(cls = self->object.getComponentType());
541             return t_Class::wrap_Object(cls);
542         }
543
544         static PyObject *t_Class_getSuperclass(t_Class *self)
545         {
546             Class cls((jobject) NULL);
547
548             OBJ_CALL(cls = self->object.getSuperclass());
549             return t_Class::wrap_Object(cls);
550         }
551
552         static PyObject *t_Class_getDeclaringClass(t_Class *self)
553         {
554             Class cls((jobject) NULL);
555
556             OBJ_CALL(cls = self->object.getDeclaringClass());
557             return t_Class::wrap_Object(cls);
558         }
559
560         static PyObject *t_Class_getEnclosingClass(t_Class *self)
561         {
562             Class cls((jobject) NULL);
563
564             OBJ_CALL(cls = self->object.getEnclosingClass());
565             return t_Class::wrap_Object(cls);
566         }
567
568         static PyObject *t_Class_getInterfaces(t_Class *self)
569         {
570             JArray<Class> interfaces((jobject) NULL);
571
572             OBJ_CALL(interfaces = self->object.getInterfaces());
573             return interfaces.toSequence(t_Class::wrap_Object);
574         }
575
576         static PyObject *t_Class_getName(t_Class *self)
577         {
578             String name((jobject) NULL);
579
580             OBJ_CALL(name = self->object.getName());
581             return j2p(name);
582         }
583
584         static PyObject *t_Class_getModifiers(t_Class *self)
585         {
586             jint modifiers;
587
588             OBJ_CALL(modifiers = self->object.getModifiers());
589             return PyInt_FromLong(modifiers);            
590         }
591
592 #ifdef _java_generics
593         static PyObject *t_Class_getTypeParameters(t_Class *self)
594         {
595             JArray<TypeVariable> result((jobject) NULL);
596             OBJ_CALL(result = self->object.getTypeParameters());
597
598             return result.toSequence(t_TypeVariable::wrap_Object);
599         }
600
601         static PyObject *t_Class_getGenericInterfaces(t_Class *self)
602         {
603             JArray<Type> result((jobject) NULL);
604             OBJ_CALL(result = self->object.getGenericInterfaces());
605
606             return result.toSequence(t_Type::wrap_Object);
607         }
608
609         static PyObject *t_Class_getGenericSuperclass(t_Class *self)
610         {
611             Type result((jobject) NULL);
612             OBJ_CALL(result = self->object.getGenericSuperclass());
613
614             return t_Type::wrap_Object(result);
615         }
616
617         static PyObject *t_Class_get__parameters_(t_Class *self, void *data)
618         {
619             return typeParameters(self->parameters, sizeof(self->parameters));
620         }
621 #endif
622     }
623 }