Class reflection : Class « Reflection « Java






Class reflection

Class reflection
         

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ShowClass {
  public static void main(String[] args) throws ClassNotFoundException {
    Class aClass = Class.forName("javax.swing.JComponent");
    if (aClass.isInterface()) {
      System.out.print(Modifier.toString(aClass.getModifiers()) + " "
          + typeName(aClass));
    } else if (aClass.getSuperclass() != null) {
      System.out.print(Modifier.toString(aClass.getModifiers()) + " class "
          + typeName(aClass) + " extends " + typeName(aClass.getSuperclass()));
    } else {    
      System.out.print(Modifier.toString(aClass.getModifiers()) + " class "
          + typeName(aClass));
    }

    Class[] interfaces = aClass.getInterfaces();
    if ((interfaces != null) && (interfaces.length > 0)) {
      if (aClass.isInterface())
        System.out.print(" extends ");
      else
        System.out.print(" implements ");
      for (int i = 0; i < interfaces.length; i++) {
        if (i > 0)
          System.out.print(", ");
        System.out.print(typeName(interfaces[i]));
      }
    }

    System.out.println(" {");

    Constructor[] constructors = aClass.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++)
      printMethodOrConstructor(constructors[i]);

    Field[] fields = aClass.getDeclaredFields(); 
    for (int i = 0; i < fields.length; i++)
      printField(fields[i]);

    Method[] methods = aClass.getDeclaredMethods(); 
    for (int i = 0; i < methods.length; i++)
      printMethodOrConstructor(methods[i]);

    System.out.println("}"); 
  }

  public static String typeName(Class t) {
    String brackets = "";
    while (t.isArray()) {
      brackets += "[]";
      t = t.getComponentType();
    }
    String name = t.getName();
    int pos = name.lastIndexOf('.');
    if (pos != -1)
      name = name.substring(pos + 1);
    return name + brackets;
  }

  public static String modifiers(int m) {
    if (m == 0)
      return "";
    else
      return Modifier.toString(m) + " ";
  }

  public static void printField(Field f) {
    System.out.println("  " + modifiers(f.getModifiers())
        + typeName(f.getType()) + " " + f.getName() + ";");
  }

  public static void printMethodOrConstructor(Member member) {
    Class returntype = null, parameters[], exceptions[];
    if (member instanceof Method) {
      Method m = (Method) member;
      returntype = m.getReturnType();
      parameters = m.getParameterTypes();
      exceptions = m.getExceptionTypes();
      System.out.print("  " + modifiers(member.getModifiers())
          + typeName(returntype) + " " + member.getName() + "(");
    } else {
      Constructor c = (Constructor) member;
      parameters = c.getParameterTypes();
      exceptions = c.getExceptionTypes();
      System.out.print("  " + modifiers(member.getModifiers())
          + typeName(c.getDeclaringClass()) + "(");
    }

    for (int i = 0; i < parameters.length; i++) {
      if (i > 0)
        System.out.print(", ");
      System.out.print(typeName(parameters[i]));
    }
    System.out.print(")");
    if (exceptions.length > 0)
      System.out.print(" throws ");
    for (int i = 0; i < exceptions.length; i++) {
      if (i > 0)
        System.out.print(", ");
      System.out.print(typeName(exceptions[i]));
    }
    System.out.println(";");
  }
}

           
         
    
    
    
    
    
    
    
    
  








Related examples in the same category

1.Class Reflection: class modifierClass Reflection: class modifier
2.Class Reflection: class nameClass Reflection: class name
3.Class Reflection: name for super classClass Reflection: name for super class
4.Object Reflection: create new instance
5.This class shows using Reflection to get a field from another classThis class shows using Reflection to get a field from another class
6.Show the class keyword and getClass() method in actionShow the class keyword and getClass() method in action
7.Simple Demonstration of a ClassLoader WILL NOT COMPILE OUT OF THE BOX
8.Demonstrate classFor to create an instance of an object
9.CrossRef prints a cross-reference about all classes named in argv
10.Make up a compilable version of a given Sun or other API
11.Show a couple of things you can do with a Class object
12.Reflect1 shows the information about the class named in argv
13.Show that you can, in fact, take the class of a primitive
14.JavaP prints structural information about classes
15.Provides a set of static methods that extend the Java metaobject
16.Demonstration of speed of reflexive versus programmatic invocation
17.Use reflection to get console char set
18.Load the class source location from Class.getResource()
19.Access the enclosing class from an inner class
20.Use reflection to dynamically discover the capabilities of a class.
21.Get the class By way of a string
22.Get the class By way of .class
23.Return a String representation of an object's overall identity
24.Manipulate Java class files in strange and mysterious ways
25.Class file reader for obtaining the parameter names for declared methods in a class
26.Convert a given String into the appropriate Class.
27.Manipulate Java classes
28.Encapsulates a class serialVersionUID and codebase.
29.Dump a class using Reflection
30.This program uses reflection to spy on objects
31.This program uses reflection to print all features of a classThis program uses reflection to print all features of a class
32.Get Unqualified Name
33.Return a paranthesis enclosed, comma sepearated String of all SimpleClass names in params.
34.Adds the class SimpleNames, comma sepearated and surrounded by paranthesis to the call StringBuffer
35.Class Finder