* java/lang/reflect/Field.java (toString): Use
authortromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 24 Aug 2001 17:24:44 +0000 (17:24 +0000)
committertromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 24 Aug 2001 17:24:44 +0000 (17:24 +0000)
Method.appendClassName.
* java/lang/reflect/Constructor.java (toString): Use
Method.appendClassName.
* java/lang/reflect/Method.java: Reindented.
(appendClassName): New method.
(toString): Use it.
* defineclass.cc (handleMethod ): Initialize `throws' field of
method.
(read_one_method_attribute): Handle Exceptions attribute.
* java/lang/reflect/natMethod.cc (ClassClass): Removed.
(ObjectClass): Removed.
(getType): Compute `exception_types'.
* java/lang/Class.h (struct _Jv_Method): Added `throws' field.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@45153 138bc75d-0d04-0410-961f-82ee72b054a4

libjava/ChangeLog
libjava/defineclass.cc
libjava/java/lang/Class.h
libjava/java/lang/reflect/Constructor.java
libjava/java/lang/reflect/Field.java
libjava/java/lang/reflect/Method.java
libjava/java/lang/reflect/natMethod.cc
libjava/mauve-libgcj

index f7092d1..a058e68 100644 (file)
@@ -1,3 +1,20 @@
+2001-08-23  Tom Tromey  <tromey@redhat.com>
+
+       * java/lang/reflect/Field.java (toString): Use
+       Method.appendClassName.
+       * java/lang/reflect/Constructor.java (toString): Use
+       Method.appendClassName.
+       * java/lang/reflect/Method.java: Reindented.
+       (appendClassName): New method.
+       (toString): Use it.
+       * defineclass.cc (handleMethod ): Initialize `throws' field of
+       method.
+       (read_one_method_attribute): Handle Exceptions attribute.
+       * java/lang/reflect/natMethod.cc (ClassClass): Removed.
+       (ObjectClass): Removed.
+       (getType): Compute `exception_types'.
+       * java/lang/Class.h (struct _Jv_Method): Added `throws' field.
+
 2001-08-21  Anthony Green  <green@redhat.com>
 
         * java/lang/natClassLoader.cc (findClass): Search for
index edf14cb..7ef51dc 100644 (file)
@@ -526,10 +526,42 @@ void _Jv_ClassReader::read_one_method_attribute (int method_index)
 
   if (is_attribute_name (name, "Exceptions"))
     {
-      /* we ignore this for now */
-      skip (length);
+      _Jv_Method *method = reinterpret_cast<_Jv_Method *>
+       (&def->methods[method_index]);
+      if (method->throws != NULL)
+       throw_class_format_error ("only one Exceptions attribute allowed per method");
+
+      int num_exceptions = read2u ();
+      // We use malloc here because the GC won't scan the method
+      // objects.  FIXME this means a memory leak if we GC a class.
+      // (Currently we never do.)
+      _Jv_Utf8Const **exceptions =
+       (_Jv_Utf8Const **) _Jv_Malloc ((num_exceptions + 1) * sizeof (_Jv_Utf8Const *));
+
+      int out = 0;
+      _Jv_word *pool_data = def->constants.data;
+      for (int i = 0; i < num_exceptions; ++i)
+       {
+         try
+           {
+             int ndx = read2u ();
+             // JLS 2nd Ed. 4.7.5 requires that the tag not be 0.
+             if (ndx != 0)
+               {
+                 check_tag (ndx, JV_CONSTANT_Class);
+                 exceptions[out++] = pool_data[ndx].utf8; 
+               }
+           }
+         catch (java::lang::Throwable *exc)
+           {
+             _Jv_Free (exceptions);
+             throw exc;
+           }
+       }
+      exceptions[out] = NULL;
+      method->throws = exceptions;
     }
-  
+
   else if (is_attribute_name (name, "Code"))
     {
       int start_off = pos;
@@ -1206,6 +1238,7 @@ void _Jv_ClassReader::handleMethod
 
   // intialize...
   method->ncode = 0;
+  method->throws = NULL;
   
   if (verify)
     {
index ac99244..dfef0a6 100644 (file)
@@ -64,10 +64,18 @@ struct _Jv_Constants
 
 struct _Jv_Method
 {
+  // Method name.
   _Jv_Utf8Const *name;
+  // Method signature.
   _Jv_Utf8Const *signature;
+  // Access flags.
   _Jv_ushort accflags;
+  // Pointer to underlying function.
   void *ncode;
+  // NULL-terminated list of exception class names; can be NULL if
+  // there are none such.
+  _Jv_Utf8Const **throws;
+
   _Jv_Method *getNextMethod ()
   { return this + 1; }
 };
index 2d527c3..cd07a43 100644 (file)
@@ -77,11 +77,11 @@ public final class Constructor extends AccessibleObject implements Member
       StringBuffer b = new StringBuffer ();
       b.append(Modifier.toString(getModifiers()));
       b.append(" ");
-      b.append(getName());
+      Method.appendClassName (b, declaringClass);
       b.append("(");
       for (int i = 0; i < parameter_types.length; ++i)
        {
-         b.append(parameter_types[i].getName());
+         Method.appendClassName (b, parameter_types[i]);
          if (i < parameter_types.length - 1)
            b.append(",");
        }
index 76243f3..aad5148 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999, 2000  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -257,9 +257,9 @@ public final class Field extends AccessibleObject implements Member
        Modifier.toString(mods, sbuf);
        sbuf.append(' ');
       }
-    sbuf.append(getType().getName());
+    Method.appendClassName (sbuf, getType ());
     sbuf.append(' ');
-    sbuf.append(getDeclaringClass().getName());
+    Method.appendClassName (sbuf, getDeclaringClass());
     sbuf.append('.');
     sbuf.append(getName());
     return sbuf.toString();
index e616670..7bd0a31 100644 (file)
@@ -25,24 +25,24 @@ import gnu.gcj.RawData;
 public final class Method extends AccessibleObject implements Member
 {
   public boolean equals (Object obj)
-    {
-      if (! (obj instanceof Method))
-       return false;
-      Method m = (Method) obj;
-      return declaringClass == m.declaringClass && offset == m.offset;
-    }
+  {
+    if (! (obj instanceof Method))
+      return false;
+    Method m = (Method) obj;
+    return declaringClass == m.declaringClass && offset == m.offset;
+  }
 
   public Class getDeclaringClass ()
-    {
-      return declaringClass;
-    }
+  {
+    return declaringClass;
+  }
 
   public Class[] getExceptionTypes ()
-    {
-      if (exception_types == null)
-       getType();
-      return (Class[]) exception_types.clone();
-    }
+  {
+    if (exception_types == null)
+      getType();
+    return (Class[]) exception_types.clone();
+  }
 
   public native int getModifiers ();
 
@@ -51,62 +51,82 @@ public final class Method extends AccessibleObject implements Member
   private native void getType ();
 
   public Class[] getParameterTypes ()
-    {
-      if (parameter_types == null)
-       getType();
-      return (Class[]) parameter_types.clone();
-    }
+  {
+    if (parameter_types == null)
+      getType();
+    return (Class[]) parameter_types.clone();
+  }
 
   public Class getReturnType ()
-    {
-      if (return_type == null)
-       getType();
-      return return_type;
-    }
+  {
+    if (return_type == null)
+      getType();
+    return return_type;
+  }
 
   public int hashCode ()
-    {
-      // FIXME.
-      return getName().hashCode() + declaringClass.getName().hashCode();
-    }
+  {
+    // FIXME.
+    return getName().hashCode() + declaringClass.getName().hashCode();
+  }
 
   public native Object invoke (Object obj, Object[] args)
     throws IllegalAccessException, IllegalArgumentException,
-           InvocationTargetException;
+    InvocationTargetException;
+
+  // Append a class name to a string buffer.  We try to print the
+  // fully-qualified name, the way that a Java programmer would expect
+  // it to be written.  Weirdly, Class has no appropriate method for
+  // this.
+  static void appendClassName (StringBuffer buf, Class k)
+  {
+    if (k.isArray ())
+      {
+       appendClassName (buf, k.getComponentType ());
+       buf.append ("[]");
+      }
+    else
+      {
+       // This is correct for primitive and reference types.  Really
+       // we'd like `Main$Inner' to be printed as `Main.Inner', I
+       // think, but that is a pain.
+       buf.append (k.getName ());
+      }
+  }
 
   public String toString ()
-    {
-      if (parameter_types == null)
-       getType ();
-
-      StringBuffer b = new StringBuffer ();
-      Modifier.toString(getModifiers(), b);
-      b.append(" ");
-      b.append(return_type.getName());
-      b.append(" ");
-      b.append(declaringClass.getName());
-      b.append(".");
-      b.append(getName());
-      b.append("(");
-      for (int i = 0; i < parameter_types.length; ++i)
-       {
-         b.append(parameter_types[i].getName());
-         if (i < parameter_types.length - 1)
-           b.append(",");
-       }
-      b.append(")");
-      if (exception_types.length > 0)
-       {
-         b.append(" throws ");
-         for (int i = 0; i < exception_types.length; ++i)
-           {
-             b.append(exception_types[i].getName());
-             if (i < exception_types.length - 1)
-               b.append(",");
-           }
-       }
-      return b.toString();
-    }
+  {
+    if (parameter_types == null)
+      getType ();
+
+    StringBuffer b = new StringBuffer ();
+    Modifier.toString(getModifiers(), b);
+    b.append(" ");
+    appendClassName (b, return_type);
+    b.append(" ");
+    appendClassName (b, declaringClass);
+    b.append(".");
+    b.append(getName());
+    b.append("(");
+    for (int i = 0; i < parameter_types.length; ++i)
+      {
+       appendClassName (b, parameter_types[i]);
+       if (i < parameter_types.length - 1)
+         b.append(",");
+      }
+    b.append(")");
+    if (exception_types.length > 0)
+      {
+       b.append(" throws ");
+       for (int i = 0; i < exception_types.length; ++i)
+         {
+           appendClassName (b, exception_types[i]);
+           if (i < exception_types.length - 1)
+             b.append(",");
+         }
+      }
+    return b.toString();
+  }
 
   private Method ()
   {
index f269d2f..96cc132 100644 (file)
@@ -1,6 +1,6 @@
 // natMethod.cc - Native code for Method class.
 
-/* Copyright (C) 1998, 1999, 2000  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -38,10 +38,6 @@ details.  */
 #include <gcj/method.h>
 #include <gnu/gcj/RawData.h>
 
-// FIXME: remove these
-#define ObjectClass java::lang::Object::class$
-#define ClassClass java::lang::Class::class$
-
 #include <stdlib.h>
 
 #if USE_LIBFFI
@@ -200,14 +196,27 @@ java::lang::reflect::Method::getName ()
 void
 java::lang::reflect::Method::getType ()
 {
-  _Jv_GetTypesFromSignature (_Jv_FromReflectedMethod (this),
+  _Jv_Method *method = _Jv_FromReflectedMethod (this);
+  _Jv_GetTypesFromSignature (method,
                             declaringClass,
                             &parameter_types,
                             &return_type);
 
-  // FIXME: for now we have no way to get exception information.
-  exception_types = (JArray<jclass> *) JvNewObjectArray (0, &ClassClass,
-                                                        NULL);
+  int count = 0;
+  if (method->throws != NULL)
+    {
+      while (method->throws[count] != NULL)
+       ++count;
+    }
+
+  exception_types
+    = (JArray<jclass> *) JvNewObjectArray (count,
+                                          &java::lang::Class::class$,
+                                          NULL);
+  jclass *elts = elements (exception_types);
+  for (int i = 0; i < count; ++i)
+    elts[i] = _Jv_FindClassFromSignature (method->throws[i]->data,
+                                         declaringClass->getClassLoader ());
 }
 
 void
@@ -254,7 +263,7 @@ _Jv_GetTypesFromSignature (jmethodID method,
     }
 
   JArray<jclass> *args = (JArray<jclass> *)
-    JvNewObjectArray (numArgs, &ClassClass, NULL);
+    JvNewObjectArray (numArgs, &java::lang::Class::class$, NULL);
   jclass* argPtr = elements (args);
   for (ptr = sig->data; *ptr != '\0'; ptr++)
     {
index f073835..a80405c 100644 (file)
@@ -32,3 +32,4 @@ java.text.StringCharacterIterator.iter
 !java.text.DecimalFormatSymbols.DumpDefault12
 !java.text.DecimalFormatSymbols.GetSet12
 !java.text.resources
+!java.lang.Math