Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Roberto Burgos
- * CS211S
- * 11/19/2012
- * ClassReflection.java
- *
- * See Java doc.
- */
- package com.rburgosnavas.jr.classreflector;
- import java.lang.reflect.*;
- import java.util.ArrayList;
- /**
- * <p>
- * ClassReflector class is a Java class inspector that gets information about
- * any class. The name of the class is passed as a String. Several methods are
- * offered to get specific information about the class, for example a list of
- * all constructors, all public, private, or protected methods, etc.
- * </p>
- * <p>
- * For example:
- * <code>ClassReflector cr = new ClassReflector("java.lang.String")</code>
- * </p>
- * @author Roberto Burgos
- * @version 1
- */
- public class ClassReflector
- {
- private Class<?> c;
- private int numInterface, numConstructors, numMethods, numFields;
- /**
- * Constructor for ClassReflector which takes a String of the name of
- * the class that we need to get data from.
- * @param className String for the name of the class.
- */
- public ClassReflector(String className)
- {
- try
- {
- c = Class.forName(className);
- numInterface = c.getInterfaces().length;
- numConstructors = c.getDeclaredConstructors().length;
- numMethods = c.getDeclaredMethods().length;
- numFields = c.getDeclaredFields().length;
- }
- catch (ClassNotFoundException e)
- {
- System.err.println("Class not found.");
- System.exit(0);
- }
- }
- /**
- * Gets the name of the class.
- * @return The name of the class as a string
- */
- public String getClassName()
- {
- return c.getName();
- }
- /**
- * Gets the name of the superclass of the class in question
- * @return The name of the superclass as a String
- */
- public String getSuperclassName()
- {
- return c.getSuperclass().getName();
- }
- /**
- * Gets a list of all the interfaces implemented by the class in question.
- * @return An Object array of the list of interfaces.
- */
- public Object[] getInterfaces()
- {
- ArrayList<String> interfaces = new ArrayList<>();
- for (int i = 0; i < numInterface; i++)
- {
- interfaces.add(c.getInterfaces()[i].getName());
- }
- return interfaces.toArray();
- }
- /**
- * Gets all the constructors of the class in question.
- * @return An Object array of the list of all constructors.
- */
- public Object[] getAllConstructors()
- {
- ArrayList<String> constr = new ArrayList<>();
- for (int i = 0; i < numConstructors; i++)
- {
- constr.add(c.getDeclaredConstructors()[i].toString());
- }
- return constr.toArray();
- }
- /**
- * Gets all public constructors of the class in question.
- * @return An Object array of the list of all public constructors.
- */
- public Object[] getPublicConstructors()
- {
- ArrayList<String> constr = new ArrayList<>();
- for (int i = 0; i < numConstructors; i++)
- {
- if (Modifier.isPublic(
- c.getDeclaredConstructors()[i].getModifiers()))
- {
- constr.add(c.getDeclaredConstructors()[i].toString());
- }
- }
- return constr.toArray();
- }
- /**
- * Gets all private constructors of the class in question.
- * @return An Object array of the list of all private constructors.
- */
- public Object[] getPrivateConstructors()
- {
- ArrayList<String> constr = new ArrayList<>();
- for (int i = 0; i < numConstructors; i++)
- {
- if (Modifier.isPrivate(
- c.getDeclaredConstructors()[i].getModifiers()))
- {
- constr.add(c.getDeclaredConstructors()[i].toString());
- }
- }
- return constr.toArray();
- }
- /**
- * Gets all protected constructors of the class in question.
- * @return An Object array of the list of all protected constructors.
- */
- public Object[] getProtectedConstructors()
- {
- ArrayList<String> constr = new ArrayList<>();
- for (int i = 0; i < numConstructors; i++)
- {
- if (Modifier.isProtected(
- c.getDeclaredConstructors()[i].getModifiers()))
- {
- constr.add(c.getDeclaredConstructors()[i].toString());
- }
- }
- return constr.toArray();
- }
- /**
- * Gets all methods of the class in question.
- * @return An Object array of the list of all methods.
- */
- public Object[] getAllMethods()
- {
- ArrayList<String> methods = new ArrayList<>();
- for (int i = 0; i < numMethods; i++)
- {
- methods.add(c.getDeclaredMethods()[i].toString());
- }
- return methods.toArray();
- }
- /**
- * Gets all public methods of the class in question.
- * @return An Object array of the list of all public methods.
- */
- public Object[] getPublicMethods()
- {
- ArrayList<String> methods = new ArrayList<>();
- for (int i = 0; i < numMethods; i++)
- {
- if (Modifier.isPublic(c.getDeclaredMethods()[i].getModifiers()))
- {
- methods.add(c.getDeclaredMethods()[i].toString());
- }
- }
- return methods.toArray();
- }
- /**
- * Gets all private methods of the class in question.
- * @return An Object array of the list of all private methods.
- */
- public Object[] getPrivateMethods()
- {
- ArrayList<String> methods = new ArrayList<>();
- for (int i = 0; i < numMethods; i++)
- {
- if (Modifier.isPrivate(c.getDeclaredMethods()[i].getModifiers()))
- {
- methods.add(c.getDeclaredMethods()[i].toString());
- }
- }
- return methods.toArray();
- }
- /**
- * Gets all protected methods of the class in question.
- * @return An Object array of the list of all protected methods.
- */
- public Object[] getProtectedMethods()
- {
- ArrayList<String> methods = new ArrayList<>();
- for (int i = 0; i < numMethods; i++)
- {
- if (Modifier.isProtected(c.getDeclaredMethods()[i].getModifiers()))
- {
- methods.add(c.getDeclaredMethods()[i].toString());
- }
- }
- return methods.toArray();
- }
- /**
- * Gets all fields of the class in question.
- * @return An Object array of the list of all fields.
- */
- public Object[] getAllFields()
- {
- ArrayList<String> fields = new ArrayList<>();
- for (int i = 0; i < numFields; i++)
- {
- fields.add(c.getDeclaredFields()[i].toString());
- }
- return fields.toArray();
- }
- /**
- * Gets all public fields of the class in question.
- * @return An Object array of the list of all public fields.
- */
- public Object[] getPublicFields()
- {
- ArrayList<String> fields = new ArrayList<>();
- for (int i = 0; i < numFields; i++)
- {
- if (Modifier.isPublic(c.getDeclaredFields()[i].getModifiers()))
- {
- fields.add(c.getDeclaredFields()[i].toString());
- }
- }
- return fields.toArray();
- }
- /**
- * Gets all private fields of the class in question.
- * @return An Object array of the list of all private fields.
- */
- public Object[] getPrivateFields()
- {
- ArrayList<String> fields = new ArrayList<>();
- for (int i = 0; i < numFields; i++)
- {
- if (Modifier.isPrivate(c.getDeclaredFields()[i].getModifiers()))
- {
- fields.add(c.getDeclaredFields()[i].toString());
- }
- }
- return fields.toArray();
- }
- /**
- * Gets all protected fields of the class in question.
- * @return An Object array of the list of all protected fields.
- */
- public Object[] getProtectedFields()
- {
- ArrayList<String> fields = new ArrayList<>();
- for (int i = 0; i < numFields; i++)
- {
- if (Modifier.isProtected(c.getDeclaredFields()[i].getModifiers()))
- {
- fields.add(c.getDeclaredFields()[i].toString());
- }
- }
- return fields.toArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement