Patch: FYI: Class.getDeclaredMethod

Tom Tromey tromey@redhat.com
Tue Sep 3 14:19:00 GMT 2002


I'm checking this in.

This patch updates Class.getDeclaredMethod to do the appropriate
SecurityManager checks.  There are still more instances of this
problem in Class (and elsewhere); this one just happened to catch my
notice today.  This patch also fixes a small bug in how _getMethod was
declared in Class.h.

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>

	* java/lang/Class.h (_getDeclaredMethod): Declare.
	(_getMethod): Now private.
	* java/lang/natClass.cc (_getDeclaredMethod): Renamed from
	getDeclaredMethod.  Now returns NULL on failure.
	* java/lang/Class.java (_getDeclaredMethod): Declare.
	(getDeclaredMethod): No longer native; implements access checks.

Index: java/lang/Class.h
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/Class.h,v
retrieving revision 1.47
diff -u -r1.47 Class.h
--- java/lang/Class.h 28 Aug 2002 20:05:34 -0000 1.47
+++ java/lang/Class.h 3 Sep 2002 20:59:10 -0000
@@ -160,6 +160,9 @@
   java::lang::reflect::Method *getPrivateMethod (jstring, JArray<jclass> *);
   java::security::ProtectionDomain *getProtectionDomain0 ();
 
+  java::lang::reflect::Method *_getMethod (jstring, JArray<jclass> *);
+  java::lang::reflect::Method *_getDeclaredMethod (jstring, JArray<jclass> *);
+
 public:
   JArray<java::lang::reflect::Field *> *getFields (void);
 
@@ -167,7 +170,6 @@
 
   void getSignature (java::lang::StringBuffer *buffer);
   static jstring getSignature (JArray<jclass> *, jboolean is_constructor);
-  java::lang::reflect::Method *_getMethod (jstring, JArray<jclass> *);
   JArray<java::lang::reflect::Method *> *getMethods (void);
 
   inline jint getModifiers (void)
Index: java/lang/Class.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/Class.java,v
retrieving revision 1.13
diff -u -r1.13 Class.java
--- java/lang/Class.java 5 Jul 2002 20:40:11 -0000 1.13
+++ java/lang/Class.java 3 Sep 2002 20:59:10 -0000
@@ -65,9 +65,31 @@
   public native Field getDeclaredField (String fieldName)
     throws NoSuchFieldException, SecurityException;
   public native Field[] getDeclaredFields () throws SecurityException;
-  public native Method getDeclaredMethod (String methodName,
-					  Class[] parameterTypes)
-    throws NoSuchMethodException, SecurityException;
+
+  private native Method _getDeclaredMethod (String methodName,
+					    Class[] parameterTypes);
+
+  public Method getDeclaredMethod (String methodName, Class[] parameterTypes)
+    throws NoSuchMethodException, SecurityException
+  {
+    SecurityManager sm = System.getSecurityManager();
+    if (sm != null)
+      {
+	sm.checkMemberAccess(this, Member.DECLARED);
+	Package p = getPackage();
+	if (p != null)
+	  sm.checkPackageAccess(p.getName());
+      }
+
+    if ("<init>".equals(methodName) || "<clinit>".equals(methodName))
+      throw new NoSuchMethodException(methodName);
+
+    Method m = _getDeclaredMethod(methodName, parameterTypes);
+    if (m == null)
+      throw new NoSuchMethodException (methodName);
+    return m;
+  }
+
   public native Method[] getDeclaredMethods () throws SecurityException;
 
   // This is marked as unimplemented in the JCL book.
Index: java/lang/natClass.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natClass.cc,v
retrieving revision 1.52
diff -u -r1.52 natClass.cc
--- java/lang/natClass.cc 5 Jul 2002 20:40:11 -0000 1.52
+++ java/lang/natClass.cc 3 Sep 2002 20:59:11 -0000
@@ -315,8 +315,8 @@
 }
 
 java::lang::reflect::Method *
-java::lang::Class::getDeclaredMethod (jstring name,
-				      JArray<jclass> *param_types)
+java::lang::Class::_getDeclaredMethod (jstring name,
+				       JArray<jclass> *param_types)
 {
   jstring partial_sig = getSignature (param_types, false);
   jint p_len = partial_sig->length();
@@ -324,7 +324,6 @@
   int i = isPrimitive () ? 0 : method_count;
   while (--i >= 0)
     {
-      // FIXME: access checks.
       if (_Jv_equalUtf8Consts (methods[i].name, utf_name)
 	  && _Jv_equaln (methods[i].signature, partial_sig, p_len))
 	{
@@ -336,7 +335,7 @@
 	  return rmethod;
 	}
     }
-  throw new java::lang::NoSuchMethodException;
+  return NULL;
 }
 
 JArray<java::lang::reflect::Method *> *



More information about the Java-patches mailing list