import{_ as p,r as e,o as c,c as t,b as n,d as s,e as l,a as o}from"./app-OEUYI7Bq.js";const r={},i=o('<h1 id="展望未来" tabindex="-1"><a class="header-anchor" href="#展望未来" aria-hidden="true">#</a> 展望未来</h1><blockquote><p>未来是美好的，也是不确定的，让我们共同期待 <code>KavaRef</code> 在未来的发展空间。</p></blockquote><h2 id="未来的计划" tabindex="-1"><a class="header-anchor" href="#未来的计划" aria-hidden="true">#</a> 未来的计划</h2><blockquote><p>这里收录了 <code>KavaRef</code> 可能会在后期添加的功能。</p></blockquote><h3 id="支持通过-classloader-过滤-class" tabindex="-1"><a class="header-anchor" href="#支持通过-classloader-过滤-class" aria-hidden="true">#</a> 支持通过 ClassLoader 过滤 Class</h3><p><code>KavaRef</code> 目前仅支持 <code>Method</code>、<code>Field</code>、<code>Constructor</code> 等反射 API 的查找和调用， 未来可能会根据需求在 Java 与 Android 平台支持通过指定类型的 <code>ClassLoader</code> 过滤 <code>Class</code> 的功能。</p>',6),y={href:"https://github.com/LuckyPray/DexKit",target:"_blank",rel:"noopener noreferrer"},A=n("h3",{id:"自动生成反射代码",tabindex:"-1"},[n("a",{class:"header-anchor",href:"#自动生成反射代码","aria-hidden":"true"},"#"),s(" 自动生成反射代码")],-1),d={href:"https://github.com/HighCapable/YukiReflection",target:"_blank",rel:"noopener noreferrer"},D=n("code",null,"KavaRef",-1),B=o(`<p>使用 <code>stub</code> 的方式创建一个 Kotlin 类，并声明其中的参数，以及其在各个版本中的不同状态。</p><p>比如下面的这个 Java 类就是我们需要反射的目标类。</p><blockquote><p>示例如下</p></blockquote><div class="language-java" data-ext="java"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#F47067;">package</span><span style="color:#ADBAC7;"> com.example.test;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F47067;">public</span><span style="color:#ADBAC7;"> </span><span style="color:#F47067;">class</span><span style="color:#ADBAC7;"> </span><span style="color:#F69D50;">MyClass</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">private</span><span style="color:#ADBAC7;"> String</span><span style="color:#F69D50;"> </span><span style="color:#ADBAC7;">myField</span><span style="color:#F69D50;"> </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#96D0FF;">&quot;test&quot;</span><span style="color:#ADBAC7;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">public</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">MyClass</span><span style="color:#ADBAC7;">() {</span></span>
<span class="line"><span style="color:#ADBAC7;">        </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">private</span><span style="color:#ADBAC7;"> String </span><span style="color:#DCBDFB;">myMethod1</span><span style="color:#ADBAC7;">(String </span><span style="color:#F69D50;">var1</span><span style="color:#ADBAC7;">, </span><span style="color:#F47067;">int</span><span style="color:#ADBAC7;"> </span><span style="color:#F69D50;">var2</span><span style="color:#ADBAC7;">) {</span></span>
<span class="line"><span style="color:#ADBAC7;">        </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">private</span><span style="color:#ADBAC7;"> </span><span style="color:#F47067;">void</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">myMethod2</span><span style="color:#ADBAC7;">() {</span></span>
<span class="line"><span style="color:#ADBAC7;">        </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">private</span><span style="color:#ADBAC7;"> </span><span style="color:#F47067;">void</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">myMethod3</span><span style="color:#ADBAC7;">(String </span><span style="color:#F69D50;">var1</span><span style="color:#ADBAC7;">) {</span></span>
<span class="line"><span style="color:#ADBAC7;">        </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">    }</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span></code></pre></div><p>通过目前 API 的现有用法可以使用如下方式反射调用这个类。</p><blockquote><p>示例如下</p></blockquote><div class="language-kotlin line-numbers-mode" data-ext="kt"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#DCBDFB;">MyClass</span><span style="color:#ADBAC7;">().</span><span style="color:#DCBDFB;">resolve</span><span style="color:#ADBAC7;">().</span><span style="color:#DCBDFB;">apply</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// 调用 myField</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> </span><span style="color:#F47067;">value</span><span style="color:#ADBAC7;"> </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">firstField</span><span style="color:#ADBAC7;"> { name </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#96D0FF;">&quot;myField&quot;</span><span style="color:#ADBAC7;"> }.</span><span style="color:#DCBDFB;">get</span><span style="color:#ADBAC7;">&lt;</span><span style="color:#F69D50;">String</span><span style="color:#ADBAC7;">&gt;()</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// 调用 myMethod1</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> methodValue </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">firstMethod</span><span style="color:#ADBAC7;"> { name </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#96D0FF;">&quot;myMethod1&quot;</span><span style="color:#ADBAC7;"> }.</span><span style="color:#DCBDFB;">invoke</span><span style="color:#ADBAC7;">&lt;</span><span style="color:#F69D50;">String</span><span style="color:#ADBAC7;">&gt;(</span><span style="color:#96D0FF;">&quot;test&quot;</span><span style="color:#ADBAC7;">, </span><span style="color:#6CB6FF;">0</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// 调用 myMethod2</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#DCBDFB;">firstMethod</span><span style="color:#ADBAC7;"> { name </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#96D0FF;">&quot;myMethod2&quot;</span><span style="color:#ADBAC7;"> }.</span><span style="color:#DCBDFB;">invoke</span><span style="color:#ADBAC7;">()</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// 调用 myMethod3</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#DCBDFB;">firstMethod</span><span style="color:#ADBAC7;"> { name </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#96D0FF;">&quot;myMethod3&quot;</span><span style="color:#ADBAC7;"> }.</span><span style="color:#DCBDFB;">invoke</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;test&quot;</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>目前要实现的功能是可以使用反射功能直接定义为如下 Kotlin 类。</p><blockquote><p>示例如下</p></blockquote><div class="language-kotlin line-numbers-mode" data-ext="kt"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#F47067;">package</span><span style="color:#ADBAC7;"> </span><span style="color:#F69D50;">com.example.test</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F69D50;">@ReflectClass</span></span>
<span class="line"><span style="color:#F47067;">class</span><span style="color:#ADBAC7;"> </span><span style="color:#F69D50;">MyClass</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F69D50;">@ReflectField</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> myField: </span><span style="color:#F69D50;">String</span><span style="color:#ADBAC7;"> </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">fieldValueOf</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;none&quot;</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F69D50;">@ReflectMethod</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">fun</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">myMethod1</span><span style="color:#ADBAC7;">(var1: </span><span style="color:#F69D50;">String</span><span style="color:#ADBAC7;">, var2: </span><span style="color:#F69D50;">Int</span><span style="color:#ADBAC7;">): </span><span style="color:#F69D50;">String</span><span style="color:#ADBAC7;"> </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">methodReturnValueOf</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;none&quot;</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F69D50;">@ReflectMethod</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">fun</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">myMethod2</span><span style="color:#ADBAC7;">() </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> MethodReturnType.Unit</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F69D50;">@ReflectMethod</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">fun</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">myMethod3</span><span style="color:#ADBAC7;">(var1: </span><span style="color:#F69D50;">String</span><span style="color:#ADBAC7;">) </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> MethodReturnType.Unit</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后我们就可以直接调用这个定义好的 Kotlin 类来实现反射功能，API 会根据注解自动生成反射代码。</p><blockquote><p>示例如下</p></blockquote><div class="language-kotlin line-numbers-mode" data-ext="kt"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#DCBDFB;">MyClass</span><span style="color:#ADBAC7;">().</span><span style="color:#DCBDFB;">also</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// 调用 myField</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> </span><span style="color:#F47067;">value</span><span style="color:#ADBAC7;"> </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> it.myField</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// 调用 myMethod1</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> methodValue </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> it.</span><span style="color:#DCBDFB;">myMethod1</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;test&quot;</span><span style="color:#ADBAC7;">, </span><span style="color:#6CB6FF;">0</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// 调用 myMethod2</span></span>
<span class="line"><span style="color:#ADBAC7;">    it.</span><span style="color:#DCBDFB;">myMethod2</span><span style="color:#ADBAC7;">()</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// 调用 myMethod3</span></span>
<span class="line"><span style="color:#ADBAC7;">    it.</span><span style="color:#DCBDFB;">myMethod3</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;test&quot;</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="custom-container tip"><p class="custom-container-title">小提示</p><p>以上功能可能会在实际推出后有所变化，最终以实际版本的功能为准。</p></div>`,14);function C(u,v){const a=e("ExternalLinkIcon");return c(),t("div",null,[i,n("p",null,[s("目前，你可以使用 "),n("a",y,[s("DexKit"),l(a)]),s(" 来完成这一需求，它同时支持更加复杂的 Method、Field、Constructor 等反射 API 的查找和调用。")]),A,n("p",null,[n("strong",null,[s("这是在 "),n("a",d,[s("YukiReflection"),l(a)]),s(" 中已经初步立项的功能，"),D,s(" 准备在未来可能的时间里继续实现它。")])]),B])}const m=p(r,[["render",C],["__file","future.html.vue"]]);export{m as default};
