natClass.cc 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. // natClass.cc - Implementation of java.lang.Class native methods.
  2. /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
  3. 2010 Free Software Foundation
  4. This file is part of libgcj.
  5. This software is copyrighted work licensed under the terms of the
  6. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  7. details. */
  8. #include <config.h>
  9. #include <limits.h>
  10. #include <string.h>
  11. #include <stddef.h>
  12. #include <stdio.h>
  13. #pragma implementation "Class.h"
  14. #include <gcj/cni.h>
  15. #include <jvm.h>
  16. #include <java-threads.h>
  17. #include <java/lang/Class.h>
  18. #include <java/lang/ClassLoader.h>
  19. #include <java/lang/String.h>
  20. #include <java/lang/reflect/Modifier.h>
  21. #include <java/lang/reflect/Member.h>
  22. #include <java/lang/reflect/Method.h>
  23. #include <java/lang/reflect/Field.h>
  24. #include <java/lang/reflect/Proxy.h>
  25. #include <java/lang/reflect/Constructor.h>
  26. #include <java/lang/AbstractMethodError.h>
  27. #include <java/lang/ArrayStoreException.h>
  28. #include <java/lang/ClassCastException.h>
  29. #include <java/lang/ClassNotFoundException.h>
  30. #include <java/lang/ExceptionInInitializerError.h>
  31. #include <java/lang/IllegalAccessException.h>
  32. #include <java/lang/IllegalAccessError.h>
  33. #include <java/lang/IllegalArgumentException.h>
  34. #include <java/lang/IncompatibleClassChangeError.h>
  35. #include <java/lang/NoSuchFieldError.h>
  36. #include <java/lang/ArrayIndexOutOfBoundsException.h>
  37. #include <java/lang/InstantiationException.h>
  38. #include <java/lang/NoClassDefFoundError.h>
  39. #include <java/lang/NoSuchFieldException.h>
  40. #include <java/lang/NoSuchMethodError.h>
  41. #include <java/lang/NoSuchMethodException.h>
  42. #include <java/lang/Thread.h>
  43. #include <java/lang/NullPointerException.h>
  44. #include <java/lang/RuntimePermission.h>
  45. #include <java/lang/System.h>
  46. #include <java/lang/SecurityException.h>
  47. #include <java/lang/SecurityManager.h>
  48. #include <java/lang/StringBuffer.h>
  49. #include <java/lang/VMClassLoader.h>
  50. #include <gcj/method.h>
  51. #include <gnu/gcj/RawData.h>
  52. #include <java/lang/VerifyError.h>
  53. #include <java/lang/InternalError.h>
  54. #include <java/lang/TypeNotPresentException.h>
  55. #include <java/lang/Byte.h>
  56. #include <java/lang/Short.h>
  57. #include <java/lang/Integer.h>
  58. #include <java/lang/Float.h>
  59. #include <java/lang/Double.h>
  60. #include <java/lang/Long.h>
  61. #include <java/lang/Character.h>
  62. #include <java/lang/Boolean.h>
  63. #include <java/lang/annotation/Annotation.h>
  64. #include <java/util/HashMap.h>
  65. #include <java/util/Map.h>
  66. #include <sun/reflect/annotation/AnnotationInvocationHandler.h>
  67. #include <java/lang/Enum.h>
  68. #include <java-cpool.h>
  69. #include <java-interp.h>
  70. #include <java-assert.h>
  71. #include <java-stack.h>
  72. #include <execution.h>
  73. using namespace gcj;
  74. jclass
  75. java::lang::Class::forName (jstring className, jboolean initialize,
  76. java::lang::ClassLoader *loader)
  77. {
  78. if (! className)
  79. throw new java::lang::NullPointerException;
  80. jsize length = _Jv_GetStringUTFLength (className);
  81. char buffer[length];
  82. _Jv_GetStringUTFRegion (className, 0, className->length(), buffer);
  83. _Jv_Utf8Const *name = _Jv_makeUtf8Const (buffer, length);
  84. if (! _Jv_VerifyClassName (name))
  85. throw new java::lang::ClassNotFoundException (className);
  86. jclass klass = (buffer[0] == '['
  87. ? _Jv_FindClassFromSignature (name->chars(), loader)
  88. : _Jv_FindClass (name, loader));
  89. if (klass == NULL)
  90. throw new java::lang::ClassNotFoundException (className);
  91. if (initialize)
  92. _Jv_InitClass (klass);
  93. return klass;
  94. }
  95. jclass
  96. java::lang::Class::forName (jstring className)
  97. {
  98. java::lang::ClassLoader *loader = NULL;
  99. jclass caller = _Jv_StackTrace::GetCallingClass (&Class::class$);
  100. if (caller)
  101. loader = caller->getClassLoaderInternal();
  102. return forName (className, true, loader);
  103. }
  104. java::lang::ClassLoader *
  105. java::lang::Class::getClassLoader (void)
  106. {
  107. java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
  108. if (s != NULL)
  109. {
  110. jclass caller = _Jv_StackTrace::GetCallingClass (&Class::class$);
  111. return getClassLoader (caller);
  112. }
  113. return loader;
  114. }
  115. java::lang::ClassLoader *
  116. java::lang::Class::getClassLoader (jclass caller)
  117. {
  118. java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
  119. if (s != NULL)
  120. {
  121. ClassLoader *caller_loader = caller->getClassLoaderInternal();
  122. // If the caller has a non-null class loader, and that loader
  123. // is not this class' loader or an ancestor thereof, then do a
  124. // security check.
  125. if (caller_loader != NULL && ! caller_loader->isAncestorOf(loader))
  126. s->checkPermission (new RuntimePermission (JvNewStringLatin1 ("getClassLoader")));
  127. }
  128. return loader;
  129. }
  130. java::lang::reflect::Constructor *
  131. java::lang::Class::getConstructor (JArray<jclass> *param_types)
  132. {
  133. memberAccessCheck(java::lang::reflect::Member::PUBLIC);
  134. jstring partial_sig = getSignature (param_types, true);
  135. jint hash = partial_sig->hashCode ();
  136. int i = isPrimitive () ? 0 : method_count;
  137. while (--i >= 0)
  138. {
  139. if (_Jv_equalUtf8Consts (methods[i].name, init_name)
  140. && _Jv_equal (methods[i].signature, partial_sig, hash))
  141. {
  142. // Found it. For getConstructor, the constructor must be
  143. // public.
  144. using namespace java::lang::reflect;
  145. if (! Modifier::isPublic(methods[i].accflags))
  146. break;
  147. Constructor *cons = new Constructor ();
  148. cons->offset = (char *) (&methods[i]) - (char *) methods;
  149. cons->declaringClass = this;
  150. return cons;
  151. }
  152. }
  153. throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
  154. }
  155. JArray<java::lang::reflect::Constructor *> *
  156. java::lang::Class::getDeclaredConstructors (jboolean publicOnly)
  157. {
  158. int numConstructors = 0;
  159. int max = isPrimitive () ? 0 : method_count;
  160. int i;
  161. for (i = max; --i >= 0; )
  162. {
  163. _Jv_Method *method = &methods[i];
  164. if (method->name == NULL
  165. || ! _Jv_equalUtf8Consts (method->name, init_name))
  166. continue;
  167. if (publicOnly
  168. && ! java::lang::reflect::Modifier::isPublic(method->accflags))
  169. continue;
  170. numConstructors++;
  171. }
  172. JArray<java::lang::reflect::Constructor *> *result
  173. = (JArray<java::lang::reflect::Constructor *> *)
  174. JvNewObjectArray (numConstructors,
  175. &java::lang::reflect::Constructor::class$,
  176. NULL);
  177. java::lang::reflect::Constructor** cptr = elements (result);
  178. for (i = 0; i < max; i++)
  179. {
  180. _Jv_Method *method = &methods[i];
  181. if (method->name == NULL
  182. || ! _Jv_equalUtf8Consts (method->name, init_name))
  183. continue;
  184. if (publicOnly
  185. && ! java::lang::reflect::Modifier::isPublic(method->accflags))
  186. continue;
  187. java::lang::reflect::Constructor *cons
  188. = new java::lang::reflect::Constructor ();
  189. cons->offset = (char *) method - (char *) methods;
  190. cons->declaringClass = this;
  191. *cptr++ = cons;
  192. }
  193. return result;
  194. }
  195. java::lang::reflect::Constructor *
  196. java::lang::Class::getDeclaredConstructor (JArray<jclass> *param_types)
  197. {
  198. memberAccessCheck(java::lang::reflect::Member::DECLARED);
  199. jstring partial_sig = getSignature (param_types, true);
  200. jint hash = partial_sig->hashCode ();
  201. int i = isPrimitive () ? 0 : method_count;
  202. while (--i >= 0)
  203. {
  204. if (_Jv_equalUtf8Consts (methods[i].name, init_name)
  205. && _Jv_equal (methods[i].signature, partial_sig, hash))
  206. {
  207. // Found it.
  208. using namespace java::lang::reflect;
  209. Constructor *cons = new Constructor ();
  210. cons->offset = (char *) (&methods[i]) - (char *) methods;
  211. cons->declaringClass = this;
  212. return cons;
  213. }
  214. }
  215. throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
  216. }
  217. java::lang::reflect::Field *
  218. java::lang::Class::getField (jstring name, jint hash)
  219. {
  220. java::lang::reflect::Field* rfield;
  221. for (int i = 0; i < field_count; i++)
  222. {
  223. _Jv_Field *field = &fields[i];
  224. if (! _Jv_equal (field->name, name, hash))
  225. continue;
  226. if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC))
  227. continue;
  228. rfield = new java::lang::reflect::Field ();
  229. rfield->offset = (char*) field - (char*) fields;
  230. rfield->declaringClass = this;
  231. rfield->name = name;
  232. return rfield;
  233. }
  234. jclass superclass = getSuperclass();
  235. if (superclass == NULL)
  236. return NULL;
  237. rfield = superclass->getField(name, hash);
  238. for (int i = 0; i < interface_count && rfield == NULL; ++i)
  239. rfield = interfaces[i]->getField (name, hash);
  240. return rfield;
  241. }
  242. java::lang::reflect::Field *
  243. java::lang::Class::getDeclaredField (jstring name)
  244. {
  245. memberAccessCheck(java::lang::reflect::Member::DECLARED);
  246. int hash = name->hashCode();
  247. for (int i = 0; i < field_count; i++)
  248. {
  249. _Jv_Field *field = &fields[i];
  250. if (! _Jv_equal (field->name, name, hash))
  251. continue;
  252. java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
  253. rfield->offset = (char*) field - (char*) fields;
  254. rfield->declaringClass = this;
  255. rfield->name = name;
  256. return rfield;
  257. }
  258. throw new java::lang::NoSuchFieldException (name);
  259. }
  260. JArray<java::lang::reflect::Field *> *
  261. java::lang::Class::getDeclaredFields (jboolean public_only)
  262. {
  263. int size;
  264. if (public_only)
  265. {
  266. size = 0;
  267. for (int i = 0; i < field_count; ++i)
  268. {
  269. _Jv_Field *field = &fields[i];
  270. if ((field->flags & java::lang::reflect::Modifier::PUBLIC))
  271. ++size;
  272. }
  273. }
  274. else
  275. size = field_count;
  276. JArray<java::lang::reflect::Field *> *result
  277. = (JArray<java::lang::reflect::Field *> *)
  278. JvNewObjectArray (size, &java::lang::reflect::Field::class$, NULL);
  279. java::lang::reflect::Field** fptr = elements (result);
  280. for (int i = 0; i < field_count; i++)
  281. {
  282. _Jv_Field *field = &fields[i];
  283. if (public_only
  284. && ! (field->flags & java::lang::reflect::Modifier::PUBLIC))
  285. continue;
  286. java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
  287. rfield->offset = (char*) field - (char*) fields;
  288. rfield->declaringClass = this;
  289. *fptr++ = rfield;
  290. }
  291. return result;
  292. }
  293. void
  294. java::lang::Class::getSignature (java::lang::StringBuffer *buffer)
  295. {
  296. if (isPrimitive())
  297. buffer->append((jchar) method_count);
  298. else
  299. {
  300. jstring name = getName();
  301. if (name->charAt(0) != '[')
  302. buffer->append((jchar) 'L');
  303. buffer->append(name);
  304. if (name->charAt(0) != '[')
  305. buffer->append((jchar) ';');
  306. }
  307. }
  308. // This doesn't have to be native. It is an implementation detail
  309. // only called from the C++ code, though, so maybe this is clearer.
  310. jstring
  311. java::lang::Class::getSignature (JArray<jclass> *param_types,
  312. jboolean is_constructor)
  313. {
  314. java::lang::StringBuffer *buf = new java::lang::StringBuffer ();
  315. buf->append((jchar) '(');
  316. // A NULL param_types means "no parameters".
  317. if (param_types != NULL)
  318. {
  319. jclass *v = elements (param_types);
  320. for (int i = 0; i < param_types->length; ++i)
  321. v[i]->getSignature(buf);
  322. }
  323. buf->append((jchar) ')');
  324. if (is_constructor)
  325. buf->append((jchar) 'V');
  326. return buf->toString();
  327. }
  328. java::lang::reflect::Method *
  329. java::lang::Class::_getDeclaredMethod (jstring name,
  330. JArray<jclass> *param_types)
  331. {
  332. jstring partial_sig = getSignature (param_types, false);
  333. jint p_len = partial_sig->length();
  334. _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
  335. int i = isPrimitive () ? 0 : method_count;
  336. while (--i >= 0)
  337. {
  338. if (_Jv_equalUtf8Consts (methods[i].name, utf_name)
  339. && _Jv_equaln (methods[i].signature, partial_sig, p_len)
  340. && (methods[i].accflags
  341. & java::lang::reflect::Modifier::INVISIBLE) == 0)
  342. {
  343. // Found it.
  344. using namespace java::lang::reflect;
  345. Method *rmethod = new Method ();
  346. rmethod->offset = (char*) (&methods[i]) - (char*) methods;
  347. rmethod->declaringClass = this;
  348. return rmethod;
  349. }
  350. }
  351. return NULL;
  352. }
  353. JArray<java::lang::reflect::Method *> *
  354. java::lang::Class::getDeclaredMethods (void)
  355. {
  356. memberAccessCheck(java::lang::reflect::Member::DECLARED);
  357. int numMethods = 0;
  358. int max = isPrimitive () ? 0 : method_count;
  359. int i;
  360. for (i = max; --i >= 0; )
  361. {
  362. _Jv_Method *method = &methods[i];
  363. if (method->name == NULL
  364. || _Jv_equalUtf8Consts (method->name, clinit_name)
  365. || _Jv_equalUtf8Consts (method->name, init_name)
  366. || _Jv_equalUtf8Consts (method->name, finit_name)
  367. || (methods[i].accflags
  368. & java::lang::reflect::Modifier::INVISIBLE) != 0)
  369. continue;
  370. numMethods++;
  371. }
  372. JArray<java::lang::reflect::Method *> *result
  373. = (JArray<java::lang::reflect::Method *> *)
  374. JvNewObjectArray (numMethods, &java::lang::reflect::Method::class$, NULL);
  375. java::lang::reflect::Method** mptr = elements (result);
  376. for (i = 0; i < max; i++)
  377. {
  378. _Jv_Method *method = &methods[i];
  379. if (method->name == NULL
  380. || _Jv_equalUtf8Consts (method->name, clinit_name)
  381. || _Jv_equalUtf8Consts (method->name, init_name)
  382. || _Jv_equalUtf8Consts (method->name, finit_name)
  383. || (methods[i].accflags
  384. & java::lang::reflect::Modifier::INVISIBLE) != 0)
  385. continue;
  386. java::lang::reflect::Method* rmethod
  387. = new java::lang::reflect::Method ();
  388. rmethod->offset = (char*) method - (char*) methods;
  389. rmethod->declaringClass = this;
  390. *mptr++ = rmethod;
  391. }
  392. return result;
  393. }
  394. jstring
  395. java::lang::Class::getName (void)
  396. {
  397. return name->toString();
  398. }
  399. JArray<jclass> *
  400. java::lang::Class::getInterfaces (void)
  401. {
  402. jobjectArray r = JvNewObjectArray (interface_count, getClass (), NULL);
  403. jobject *data = elements (r);
  404. for (int i = 0; i < interface_count; ++i)
  405. {
  406. typedef unsigned int uaddr __attribute__ ((mode (pointer)));
  407. data[i] = interfaces[i];
  408. if ((uaddr)data[i] < (uaddr)constants.size)
  409. fprintf (stderr, "ERROR !!!\n");
  410. }
  411. return reinterpret_cast<JArray<jclass> *> (r);
  412. }
  413. java::lang::reflect::Method *
  414. java::lang::Class::_getMethod (jstring name, JArray<jclass> *param_types)
  415. {
  416. jstring partial_sig = getSignature (param_types, false);
  417. jint p_len = partial_sig->length();
  418. _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
  419. for (Class *klass = this; klass; klass = klass->getSuperclass())
  420. {
  421. int i = klass->isPrimitive () ? 0 : klass->method_count;
  422. while (--i >= 0)
  423. {
  424. if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
  425. && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len)
  426. && (klass->methods[i].accflags
  427. & java::lang::reflect::Modifier::INVISIBLE) == 0)
  428. {
  429. // Found it.
  430. using namespace java::lang::reflect;
  431. // Method must be public.
  432. if (! Modifier::isPublic (klass->methods[i].accflags))
  433. break;
  434. Method *rmethod = new Method ();
  435. rmethod->offset = ((char *) (&klass->methods[i])
  436. - (char *) klass->methods);
  437. rmethod->declaringClass = klass;
  438. return rmethod;
  439. }
  440. }
  441. }
  442. // If we haven't found a match, and this class is an interface, then
  443. // check all the superinterfaces.
  444. if (isInterface())
  445. {
  446. for (int i = 0; i < interface_count; ++i)
  447. {
  448. using namespace java::lang::reflect;
  449. Method *rmethod = interfaces[i]->_getMethod (name, param_types);
  450. if (rmethod != NULL)
  451. return rmethod;
  452. }
  453. }
  454. return NULL;
  455. }
  456. // This is a very slow implementation, since it re-scans all the
  457. // methods we've already listed to make sure we haven't duplicated a
  458. // method. It also over-estimates the required size, so we have to
  459. // shrink the result array later.
  460. jint
  461. java::lang::Class::_getMethods (JArray<java::lang::reflect::Method *> *result,
  462. jint offset)
  463. {
  464. jint count = 0;
  465. // First examine all local methods
  466. for (int i = isPrimitive () ? 0 : method_count; --i >= 0; )
  467. {
  468. _Jv_Method *method = &methods[i];
  469. if (method->name == NULL
  470. || _Jv_equalUtf8Consts (method->name, clinit_name)
  471. || _Jv_equalUtf8Consts (method->name, init_name)
  472. || _Jv_equalUtf8Consts (method->name, finit_name)
  473. || (method->accflags
  474. & java::lang::reflect::Modifier::INVISIBLE) != 0)
  475. continue;
  476. // Only want public methods.
  477. if (! java::lang::reflect::Modifier::isPublic (method->accflags))
  478. continue;
  479. // This is where we over-count the slots required if we aren't
  480. // filling the result for real.
  481. if (result != NULL)
  482. {
  483. jboolean add = true;
  484. java::lang::reflect::Method **mp = elements (result);
  485. // If we already have a method with this name and signature,
  486. // then ignore this one. This can happen with virtual
  487. // methods.
  488. for (int j = 0; j < offset; ++j)
  489. {
  490. _Jv_Method *meth_2 = _Jv_FromReflectedMethod (mp[j]);
  491. if (_Jv_equalUtf8Consts (method->name, meth_2->name)
  492. && _Jv_equalUtf8Consts (method->signature,
  493. meth_2->signature))
  494. {
  495. add = false;
  496. break;
  497. }
  498. }
  499. if (! add)
  500. continue;
  501. }
  502. if (result != NULL)
  503. {
  504. using namespace java::lang::reflect;
  505. Method *rmethod = new Method ();
  506. rmethod->offset = (char *) method - (char *) methods;
  507. rmethod->declaringClass = this;
  508. Method **mp = elements (result);
  509. mp[offset + count] = rmethod;
  510. }
  511. ++count;
  512. }
  513. offset += count;
  514. // Now examine superclasses.
  515. if (getSuperclass () != NULL)
  516. {
  517. jint s_count = getSuperclass()->_getMethods (result, offset);
  518. offset += s_count;
  519. count += s_count;
  520. }
  521. // Finally, examine interfaces.
  522. for (int i = 0; i < interface_count; ++i)
  523. {
  524. int f_count = interfaces[i]->_getMethods (result, offset);
  525. count += f_count;
  526. offset += f_count;
  527. }
  528. return count;
  529. }
  530. JArray<java::lang::reflect::Method *> *
  531. java::lang::Class::getMethods (void)
  532. {
  533. using namespace java::lang::reflect;
  534. memberAccessCheck(Member::PUBLIC);
  535. // This will overestimate the size we need.
  536. jint count = _getMethods (NULL, 0);
  537. JArray<Method *> *result
  538. = ((JArray<Method *> *) JvNewObjectArray (count,
  539. &Method::class$,
  540. NULL));
  541. // When filling the array for real, we get the actual count. Then
  542. // we resize the array.
  543. jint real_count = _getMethods (result, 0);
  544. if (real_count != count)
  545. {
  546. JArray<Method *> *r2
  547. = ((JArray<Method *> *) JvNewObjectArray (real_count,
  548. &Method::class$,
  549. NULL));
  550. Method **destp = elements (r2);
  551. Method **srcp = elements (result);
  552. for (int i = 0; i < real_count; ++i)
  553. *destp++ = *srcp++;
  554. result = r2;
  555. }
  556. return result;
  557. }
  558. jboolean
  559. java::lang::Class::isAssignableFrom (jclass klass)
  560. {
  561. // Arguments may not have been initialized, given ".class" syntax.
  562. // This ensures we can at least look at their superclasses.
  563. _Jv_Linker::wait_for_state (this, JV_STATE_LOADING);
  564. _Jv_Linker::wait_for_state (klass, JV_STATE_LOADING);
  565. return _Jv_IsAssignableFrom (klass, this);
  566. }
  567. jboolean
  568. java::lang::Class::isInstance (jobject obj)
  569. {
  570. if (! obj)
  571. return false;
  572. return _Jv_IsAssignableFrom (JV_CLASS (obj), this);
  573. }
  574. jobject
  575. java::lang::Class::newInstance (void)
  576. {
  577. memberAccessCheck(java::lang::reflect::Member::PUBLIC);
  578. if (isPrimitive ()
  579. || isInterface ()
  580. || isArray ()
  581. || java::lang::reflect::Modifier::isAbstract(accflags))
  582. throw new java::lang::InstantiationException (getName ());
  583. _Jv_InitClass (this);
  584. _Jv_Method *meth = _Jv_GetMethodLocal (this, init_name, void_signature);
  585. if (! meth)
  586. throw new java::lang::InstantiationException (getName());
  587. jobject r = _Jv_AllocObject (this);
  588. /* Class constructors/destructors have __thiscall calling
  589. convention for 32-bit native Windows ABI. */
  590. #if defined (__MINGW32__) && defined (__i386__)
  591. ((void (__thiscall *) (jobject)) meth->ncode) (r);
  592. #else
  593. ((void (*) (jobject)) meth->ncode) (r);
  594. #endif
  595. return r;
  596. }
  597. void
  598. java::lang::Class::finalize (void)
  599. {
  600. // Array classes don't have an engine, and don't need to be finalized.
  601. if (engine)
  602. engine->unregister(this);
  603. }
  604. #ifdef INTERPRETER
  605. void
  606. _Jv_ClosureList::releaseClosures (_Jv_ClosureList **closures)
  607. {
  608. if (!closures)
  609. return;
  610. while (_Jv_ClosureList *current = *closures)
  611. {
  612. *closures = current->next;
  613. ffi_closure_free (current->ptr);
  614. }
  615. }
  616. void
  617. _Jv_ClosureList::registerClosure (jclass klass, void *ptr)
  618. {
  619. _Jv_ClosureList **closures = klass->engine->get_closure_list (klass);
  620. if (closures)
  621. {
  622. this->ptr = ptr;
  623. this->next = *closures;
  624. *closures = this;
  625. }
  626. }
  627. #endif
  628. // This implements the initialization process for a class. From Spec
  629. // section 12.4.2.
  630. void
  631. java::lang::Class::initializeClass (void)
  632. {
  633. // Short-circuit to avoid needless locking (expression includes
  634. // JV_STATE_PHANTOM and JV_STATE_DONE).
  635. if (state >= JV_STATE_PHANTOM)
  636. return;
  637. // Step 1. We introduce a new scope so we can synchronize more
  638. // easily.
  639. {
  640. JvSynchronize sync (this);
  641. if (state < JV_STATE_LINKED)
  642. {
  643. try
  644. {
  645. _Jv_Linker::wait_for_state(this, JV_STATE_LINKED);
  646. }
  647. catch (java::lang::SecurityException *x)
  648. {
  649. throw x;
  650. }
  651. catch (java::lang::Throwable *x)
  652. {
  653. // Turn into a NoClassDefFoundError.
  654. java::lang::NoClassDefFoundError *result
  655. = new java::lang::NoClassDefFoundError(getName());
  656. result->initCause(x);
  657. throw result;
  658. }
  659. }
  660. // Step 2.
  661. java::lang::Thread *self = java::lang::Thread::currentThread();
  662. self = (java::lang::Thread *) ((long) self | 1);
  663. while (state == JV_STATE_IN_PROGRESS && thread && thread != self)
  664. wait ();
  665. // Steps 3 & 4.
  666. if (state == JV_STATE_DONE || state == JV_STATE_IN_PROGRESS)
  667. return;
  668. // Step 5.
  669. if (state == JV_STATE_ERROR)
  670. throw new java::lang::NoClassDefFoundError (getName());
  671. // Step 6.
  672. thread = self;
  673. _Jv_Linker::wait_for_state (this, JV_STATE_LINKED);
  674. state = JV_STATE_IN_PROGRESS;
  675. }
  676. // Step 7.
  677. if (! isInterface () && superclass)
  678. {
  679. try
  680. {
  681. _Jv_InitClass (superclass);
  682. }
  683. catch (java::lang::SecurityException *x)
  684. {
  685. throw x;
  686. }
  687. catch (java::lang::Throwable *except)
  688. {
  689. // Caught an exception.
  690. JvSynchronize sync (this);
  691. state = JV_STATE_ERROR;
  692. notifyAll ();
  693. throw except;
  694. }
  695. }
  696. // Steps 8, 9, 10, 11.
  697. try
  698. {
  699. _Jv_Method *meth = _Jv_GetMethodLocal (this, clinit_name,
  700. void_signature);
  701. if (meth)
  702. ((void (*) (void)) meth->ncode) ();
  703. }
  704. catch (java::lang::SecurityException *x)
  705. {
  706. throw x;
  707. }
  708. catch (java::lang::Throwable *except)
  709. {
  710. if (! java::lang::Error::class$.isInstance(except))
  711. {
  712. try
  713. {
  714. except = new ExceptionInInitializerError (except);
  715. }
  716. catch (java::lang::Throwable *t)
  717. {
  718. except = t;
  719. }
  720. }
  721. JvSynchronize sync (this);
  722. state = JV_STATE_ERROR;
  723. notifyAll ();
  724. throw except;
  725. }
  726. JvSynchronize sync (this);
  727. state = JV_STATE_DONE;
  728. notifyAll ();
  729. }
  730. // Only used by serialization
  731. java::lang::reflect::Field *
  732. java::lang::Class::getPrivateField (jstring name)
  733. {
  734. int hash = name->hashCode ();
  735. java::lang::reflect::Field* rfield;
  736. for (int i = 0; i < field_count; i++)
  737. {
  738. _Jv_Field *field = &fields[i];
  739. if (! _Jv_equal (field->name, name, hash))
  740. continue;
  741. rfield = new java::lang::reflect::Field ();
  742. rfield->offset = (char*) field - (char*) fields;
  743. rfield->declaringClass = this;
  744. rfield->name = name;
  745. return rfield;
  746. }
  747. jclass superclass = getSuperclass();
  748. if (superclass == NULL)
  749. return NULL;
  750. rfield = superclass->getPrivateField(name);
  751. for (int i = 0; i < interface_count && rfield == NULL; ++i)
  752. rfield = interfaces[i]->getPrivateField (name);
  753. return rfield;
  754. }
  755. // Only used by serialization
  756. java::lang::reflect::Method *
  757. java::lang::Class::getPrivateMethod (jstring name, JArray<jclass> *param_types)
  758. {
  759. jstring partial_sig = getSignature (param_types, false);
  760. jint p_len = partial_sig->length();
  761. _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
  762. for (Class *klass = this; klass; klass = klass->getSuperclass())
  763. {
  764. int i = klass->isPrimitive () ? 0 : klass->method_count;
  765. while (--i >= 0)
  766. {
  767. if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
  768. && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len))
  769. {
  770. // Found it.
  771. using namespace java::lang::reflect;
  772. Method *rmethod = new Method ();
  773. rmethod->offset = ((char *) (&klass->methods[i])
  774. - (char *) klass->methods);
  775. rmethod->declaringClass = klass;
  776. return rmethod;
  777. }
  778. }
  779. }
  780. throw new java::lang::NoSuchMethodException (name);
  781. }
  782. // Private accessor method for Java code to retrieve the protection domain.
  783. java::security::ProtectionDomain *
  784. java::lang::Class::getProtectionDomain0 ()
  785. {
  786. return protectionDomain;
  787. }
  788. JArray<jobject> *
  789. java::lang::Class::getSigners()
  790. {
  791. return hack_signers;
  792. }
  793. void
  794. java::lang::Class::setSigners(JArray<jobject> *s)
  795. {
  796. hack_signers = s;
  797. }
  798. static unsigned char
  799. read_u1 (unsigned char *&p)
  800. {
  801. return *p++;
  802. }
  803. static unsigned char
  804. read_u1 (unsigned char *&p, unsigned char *next)
  805. {
  806. if (next - p < 1)
  807. throw new java::lang::InternalError();
  808. return *p++;
  809. }
  810. static unsigned int
  811. read_u2 (unsigned char *&p)
  812. {
  813. unsigned int b1 = *p++;
  814. unsigned int b2 = *p++;
  815. return (b1 << 8) | b2;
  816. }
  817. static unsigned int
  818. read_u2 (unsigned char *&p, unsigned char *next)
  819. {
  820. if (next - p < 2)
  821. throw new java::lang::InternalError();
  822. return read_u2 (p);
  823. }
  824. static int
  825. read_4 (unsigned char *&p)
  826. {
  827. int b1 = *p++;
  828. int b2 = *p++;
  829. int b3 = *p++;
  830. int b4 = *p++;
  831. return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
  832. }
  833. jstring
  834. java::lang::Class::getReflectionSignature (jint /*jv_attr_type*/ type,
  835. jint obj_index)
  836. {
  837. // We just re-parse the bytecode for this data each time. If
  838. // necessary we can cache results, but I suspect this is not
  839. // performance sensitive.
  840. unsigned char *bytes = reflection_data;
  841. if (bytes == NULL)
  842. return NULL;
  843. while (true)
  844. {
  845. int kind = read_u1 (bytes);
  846. if (kind == JV_DONE_ATTR)
  847. return NULL;
  848. int len = read_4 (bytes);
  849. unsigned char *next = bytes + len;
  850. if (kind != type)
  851. {
  852. bytes = next;
  853. continue;
  854. }
  855. if (type != JV_CLASS_ATTR)
  856. {
  857. unsigned short index = read_u2 (bytes, next);
  858. if (index != obj_index)
  859. {
  860. bytes = next;
  861. continue;
  862. }
  863. }
  864. int nt = read_u1 (bytes, next);
  865. if (nt != JV_SIGNATURE_KIND)
  866. {
  867. bytes = next;
  868. continue;
  869. }
  870. unsigned int cpool_idx = read_u2 (bytes, next);
  871. if (cpool_idx >= (unsigned int) constants.size
  872. || constants.tags[cpool_idx] != JV_CONSTANT_Utf8)
  873. {
  874. // We just ignore errors for now. It isn't clear what is
  875. // best to do here, as an encoding error here means a bug
  876. // either in the compiler or in defineclass.cc.
  877. return NULL;
  878. }
  879. return _Jv_NewStringUtf8Const (constants.data[cpool_idx].utf8);
  880. }
  881. }
  882. jstring
  883. java::lang::Class::getReflectionSignature (::java::lang::reflect::Constructor *c)
  884. {
  885. _Jv_Method *meth = _Jv_FromReflectedConstructor (c);
  886. unsigned short meth_index = meth - methods;
  887. return getReflectionSignature (JV_METHOD_ATTR, meth_index);
  888. }
  889. jstring
  890. java::lang::Class::getReflectionSignature (::java::lang::reflect::Method *m)
  891. {
  892. _Jv_Method *meth = _Jv_FromReflectedMethod (m);
  893. unsigned short meth_index = meth - methods;
  894. return getReflectionSignature (JV_METHOD_ATTR, meth_index);
  895. }
  896. jstring
  897. java::lang::Class::getReflectionSignature (::java::lang::reflect::Field *f)
  898. {
  899. _Jv_Field *fld = _Jv_FromReflectedField (f);
  900. unsigned short fld_index = fld - fields;
  901. return getReflectionSignature (JV_FIELD_ATTR, fld_index);
  902. }
  903. jstring
  904. java::lang::Class::getClassSignature()
  905. {
  906. return getReflectionSignature (JV_CLASS_ATTR, 0);
  907. }
  908. jint
  909. java::lang::Class::getEnclosingMethodData()
  910. {
  911. unsigned char *bytes = reflection_data;
  912. if (bytes == NULL)
  913. return 0;
  914. while (true)
  915. {
  916. int kind = read_u1 (bytes);
  917. if (kind == JV_DONE_ATTR)
  918. return 0;
  919. int len = read_4 (bytes);
  920. unsigned char *next = bytes + len;
  921. if (kind != JV_CLASS_ATTR)
  922. {
  923. bytes = next;
  924. continue;
  925. }
  926. int type = read_u1 (bytes, next);
  927. if (type != JV_ENCLOSING_METHOD_KIND)
  928. {
  929. bytes = next;
  930. continue;
  931. }
  932. int class_index = read_u2 (bytes, next);
  933. int method_index = read_u2 (bytes, next);
  934. _Jv_word result;
  935. _Jv_storeIndexes (&result, class_index, method_index);
  936. return result.i;
  937. }
  938. }
  939. jclass
  940. java::lang::Class::getEnclosingClass()
  941. {
  942. _Jv_word indexes;
  943. indexes.i = getEnclosingMethodData();
  944. if (indexes.i == 0)
  945. // No enclosing method, but perhaps a member or anonymous class
  946. return getDeclaringClass();
  947. _Jv_ushort class_index, method_index;
  948. _Jv_loadIndexes (&indexes, class_index, method_index);
  949. return _Jv_Linker::resolve_pool_entry (this, class_index).clazz;
  950. }
  951. ::java::lang::reflect::Method *
  952. java::lang::Class::getEnclosingMethod()
  953. {
  954. _Jv_word indexes;
  955. indexes.i = getEnclosingMethodData();
  956. if (indexes.i == 0)
  957. return NULL;
  958. _Jv_ushort class_index, method_index;
  959. _Jv_loadIndexes (&indexes, class_index, method_index);
  960. jclass found_class;
  961. _Jv_Method *method = _Jv_Linker::resolve_method_entry (this, found_class,
  962. class_index,
  963. method_index,
  964. false, false);
  965. using namespace java::lang::reflect;
  966. Method *rmethod = new Method ();
  967. rmethod->offset = (char *) method - (char *) found_class->methods;
  968. rmethod->declaringClass = found_class;
  969. return rmethod;
  970. }
  971. ::java::lang::reflect::Constructor *
  972. java::lang::Class::getEnclosingConstructor()
  973. {
  974. _Jv_word indexes;
  975. indexes.i = getEnclosingMethodData();
  976. if (indexes.i == 0)
  977. return NULL;
  978. _Jv_ushort class_index, method_index;
  979. _Jv_loadIndexes (&indexes, class_index, method_index);
  980. jclass found_class;
  981. _Jv_Method *method = _Jv_Linker::resolve_method_entry (this, found_class,
  982. class_index,
  983. method_index,
  984. false, false);
  985. using namespace java::lang::reflect;
  986. Constructor *cons = new Constructor ();
  987. cons->offset = (char *) method - (char *) found_class->methods;
  988. cons->declaringClass = this;
  989. return cons;
  990. }
  991. static void
  992. check_constant (_Jv_Constants *pool, jint cpool_index, jint type)
  993. {
  994. if (cpool_index <= 0 || cpool_index >= pool->size)
  995. throw new InternalError(JvNewStringLatin1("invalid constant pool index"));
  996. if ((pool->tags[cpool_index] &
  997. ~(JV_CONSTANT_ResolvedFlag|JV_CONSTANT_LazyFlag)) != type)
  998. {
  999. ::java::lang::StringBuffer *sb = new ::java::lang::StringBuffer();
  1000. sb->append(JvNewStringLatin1("expected pool constant "));
  1001. sb->append(type);
  1002. sb->append(JvNewStringLatin1(" but got "));
  1003. sb->append(jint (pool->tags[cpool_index]));
  1004. throw new InternalError(sb->toString());
  1005. }
  1006. }
  1007. // Forward declaration
  1008. static ::java::lang::annotation::Annotation *
  1009. parseAnnotation(jclass klass, _Jv_Constants *pool,
  1010. unsigned char *&bytes, unsigned char *last);
  1011. static jobject
  1012. parseAnnotationElement(jclass klass, _Jv_Constants *pool,
  1013. unsigned char *&bytes, unsigned char *last)
  1014. {
  1015. int tag = read_u1 (bytes, last);
  1016. jobject result;
  1017. switch (tag)
  1018. {
  1019. case 'B':
  1020. {
  1021. int cindex = read_u2 (bytes, last);
  1022. check_constant (pool, cindex, JV_CONSTANT_Integer);
  1023. result = Byte::valueOf (pool->data[cindex].i);
  1024. }
  1025. break;
  1026. case 'C':
  1027. {
  1028. int cindex = read_u2 (bytes, last);
  1029. check_constant (pool, cindex, JV_CONSTANT_Integer);
  1030. result = Character::valueOf (pool->data[cindex].i);
  1031. }
  1032. break;
  1033. case 'S':
  1034. {
  1035. int cindex = read_u2 (bytes, last);
  1036. check_constant (pool, cindex, JV_CONSTANT_Integer);
  1037. result = Short::valueOf (pool->data[cindex].i);
  1038. }
  1039. break;
  1040. case 'Z':
  1041. {
  1042. int cindex = read_u2 (bytes, last);
  1043. check_constant (pool, cindex, JV_CONSTANT_Integer);
  1044. result = Boolean::valueOf (jboolean (pool->data[cindex].i));
  1045. }
  1046. break;
  1047. case 'I':
  1048. {
  1049. int cindex = read_u2 (bytes, last);
  1050. check_constant (pool, cindex, JV_CONSTANT_Integer);
  1051. result = Integer::valueOf (pool->data[cindex].i);
  1052. }
  1053. break;
  1054. case 'D':
  1055. {
  1056. int cindex = read_u2 (bytes, last);
  1057. check_constant (pool, cindex, JV_CONSTANT_Double);
  1058. _Jv_word2 word;
  1059. memcpy (&word, &pool->data[cindex], 2 * sizeof (_Jv_word));
  1060. result = Double::valueOf (word.d);
  1061. }
  1062. break;
  1063. case 'F':
  1064. {
  1065. int cindex = read_u2 (bytes, last);
  1066. check_constant (pool, cindex, JV_CONSTANT_Float);
  1067. result = Float::valueOf (pool->data[cindex].f);
  1068. }
  1069. break;
  1070. case 'J':
  1071. {
  1072. int cindex = read_u2 (bytes, last);
  1073. check_constant (pool, cindex, JV_CONSTANT_Long);
  1074. _Jv_word2 word;
  1075. memcpy (&word, &pool->data[cindex], 2 * sizeof (_Jv_word));
  1076. result = Long::valueOf (word.l);
  1077. }
  1078. break;
  1079. case 's':
  1080. {
  1081. int cindex = read_u2 (bytes, last);
  1082. // Despite what the JVM spec says, compilers generate a Utf8
  1083. // constant here, not a String.
  1084. check_constant (pool, cindex, JV_CONSTANT_Utf8);
  1085. result = pool->data[cindex].utf8->toString();
  1086. }
  1087. break;
  1088. case 'e':
  1089. {
  1090. int type_name_index = read_u2 (bytes, last);
  1091. check_constant (pool, type_name_index, JV_CONSTANT_Utf8);
  1092. int const_name_index = read_u2 (bytes, last);
  1093. check_constant (pool, const_name_index, JV_CONSTANT_Utf8);
  1094. _Jv_Utf8Const *u_name = pool->data[type_name_index].utf8;
  1095. _Jv_Utf8Const *e_name = pool->data[const_name_index].utf8;
  1096. // FIXME: throw correct exceptions at the correct times.
  1097. jclass e_class = _Jv_FindClassFromSignature(u_name->chars(),
  1098. klass->getClassLoaderInternal());
  1099. result = ::java::lang::Enum::valueOf(e_class, e_name->toString());
  1100. }
  1101. break;
  1102. case 'c':
  1103. {
  1104. int cindex = read_u2 (bytes, last);
  1105. check_constant (pool, cindex, JV_CONSTANT_Utf8);
  1106. _Jv_Utf8Const *u_name = pool->data[cindex].utf8;
  1107. jclass anno_class
  1108. = _Jv_FindClassFromSignatureNoException(u_name->chars(),
  1109. klass->getClassLoaderInternal());
  1110. // FIXME: not correct: we should lazily do this when trying to
  1111. // read the element. This means that
  1112. // AnnotationInvocationHandler needs to have a special case.
  1113. if (! anno_class)
  1114. // FIXME: original exception...
  1115. throw new TypeNotPresentException(u_name->toString(), NULL);
  1116. result = anno_class;
  1117. }
  1118. break;
  1119. case '@':
  1120. result = parseAnnotation (klass, pool, bytes, last);
  1121. break;
  1122. case '[':
  1123. {
  1124. int n_array_elts = read_u2 (bytes, last);
  1125. jobjectArray aresult = _Jv_NewObjectArray (n_array_elts,
  1126. &Object::class$, NULL);
  1127. jobject *elts = elements (aresult);
  1128. for (int i = 0; i < n_array_elts; ++i)
  1129. elts[i] = parseAnnotationElement(klass, pool, bytes, last);
  1130. result = aresult;
  1131. }
  1132. break;
  1133. default:
  1134. throw new java::lang::InternalError();
  1135. }
  1136. return result;
  1137. }
  1138. static ::java::lang::annotation::Annotation *
  1139. parseAnnotation(jclass klass, _Jv_Constants *pool,
  1140. unsigned char *&bytes, unsigned char *last)
  1141. {
  1142. int type_index = read_u2 (bytes, last);
  1143. check_constant (pool, type_index, JV_CONSTANT_Utf8);
  1144. _Jv_Utf8Const *u_name = pool->data[type_index].utf8;
  1145. jclass anno_class = _Jv_FindClassFromSignatureNoException(u_name->chars(),
  1146. klass->getClassLoaderInternal());
  1147. // FIXME: what to do if anno_class==NULL?
  1148. ::java::util::HashMap *hmap = new ::java::util::HashMap();
  1149. int npairs = read_u2 (bytes, last);
  1150. for (int i = 0; i < npairs; ++i)
  1151. {
  1152. int name_index = read_u2 (bytes, last);
  1153. check_constant (pool, name_index, JV_CONSTANT_Utf8);
  1154. jstring name = _Jv_NewStringUtf8Const (pool->data[name_index].utf8);
  1155. jobject value = parseAnnotationElement (klass, pool, bytes, last);
  1156. // FIXME: any checks needed for name?
  1157. hmap->put(name, value);
  1158. }
  1159. using namespace ::sun::reflect::annotation;
  1160. return AnnotationInvocationHandler::create (anno_class,
  1161. (::java::util::Map *) hmap);
  1162. }
  1163. static jobjectArray
  1164. parseAnnotations(jclass klass, _Jv_Constants *pool,
  1165. unsigned char *&bytes, unsigned char *last)
  1166. {
  1167. int num = read_u2 (bytes, last);
  1168. jobjectArray result = _Jv_NewObjectArray (num,
  1169. &::java::lang::annotation::Annotation::class$,
  1170. NULL);
  1171. jobject *elts = elements (result);
  1172. for (int i = 0; i < num; ++i)
  1173. elts[i] = parseAnnotation(klass, pool, bytes, last);
  1174. return result;
  1175. }
  1176. static jobjectArray
  1177. parseParameterAnnotations(jclass klass, _Jv_Constants *pool,
  1178. unsigned char *&bytes, unsigned char *last)
  1179. {
  1180. jclass anno = &::java::lang::annotation::Annotation::class$;
  1181. jclass annoary = _Jv_GetArrayClass (anno, anno->getClassLoaderInternal());
  1182. // FIXME: something should check the number of params versus the
  1183. // method
  1184. int n_params = read_u1 (bytes, last);
  1185. jobjectArray result = _Jv_NewObjectArray (n_params, annoary, NULL);
  1186. jobject *elts = elements (result);
  1187. for (int i = 0; i < n_params; ++i)
  1188. elts[i] = parseAnnotations(klass, pool, bytes, last);
  1189. return result;
  1190. }
  1191. jobject
  1192. java::lang::Class::getMethodDefaultValue(::java::lang::reflect::Method *meth)
  1193. {
  1194. // FIXME: could cache the value here...
  1195. unsigned char *bytes = reflection_data;
  1196. if (bytes == NULL)
  1197. return 0;
  1198. unsigned short meth_index = _Jv_FromReflectedMethod (meth) - methods;
  1199. while (true)
  1200. {
  1201. int type = read_u1 (bytes);
  1202. if (type == JV_DONE_ATTR)
  1203. return NULL;
  1204. int len = read_4 (bytes);
  1205. unsigned char *next = bytes + len;
  1206. if (type != JV_METHOD_ATTR)
  1207. {
  1208. bytes = next;
  1209. continue;
  1210. }
  1211. int kind = read_u1 (bytes, next);
  1212. if (kind != JV_ANNOTATION_DEFAULT_KIND)
  1213. {
  1214. bytes = next;
  1215. continue;
  1216. }
  1217. int index = read_u2 (bytes, next);
  1218. if (meth_index != index)
  1219. {
  1220. bytes = next;
  1221. continue;
  1222. }
  1223. // FIXME: could cache here. If we do then we have to clone any
  1224. // array result.
  1225. return parseAnnotationElement(this, &this->constants, bytes, next);
  1226. }
  1227. }
  1228. jobjectArray
  1229. java::lang::Class::getDeclaredAnnotations(jint /* jv_attr_type */ member_type,
  1230. jint member_index,
  1231. jint /* jv_attr_kind */ kind_req)
  1232. {
  1233. using namespace java::lang::annotation;
  1234. jobjectArray result;
  1235. unsigned char *bytes = reflection_data;
  1236. if (bytes == NULL)
  1237. return 0;
  1238. if (loader == NULL)
  1239. loader = (ClassLoader *)VMClassLoader::bootLoader;
  1240. result = (loader->getDeclaredAnnotations
  1241. (this, member_type, member_index, kind_req));
  1242. if (result)
  1243. return result;
  1244. for (;;)
  1245. {
  1246. int type = read_u1 (bytes);
  1247. if (type == JV_DONE_ATTR)
  1248. return NULL;
  1249. int len = read_4 (bytes);
  1250. unsigned char *next = bytes + len;
  1251. if (type != member_type)
  1252. {
  1253. bytes = next;
  1254. continue;
  1255. }
  1256. int kind = read_u1 (bytes, next);
  1257. if (kind != kind_req)
  1258. {
  1259. bytes = next;
  1260. continue;
  1261. }
  1262. if (member_type != JV_CLASS_ATTR)
  1263. {
  1264. int index = read_u2 (bytes, next);
  1265. if (member_index != index)
  1266. {
  1267. bytes = next;
  1268. continue;
  1269. }
  1270. }
  1271. if (kind_req == JV_PARAMETER_ANNOTATIONS_KIND)
  1272. result = ((parseParameterAnnotations
  1273. (this, &this->constants, bytes, next)));
  1274. else
  1275. result = ((parseAnnotations (this, &this->constants, bytes, next)));
  1276. break;
  1277. }
  1278. return (loader->putDeclaredAnnotations
  1279. (this, member_type, member_index, kind_req, result));
  1280. }
  1281. jobjectArray
  1282. java::lang::Class::getDeclaredAnnotations(::java::lang::reflect::Method *meth,
  1283. jboolean is_param)
  1284. {
  1285. unsigned short meth_index = _Jv_FromReflectedMethod (meth) - methods;
  1286. return getDeclaredAnnotations(JV_METHOD_ATTR, meth_index,
  1287. (is_param
  1288. ? JV_PARAMETER_ANNOTATIONS_KIND
  1289. : JV_ANNOTATIONS_KIND));
  1290. }
  1291. jobjectArray
  1292. java::lang::Class::getDeclaredAnnotations(::java::lang::reflect::Constructor *cons,
  1293. jboolean is_param)
  1294. {
  1295. unsigned short meth_index = _Jv_FromReflectedConstructor (cons) - methods;
  1296. return getDeclaredAnnotations(JV_METHOD_ATTR, meth_index,
  1297. (is_param
  1298. ? JV_PARAMETER_ANNOTATIONS_KIND
  1299. : JV_ANNOTATIONS_KIND));
  1300. }
  1301. jobjectArray
  1302. java::lang::Class::getDeclaredAnnotations(::java::lang::reflect::Field *fld)
  1303. {
  1304. unsigned short field_index = _Jv_FromReflectedField (fld) - fields;
  1305. return getDeclaredAnnotations(JV_FIELD_ATTR, field_index,
  1306. JV_ANNOTATIONS_KIND);
  1307. }
  1308. JArray< ::java::lang::annotation::Annotation *> *
  1309. java::lang::Class::getDeclaredAnnotationsInternal()
  1310. {
  1311. return (JArray< ::java::lang::annotation::Annotation *> *) getDeclaredAnnotations(JV_CLASS_ATTR, 0, JV_ANNOTATIONS_KIND);
  1312. }
  1313. static jclass
  1314. resolve_class_constant (jclass klass, _Jv_Constants *pool, int cpool_index)
  1315. {
  1316. check_constant (pool, cpool_index, JV_CONSTANT_Class);
  1317. // FIXME: what is the correct thing to do with an exception here?
  1318. return _Jv_Linker::resolve_pool_entry (klass, cpool_index, false).clazz;
  1319. }
  1320. jint
  1321. java::lang::Class::findInnerClassAttribute()
  1322. {
  1323. unsigned char *bytes = reflection_data;
  1324. if (bytes == NULL)
  1325. return -1;
  1326. while (true)
  1327. {
  1328. int type = read_u1 (bytes);
  1329. if (type == JV_DONE_ATTR)
  1330. break;
  1331. // After the type but before the length.
  1332. unsigned char *save = bytes;
  1333. int len = read_4 (bytes);
  1334. unsigned char *next = bytes + len;
  1335. if (type != JV_CLASS_ATTR)
  1336. {
  1337. bytes = next;
  1338. continue;
  1339. }
  1340. int kind = read_u1 (bytes, next);
  1341. if (kind != JV_INNER_CLASSES_KIND)
  1342. {
  1343. bytes = next;
  1344. continue;
  1345. }
  1346. return save - reflection_data;
  1347. }
  1348. return -1;
  1349. }
  1350. jint
  1351. java::lang::Class::findDeclaredClasses(JArray<jclass> *result,
  1352. jboolean publicOnly,
  1353. jint offset)
  1354. {
  1355. unsigned char *bytes = reflection_data + offset;
  1356. int len = read_4 (bytes);
  1357. unsigned char *next = bytes + len;
  1358. // Skip a byte.
  1359. read_u1 (bytes, next);
  1360. int n_classes = read_u2 (bytes, next);
  1361. int count = 0;
  1362. for (int i = 0; i < n_classes; ++i)
  1363. {
  1364. int inner_class_index = read_u2 (bytes, next);
  1365. int outer_class_index = read_u2 (bytes, next);
  1366. /*int inner_name_index = */ read_u2 (bytes, next);
  1367. int inner_flags = read_u2 (bytes, next);
  1368. if (inner_class_index == 0 || outer_class_index == 0)
  1369. continue;
  1370. if (resolve_class_constant (this, &constants, outer_class_index) == this)
  1371. {
  1372. jclass inner = resolve_class_constant (this, &constants,
  1373. inner_class_index);
  1374. if (! publicOnly
  1375. || ((inner_flags
  1376. & java::lang::reflect::Modifier::PUBLIC) != 0))
  1377. {
  1378. if (result)
  1379. {
  1380. jclass *elts = elements (result);
  1381. elts[count] = inner;
  1382. }
  1383. ++count;
  1384. }
  1385. }
  1386. }
  1387. return count;
  1388. }
  1389. JArray<jclass> *
  1390. java::lang::Class::getDeclaredClasses (jboolean publicOnly)
  1391. {
  1392. int offset = findInnerClassAttribute();
  1393. int count;
  1394. if (offset == -1)
  1395. {
  1396. // No InnerClasses attribute, so no declared classes.
  1397. count = 0;
  1398. }
  1399. else
  1400. count = findDeclaredClasses(NULL, publicOnly, offset);
  1401. JArray<jclass> *result
  1402. = (JArray<jclass> *) JvNewObjectArray (count, &java::lang::Class::class$,
  1403. NULL);
  1404. if (count > 0)
  1405. findDeclaredClasses(result, publicOnly, offset);
  1406. return result;
  1407. }
  1408. jclass
  1409. java::lang::Class::getDeclaringClass (void)
  1410. {
  1411. int offset = findInnerClassAttribute();
  1412. if (offset == -1)
  1413. return NULL;
  1414. unsigned char *bytes = reflection_data + offset;
  1415. int len = read_4 (bytes);
  1416. unsigned char *next = bytes + len;
  1417. // Skip a byte.
  1418. read_u1 (bytes, next);
  1419. int n_classes = read_u2 (bytes, next);
  1420. for (int i = 0; i < n_classes; ++i)
  1421. {
  1422. int inner_class_index = read_u2 (bytes, next);
  1423. int outer_class_index = read_u2 (bytes, next);
  1424. /*int inner_name_index = */read_u2 (bytes, next);
  1425. /*int inner_flags = */read_u2 (bytes, next);
  1426. if (inner_class_index == 0 || outer_class_index == 0)
  1427. continue;
  1428. if (resolve_class_constant (this, &constants, inner_class_index) == this)
  1429. return resolve_class_constant (this, &constants, outer_class_index);
  1430. }
  1431. return NULL;
  1432. }
  1433. jboolean
  1434. java::lang::Class::isAnonymousClass()
  1435. {
  1436. int offset = findInnerClassAttribute();
  1437. if (offset == -1)
  1438. return false;
  1439. unsigned char *bytes = reflection_data + offset;
  1440. int len = read_4 (bytes);
  1441. unsigned char *next = bytes + len;
  1442. // Skip a byte.
  1443. read_u1 (bytes, next);
  1444. int n_classes = read_u2 (bytes, next);
  1445. for (int i = 0; i < n_classes; ++i)
  1446. {
  1447. int inner_class_index = read_u2 (bytes, next);
  1448. /*int outer_class_index = */read_u2 (bytes, next);
  1449. int inner_name_index = read_u2 (bytes, next);
  1450. /*int inner_flags = */read_u2 (bytes, next);
  1451. if (inner_class_index == 0)
  1452. continue;
  1453. if (resolve_class_constant (this, &constants, inner_class_index) == this)
  1454. return inner_name_index == 0;
  1455. }
  1456. return false;
  1457. }
  1458. jboolean
  1459. java::lang::Class::isLocalClass()
  1460. {
  1461. _Jv_word indexes;
  1462. indexes.i = getEnclosingMethodData();
  1463. return indexes.i != 0;
  1464. }
  1465. jboolean
  1466. java::lang::Class::isMemberClass()
  1467. {
  1468. // FIXME: is this correct?
  1469. return !isLocalClass() && getDeclaringClass() != NULL;
  1470. }
  1471. //
  1472. // Some class-related convenience functions.
  1473. //
  1474. // Find a method declared in the class. If it is not declared locally
  1475. // (or if it is inherited), return NULL.
  1476. _Jv_Method *
  1477. _Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
  1478. _Jv_Utf8Const *signature)
  1479. {
  1480. for (int i = 0; i < klass->method_count; ++i)
  1481. {
  1482. if (_Jv_equalUtf8Consts (name, klass->methods[i].name)
  1483. && _Jv_equalUtf8Consts (signature, klass->methods[i].signature))
  1484. return &klass->methods[i];
  1485. }
  1486. return NULL;
  1487. }
  1488. _Jv_Method *
  1489. _Jv_LookupDeclaredMethod (jclass klass, _Jv_Utf8Const *name,
  1490. _Jv_Utf8Const *signature,
  1491. jclass *declarer_result)
  1492. {
  1493. for (; klass; klass = klass->getSuperclass())
  1494. {
  1495. _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
  1496. if (meth)
  1497. {
  1498. if (declarer_result)
  1499. *declarer_result = klass;
  1500. return meth;
  1501. }
  1502. }
  1503. return NULL;
  1504. }
  1505. java::lang::reflect::Method *
  1506. _Jv_GetReflectedMethod (jclass klass, _Jv_Utf8Const *name,
  1507. _Jv_Utf8Const *signature)
  1508. {
  1509. for (; klass; klass = klass->getSuperclass())
  1510. {
  1511. _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
  1512. if (meth)
  1513. {
  1514. using namespace java::lang::reflect;
  1515. Method *rmethod = new Method ();
  1516. rmethod->offset = (char*) meth - (char*) klass->methods;
  1517. rmethod->declaringClass = klass;
  1518. return rmethod;
  1519. }
  1520. }
  1521. return NULL;
  1522. }
  1523. #ifdef HAVE_TLS
  1524. // NOTE: MCACHE_SIZE should be a power of 2 minus one.
  1525. #define MCACHE_SIZE 31
  1526. struct _Jv_mcache
  1527. {
  1528. jclass klass;
  1529. _Jv_Method *method;
  1530. };
  1531. static __thread _Jv_mcache *method_cache;
  1532. #endif // HAVE_TLS
  1533. static void *
  1534. _Jv_FindMethodInCache (jclass klass MAYBE_UNUSED,
  1535. _Jv_Utf8Const *name MAYBE_UNUSED,
  1536. _Jv_Utf8Const *signature MAYBE_UNUSED)
  1537. {
  1538. #ifdef HAVE_TLS
  1539. _Jv_mcache *cache = method_cache;
  1540. if (cache)
  1541. {
  1542. int index = name->hash16 () & MCACHE_SIZE;
  1543. _Jv_mcache *mc = &cache[index];
  1544. _Jv_Method *m = mc->method;
  1545. if (mc->klass == klass
  1546. && _Jv_equalUtf8Consts (m->name, name)
  1547. && _Jv_equalUtf8Consts (m->signature, signature))
  1548. return mc->method->ncode;
  1549. }
  1550. #endif // HAVE_TLS
  1551. return NULL;
  1552. }
  1553. static void
  1554. _Jv_AddMethodToCache (jclass klass MAYBE_UNUSED,
  1555. _Jv_Method *method MAYBE_UNUSED)
  1556. {
  1557. #ifdef HAVE_TLS
  1558. if (method_cache == NULL)
  1559. method_cache = (_Jv_mcache *) _Jv_MallocUnchecked((MCACHE_SIZE + 1)
  1560. * sizeof (_Jv_mcache));
  1561. // If the allocation failed, just keep going.
  1562. if (method_cache != NULL)
  1563. {
  1564. int index = method->name->hash16 () & MCACHE_SIZE;
  1565. method_cache[index].method = method;
  1566. method_cache[index].klass = klass;
  1567. }
  1568. #endif // HAVE_TLS
  1569. }
  1570. // Free this thread's method cache. We explicitly manage this memory
  1571. // as the GC does not yet know how to scan TLS on all platforms.
  1572. void
  1573. _Jv_FreeMethodCache ()
  1574. {
  1575. #ifdef HAVE_TLS
  1576. if (method_cache != NULL)
  1577. {
  1578. _Jv_Free(method_cache);
  1579. method_cache = NULL;
  1580. }
  1581. #endif // HAVE_TLS
  1582. }
  1583. void *
  1584. _Jv_LookupInterfaceMethod (jclass klass, _Jv_Utf8Const *name,
  1585. _Jv_Utf8Const *signature)
  1586. {
  1587. using namespace java::lang::reflect;
  1588. void *ncode = _Jv_FindMethodInCache (klass, name, signature);
  1589. if (ncode != 0)
  1590. return ncode;
  1591. for (; klass; klass = klass->getSuperclass())
  1592. {
  1593. _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
  1594. if (! meth)
  1595. continue;
  1596. if (Modifier::isStatic(meth->accflags))
  1597. throw new java::lang::IncompatibleClassChangeError
  1598. (_Jv_GetMethodString (klass, meth));
  1599. if (Modifier::isAbstract(meth->accflags))
  1600. throw new java::lang::AbstractMethodError
  1601. (_Jv_GetMethodString (klass, meth));
  1602. if (! Modifier::isPublic(meth->accflags))
  1603. throw new java::lang::IllegalAccessError
  1604. (_Jv_GetMethodString (klass, meth));
  1605. _Jv_AddMethodToCache (klass, meth);
  1606. return meth->ncode;
  1607. }
  1608. throw new java::lang::IncompatibleClassChangeError;
  1609. }
  1610. // Fast interface method lookup by index.
  1611. void *
  1612. _Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, int method_idx)
  1613. {
  1614. _Jv_IDispatchTable *cldt = klass->idt;
  1615. int idx = iface->ioffsets[cldt->iindex] + method_idx;
  1616. return cldt->itable[idx];
  1617. }
  1618. jboolean
  1619. _Jv_IsAssignableFrom (jclass source, jclass target)
  1620. {
  1621. if (source == target)
  1622. return true;
  1623. // If target is array, so must source be.
  1624. while (target->isArray ())
  1625. {
  1626. if (! source->isArray())
  1627. return false;
  1628. target = target->getComponentType();
  1629. source = source->getComponentType();
  1630. }
  1631. if (target->isInterface())
  1632. {
  1633. // Abstract classes have no IDT, and IDTs provide no way to check
  1634. // two interfaces for assignability.
  1635. if (__builtin_expect
  1636. (source->idt == NULL || source->isInterface(), false))
  1637. return _Jv_InterfaceAssignableFrom (source, target);
  1638. _Jv_IDispatchTable *cl_idt = source->idt;
  1639. if (__builtin_expect ((target->ioffsets == NULL), false))
  1640. return false; // No class implementing TARGET has been loaded.
  1641. jshort cl_iindex = cl_idt->iindex;
  1642. if (cl_iindex < target->ioffsets[0])
  1643. {
  1644. jshort offset = target->ioffsets[cl_iindex];
  1645. if (offset != -1 && offset < cl_idt->itable_length
  1646. && cl_idt->itable[offset] == target)
  1647. return true;
  1648. }
  1649. return false;
  1650. }
  1651. // Primitive TYPE classes are only assignable to themselves.
  1652. if (__builtin_expect (target->isPrimitive() || source->isPrimitive(), false))
  1653. return false;
  1654. if (target == &java::lang::Object::class$)
  1655. return true;
  1656. else if (source->ancestors == NULL || target->ancestors == NULL)
  1657. {
  1658. // We need this case when either SOURCE or TARGET has not has
  1659. // its constant-time tables prepared.
  1660. // At this point we know that TARGET can't be Object, so it is
  1661. // safe to use that as the termination point.
  1662. while (source && source != &java::lang::Object::class$)
  1663. {
  1664. if (source == target)
  1665. return true;
  1666. source = source->getSuperclass();
  1667. }
  1668. }
  1669. else if (source->depth >= target->depth
  1670. && source->ancestors[source->depth - target->depth] == target)
  1671. return true;
  1672. return false;
  1673. }
  1674. // Interface type checking, the slow way. Returns TRUE if IFACE is a
  1675. // superinterface of SOURCE. This is used when SOURCE is also an interface,
  1676. // or a class with no interface dispatch table.
  1677. jboolean
  1678. _Jv_InterfaceAssignableFrom (jclass source, jclass iface)
  1679. {
  1680. for (int i = 0; i < source->interface_count; i++)
  1681. {
  1682. jclass source_interface = source->interfaces[i];
  1683. if (iface == source_interface
  1684. || _Jv_InterfaceAssignableFrom (source_interface, iface))
  1685. return true;
  1686. }
  1687. if (!source->isInterface()
  1688. && source->superclass
  1689. && _Jv_InterfaceAssignableFrom (source->superclass, iface))
  1690. return true;
  1691. return false;
  1692. }
  1693. jboolean
  1694. _Jv_IsInstanceOf(jobject obj, jclass cl)
  1695. {
  1696. if (__builtin_expect (!obj, false))
  1697. return false;
  1698. return _Jv_IsAssignableFrom (JV_CLASS (obj), cl);
  1699. }
  1700. void *
  1701. _Jv_CheckCast (jclass c, jobject obj)
  1702. {
  1703. if (__builtin_expect
  1704. (obj != NULL && ! _Jv_IsAssignableFrom(JV_CLASS (obj), c), false))
  1705. throw new java::lang::ClassCastException
  1706. ((new java::lang::StringBuffer
  1707. (obj->getClass()->getName()))->append
  1708. (JvNewStringUTF(" cannot be cast to "))->append
  1709. (c->getName())->toString());
  1710. return obj;
  1711. }
  1712. void
  1713. _Jv_CheckArrayStore (jobject arr, jobject obj)
  1714. {
  1715. if (obj)
  1716. {
  1717. JvAssert (arr != NULL);
  1718. jclass elt_class = (JV_CLASS (arr))->getComponentType();
  1719. if (elt_class == &java::lang::Object::class$)
  1720. return;
  1721. jclass obj_class = JV_CLASS (obj);
  1722. if (__builtin_expect
  1723. (! _Jv_IsAssignableFrom (obj_class, elt_class), false))
  1724. throw new java::lang::ArrayStoreException
  1725. ((new java::lang::StringBuffer
  1726. (JvNewStringUTF("Cannot store ")))->append
  1727. (obj_class->getName())->append
  1728. (JvNewStringUTF(" in array of type "))->append
  1729. (elt_class->getName())->toString());
  1730. }
  1731. }
  1732. jboolean
  1733. _Jv_IsAssignableFromSlow (jclass source, jclass target)
  1734. {
  1735. // First, strip arrays.
  1736. while (target->isArray ())
  1737. {
  1738. // If target is array, source must be as well.
  1739. if (! source->isArray ())
  1740. return false;
  1741. target = target->getComponentType ();
  1742. source = source->getComponentType ();
  1743. }
  1744. // Quick success.
  1745. if (target == &java::lang::Object::class$)
  1746. return true;
  1747. // Ensure that the classes have their supers installed.
  1748. _Jv_Linker::wait_for_state (source, JV_STATE_LOADING);
  1749. _Jv_Linker::wait_for_state (target, JV_STATE_LOADING);
  1750. do
  1751. {
  1752. if (source == target)
  1753. return true;
  1754. if (target->isPrimitive () || source->isPrimitive ())
  1755. return false;
  1756. if (target->isInterface ())
  1757. {
  1758. for (int i = 0; i < source->interface_count; ++i)
  1759. {
  1760. // We use a recursive call because we also need to
  1761. // check superinterfaces.
  1762. if (_Jv_IsAssignableFromSlow (source->getInterface (i), target))
  1763. return true;
  1764. }
  1765. }
  1766. source = source->getSuperclass ();
  1767. }
  1768. while (source != NULL);
  1769. return false;
  1770. }
  1771. // Lookup an interface method by name. This is very similar to
  1772. // purpose to _getMethod, but the interfaces are quite different. It
  1773. // might be a good idea for _getMethod to call this function.
  1774. //
  1775. // Return true of the method is found, with the class in FOUND_CLASS
  1776. // and the index in INDEX.
  1777. bool
  1778. _Jv_getInterfaceMethod (jclass search_class, jclass &found_class, int &index,
  1779. const _Jv_Utf8Const *utf_name,
  1780. const _Jv_Utf8Const *utf_sig)
  1781. {
  1782. for (jclass klass = search_class; klass; klass = klass->getSuperclass())
  1783. {
  1784. // FIXME: Throw an exception?
  1785. if (!klass->isInterface ())
  1786. return false;
  1787. int max = klass->method_count;
  1788. int offset = 0;
  1789. for (int i = 0; i < max; ++i)
  1790. {
  1791. // Skip <clinit> here, as it will not be in the IDT.
  1792. if (klass->methods[i].name->first() == '<')
  1793. continue;
  1794. if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
  1795. && _Jv_equalUtf8Consts (klass->methods[i].signature, utf_sig))
  1796. {
  1797. // Found it.
  1798. using namespace java::lang::reflect;
  1799. // FIXME: Method must be public. Throw an exception?
  1800. if (! Modifier::isPublic (klass->methods[i].accflags))
  1801. break;
  1802. found_class = klass;
  1803. // Interface method indexes count from 1.
  1804. index = offset + 1;
  1805. return true;
  1806. }
  1807. ++offset;
  1808. }
  1809. }
  1810. // If we haven't found a match, and this class is an interface, then
  1811. // check all the superinterfaces.
  1812. if (search_class->isInterface())
  1813. {
  1814. for (int i = 0; i < search_class->interface_count; ++i)
  1815. {
  1816. using namespace java::lang::reflect;
  1817. bool found = _Jv_getInterfaceMethod (search_class->interfaces[i],
  1818. found_class, index,
  1819. utf_name, utf_sig);
  1820. if (found)
  1821. return true;
  1822. }
  1823. }
  1824. return false;
  1825. }
  1826. #ifdef INTERPRETER
  1827. _Jv_MethodBase *
  1828. _Jv_FindInterpreterMethod (jclass klass, jmethodID desired_method)
  1829. {
  1830. using namespace java::lang::reflect;
  1831. _Jv_InterpClass *iclass
  1832. = reinterpret_cast<_Jv_InterpClass *> (klass->aux_info);
  1833. _Jv_MethodBase **imethods = _Jv_GetFirstMethod (iclass);
  1834. for (int i = 0; i < JvNumMethods (klass); ++i)
  1835. {
  1836. _Jv_MethodBase *imeth = imethods[i];
  1837. if (imeth->get_method () == desired_method)
  1838. return imeth;
  1839. }
  1840. return NULL;
  1841. }
  1842. #endif
  1843. // Return Utf8 name of a class. This function is here for code that
  1844. // can't access klass->name directly.
  1845. _Jv_Utf8Const*
  1846. _Jv_GetClassNameUtf8 (jclass klass)
  1847. {
  1848. return klass->name;
  1849. }
  1850. jclass
  1851. _Jv_GetMethodDeclaringClass (jmethodID method)
  1852. {
  1853. _Jv_StackTrace::UpdateNCodeMap ();
  1854. jobject obj = reinterpret_cast<jobject> (method->ncode);
  1855. return reinterpret_cast<jclass> (_Jv_StackTrace::ncodeMap->get (obj));
  1856. }
  1857. jbyte
  1858. _Jv_GetClassState (jclass klass)
  1859. {
  1860. return klass->state;
  1861. }
  1862. #ifdef INTERPRETER
  1863. jstring
  1864. _Jv_GetInterpClassSourceFile (jclass klass)
  1865. {
  1866. if (_Jv_IsInterpretedClass (klass))
  1867. {
  1868. _Jv_InterpClass *iclass =
  1869. reinterpret_cast<_Jv_InterpClass *> (klass->aux_info);
  1870. return iclass->source_file_name;
  1871. }
  1872. return NULL;
  1873. }
  1874. #endif