/**
* the tools of looking for the properties of a class
* print the constructor and method
*
* @author Roy
* @since 2007-09-10
* @version 1.0
*/
public class GetProperties {
/**
* return the parameters of the specified methods,constructors or fields fit
* the jdk1.4
*
* @param acceObject AccessibleObject
* @return
* @author Roy
*/
private static String getParameter(AccessibleObject acceObject) {
String result = "";
Class[] parameters = null;
if (acceObject instanceof Method) {
parameters = ((Method) acceObject).getParameterTypes();
}
if (acceObject instanceof Constructor) {
parameters = ((Constructor) acceObject).getParameterTypes();
}
int paramCount = parameters.length;
// if no parameters
if (paramCount < 1) {
return result;
}
StringBuffer str = new StringBuffer();
for (int i = 0; i < paramCount; i++) {
String s = parameters[i].getName();// generally,the s is contain
str.append(getShortName(s)).append(",");
}
if (str.length() > 1)//delete the last ","
result = str.substring(0, str.length() - 1);
return result;
}
/**
* print the field
* @param object Class
*/
public static void printFields(Class object){
System.out
.println("----------------------Fileds----------------------");
StringBuffer str = new StringBuffer();
Field[] field = object.getDeclaredFields();
for(int i=0;i<field.length; i++){
int type =field[i].getModifiers();
str.append(Modifier.toString(type)).append(" ");
Class declared =field[i].getType();
str.append(getShortName(declared.getName())).append(" ");
str.append(field[i].getName()).append("\n");
//str.append(field[i].get(field.[i]).toString()).append("\n");
//System.out.println(field[i].toString());
}
System.out.print(str.toString());
}
/**
* print the construct function and its parameters
*
* @param object Class
* @return
*/
public static void printConstructs(Class object) {
System.out
.println("----------------------Constructors----------------------");
Constructor[] cos = object.getConstructors();
for (int i = 0; i < cos.length; i++) {
System.out.print(cos[i].getName() + "(");
System.out.print(getParameter(cos[i]));
System.out.print(")" + "\n");
}
}
/**
* print the declared method and its parameter
*
* @param object Class
*/
public static void printMethods(Class object) {
System.out
.println("----------------------Methods----------------------");
StringBuffer str = new StringBuffer();
Method[] methods = object.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
int type = methods[i].getModifiers();// Returns the Java language
// modifiers
// The Modifier class should be used to decode the modifiers
if(type != 0)
str.append(Modifier.toString(type)).append(" ");
/**
* if the return type is a array,the str is "[*", for example the
* function" int[] getNumber(){..}" the
* "methods[i].getReturnType().getName()" equals "[I"
*
* @param str String
*/
private static String getShortName(String str) {
StringBuffer result = new StringBuffer();
int i = -50;
if (str.indexOf("[") != -1) {//array type
if (str.length() == 2) {
switch (str.charAt(1)) {
case 'B':
result.append("byte[]");
break;
case 'S':
result.append("short[]");
break;
case 'I':
result.append("int[]");
break;
case 'C':
result.append("char[]");
break;
case 'J':
result.append("long[]");
break;
case 'F':
result.append("float[]");
break;
case 'D':
result.append("double[]");
break;
}
} else {
if ((i = str.lastIndexOf(".")) != -1) {//because the end of the str is ";"
result.append(str.substring(i + 1,str.length()-1)).append("[]");
}
}
} else {//no array type
if ((i = str.lastIndexOf(".")) != -1) {
return str.substring(i + 1);
}
return str;
}
return result.toString();
}
public static void main(String[] args) throws Exception {
if(args.length<1){
StringBuffer str=new StringBuffer();
str.append("usage:java GetProperties ARGS").append("\n");
str.append(" the args contain the package:").append("\n");
str.append(" example: java GetProperties java.lang.String");
System.out.println(str.toString());
System.exit(0);
}
public void getType(Object object){
int length= object.getClass().getClassName().lastIndexOf(".");
String type = object.getClass().getClassName().subSstring(length+1);
System.out.prinfln(type);
}