⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 natclass.cc

📁 this gcc-g++-3.3.1.tar.gz is a source file of gcc, you can learn more about gcc through this codes f
💻 CC
📖 第 1 页 / 共 4 页
字号:
// natClass.cc - Implementation of java.lang.Class native methods./* Copyright (C) 1998, 1999, 2000, 2001, 2002  Free Software Foundation   This file is part of libgcj.This software is copyrighted work licensed under the terms of theLibgcj License.  Please consult the file "LIBGCJ_LICENSE" fordetails.  */#include <config.h>#include <limits.h>#include <string.h>#pragma implementation "Class.h"#include <gcj/cni.h>#include <jvm.h>#include <java-threads.h>#include <java/lang/Class.h>#include <java/lang/ClassLoader.h>#include <java/lang/String.h>#include <java/lang/reflect/Modifier.h>#include <java/lang/reflect/Member.h>#include <java/lang/reflect/Method.h>#include <java/lang/reflect/Field.h>#include <java/lang/reflect/Constructor.h>#include <java/lang/AbstractMethodError.h>#include <java/lang/ArrayStoreException.h>#include <java/lang/ClassCastException.h>#include <java/lang/ClassNotFoundException.h>#include <java/lang/ExceptionInInitializerError.h>#include <java/lang/IllegalAccessException.h>#include <java/lang/IllegalAccessError.h>#include <java/lang/IllegalArgumentException.h>#include <java/lang/IncompatibleClassChangeError.h>#include <java/lang/ArrayIndexOutOfBoundsException.h>#include <java/lang/InstantiationException.h>#include <java/lang/NoClassDefFoundError.h>#include <java/lang/NoSuchFieldException.h>#include <java/lang/NoSuchMethodError.h>#include <java/lang/NoSuchMethodException.h>#include <java/lang/Thread.h>#include <java/lang/NullPointerException.h>#include <java/lang/RuntimePermission.h>#include <java/lang/System.h>#include <java/lang/SecurityManager.h>#include <java/lang/StringBuffer.h>#include <gnu/gcj/runtime/StackTrace.h>#include <gcj/method.h>#include <gnu/gcj/runtime/MethodRef.h>#include <gnu/gcj/RawData.h>#include <java-cpool.h>using namespace gcj;jclassjava::lang::Class::forName (jstring className, jboolean initialize,                            java::lang::ClassLoader *loader){  if (! className)    throw new java::lang::NullPointerException;  jsize length = _Jv_GetStringUTFLength (className);  char buffer[length];  _Jv_GetStringUTFRegion (className, 0, length, buffer);  _Jv_Utf8Const *name = _Jv_makeUtf8Const (buffer, length);  if (! _Jv_VerifyClassName (name))    throw new java::lang::ClassNotFoundException (className);  jclass klass = (buffer[0] == '[' 		  ? _Jv_FindClassFromSignature (name->data, loader)		  : _Jv_FindClass (name, loader));  if (klass == NULL)    throw new java::lang::ClassNotFoundException (className);  if (initialize)    _Jv_InitClass (klass);  return klass;}jclassjava::lang::Class::forName (jstring className){  java::lang::ClassLoader *loader = NULL;  gnu::gcj::runtime::StackTrace *t     = new gnu::gcj::runtime::StackTrace(4);  java::lang::Class *klass = NULL;  try    {      for (int i = 1; !klass; i++)	{	  klass = t->classAt (i);	}      loader = klass->getClassLoader();    }  catch (::java::lang::ArrayIndexOutOfBoundsException *e)    {    }  return forName (className, true, loader);}java::lang::ClassLoader *java::lang::Class::getClassLoader (void){#if 0  // FIXME: the checks we need to do are more complex.  See the spec.  // Currently we can't implement them.  java::lang::SecurityManager *s = java::lang::System::getSecurityManager();  if (s != NULL)    s->checkPermission (new RuntimePermission (JvNewStringLatin1 ("getClassLoader")));#endif  // The spec requires us to return `null' for primitive classes.  In  // other cases we have the option of returning `null' for classes  // loaded with the bootstrap loader.  All gcj-compiled classes which  // are linked into the application used to return `null' here, but  // that confuses some poorly-written applications.  It is a useful  // and apparently harmless compatibility hack to simply never return  // `null' instead.  if (isPrimitive ())    return NULL;  return loader ? loader : ClassLoader::getSystemClassLoader ();}java::lang::reflect::Constructor *java::lang::Class::getConstructor (JArray<jclass> *param_types){  jstring partial_sig = getSignature (param_types, true);  jint hash = partial_sig->hashCode ();  int i = isPrimitive () ? 0 : method_count;  while (--i >= 0)    {      // FIXME: access checks.      if (_Jv_equalUtf8Consts (methods[i].name, init_name)	  && _Jv_equal (methods[i].signature, partial_sig, hash))	{	  // Found it.  For getConstructor, the constructor must be	  // public.	  using namespace java::lang::reflect;	  if (! Modifier::isPublic(methods[i].accflags))	    break;	  Constructor *cons = new Constructor ();	  cons->offset = (char *) (&methods[i]) - (char *) methods;	  cons->declaringClass = this;	  return cons;	}    }  throw new java::lang::NoSuchMethodException;}JArray<java::lang::reflect::Constructor *> *java::lang::Class::_getConstructors (jboolean declared){  // FIXME: this method needs access checks.  int numConstructors = 0;  int max = isPrimitive () ? 0 : method_count;  int i;  for (i = max; --i >= 0; )    {      _Jv_Method *method = &methods[i];      if (method->name == NULL	  || ! _Jv_equalUtf8Consts (method->name, init_name))	continue;      if (! declared	  && ! java::lang::reflect::Modifier::isPublic(method->accflags))	continue;      numConstructors++;    }  JArray<java::lang::reflect::Constructor *> *result    = (JArray<java::lang::reflect::Constructor *> *)    JvNewObjectArray (numConstructors,		      &java::lang::reflect::Constructor::class$,		      NULL);  java::lang::reflect::Constructor** cptr = elements (result);  for (i = 0;  i < max;  i++)    {      _Jv_Method *method = &methods[i];      if (method->name == NULL	  || ! _Jv_equalUtf8Consts (method->name, init_name))	continue;      if (! declared	  && ! java::lang::reflect::Modifier::isPublic(method->accflags))	continue;      java::lang::reflect::Constructor *cons	= new java::lang::reflect::Constructor ();      cons->offset = (char *) method - (char *) methods;      cons->declaringClass = this;      *cptr++ = cons;    }  return result;}java::lang::reflect::Constructor *java::lang::Class::getDeclaredConstructor (JArray<jclass> *param_types){  jstring partial_sig = getSignature (param_types, true);  jint hash = partial_sig->hashCode ();  int i = isPrimitive () ? 0 : method_count;  while (--i >= 0)    {      // FIXME: access checks.      if (_Jv_equalUtf8Consts (methods[i].name, init_name)	  && _Jv_equal (methods[i].signature, partial_sig, hash))	{	  // Found it.	  using namespace java::lang::reflect;	  Constructor *cons = new Constructor ();	  cons->offset = (char *) (&methods[i]) - (char *) methods;	  cons->declaringClass = this;	  return cons;	}    }  throw new java::lang::NoSuchMethodException;}java::lang::reflect::Field *java::lang::Class::getField (jstring name, jint hash){  java::lang::reflect::Field* rfield;  for (int i = 0;  i < field_count;  i++)    {      _Jv_Field *field = &fields[i];      if (! _Jv_equal (field->name, name, hash))	continue;      if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC))	continue;      rfield = new java::lang::reflect::Field ();      rfield->offset = (char*) field - (char*) fields;      rfield->declaringClass = this;      rfield->name = name;      return rfield;    }  jclass superclass = getSuperclass();  if (superclass == NULL)    return NULL;  rfield = superclass->getField(name, hash);  for (int i = 0; i < interface_count && rfield == NULL; ++i)    rfield = interfaces[i]->getField (name, hash);  return rfield;}java::lang::reflect::Field *java::lang::Class::getDeclaredField (jstring name){  java::lang::SecurityManager *s = java::lang::System::getSecurityManager();  if (s != NULL)    s->checkMemberAccess (this, java::lang::reflect::Member::DECLARED);  int hash = name->hashCode();  for (int i = 0;  i < field_count;  i++)    {      _Jv_Field *field = &fields[i];      if (! _Jv_equal (field->name, name, hash))	continue;      java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();      rfield->offset = (char*) field - (char*) fields;      rfield->declaringClass = this;      rfield->name = name;      return rfield;    }  throw new java::lang::NoSuchFieldException (name);}JArray<java::lang::reflect::Field *> *java::lang::Class::getDeclaredFields (void){  java::lang::SecurityManager *s = java::lang::System::getSecurityManager();  if (s != NULL)    s->checkMemberAccess (this, java::lang::reflect::Member::DECLARED);  JArray<java::lang::reflect::Field *> *result    = (JArray<java::lang::reflect::Field *> *)    JvNewObjectArray (field_count, &java::lang::reflect::Field::class$, NULL);  java::lang::reflect::Field** fptr = elements (result);  for (int i = 0;  i < field_count;  i++)    {      _Jv_Field *field = &fields[i];      java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();      rfield->offset = (char*) field - (char*) fields;      rfield->declaringClass = this;      *fptr++ = rfield;    }  return result;}voidjava::lang::Class::getSignature (java::lang::StringBuffer *buffer){  if (isPrimitive())    buffer->append((jchar) method_count);  else    {      jstring name = getName();      if (name->charAt(0) != '[')	buffer->append((jchar) 'L');      buffer->append(name);      if (name->charAt(0) != '[')	buffer->append((jchar) ';');    }}// This doesn't have to be native.  It is an implementation detail// only called from the C++ code, though, so maybe this is clearer.jstringjava::lang::Class::getSignature (JArray<jclass> *param_types,				 jboolean is_constructor){  java::lang::StringBuffer *buf = new java::lang::StringBuffer ();  buf->append((jchar) '(');  // A NULL param_types means "no parameters".  if (param_types != NULL)    {      jclass *v = elements (param_types);      for (int i = 0; i < param_types->length; ++i)	v[i]->getSignature(buf);    }  buf->append((jchar) ')');  if (is_constructor)    buf->append((jchar) 'V');  return buf->toString();}java::lang::reflect::Method *java::lang::Class::_getDeclaredMethod (jstring name,				       JArray<jclass> *param_types){  jstring partial_sig = getSignature (param_types, false);  jint p_len = partial_sig->length();  _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);  int i = isPrimitive () ? 0 : method_count;  while (--i >= 0)    {      if (_Jv_equalUtf8Consts (methods[i].name, utf_name)	  && _Jv_equaln (methods[i].signature, partial_sig, p_len)	  && (methods[i].accflags	      & java::lang::reflect::Modifier::INVISIBLE) == 0)	{	  // Found it.	  using namespace java::lang::reflect;	  Method *rmethod = new Method ();	  rmethod->offset = (char*) (&methods[i]) - (char*) methods;	  rmethod->declaringClass = this;	  return rmethod;	}    }  return NULL;}JArray<java::lang::reflect::Method *> *java::lang::Class::getDeclaredMethods (void){  int numMethods = 0;  int max = isPrimitive () ? 0 : method_count;  int i;  for (i = max; --i >= 0; )    {      _Jv_Method *method = &methods[i];      if (method->name == NULL	  || _Jv_equalUtf8Consts (method->name, clinit_name)	  || _Jv_equalUtf8Consts (method->name, init_name)	  || _Jv_equalUtf8Consts (method->name, finit_name)	  || (methods[i].accflags	      & java::lang::reflect::Modifier::INVISIBLE) != 0)	continue;      numMethods++;    }  JArray<java::lang::reflect::Method *> *result    = (JArray<java::lang::reflect::Method *> *)    JvNewObjectArray (numMethods, &java::lang::reflect::Method::class$, NULL);  java::lang::reflect::Method** mptr = elements (result);  for (i = 0;  i < max;  i++)    {      _Jv_Method *method = &methods[i];      if (method->name == NULL	  || _Jv_equalUtf8Consts (method->name, clinit_name)	  || _Jv_equalUtf8Consts (method->name, init_name)	  || _Jv_equalUtf8Consts (method->name, finit_name)	  || (methods[i].accflags	      & java::lang::reflect::Modifier::INVISIBLE) != 0)	continue;      java::lang::reflect::Method* rmethod	= new java::lang::reflect::Method ();      rmethod->offset = (char*) method - (char*) methods;      rmethod->declaringClass = this;      *mptr++ = rmethod;    }  return result;}jstringjava::lang::Class::getName (void){  char buffer[name->length + 1];    memcpy (buffer, name->data, name->length);   buffer[name->length] = '\0';  return _Jv_NewStringUTF (buffer);}JArray<jclass> *java::lang::Class::getClasses (void){  // FIXME: security checking.  // Until we have inner classes, it always makes sense to return an  // empty array.  JArray<jclass> *result    = (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,					   NULL);  return result;}JArray<jclass> *java::lang::Class::getDeclaredClasses (void){  checkMemberAccess (java::lang::reflect::Member::DECLARED);  // Until we have inner classes, it always makes sense to return an  // empty array.  JArray<jclass> *result    = (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,					   NULL);  return result;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -