Java Reflection
·´ÉäÊÇJavaÓïÑÔÖкÜÖØÒªµÄÒ»¸ö×é³É²¿·Ö£¬ËùÒԾʹ˻°ÌâÌÖÂÛµÄ×ÊÔ´¿ÉνÊýÖ®²»¾¡£¬ÈÕ³£¿ª·¢Ò²»á¾³£Ê¹Óõ½¹ØÓÚ·´ÉäµÄReflection
API¡£Java5.0
Tiger³öÏÖÒԺ󣬸ü¶Ô·´ÉäAPIÓÐÁËеÄÀ©Õ¹£¬¾¡¹ÜÌÖÂ۵ϰÌâºÜ¶à£¬²»¹ýÎÒ»¹ÊǾõµÃ²»¹»È«Ã棬ÓÈÆäÊǶԷºÐÍÕâÒ»¿é£¬ËùÒÔ¾ÍÎÒËùÖª£¬ÔÙ»¨Á¦Æø×ܽáÒ»·¬
Ê×ÏÈ·´ÉäµÄÈë¿ÚÊÇ´ÓClass¿ªÊ¼µÄ£¬ËùÒÔÈçºÎ»ñÈ¡Class¾Í±äµÃÊ®·Ö¹Ø¼üÁË¡£ÕâÀï×ܽáÁ˼¸ÖÖ·½Ê½£º
1.ͨ¹ý${name}.class Óï·¨¡£ÕâÀï${name}¿ÉÒÔÊǶÔÏó£¬Ò²¿ÉÒÔÊÇÔʼÊý¾ÝÀàÐÍ£¬²»¹ý±ðÍüÁËvoid.classºÍVoid.class
2.ͨ¹ý${name}.TYPE Óï·¨¡£ÕâÀï${name}ÊǰËÖÖÔʼÊý¾ÝµÄ°ü×°ÀàºÍVoid.TYPE
3.ͨ¹ý¶ÔÏóµÄgetClass()·½·¨¡£
4.ͨ¹ýClass¶ÔÏóµÄforName()·½·¨
5.ͨ¹ýÀàClassµÄgetSuperclass()»ñÈ¡¸¸Ç×ÀàClass
6.ͨ¹ýÀàClassµÄgetEnclosingClass()»ñÈ¡ÍⲿÀàClass
7.ͨ¹ýÀàClassµÄgetClasses()ºÍgetDeclaredClasses()»ñÈ¡ÄÚ²¿ÀàClass
ÏÂÃæÊÇÒ»ÕűíÓÃÀ´ËµÃ÷getClasses()ºÍgetDeclaredClasses()Á½¸ö·½·¨£¬ÉÔºó»¹»áÓøñí˵Ã÷ÆäËûReflection API
--------------------------------------------------------------------------------------------
Member
| Class API
| Return type
| Inherited members
| Private members
--------------------------------------------------------------------------------------------
Class | getDeclaredClasses() | Array | N | Y
| getClasses() | Array | Y | N
--------------------------------------------------------------------------------------------
Field | getDeclaredField() &
Ïà¹ØÎĵµ£º
ÔÚJavaÖУ¬“Ïָ߳̔Á½¼þ²»Í¬µÄÊÂÇ飺
1¡¢java.lang.ThreadÀàµÄÒ»¸öʵÀý£»
2¡¢Ï̵߳ÄÖ´ÐС£
ʹÓÃjava.lang.ThreadÀà»òÕßjava.lang.Runnable½Ó¿Ú±àд´úÂëÀ´¶¨Ò塢ʵÀý»¯ºÍÆô¶¯ÐÂÏ̡߳£
Ò»¸öThreadÀàʵÀýÖ»ÊÇÒ»¸ö¶ÔÏó£¬ÏñJavaÖеÄÈÎºÎÆäËû¶ÔÏóÒ»Ñù£¬¾ßÓбäÁ¿ºÍ·½·¨£¬ÉúËÀÓÚ¶ÑÉÏ¡£
JavaÖУ¬Ã¿¸öÏ̶߳¼ÓÐÒ»¸öµ÷ ......
ÓÐÁ½ÖÖ·½Ê½£º¾²Ì¬²¿ÊðºÍ¶¯Ì¬²¿Êð¡£ÔÚÏÂÎÄÖÐ$CATALINA_HOMEÖ¸µÄÊÇTomcat¸ùĿ¼¡£
Ò»¡¢¾²Ì¬²¿Êð
¾²Ì¬²¿ÊðÖ¸µÄÊÇÎÒÃÇÔÚ·þÎñÆ÷Æô¶¯Ö®Ç°²¿Êð³ÌÐò£¬Ö»Óе±·þÎñÆ÷Æô¶¯Ö®ºó£¬WebÓ¦ÓóÌÐò²ÅÄÜ·ÃÎÊ¡£ÒÔÏÂ3Öз½Ê½¶¼¿ÉÒÔ²¿Êð£º
1¡¢½«PetWebĿ¼¿½±´µ½$CATALINA_HOME\webappsÏ£¬È»ºóÆô¶¯·þÎñÆ÷¾Í¿ÉÒÔÁË¡£ÕâÖÖ·½Ê½±È½Ï¼òµ¥£¬µ«ÊÇweb ......
ÔÚ»ñÈ¡Îı¾µÄʱºò£¬¾³£Óöµ½Êý¾Ý¸ñʽµÄÎÊÌ⣬ÐèÒªÅжϻòת»»¡£
±¾ÎÄÖ¼ÔÚ½â¾öÈ«½Ç·ûºÅת»»Îª°ë½Ç·ûºÅµÄÎÊÌâ¡£
ÍøÉÏ´óÌåÓÐÁ½ÖÖ·½·¨£º×ª»»±àÂë»òÔòÌæ»»¡£
ÎÒʹÓõÄÊÇת»»±àÂë
1. string checkStr="£±£²£³456";
2. //ÅжÏÊÇ·ñ°üº¬È«½Ç×Ö·û[1]
3. if(checkStr.length() != checkStr.getBytes().length) ......
package com.infowarelab.java.test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectTester {
@SuppressWarnings("unchec ......
javaÖжÑÕ»(stack)ºÍ¶Ñ(heap)
Ò»¡¢¶ÑÕ»(stack)ºÍ¶Ñ(heap)£¿
£¨£±£©ÄÚ´æ·ÖÅäµÄ²ßÂÔ
¡¡¡¡°´ÕÕ±àÒëÔÀíµÄ¹Ûµã,³ÌÐòÔËÐÐʱµÄÄÚ´æ·ÖÅäÓÐÈýÖÖ²ßÂÔ,·Ö±ðÊǾ²Ì¬µÄ,ջʽµÄ,ºÍ¶ÑʽµÄ.
¡¡¾²Ì¬´æ´¢·ÖÅäÊÇÖ¸ÔÚ±àÒëʱ¾ÍÄÜÈ·¶¨Ã¿¸öÊý¾ÝÄ¿±êÔÚÔËÐÐʱ¿ÌµÄ´æ´¢¿Õ¼äÐèÇó,Òò¶øÔÚ±àÒëʱ¾Í¿ÉÒÔ¸øËûÃÇ·ÖÅä¹Ì¶¨µÄÄ ......