Java Reference
In-Depth Information
All the above methods require a security check before they can proceed
and so will interact with any installed security managersee " Security " on
page 677 . If no security manager is installed then all these methods will
be allowed. Typically, security managers will allow any code to invoke
methods that request information on the public members of a typethis
enforces the normal language level access rules. However, access to
non-public member information will usually be restricted to privileged
code within the system. Note that the only distinction made is between
public and non-public memberssecurity managers do not have enough
information to enforce protected or package-level access. If access is
not allowed, a SecurityException is thrown.
The following program lists the public fields, methods, and constructors
of a given class:
import java.lang.reflect.*;
public class ClassContents {
public static void main(String[] args) {
try {
Class<?> c = Class.forName(args[0]);
System.out.println(c);
printMembers(c.getFields());
printMembers(c.getConstructors());
printMembers(c.getMethods());
} catch (ClassNotFoundException e) {
System.out.println("unknown class: " +
args[0]);
}
}
private static void printMembers(Member[] mems) {
for (Member m : mems) {
if (m.getDeclaringClass() == Object.class)
continue;
String decl = m.toString();
System.out.print(" ");
System.out.println(strip(decl, "java.lang."));
}
 
Search WWH ::




Custom Search