学习java很长时间了,但是比较惭愧,很少运用java比较神秘的反射机制来开发项目,就是平时的娱乐也没用到过。但是现在我终于可以摆脱这个魔咒了,哈哈。现在也跟大家一起分享一下,java反射的奥妙。
JAVA反射机制定义:
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
Java反射机制主要提供了以下功能: 在运行时判断任意一个对象所属的类;在运行时构造任意一个类的对象;在运行时判断任意一个类所具有的成员变量和方法;在运行时调用任意一个对象的方法;生成动态代理。
有时候我们说某个语言具有很强的动态性,有时候我们会区分动态和静态的不同技术与作法。我们朗朗上口动态绑定(dynamic binding)、动态链接(dynamic linking)、动态加载(dynamic loading)等。然而“动态”一词其实没有绝对而普遍适用的严格定义,有时候甚至像对象导向当初被导入编程领域一样,一人一把号,各吹各的调。
一般而言,开发者社群说到动态语言,大致认同的一个定义是:“程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言”。从这个观点看,Perl,Python,Ruby是动态语言,C++,Java,C#不是动态语言。
尽管在这样的定义与分类下Java不是动态语言,它却有着一个非常突出的动态相关机制:Reflection。这个字的意思是“反射、映象、倒影”,用在Java身上指的是我们可以于运行时加载、探知、使用编译期间完全未知的classes。换句话说,Java程序可以加载一个运行时才得知名称的class,获悉其完整构造(但不包括methods定义),并生成其对象实体、或对其fields设值、或唤起其methods1。这种“看透class”的能力(the ability of the program to examine itself)被称为introspection(内省、内观、反省)。Reflection和introspection是常被并提的两个术语。
现在我们不来虚的了,来点实际东西让大家体验一下java反射机制的应用。
首先搞个数据库,用mysql数据库哦:
CREATE DATABASE test USE `test`; /*Table structure for table `sync_config` */ DROP TABLE IF EXISTS `sync_config`; CREATE TABLE `sync_config` ( `rkey` varchar(255) NOT NULL DEFAULT '', `rvalue` varchar(255) NOT NULL DEFAULT '', `desc` varchar(255) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; /*Data for the table `sync_config` */ insert into `sync_config`(`rkey`,`rvalue`,`desc`) values ('ServerMaxCopyTaskCount','15',NULL),('NormalJobHarddiskRate','90',NULL),('ClientJobHarddiskRate','99',NULL),('ServerMaxDelTaskCount','50',NULL),('ClientMaxDelTaskCount','50',NULL),('ClientMaxCopyTaskCount','50',NULL),('MinSrcCount','3',NULL),('TouchTimeInSecond','5',NULL),('sync_version','0.11',NULL),('http_443_version','0',NULL),('http_80_version','1.10.1124.0',NULL),('NormalJobDeleteRate','1',NULL),('isStarted_CheckTimeoutServerCron','1',NULL),('isStarted_DelFileCron','1',NULL),('isStarted_EditorHotFileCron','1',NULL),('isStarted_NewFileCron','1',NULL),('isStarted_ServerInfoCron','1',NULL),('isStarted_ServerNodeSpeedCron','1',NULL),('isStarted_SyncCountCron','1',NULL),('isStarted_TaskCron','1',NULL),('isStarted_WorkerThread','1',NULL),('isStarted_WorkerServletGiveTaskToClient','1','send task to client');
数据库搞好了 咱们开始写代码。这里我先说明使用反射机制的原因。因为代码中的一个类ConfigInDB中的每一个属性是和数据库的字段一一对应的,并且初始化了值。项目启动一开始就会去初始化这个ConfigInDB中的属性。为了动态方便地初始化如此多的属性,我们有理由用反射机制去动态的萃取其数据库你的对应字段的值。代码如下所示:
public class ConfigInDB implements BasicInterface { public static volatile int isStarted_CheckTimeoutServerCron = 1; // 1.set '1' to start cron 2.set '2' to stop cron public static volatile int isStarted_DelFileCron = 1; // 1.set '1' to start cron 2.set '2' to stop cron public static volatile int isStarted_EditorHotFileCron = 1; // 1.set '1' to start cron 2.set '2' to stop cron public static volatile int isStarted_NewFileCron = 1; // 1.set '1' to start cron 2.set '2' to stop cron public static volatile int isStarted_ServerInfoCron = 1; // 1.set '1' to start cron 2.set '2' to stop cron public static volatile int isStarted_ServerNodeSpeedCron = 1; // 1.set '1' to start cron 2.set '2' to stop cron public static volatile int isStarted_SyncCountCron = 1; // 1.set '1' to start cron 2.set '2' to stop cron public static volatile int isStarted_TaskCron = 1; // 1.set '1' to start cron 2.set '2' to stop cron public static volatile int isStarted_ReadVirtualNodeXmlCron = 1; // 1.set '1' to start cron 2.set '2' to stop cron public static volatile int isStarted_WorkerThread = 1; // 1.set '1' to start 2.set '2' to stop public static volatile int isStarted_WorkerServletGiveTaskToClient = 1; // 1.set '1' to start 2.set '2' to stop public static volatile String virtualNodeXmlPath = System.getProperty("user.home") + "/virtualNode.xml"; public static volatile String kaFilePath = System.getProperty("user.home") + "/kaFile.txt"; public static String ServerVersion; public static volatile int // ClientJobHarddiskRate = 99, // EmergecyJobHarddiskRate = 98, // NormalJobHarddiskRate = 95, // NormalJobDeleteRate = 80; // public static volatile long // //not main Cron ConfigUpdateCron = Constant.Minute, // ServerNodeSpeedCron = 1 * Constant.Minute, // ServerInfoCron = 10 * Constant.Second, // TaskCron = 10 * Constant.Second, // ReadVirtualNodeXmlCron = 10 * Constant.Minute, // //main Cron SyncCountCron = 10 * Constant.Second, //SyncCountCron EditorHotFileCron = 1 * Constant.Minute, //EditorHotFileCron NewFileCron = 10 * Constant.Second, //NewFileCron DelFileCron = 15 * Constant.Minute, //DelFileCron CheckTimeoutServerCron = 1 * Constant.Minute, //CheckTimeoutServerCron CronSleepTime = 5 * Constant.Second; // public static int MinSrcCount = 2; public static int DBPoolSize = 10; public static int CdnDBPoolSize = 2; public static int EditorDBPoolSize = 1; public static int ToodouDBPoolSize = 1; public static int DelTimeoutInSecond = 60; public static int MinCopyTimeoutInSecond = 60 * 10; public static int MaxCopyTimeoutInSecond = 60 * 60 * 2; public static int CopyMinSpeed = 10 * 1024; public static int ClientMaxCopyTaskCount = 100; public static int ServerMaxCopyTaskCount = 50; public static int ClientMaxDelTaskCount = 100; public static int ServerMaxDelTaskCount = 50; public static int TouchTimeInSecond = 10; public static int ClientCopyThreadCount = 3; public static int TaskQueueBusyLength = 10000; public static int CdnFileCacheSize = 100; public static int CheckTimeoutServer = 1; public static String http_443_version = "asd"; public static String http_80_version = "asd"; public static String sync_version = "asd"; public static long TimeJobCheckCron = 6 * Constant.Hour; }
接下来是动态的赋值代码:
public class ConfigUpdateCron extends ICronJob implements BasicInterface { public boolean Arun(Connection conn) throws SQLException { List<StringPair> list = null; Statement stmt = conn.createStatement(); ResultSet srs = stmt.executeQuery(SqlString.SelectConfigTable); while (srs.next()) { if (list == null) list = new ArrayList<StringPair>(); list.add(new StringPair(srs.getString(1), srs.getString(2))); } srs.close(); stmt.close(); if (list != null) { return SetValue(list); } return true; } private boolean SetValue(List<StringPair> list) { Class<ConfigInDB> c = ConfigInDB.class; Field f; String s; int i; long l; byte b; float d; for (StringPair pair : list) { try { f = c.getField(pair.key); s = f.getType().getName(); if (s.compareTo("int") == 0) { try { i = Integer.parseInt(pair.value); } catch (Exception ex) { log.error("bad value of " + s, ex); continue; } if (f.getInt(c) != i) { log.info("Config." + pair.key + " \"" + f.getInt(c) + "\" to \"" + i + "\""); f.setInt(c, i); } } else if (s.compareTo("byte") == 0) { try { b = Byte.parseByte(pair.value); } catch (Exception ex) { log.error("bad value of " + s, ex); continue; } if (f.getByte(c) != b) { log.info("Config." + pair.key + " \"" + f.getByte(c) + "\" to \"" + b + "\""); f.setInt(c, b); } } else if (s.compareTo("float") == 0) { try { d = Float.parseFloat(pair.value); } catch (Exception ex) { log.error("bad value of " + s, ex); continue; } if (f.getFloat(c) != d) { log.info("Config." + pair.key + " \"" + f.getFloat(c) + "\" to \"" + d + "\""); f.setFloat(c, d); } } else if (s.compareTo("[I") == 0)//int[] { String[] ss = pair.value.split(","); int[] old = (int[]) f.get(c); if (old.length != ss.length) { //error continue; } for (int n = 0; n < ss.length; n++) { i = Integer.parseInt(ss[n]); if (i != old[n]) { old[n] = i; // } } } else if (s.compareTo("long") == 0) { try { l = Long.parseLong(pair.value); } catch (Exception ex) { log.error("bad value of " + s, ex); continue; } if (f.getLong(c) != l) { log.info("Config." + pair.key + " \"" + f.getLong(c) + "\" to \"" + l + "\""); f.setLong(c, l); } } else if (s.compareTo("java.lang.String") == 0) { if (((String) f.get(c)).compareTo(pair.value) != 0) { log.info("Config." + pair.key + " \"" + f.get(c) + "\" to \"" + pair.value + "\""); f.set(c, pair.value); } } else { Nagios.Alert("unsupport type:" + s + " on Config." + pair.key); } } catch (Exception e) { log.error("ex", e); return false; } } return true; } private ConfigUpdateCron() { } private static ConfigUpdateCron instance = new ConfigUpdateCron(); public static ConfigUpdateCron GetInstance() { return instance; } public boolean Init() { this.initLogger(); return true; } }
大家我的数据库连接就不写了,大家可以补充一下我的数据库连接,就可以运行代码了。