Thinking in Java 8 - 리 플 렉 션
It's important to realize that there's nothing magic about reflection. When you're using reflection to interact with an object of an unknown type, the JVM will simply look at the object and see that it belongs to a particular class (just like ordinary RTTI). Before anything can be done with it, the Class object must be loaded. Thus, the .class file for that particular type must still be available to the JVM, either on the local machine or across the network. So the true difference between RTTI and reflection is that with RTTI, the compiler opens and examines the .class file at compile time. Put another way, you can call all the methods of an object in the "normal" way. With reflection, the .class file is unavailable at complile time; it is opened and examined by the runtime environment.
A class method extractor Normally you won't need to use the reflection tools directly, but they can be helpful when you need create more dynamic code. Reflection is in the language to support other Java features, such as object serialization and JavaBeans. However, there are times when it's quite useful to dynamically extract information about a class. Consider a class method extractor. Looking at a class definition source code or JDK documentation shows only the methods that are defined or overriddedn within that class definition. But there might be dozens more available to you that have come from base classes. To locate these is both tedious and time consuming. Fortunately, reflection provides a way to write a simple tool that will automatcially show you the entire interface.
import java.lang.reflect.*;
import java.util.regex.*;
public class ShowMethods {
private static String usage = "usage:
" + "ShowMethods qualified.class.name
" + "To show all methods in class or:
" +
"ShowMethods qualified.class.name word
" + "To search for methods involving 'word'";
private static Pattern p = Pattern.compile("\\w+\\.");
public static void main(String[] args){
if(args.length < 1){
System.out.println(usage);
System.exit(0);
}
int lines = 0;
try{
Class<?> c = Class.forName(args[0]);
Method[] methods = c.getMethods();
Constructor[] ctors = c.getConstructors();
if(args.length == 1){
for(Method method : methods){
System.out.println(p.matcher(method.toString()).replaceAll(""));
System.out.println("==" + method.toString());
}
for(Constructor ctor : ctors){
System.out.println(p.matcher(ctor.toString()).replaceAll(""));
}
lines = methods.length + ctors.length;
} else {
for(Method method : methods){
if(method.toString().indexOf(args[1]) != -1){
System.out.println(p.matcher(method.toString()).replaceAll(""));
lines++;
}
}
}
} catch(ClassNotFoundException e){
System.out.println("No such class: " + e);
}
}
}
/* Output:
public static void main(String[])
==public static void ShowMethods.main(java.lang.String[])
public native int hashCode()
==public native int java.lang.Object.hashCode()
public final native Class getClass()
==public final native java.lang.Class java.lang.Object.getClass()
public final void wait() throws InterruptedException
==public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void wait(long,int) throws InterruptedException
==public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void wait(long) throws InterruptedException
==public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean equals(Object)
==public boolean java.lang.Object.equals(java.lang.Object)
public final native void notify()
==public final native void java.lang.Object.notify()
public final native void notifyAll()
==public final native void java.lang.Object.notifyAll()
public String toString()
==public java.lang.String java.lang.Object.toString()
public ShowMethods()
*/
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.