1. Get Class object (Three possible ways)
1) Using Class.forName(className). The static method of Class class
package edu.xmu.service; import org.junit.Test; public class UserServiceTest { @Test public void test() { try { Class<?> userServiceClazz = Class.forName("edu.xmu.service.UserService"); System.out.println(userServiceClazz.getName()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2) Fetch the Class reference by calling a method that's part of the Object root class: getClass()
package edu.xmu.service; import org.junit.Test; public class UserServiceTest { @Test public void test() { UserService userService = new UserService(); Class<?> userServiceClazz = userService.getClass(); System.out.println(userServiceClazz.getName()); } }
3) Using class literals
package edu.xmu.service; import org.junit.Test; public class UserServiceTest { @Test public void test() { Class<?> userServiceClazz = UserService.class; System.out.println(userServiceClazz.getName()); } }
2. Instantiate Object Using Constructor (Two possible approaches)
1) Using newInstance() method in Class object for non-param constructor
package edu.xmu.service; import org.junit.Test; public class UserServiceTest { @Test public void test() throws InstantiationException, IllegalAccessException { Class<?> userServiceClazz = UserService.class; UserService userService = (UserService) userServiceClazz.newInstance(); userService.addUser(); } }
2) Using getConstructor(paramClasses) in Class object. Then use newInstance(paramValues) in Constructor instance.
package edu.xmu.service; import java.lang.reflect.Constructor; import org.junit.Test; public class UserServiceTest { @Test public void test() throws Exception { Class<?> userServiceClazz = UserService.class; Class<?>[] paramClasses = new Class<?>[] {String.class}; Object[] paramValues = new Object[] {"Davy"}; Constructor<?> cons = userServiceClazz.getConstructor(paramClasses); UserService userService = (UserService) cons.newInstance(paramValues); userService.addUser(); } }
Comments:
1) When we regard UserService as an interface, and the real Object created from cons.newInstance(...) as an implementation. Then that would be much more useful.
3. Get Methods and Invoke Methods
1) Possible ways to get Methods from Class object
package edu.xmu.service; import java.lang.reflect.Method; import org.junit.Test; public class UserServiceTest { @Test public void test() throws Exception { Class<?> userServiceClazz = UserService.class; Object userService = userServiceClazz.newInstance(); // Gets all public methods of the class including methods that derived // from parents // Method[] methods = userServiceClazz.getMethods(); // for (Method method : methods) // { // System.out.println(method.getName()); // } // Gets all methods of the class including methods that are private and // protected, excluding methods that derived from parents // Method[] methods = userServiceClazz.getDeclaredMethods(); // for (Method method : methods) // { // System.out.println(method.getName()); // } // getMethod(...) can only get method that is public in current class or // its parent class // userServiceClass.getMethod("addUser", new Class<?>[] {}); // Get method by methodName and method Params Types. Method method = userServiceClazz.getDeclaredMethod("getUser", new Class<?>[] { String.class }); // As the getUser method is private so we have to setAccessible(true) to // suppress access checking method.setAccessible(true); // Then invoke the method, userService is the instance in which the // method will be invoked method.invoke(userService, new Object[] { "Davy" }); } }
2) Possible ways to invoke a method in an object (Refer to Upper Example)
4. Get Fields From Class Object and Get Field Value From Specific Instance
package edu.xmu.service; import java.lang.reflect.Field; import org.junit.Test; public class UserServiceTest { @Test public void test() throws Exception { Class<?> userServiceClazz = UserService.class; UserService userService = (UserService) userServiceClazz.newInstance(); userService.setUsername("Davy"); Field[] fields = userServiceClazz.getDeclaredFields(); for(Field field : fields) { field.setAccessible(true); System.out.println(field.get(userService)); } } }
5. Get Annotations
package edu.xmu.service; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import org.junit.Test; public class UserServiceTest { @Test public void test() throws Exception { Class<?> userServiceClazz = UserService.class; Method method = userServiceClazz.getDeclaredMethod("getUser", new Class<?>[]{String.class}); Annotation[] annotations = method.getAnnotations(); for(Annotation annotation : annotations) { System.out.println(annotation.annotationType()); } // Annotations' length would be zero! // Think about the reason! // Because the annotation @SuppressWarnings is not @Retention(RetentionPolicy.RUNTIME) // It's more likely the RetentionPolicy.SOURCE or RetentionPolicy.COMPILE // We can only capture the annotation who is annotated as @Retention(RetentionPolicy.RUNTIME)! } }
P.S
1) Classes used
package edu.xmu.service; public class UserService { private String username; public UserService() { } public UserService(String username) { System.out.println(username); } public void addUser() { System.out.println("User added!"); } @SuppressWarnings("unused") private void getUser(String username) { System.out.println("User got!"); } protected void delUser() { System.out.println("User deleted!"); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
相关推荐
了解魔术般的技巧:RTTI初学者指南.flv
主要介绍了Java的RTTI运行时类型识别机制,包括泛化的Class引用以及类型检查instanceof等知识点,需要的朋友可以参考下
主要介绍了Java进阶教程之运行时类型识别RTTI机制,在Java运行时,RTTI维护类的相关信息,比如多态(polymorphism)就是基于RTTI实现的,需要的朋友可以参考下
主要涉及了Java的RTTI和反射机制代码分析的相关内容,在介绍运行时类型识别的同时,又向大家展示了其实例以及什么时候会用到反射机制,内容丰富,需要的朋友可以参考下。
Delphi的RTTI机制文档
MFC源代码 RTTI.02MFC源代码 RTTI.02MFC源代码 RTTI.02MFC源代码 RTTI.02MFC源代码 RTTI.02MFC源代码 RTTI.02MFC源代码 RTTI.02MFC源代码 RTTI.02MFC源代码 RTTI.02MFC源代码 RTTI.02MFC源代码 RTTI.02MFC源代码 ...
Rtti(反思)实践此回购包含rtti(反射库)实践
个人收集的RTTI机制....好的话就顶
blog文章:一种线程消息驱动及RTTI分发操作的示例
博客中使用的完整源码【MFC(2)】,仿真了 MFC 的 RTTI。博客地址: http://blog.csdn.net/raito__/article/details/51684826
delphi RTTI功能示例 演示如何在delphi 中使用rtti
在C++中如果使用多态继承类,对于多态性的对象,在程序编译时可能会出现无法确定对象的类型的情况,这事就需要通过RTTI动态识别对象的类型。因此,本文探讨了RTTI的使用细节。同时,由于有些平台C++编译器(如...
c++ RTTI深度解析, 运行时类型识别技术
MFC框架仿真,实现RTTI和动态创建
候老师讲的RTTI 资源难得 希望大家喜欢
随后介绍了Java中的高级技术,详细讨论了其中的多线程、集合、泛型、RTTI等。这些基本知识介绍完毕之后,就进入到实际编程阶段,先后介绍了GUI程序设计、多媒体程序设计、数据库和网络程序设计,这些知识均以示例...
Delphi RTTI usage example
RTTI应用:let's cast
MFC中的RTTI,动态创建,消息映射,消息传递。
运行时类型识别演示