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
6 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
26 #include "java/lang/reflect/Type.h"
27 #include "java/lang/reflect/TypeVariable.h"
32 using namespace reflect;
36 mid_getDeclaredMethods,
39 mid_getDeclaredMethod,
40 mid_getDeclaredConstructors,
41 mid_getDeclaredFields,
42 mid_getDeclaredClasses,
49 mid_getDeclaringClass,
50 mid_getEnclosingClass,
56 mid_getTypeParameters,
57 mid_getGenericInterfaces,
58 mid_getGenericSuperclass,
63 Class *Class::class$ = NULL;
64 jmethodID *Class::_mids = NULL;
66 jclass Class::initializeClass()
70 jclass cls = env->findClass("java/lang/Class");
72 _mids = new jmethodID[max_mid];
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;");
98 env->getMethodID(cls, "isArray",
100 _mids[mid_isPrimitive] =
101 env->getMethodID(cls, "isPrimitive",
103 _mids[mid_isInterface] =
104 env->getMethodID(cls, "isInterface",
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;");
125 env->getMethodID(cls, "getName",
126 "()Ljava/lang/String;");
127 _mids[mid_getModifiers] =
128 env->getMethodID(cls, "getModifiers",
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;");
144 class$ = (Class *) new JObject(cls);
147 return (jclass) class$->this$;
151 Class Class::forName(const String& className)
153 jclass cls = initializeClass();
154 jobject obj = env->callStaticObjectMethod(cls, _mids[mid_forName], className.this$);
156 return Class((jclass) obj);
159 JArray<Method> Class::getDeclaredMethods() const
161 jobjectArray array = (jobjectArray)
162 env->callObjectMethod(this$, _mids[mid_getDeclaredMethods]);
164 return JArray<Method>(array);
167 JArray<Method> Class::getMethods() const
169 jobjectArray array = (jobjectArray)
170 env->callObjectMethod(this$, _mids[mid_getMethods]);
172 return JArray<Method>(array);
175 Method Class::getMethod(const String& name, const JArray<Class>& params) const
177 return Method(env->callObjectMethod(this$, _mids[mid_getMethod], name.this$, params.this$));
180 Method Class::getDeclaredMethod(const String& name, const JArray<Class>& params) const
182 return Method(env->callObjectMethod(this$, _mids[mid_getDeclaredMethod], name.this$, params.this$));
185 JArray<Constructor> Class::getDeclaredConstructors() const
187 jobjectArray array = (jobjectArray)
188 env->callObjectMethod(this$, _mids[mid_getDeclaredConstructors]);
190 return JArray<Constructor>(array);
193 JArray<Field> Class::getDeclaredFields() const
195 jobjectArray array = (jobjectArray)
196 env->callObjectMethod(this$, _mids[mid_getDeclaredFields]);
198 return JArray<Field>(array);
201 JArray<Class> Class::getDeclaredClasses() const
203 jobjectArray array = (jobjectArray)
204 env->callObjectMethod(this$, _mids[mid_getDeclaredClasses]);
206 return JArray<Class>(array);
209 int Class::isArray() const
211 return (int) env->callBooleanMethod(this$, _mids[mid_isArray]);
214 int Class::isPrimitive() const
216 return (int) env->callBooleanMethod(this$, _mids[mid_isPrimitive]);
219 int Class::isInterface() const
221 return (int) env->callBooleanMethod(this$, _mids[mid_isInterface]);
224 int Class::isAssignableFrom(const Class& obj) const
226 return (int) env->callBooleanMethod(this$, _mids[mid_isAssignableFrom], obj.this$);
229 Class Class::getComponentType() const
231 return Class(env->callObjectMethod(this$, _mids[mid_getComponentType]));
234 Class Class::getSuperclass() const
236 return Class(env->callObjectMethod(this$, _mids[mid_getSuperclass]));
239 Class Class::getDeclaringClass() const
241 return Class(env->callObjectMethod(this$, _mids[mid_getDeclaringClass]));
244 Class Class::getEnclosingClass() const
246 return Class(env->callObjectMethod(this$, _mids[mid_getEnclosingClass]));
249 JArray<Class> Class::getInterfaces() const
251 jobjectArray array = (jobjectArray)
252 env->callObjectMethod(this$, _mids[mid_getInterfaces]);
254 return JArray<Class>(array);
257 String Class::getName() const
259 return String(env->callObjectMethod(this$, _mids[mid_getName]));
262 int Class::getModifiers() const
264 return env->callIntMethod(this$, _mids[mid_getModifiers]);
267 int Class::isInstance(const Object &obj) const
269 return env->callBooleanMethod(this$, _mids[mid_isInstance],
273 #ifdef _java_generics
274 JArray<TypeVariable> Class::getTypeParameters() const
276 return JArray<TypeVariable>(env->callObjectMethod(this$, _mids[mid_getTypeParameters]));
279 JArray<Type> Class::getGenericInterfaces() const
281 return JArray<Type>(env->callObjectMethod(this$, _mids[mid_getGenericInterfaces]));
284 Type Class::getGenericSuperclass() const
286 return Type(env->callObjectMethod(this$, _mids[mid_getGenericSuperclass]));
293 #include "structmember.h"
294 #include "functions.h"
299 using namespace reflect;
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);
328 static PyGetSetDef t_Class__fields_[] = {
329 DECLARE_GET_FIELD(t_Class, parameters_),
330 { NULL, NULL, NULL, NULL, NULL }
333 static PyGetSetDef t_Class__fields_[] = {
334 { NULL, NULL, NULL, NULL, NULL }
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),
365 { NULL, NULL, 0, NULL }
368 DECLARE_TYPE(Class, t_Class, Object, java::lang::Class,
369 abstract_init, 0, 0, t_Class__fields_, 0, 0);
371 #ifdef _java_generics
372 PyObject *t_Class::wrap_Object(const Class& object, PyTypeObject *T)
374 PyObject *obj = t_Class::wrap_Object(object);
377 t_Class *self = (t_Class *) obj;
378 self->parameters[0] = T;
383 static PyObject *t_Class_cast_(PyTypeObject *type, PyObject *arg)
385 if (!(arg = castCheck(arg, Class::initializeClass, 1)))
387 return t_Class::wrap_Object(Class(((t_Class *) arg)->object.this$));
389 static PyObject *t_Class_instance_(PyTypeObject *type, PyObject *arg)
391 if (!castCheck(arg, Class::initializeClass, 0))
396 static PyObject *t_Class_forName(PyTypeObject *type, PyObject *arg)
398 if (!PyString_Check(arg))
400 PyErr_SetObject(PyExc_TypeError, arg);
405 char *className = PyString_AsString(arg);
406 String name = String(env->fromUTF(className));
408 return t_Class::wrap_Object(Class::forName(name));
412 return PyErr_SetJavaError();
419 static PyObject *t_Class_getDeclaredConstructors(t_Class *self)
421 JArray<Constructor> constructors((jobject) NULL);
423 OBJ_CALL(constructors = self->object.getDeclaredConstructors());
424 return constructors.toSequence(t_Constructor::wrap_Object);
427 static PyObject *t_Class_getDeclaredMethods(t_Class *self)
429 JArray<Method> methods((jobject) NULL);
431 OBJ_CALL(methods = self->object.getDeclaredMethods());
432 return methods.toSequence(t_Method::wrap_Object);
435 static PyObject *t_Class_getMethods(t_Class *self)
437 JArray<Method> methods((jobject) NULL);
439 OBJ_CALL(methods = self->object.getMethods());
440 return methods.toSequence(t_Method::wrap_Object);
443 static PyObject *t_Class_getMethod(t_Class *self, PyObject *args)
445 String name((jobject) NULL);
446 JArray<Class> params((jobject) NULL);
447 Method method((jobject) NULL);
449 if (!parseArgs(args, "s[j", Class::class$, &name, ¶ms))
451 OBJ_CALL(method = self->object.getMethod(name, params));
452 return t_Method::wrap_Object(method);
455 return PyErr_SetArgsError((PyObject *) self, "getMethod", args);
458 static PyObject *t_Class_getDeclaredMethod(t_Class *self, PyObject *args)
460 String name((jobject) NULL);
461 JArray<Class> params((jobject) NULL);
462 Method method((jobject) NULL);
464 if (!parseArgs(args, "s[j", Class::class$, &name, ¶ms))
466 OBJ_CALL(method = self->object.getDeclaredMethod(name, params));
467 return t_Method::wrap_Object(method);
470 return PyErr_SetArgsError((PyObject *) self, "getMethod", args);
473 static PyObject *t_Class_getDeclaredFields(t_Class *self)
475 JArray<Field> fields((jobject) NULL);
477 OBJ_CALL(fields = self->object.getDeclaredFields());
478 return fields.toSequence(t_Field::wrap_Object);
481 static PyObject *t_Class_getDeclaredClasses(t_Class *self)
483 JArray<Class> array((jobject) NULL);
485 OBJ_CALL(array = self->object.getDeclaredClasses());
486 return array.toSequence(t_Class::wrap_Object);
489 static PyObject *t_Class_isArray(t_Class *self)
493 OBJ_CALL(isArray = self->object.isArray());
494 Py_RETURN_BOOL(isArray);
497 static PyObject *t_Class_isPrimitive(t_Class *self)
501 OBJ_CALL(isPrimitive = self->object.isPrimitive());
502 Py_RETURN_BOOL(isPrimitive);
505 static PyObject *t_Class_isInterface(t_Class *self)
509 OBJ_CALL(isInterface = self->object.isInterface());
510 Py_RETURN_BOOL(isInterface);
513 static PyObject *t_Class_isAssignableFrom(t_Class *self, PyObject *arg)
515 if (!PyObject_TypeCheck(arg, &PY_TYPE(Class)))
517 PyErr_SetObject(PyExc_TypeError, arg);
522 Class cls = ((t_Class *) arg)->object;
523 int isAssignableFrom = self->object.isAssignableFrom(cls);
525 Py_RETURN_BOOL(isAssignableFrom);
529 return PyErr_SetJavaError();
536 static PyObject *t_Class_getComponentType(t_Class *self)
538 Class cls((jobject) NULL);
540 OBJ_CALL(cls = self->object.getComponentType());
541 return t_Class::wrap_Object(cls);
544 static PyObject *t_Class_getSuperclass(t_Class *self)
546 Class cls((jobject) NULL);
548 OBJ_CALL(cls = self->object.getSuperclass());
549 return t_Class::wrap_Object(cls);
552 static PyObject *t_Class_getDeclaringClass(t_Class *self)
554 Class cls((jobject) NULL);
556 OBJ_CALL(cls = self->object.getDeclaringClass());
557 return t_Class::wrap_Object(cls);
560 static PyObject *t_Class_getEnclosingClass(t_Class *self)
562 Class cls((jobject) NULL);
564 OBJ_CALL(cls = self->object.getEnclosingClass());
565 return t_Class::wrap_Object(cls);
568 static PyObject *t_Class_getInterfaces(t_Class *self)
570 JArray<Class> interfaces((jobject) NULL);
572 OBJ_CALL(interfaces = self->object.getInterfaces());
573 return interfaces.toSequence(t_Class::wrap_Object);
576 static PyObject *t_Class_getName(t_Class *self)
578 String name((jobject) NULL);
580 OBJ_CALL(name = self->object.getName());
584 static PyObject *t_Class_getModifiers(t_Class *self)
588 OBJ_CALL(modifiers = self->object.getModifiers());
589 return PyInt_FromLong(modifiers);
592 #ifdef _java_generics
593 static PyObject *t_Class_getTypeParameters(t_Class *self)
595 JArray<TypeVariable> result((jobject) NULL);
596 OBJ_CALL(result = self->object.getTypeParameters());
598 return result.toSequence(t_TypeVariable::wrap_Object);
601 static PyObject *t_Class_getGenericInterfaces(t_Class *self)
603 JArray<Type> result((jobject) NULL);
604 OBJ_CALL(result = self->object.getGenericInterfaces());
606 return result.toSequence(t_Type::wrap_Object);
609 static PyObject *t_Class_getGenericSuperclass(t_Class *self)
611 Type result((jobject) NULL);
612 OBJ_CALL(result = self->object.getGenericSuperclass());
614 return t_Type::wrap_Object(result);
617 static PyObject *t_Class_get__parameters_(t_Class *self, void *data)
619 return typeParameters(self->parameters, sizeof(self->parameters));