Question: Extend the code of Figure 16.4 to print information about (a) Fields (b) Constructors (c) Nested classes (d) Implemented interfaces (e) Ancestor classes, and their

Extend the code of Figure 16.4 to print information about

(a) Fields

(b) Constructors

(c) Nested classes

(d) Implemented interfaces

(e) Ancestor classes, and their methods, fields, and constructors

(f) Exceptions thrown by methods

(g) Generic type parameters

Figure 16.4:

import static java.lang.System.out; public static void listMethods (String s) throws java.lang.ClassNotFoundException { Class c = Class.forName (s); for (Method m : c.getDeclaredMethods ()) { out.print (Modifier.toString (m.getModifiers ()) +

import static java.lang.System.out; public static void listMethods (String s) throws java.lang.ClassNotFoundException { Class c = Class.forName (s); for (Method m : c.getDeclaredMethods ()) { out.print (Modifier.toString (m.getModifiers ()) + " "); out .print (m.getReturnType ().getName () + " "); out.print (m.getName () + "("); // throws if class not found boolean first = true; for (Class p : m.getParameterTypes ()) { if (!first) out.print (", "); first = false; out.print (p.getName ()); out.println(") "); Sample output for listMethods ("java.lang.reflect.Accessible0bject"): public java.lang.annotation. Annotation getAnnotation (java.lang.Class) public boolean isAnnotationPresent (java.lang.Class) public [Ljava.lang.annotation. Annotation; getAnnotations () public [Ljava.lang.annotation. Annotation; getDeclaredAnnotations () public static void setAccessible( [Ljava.lang.reflect. Accessible0bject;, boolean) public void setAccessible (boolean) private static void setAccessible0(java.lang.reflect.Accessible0bject, boolean) public boolean isAccessible ()

Step by Step Solution

3.41 Rating (157 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

ANSWER import static javalangSystemout public static void listMethods String s throws javalangClassNotFoundException Class c ClassforNames throws if c... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Language Pragmatics Questions!