1. 记录Java反射机制几篇写得不错的博客
2. 什么是反射?
在Java运行时环境中,对于任意一个类,能否知道这个类有哪些属性和方法?对于任意一个对象,能否调用它的任意一个方法?
答案是肯定的。
这种动态获取类的信息以及动态调用对象的方法的功能来自于Java语言的反射(Reflection)机制。
3. 反射机制主要提供的功能
(1)在运行时判断任意一个对象所属的类。
(2).在运行时构造任意一个类的对象。
(3).在运行时判断任意一个类所具有的成员变量和方法。
(4).在运行时调用任意一个对象的方法。
3. 示例
被反射的类
public class Book{
private final static String TAG = "BookTag";
private String name;
private String author;
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
public Book() {
}
private Book(String name, String author) {
this.name = name;
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
private String declaredMethod(int index) {
String string = null;
switch (index) {
case 0:
string = "I am declaredMethod 1 !";
break;
case 1:
string = "I am declaredMethod 2 !";
break;
default:
string = "I am declaredMethod 1 !";
}
return string;
}
}
反射逻辑封装在ReflectClass.java
public class ReflectClass {
private final static String TAG = "peter.log.ReflectClass";
// 创建对象
public static void reflectNewInstance() {
try {
Class<?> classBook = Class.forName("com.android.peter.reflectdemo.Book");
Object objectBook = classBook.newInstance();
Book book = (Book) objectBook;
book.setName("Android进阶之光");
book.setAuthor("刘望舒");
Log.d(TAG,"reflectNewInstance book = " + book.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 反射私有的构造方法
public static void reflectPrivateConstructor() {
try {
Class<?> classBook = Class.forName("com.android.peter.reflectdemo.Book");
Constructor<?> declaredConstructorBook = classBook.getDeclaredConstructor(String.class,String.class);
declaredConstructorBook.setAccessible(true);
Object objectBook = declaredConstructorBook.newInstance("Android开发艺术探索","任玉刚");
Book book = (Book) objectBook;
Log.d(TAG,"reflectPrivateConstructor book = " + book.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 反射私有属性
public static void reflectPrivateField() {
try {
Class<?> classBook = Class.forName("com.android.peter.reflectdemo.Book");
Object objectBook = classBook.newInstance();
Field fieldTag = classBook.getDeclaredField("TAG");
fieldTag.setAccessible(true);
String tag = (String) fieldTag.get(objectBook);
Log.d(TAG,"reflectPrivateField tag = " + tag);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 反射私有方法
public static void reflectPrivateMethod() {
try {
Class<?> classBook = Class.forName("com.android.peter.reflectdemo.Book");
Method methodBook = classBook.getDeclaredMethod("declaredMethod",int.class);
methodBook.setAccessible(true);
Object objectBook = classBook.newInstance();
String string = (String) methodBook.invoke(objectBook,0);
Log.d(TAG,"reflectPrivateMethod string = " + string);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 获得系统Zenmode值
public static int getZenMode() {
int zenMode = -1;
try {
Class<?> cServiceManager = Class.forName("android.os.ServiceManager");
Method mGetService = cServiceManager.getMethod("getService", String.class);
Object oNotificationManagerService = mGetService.invoke(null, Context.NOTIFICATION_SERVICE);
Class<?> cINotificationManagerStub = Class.forName("android.app.INotificationManager$Stub");
Method mAsInterface = cINotificationManagerStub.getMethod("asInterface",IBinder.class);
Object oINotificationManager = mAsInterface.invoke(null,oNotificationManagerService);
Method mGetZenMode = cINotificationManagerStub.getMethod("getZenMode");
zenMode = (int) mGetZenMode.invoke(oINotificationManager);
} catch (Exception ex) {
ex.printStackTrace();
}
return zenMode;
}
// 关闭手机
public static void shutDown() {
try {
Class<?> cServiceManager = Class.forName("android.os.ServiceManager");
Method mGetService = cServiceManager.getMethod("getService",String.class);
Object oPowerManagerService = mGetService.invoke(null,Context.POWER_SERVICE);
Class<?> cIPowerManagerStub = Class.forName("android.os.IPowerManager$Stub");
Method mShutdown = cIPowerManagerStub.getMethod("shutdown",boolean.class,String.class,boolean.class);
Method mAsInterface = cIPowerManagerStub.getMethod("asInterface",IBinder.class);
Object oIPowerManager = mAsInterface.invoke(null,oPowerManagerService);
mShutdown.invoke(oIPowerManager,true,null,true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void shutdownOrReboot(final boolean shutdown, final boolean confirm) {
try {
Class<?> ServiceManager = Class.forName("android.os.ServiceManager");
// 获得ServiceManager的getService方法
Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
// 调用getService获取RemoteService
Object oRemoteService = getService.invoke(null, Context.POWER_SERVICE);
// 获得IPowerManager.Stub类
Class<?> cStub = Class.forName("android.os.IPowerManager$Stub");
// 获得asInterface方法
Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
// 调用asInterface方法获取IPowerManager对象
Object oIPowerManager = asInterface.invoke(null, oRemoteService);
if (shutdown) {
// 获得shutdown()方法
Method shutdownMethod = oIPowerManager.getClass().getMethod(
"shutdown", boolean.class, String.class, boolean.class);
// 调用shutdown()方法
shutdownMethod.invoke(oIPowerManager, confirm, null, false);
} else {
// 获得reboot()方法
Method rebootMethod = oIPowerManager.getClass().getMethod("reboot",
boolean.class, String.class, boolean.class);
// 调用reboot()方法
rebootMethod.invoke(oIPowerManager, confirm, null, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
来源:CSDN
作者:生活不允许普通人内向
链接:https://blog.csdn.net/xiaoyu_wu/article/details/104117130