一道反射题,很全。
import java.lang.reflect.*;
public class utils {
static void printClassMessage(Object obj){
Class c = obj.getClass();
//获取方法名
Method[] methods = c.getMethods();
for(int i =0;i<methods.length;i++){
//获取方法的返回值
Class returnType = methods.getReturnType();
System.out.print(returnType.getSimpleName()+" ");
//获取方法的方法名
String methodName = methods.getName();
System.out.print(methodName+"(");
//获取参数信息
Class<?>[] paramTypes = methods.getParameterTypes();
int length = paramTypes.length;
for (int j = 0; j < length; j++) {
if (j == length - 1) {
System.out.print(paramTypes[j].getSimpleName());
break;
}
System.out.print(paramTypes[j].getSimpleName() + ",");
}
System.out.println(")");
}
}}
解析String类,输出结果:
int hashCode()
boolean equals(Object)
String toString()
char charAt(int)
int codePointAt(int)
int codePointBefore(int)
int codePointCount(int,int)
int compareTo(Object)
int compareTo(String)
int compareToIgnoreCase(String)
String concat(String)
boolean contains(CharSequence)
boolean contentEquals(CharSequence)
boolean contentEquals(StringBuffer)
String copyValueOf(char[],int,int)
String copyValueOf(char[])
boolean endsWith(String)
boolean equalsIgnoreCase(String)
String format(Locale,String,Object[])
String format(String,Object[])
byte[] getBytes(Charset)
byte[] getBytes(String)
void getBytes(int,int,byte[],int)
byte[] getBytes()
void getChars(int,int,char[],int)
int indexOf(String)
int indexOf(int,int)
int indexOf(int)
int indexOf(String,int)
String intern()
boolean isEmpty()
int lastIndexOf(int)
int lastIndexOf(int,int)
int lastIndexOf(String)
int lastIndexOf(String,int)
int length()
boolean matches(String)
int offsetByCodePoints(int,int)
boolean regionMatches(int,String,int,int)
boolean regionMatches(boolean,int,String,int,int)
String replace(char,char)
String replace(CharSequence,CharSequence)
String replaceAll(String,String)
String replaceFirst(String,String)
String[] split(String)
String[] split(String,int)
boolean startsWith(String)
boolean startsWith(String,int)
CharSequence subSequence(int,int)
String substring(int,int)
String substring(int)
char[] toCharArray()
String toLowerCase(Locale)
String toLowerCase()
String toUpperCase()
String toUpperCase(Locale)
String trim()
String valueOf(double)
String valueOf(long)
String valueOf(float)
String valueOf(char)
String valueOf(boolean)
String valueOf(int)
String valueOf(Object)
String valueOf(char[],int,int)
String valueOf(char[])
Class getClass()
void notify()
void notifyAll()
void wait(long)
void wait(long,int)
void wait()
|
|