<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/java-code-reflect-html/</link>
<title><![CDATA[JAVA的反射机制]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[编程杂谈]]></category>
<pubDate>Fri, 15 Oct 2010 06:20:42 +0000</pubDate> 
<guid>https://www.heckjj.com/java-code-reflect-html/</guid> 
<description>
<![CDATA[ 
	<span style="font-family: 微软雅黑;">反射：<br/>Class c = Class.forName (&quot;java.lang.Integer&quot;);<br/><br/>这条语句得到一个 Integer类的类对象。还有另一种方法，如下面的语句：<br/>Class c = Integer.class;<br/><br/>或者<br/>Class c = Integer.TYPE;<br/><br/>它们可获得基本类型的类信息。其中后一种方法中访问的是基本类型的封装类 (如 Integer) 中预先定义好的 TYPE 字段。<br/>第二步是调用诸如 getDeclaredMethods 的方法，以取得该类中定义的所有方法的列表。<br/>一旦取得这个信息，就可以进行第三步了。<br/>第三步是使用 reflection API 来操作这些信息，如下面这段代码：</span><textarea name="code" class="java" rows="15" cols="100">
Class c = Class.forName(&quot;java.lang.String&quot;);
Method m[] = c.getDeclaredMethods();
System.out.println(m[0].toString());
</textarea><span style="font-family: 微软雅黑;"><br/>它将以文本方式打印出 String 中定义的第一个方法的原型。<br/><br/>　　反射经常由框架型代码使用，由于这一点，我们可能希望框架能够全面接入代码，无需考虑常规的接入限制。例如当代码在不值得信任的代码共享的环境中运行时。<br/><br/>假设有以下这个类的声明：</span><textarea name="code" class="java" rows="15" cols="100">
class DemoReflection
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private String name = null;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private void doPrint(String s) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;print.....&quot;＋s);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;;</textarea><br/><span style="font-family: 微软雅黑;">可以肯定的是，这个类中的属性name和方法doPrint都是无法对外展示的，但是使用了反射以后就可以办到。</span><textarea name="code" class="java" rows="15" cols="100">
package com.glsc.xu.search;
import java.lang.reflect.Method;
public class TestReflection &#123;
public static void main(String args[]) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 通过反映射技术得到DemoReflection的类型
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class cls = Class.forName(&quot;com.test.DemoReflection&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 动态创建DemoReflection类的实力
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object instance = cls.newInstance();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 通过反映射技术得到DemoReflection的非公有方法doPrint
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method m = cls.getDeclaredMethod(&quot;doPrint&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new Class[] &#123; String.class &#125;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 表示可以随意访问该类中的方法
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.setAccessible(true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 调用doPrint方法
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String s=&quot;hello world&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.invoke(instance, new Object[] &#123; s &#125;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; catch (Exception ex) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(ex);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;
&#125;</textarea><br/>　　<span style="font-family: 微软雅黑;">在该代码中，读者可能看到了一个比较陌生的方法setAccessible，这个方法非常重要，如果它不被设置成true那么所有非公有方法仍然无法调用，所以在调用非公有方法的时候需要注意这点。</span><br/>Tags - <a href="https://www.heckjj.com/tags/java/" rel="tag">java</a> , <a href="https://www.heckjj.com/tags/%25E7%25BC%2596%25E7%25A8%258B/" rel="tag">编程</a> , <a href="https://www.heckjj.com/tags/%25E5%258F%258D%25E5%25B0%2584/" rel="tag">反射</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/java-code-reflect-html/#blogcomment</link>
<title><![CDATA[[评论] JAVA的反射机制]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/java-code-reflect-html/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>