Advertisement
rburgosnavas

ClassReflector

Dec 4th, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.29 KB | None | 0 0
  1. /*
  2.  * Roberto Burgos
  3.  * CS211S
  4.  * 11/19/2012
  5.  * ClassReflection.java
  6.  *
  7.  * See Java doc.
  8.  */
  9. package com.rburgosnavas.jr.classreflector;
  10.  
  11. import java.lang.reflect.*;
  12. import java.util.ArrayList;
  13.  
  14. /**
  15.  * <p>
  16.  * ClassReflector class is a Java class inspector that gets information about
  17.  * any class. The name of the class is passed as a String. Several methods are
  18.  * offered to get specific information about the class, for example a list of
  19.  * all constructors, all public, private, or protected methods, etc.
  20.  * </p>
  21.  * <p>
  22.  * For example:
  23.  * <code>ClassReflector cr = new ClassReflector("java.lang.String")</code>
  24.  * </p>
  25.  * @author Roberto Burgos
  26.  * @version 1
  27.  */
  28. public class ClassReflector
  29. {
  30.  
  31.     private Class<?> c;
  32.     private int numInterface, numConstructors, numMethods, numFields;
  33.    
  34.     /**
  35.      * Constructor for ClassReflector which takes a String of the name of
  36.      * the class that we need to get data from.
  37.      * @param className String for the name of the class.
  38.      */
  39.     public ClassReflector(String className)
  40.     {
  41.         try
  42.         {
  43.             c = Class.forName(className);
  44.             numInterface = c.getInterfaces().length;
  45.             numConstructors = c.getDeclaredConstructors().length;
  46.             numMethods = c.getDeclaredMethods().length;
  47.             numFields = c.getDeclaredFields().length;
  48.         }
  49.         catch (ClassNotFoundException e)
  50.         {
  51.             System.err.println("Class not found.");
  52.             System.exit(0);
  53.         }
  54.     }
  55.    
  56.     /**
  57.      * Gets the name of the class.
  58.      * @return The name of the class as a string
  59.      */
  60.     public String getClassName()
  61.     {
  62.         return c.getName();    
  63.     }
  64.    
  65.     /**
  66.      * Gets the name of the superclass of the class in question
  67.      * @return The name of the superclass as a String
  68.      */
  69.     public String getSuperclassName()
  70.     {
  71.         return c.getSuperclass().getName();
  72.     }
  73.    
  74.     /**
  75.      * Gets a list of all the interfaces implemented by the class in question.
  76.      * @return An Object array of the list of interfaces.
  77.      */
  78.     public Object[] getInterfaces()
  79.     {
  80.         ArrayList<String> interfaces = new ArrayList<>();
  81.         for (int i = 0; i < numInterface; i++)
  82.         {
  83.             interfaces.add(c.getInterfaces()[i].getName());
  84.         }
  85.         return interfaces.toArray();
  86.     }
  87.    
  88.     /**
  89.      * Gets all the constructors of the class in question.
  90.      * @return An Object array of the list of all constructors.
  91.      */
  92.     public Object[] getAllConstructors()
  93.     {
  94.         ArrayList<String> constr = new ArrayList<>();
  95.         for (int i = 0; i < numConstructors; i++)
  96.         {
  97.             constr.add(c.getDeclaredConstructors()[i].toString()); 
  98.         }
  99.         return constr.toArray();
  100.     }
  101.    
  102.     /**
  103.      * Gets all public constructors of the class in question.
  104.      * @return An Object array of the list of all public constructors.
  105.      */
  106.     public Object[] getPublicConstructors()
  107.     {
  108.         ArrayList<String> constr = new ArrayList<>();
  109.         for (int i = 0; i < numConstructors; i++)
  110.         {
  111.             if (Modifier.isPublic(
  112.                     c.getDeclaredConstructors()[i].getModifiers()))
  113.             {
  114.                 constr.add(c.getDeclaredConstructors()[i].toString());             
  115.             }
  116.         }
  117.         return constr.toArray();
  118.     }  
  119.  
  120.     /**
  121.      * Gets all private constructors of the class in question.
  122.      * @return An Object array of the list of all private constructors.
  123.      */
  124.     public Object[] getPrivateConstructors()
  125.     {
  126.         ArrayList<String> constr = new ArrayList<>();
  127.         for (int i = 0; i < numConstructors; i++)
  128.         {
  129.             if (Modifier.isPrivate(
  130.                     c.getDeclaredConstructors()[i].getModifiers()))
  131.             {
  132.                 constr.add(c.getDeclaredConstructors()[i].toString());             
  133.             }
  134.         }
  135.         return constr.toArray();
  136.     }
  137.    
  138.     /**
  139.      * Gets all protected constructors of the class in question.
  140.      * @return An Object array of the list of all protected constructors.
  141.      */
  142.     public Object[] getProtectedConstructors()
  143.     {
  144.         ArrayList<String> constr = new ArrayList<>();
  145.         for (int i = 0; i < numConstructors; i++)
  146.         {
  147.             if (Modifier.isProtected(
  148.                     c.getDeclaredConstructors()[i].getModifiers()))
  149.             {
  150.                 constr.add(c.getDeclaredConstructors()[i].toString());             
  151.             }
  152.         }
  153.         return constr.toArray();
  154.     }
  155.    
  156.     /**
  157.      * Gets all methods of the class in question.
  158.      * @return An Object array of the list of all methods.
  159.      */
  160.     public Object[] getAllMethods()
  161.     {
  162.         ArrayList<String> methods = new ArrayList<>();
  163.         for (int i = 0; i < numMethods; i++)
  164.         {
  165.             methods.add(c.getDeclaredMethods()[i].toString());
  166.         }
  167.         return methods.toArray();
  168.     }
  169.    
  170.     /**
  171.      * Gets all public methods of the class in question.
  172.      * @return An Object array of the list of all public methods.
  173.      */
  174.     public Object[] getPublicMethods()
  175.     {
  176.         ArrayList<String> methods = new ArrayList<>();
  177.         for (int i = 0; i < numMethods; i++)
  178.         {
  179.             if (Modifier.isPublic(c.getDeclaredMethods()[i].getModifiers()))
  180.             {
  181.                 methods.add(c.getDeclaredMethods()[i].toString());             
  182.             }
  183.         }
  184.         return methods.toArray();
  185.     }
  186.    
  187.     /**
  188.      * Gets all private methods of the class in question.
  189.      * @return An Object array of the list of all private methods.
  190.      */
  191.     public Object[] getPrivateMethods()
  192.     {
  193.         ArrayList<String> methods = new ArrayList<>();
  194.         for (int i = 0; i < numMethods; i++)
  195.         {
  196.             if (Modifier.isPrivate(c.getDeclaredMethods()[i].getModifiers()))
  197.             {
  198.                 methods.add(c.getDeclaredMethods()[i].toString());             
  199.             }
  200.         }
  201.         return methods.toArray();
  202.     }
  203.    
  204.     /**
  205.      * Gets all protected methods of the class in question.
  206.      * @return An Object array of the list of all protected methods.
  207.      */
  208.     public Object[] getProtectedMethods()
  209.     {
  210.         ArrayList<String> methods = new ArrayList<>();
  211.         for (int i = 0; i < numMethods; i++)
  212.         {
  213.             if (Modifier.isProtected(c.getDeclaredMethods()[i].getModifiers()))
  214.             {
  215.                 methods.add(c.getDeclaredMethods()[i].toString());             
  216.             }
  217.         }
  218.         return methods.toArray();
  219.     }
  220.    
  221.     /**
  222.      * Gets all fields of the class in question.
  223.      * @return An Object array of the list of all fields.
  224.      */
  225.     public Object[] getAllFields()
  226.     {
  227.         ArrayList<String> fields = new ArrayList<>();
  228.         for (int i = 0; i < numFields; i++)
  229.         {
  230.             fields.add(c.getDeclaredFields()[i].toString());
  231.         }
  232.         return fields.toArray();
  233.     }
  234.    
  235.     /**
  236.      * Gets all public fields of the class in question.
  237.      * @return An Object array of the list of all public fields.
  238.      */
  239.     public Object[] getPublicFields()
  240.     {
  241.         ArrayList<String> fields = new ArrayList<>();
  242.         for (int i = 0; i < numFields; i++)
  243.         {
  244.             if (Modifier.isPublic(c.getDeclaredFields()[i].getModifiers()))
  245.             {
  246.                 fields.add(c.getDeclaredFields()[i].toString());
  247.             }          
  248.         }
  249.         return fields.toArray();
  250.     }
  251.    
  252.     /**
  253.      * Gets all private fields of the class in question.
  254.      * @return An Object array of the list of all private fields.
  255.      */
  256.     public Object[] getPrivateFields()
  257.     {
  258.         ArrayList<String> fields = new ArrayList<>();
  259.         for (int i = 0; i < numFields; i++)
  260.         {
  261.             if (Modifier.isPrivate(c.getDeclaredFields()[i].getModifiers()))
  262.             {
  263.                 fields.add(c.getDeclaredFields()[i].toString());
  264.             }          
  265.         }
  266.         return fields.toArray();
  267.     }
  268.    
  269.     /**
  270.      * Gets all protected fields of the class in question.
  271.      * @return An Object array of the list of all protected fields.
  272.      */
  273.     public Object[] getProtectedFields()
  274.     {
  275.         ArrayList<String> fields = new ArrayList<>();
  276.         for (int i = 0; i < numFields; i++)
  277.         {
  278.             if (Modifier.isProtected(c.getDeclaredFields()[i].getModifiers()))
  279.             {
  280.                 fields.add(c.getDeclaredFields()[i].toString());
  281.             }          
  282.         }
  283.         return fields.toArray();
  284.     }
  285.    
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement