import{_ as o,r as t,o as p,c,b as e,d as s,e as l,a}from"./app-OEUYI7Bq.js";const i={},r=a(`<h1 id="kavaref-extension" tabindex="-1"><a class="header-anchor" href="#kavaref-extension" aria-hidden="true">#</a> kavaref-extension</h1><p><img src="https://img.shields.io/maven-central/v/com.highcapable.kavaref/kavaref-extension?logo=apachemaven&amp;logoColor=orange&amp;style=flat-square" alt="Maven Central"><span style="margin-left:5px;"></span><img src="https://img.shields.io/maven-metadata/v?metadataUrl=https%3A%2F%2Fraw.githubusercontent.com%2FHighCapable%2Fmaven-repository%2Frefs%2Fheads%2Fmain%2Frepository%2Freleases%2Fcom%2Fhighcapable%2Fkavaref%2Fkavaref-extension%2Fmaven-metadata.xml&amp;logo=apachemaven&amp;logoColor=orange&amp;label=highcapable-maven-releases&amp;style=flat-square" alt="Maven metadata URL"></p><p>This is an extended dependency for KavaRef-related features.</p><h2 id="configure-dependency" tabindex="-1"><a class="header-anchor" href="#configure-dependency" aria-hidden="true">#</a> Configure Dependency</h2><p>You can add this module to your project using the following method.</p><h3 id="version-catalog-recommended" tabindex="-1"><a class="header-anchor" href="#version-catalog-recommended" aria-hidden="true">#</a> Version Catalog (Recommended)</h3><p>Add dependency in your project&#39;s <code>gradle/libs.versions.toml</code>.</p><div class="language-toml line-numbers-mode" data-ext="toml"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#ADBAC7;">[</span><span style="color:#F69D50;">versions</span><span style="color:#ADBAC7;">]</span></span>
<span class="line"><span style="color:#ADBAC7;">kavaref-extension = </span><span style="color:#96D0FF;">&quot;&lt;version&gt;&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ADBAC7;">[</span><span style="color:#F69D50;">libraries</span><span style="color:#ADBAC7;">]</span></span>
<span class="line"><span style="color:#ADBAC7;">kavaref-extension = { module = </span><span style="color:#96D0FF;">&quot;com.highcapable.kavaref:kavaref-extension&quot;</span><span style="color:#ADBAC7;">, version.ref = </span><span style="color:#96D0FF;">&quot;kavaref-extension&quot;</span><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></div><p>Configure dependency in your project&#39;s <code>build.gradle.kts</code>.</p><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;">implementation</span><span style="color:#ADBAC7;">(libs.kavaref.extension)</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>Please change <code>&lt;version&gt;</code> to the version displayed at the top of this document.</p><h3 id="traditional-method" tabindex="-1"><a class="header-anchor" href="#traditional-method" aria-hidden="true">#</a> Traditional Method</h3><p>Configure dependency in your project&#39;s <code>build.gradle.kts</code>.</p><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;">implementation</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;com.highcapable.kavaref:kavaref-extension:&lt;version&gt;&quot;</span><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></div><p>Please change <code>&lt;version&gt;</code> to the version displayed at the top of this document.</p><h2 id="function-introduction" tabindex="-1"><a class="header-anchor" href="#function-introduction" aria-hidden="true">#</a> Function Introduction</h2>`,16),d={href:"https://highcapable.github.io/KavaRef/KDoc/kavaref-extension",target:"_blank",rel:"noopener noreferrer"},y=a(`<h3 id="class-extensions" tabindex="-1"><a class="header-anchor" href="#class-extensions" aria-hidden="true">#</a> Class Extensions</h3><p>KavaRef provides some extensions that are more convenient when dealing with <code>Class</code> objects.</p><p>KavaRef also adds <code>KClass</code> extensions to the <code>Class</code> extensions, which is used to call <code>KClass.java</code>, making it more concise than using <code>Some::class.java</code> directly.</p><h4 id="create-class-object" tabindex="-1"><a class="header-anchor" href="#create-class-object" aria-hidden="true">#</a> Create Class Object</h4><p>For example, we need to create a <code>Class</code> object using the string class name.</p><blockquote><p>The following example</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;">val</span><span style="color:#ADBAC7;"> myClass </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#96D0FF;">&quot;com.example.MyClass&quot;</span><span style="color:#ADBAC7;">.</span><span style="color:#DCBDFB;">toClass</span><span style="color:#ADBAC7;">()</span></span>
<span class="line"><span style="color:#768390;">// You can use a method with OrNull suffix to return null</span></span>
<span class="line"><span style="color:#768390;">// when the Class is not found instead of throwing an exception.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> myClassOrNull </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#96D0FF;">&quot;com.example.MyClass&quot;</span><span style="color:#ADBAC7;">.</span><span style="color:#DCBDFB;">toClassOrNull</span><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></div><p>These methods use <code>ClassLoaderProvider</code> to get the default <code>ClassLoader</code>, you can set the default <code>ClassLoader</code> to affect global functionality.</p><blockquote><p>The following example</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:#ADBAC7;">ClassLoaderProvider.classLoader </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">MyCustomClassLoader</span><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></div><p>You can also manually pass a <code>ClassLoader</code> parameter to the <code>toClass</code> method to specify which <code>ClassLoader</code> to use.</p><h4 id="class-object-reference" tabindex="-1"><a class="header-anchor" href="#class-object-reference" aria-hidden="true">#</a> Class Object Reference</h4><p>Referring to a Java Class in Kotlin requires writing a very long statement, such as <code>MyClass::class.java</code>, which you can simplify in the following way.</p><blockquote><p>The following example</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;">val</span><span style="color:#ADBAC7;"> myClass </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">classOf</span><span style="color:#ADBAC7;">&lt;</span><span style="color:#F69D50;">MyClass</span><span style="color:#ADBAC7;">&gt;()</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>You can use the <code>isSubclassOf</code> method to determine whether a <code>Class</code> is another <code>Class</code> subclass.</p><blockquote><p>The following example</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;">val</span><span style="color:#ADBAC7;"> isSubclass </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> MyClass::</span><span style="color:#DCBDFB;">class</span><span style="color:#ADBAC7;"> isSubclassOf MySuperClass::</span><span style="color:#DCBDFB;">class</span></span>
<span class="line"><span style="color:#768390;">// Of course, it also has a corresponding antonym of judgment.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> isNotSubclass </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> MyClass::</span><span style="color:#DCBDFB;">class</span><span style="color:#ADBAC7;"> isNotSubclassOf MySuperClass::</span><span style="color:#DCBDFB;">class</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></div><p>You can also use the <code>hasSuperclass</code> and <code>hasInterfaces</code> methods to determine whether a <code>Class</code> has a superclass or an interface.</p><div class="custom-container danger"><p class="custom-container-title">Pay Attention</p><p>The <code>Class</code> passed in by the <code>classOf</code> method will perform unboxing of Java wrapper classes by default, whether you pass in something like <code>kotlin.Boolean</code> or <code>java.lang.Boolean</code> (see <a href="#java-wrapper-classes-extensions">Java Wrapper Classes Extensions</a> below), If you need to avoid the incoming <code>Class</code> being unboxed into primitive types, you need to explicitly set the <code>primitiveType = false</code> parameter.</p></div><h4 id="create-new-instance" tabindex="-1"><a class="header-anchor" href="#create-new-instance" aria-hidden="true">#</a> Create New Instance</h4><p>KavaRef provides a way for <code>Class</code> to easily create a new instance. You don&#39;t need to consider the type of constructing parameters, you just need to pass in the corresponding parameters to create a new instance immediately.</p><blockquote><p>The following example</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;">val</span><span style="color:#ADBAC7;"> myClass </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> MyClass::</span><span style="color:#DCBDFB;">class</span><span style="color:#ADBAC7;">.</span><span style="color:#DCBDFB;">createInstance</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;Hello&quot;</span><span style="color:#ADBAC7;">, </span><span style="color:#6CB6FF;">123</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#768390;">// You can also use a method with the OrNull suffix to return null</span></span>
<span class="line"><span style="color:#768390;">// when creation fails instead of throwing an exception.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> myClassOrNull </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> MyClass::</span><span style="color:#DCBDFB;">class</span><span style="color:#ADBAC7;">.</span><span style="color:#DCBDFB;">createInstanceOrNull</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;Hello&quot;</span><span style="color:#ADBAC7;">, </span><span style="color:#6CB6FF;">123</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#768390;">// The createInstance method only filters public constructors by default.</span></span>
<span class="line"><span style="color:#768390;">// If you need to call non-public constructors, please set isPublic = false.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> myClassWithPrivateConstructor </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> MyClass::</span><span style="color:#DCBDFB;">class</span><span style="color:#ADBAC7;">.</span><span style="color:#DCBDFB;">createInstance</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;Private!&quot;</span><span style="color:#ADBAC7;">, isPublic </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#6CB6FF;">false</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#768390;">// If you want to specify the type to create an instance to use another type,</span></span>
<span class="line"><span style="color:#768390;">// you can use the following method.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> mySuperClass </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> MyClass::</span><span style="color:#DCBDFB;">class</span><span style="color:#ADBAC7;">.</span><span style="color:#DCBDFB;">createInstanceAsType</span><span style="color:#ADBAC7;">&lt;</span><span style="color:#F69D50;">MySuperClass</span><span style="color:#ADBAC7;">&gt;(</span><span style="color:#96D0FF;">&quot;Hello&quot;</span><span style="color:#ADBAC7;">, </span><span style="color:#6CB6FF;">123</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#768390;">// Similarly, you can use a method with the OrNull suffix to return null when</span></span>
<span class="line"><span style="color:#768390;">// creation fails instead of throwing an exception.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> mySuperClassOrNull </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> MyClass::</span><span style="color:#DCBDFB;">class</span><span style="color:#ADBAC7;">.</span><span style="color:#DCBDFB;">createInstanceAsTypeOrNull</span><span style="color:#ADBAC7;">&lt;</span><span style="color:#F69D50;">MySuperClass</span><span style="color:#ADBAC7;">&gt;(</span><span style="color:#96D0FF;">&quot;Hello&quot;</span><span style="color:#ADBAC7;">, </span><span style="color:#6CB6FF;">123</span><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></div><div class="custom-container tip"><p class="custom-container-title">Tips</p><p>After the <code>createInstance</code> method is successfully matched once, it will cache the results to prevent performance losses caused by duplicated reflections. It is thread-safe and you can use it in any standard scenario with confidence.</p></div><div class="custom-container danger"><p class="custom-container-title">Pay Attention</p><p>When you pass in a parameter with <code>null</code>, KavaRef tries to use it as part of the matchable condition (vague condition), and the accuracy may decrease.</p><p>The <code>createInstance</code> method does not allow all parameters to be <code>null</code> (the conditions are completely vague), and an exception will be thrown directly because this situation cannot be determined which instance to create.</p></div><h4 id="class-modifier" tabindex="-1"><a class="header-anchor" href="#class-modifier" aria-hidden="true">#</a> Class Modifier</h4><p>KavaRef also extends <code>Modifier</code>, you can directly use <code>Class.isPublic</code> and other methods to judge a <code>Class</code> modifier.</p><h4 id="variousclass" tabindex="-1"><a class="header-anchor" href="#variousclass" aria-hidden="true">#</a> VariousClass</h4><p>KavaRef provides the <code>VariousClass</code> class to load the <code>Class</code> object with an indeterminate full class name and return the first match successfully.</p><p>This feature is usually used for class names in Android apps that are obfuscated by R8.</p><blockquote><p>The following example</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:#768390;">// Assume that in version A, this class is com.example.a,</span></span>
<span class="line"><span style="color:#768390;">// In version B, this class is com.example.b.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> myClass </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">VariousClass</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;com.example.a&quot;</span><span style="color:#ADBAC7;">, </span><span style="color:#96D0FF;">&quot;com.example.b&quot;</span><span style="color:#ADBAC7;">).</span><span style="color:#DCBDFB;">load</span><span style="color:#ADBAC7;">()</span></span>
<span class="line"><span style="color:#768390;">// You can also use a method with the suffix OrNull to return null</span></span>
<span class="line"><span style="color:#768390;">// instead of throwing an exception if Class is not found.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> myClassOrNull </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">VariousClass</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;com.example.a&quot;</span><span style="color:#ADBAC7;">, </span><span style="color:#96D0FF;">&quot;com.example.b&quot;</span><span style="color:#ADBAC7;">).</span><span style="color:#DCBDFB;">loadOrNull</span><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></div><h4 id="lazy-loading-class-object" tabindex="-1"><a class="header-anchor" href="#lazy-loading-class-object" aria-hidden="true">#</a> Lazy Loading Class Object</h4><p>KavaRef provides the <code>LazyClass</code> class to lazy loading the <code>Class</code> object.</p><p>You can load <code>Class</code> when needed, instead of loading it immediately when created, which can solve some <code>Class</code> that need to be loaded when run or run to specific conditions.</p><blockquote><p>The following example</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:#768390;">// Define a Class that cannot be loaded for null and hosts it to myClass.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> myClass </span><span style="color:#F47067;">by</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">lazyClass</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;com.example.MyClass&quot;</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#768390;">// Define a Class that can be loaded for null delay and host it to myClassOrNull.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> myClassOrNull </span><span style="color:#F47067;">by</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">lazyClassOrNull</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;com.example.MyClass&quot;</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#768390;">// It can also support incoming VariousClass.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> otherClassOrNull </span><span style="color:#F47067;">by</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">lazyClassOrNull</span><span style="color:#ADBAC7;">(</span><span style="color:#DCBDFB;">VariousClass</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;com.example.a&quot;</span><span style="color:#ADBAC7;">, </span><span style="color:#96D0FF;">&quot;com.example.b&quot;</span><span style="color:#ADBAC7;">))</span></span>
<span class="line"><span style="color:#768390;">// Called and loaded when needed.</span></span>
<span class="line"><span style="color:#ADBAC7;">myClass.</span><span style="color:#DCBDFB;">resolve</span><span style="color:#ADBAC7;">()</span></span>
<span class="line"><span style="color:#ADBAC7;">myClassOrNull?.</span><span style="color:#DCBDFB;">resolve</span><span style="color:#ADBAC7;">()</span></span>
<span class="line"><span style="color:#ADBAC7;">otherClassOrNull?.</span><span style="color:#DCBDFB;">resolve</span><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><h4 id="classloader-extensions" tabindex="-1"><a class="header-anchor" href="#classloader-extensions" aria-hidden="true">#</a> ClassLoader Extensions</h4><p>KavaRef also provides some practical extension methods for <code>ClassLoader</code>.</p><blockquote><p>The following example</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:#768390;">// Assume that&#39;s your ClassLoader.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> classLoader: </span><span style="color:#F69D50;">ClassLoader</span></span>
<span class="line"><span style="color:#768390;">// Load a Class and return null if the load fails.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> myClassOrNull </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> classLoader.</span><span style="color:#DCBDFB;">loadClassOrNull</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;com.example.MyClass&quot;</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#768390;">// Determine whether this Class exists in the current ClassLoader.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> isClassExists </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> classLoader.</span><span style="color:#DCBDFB;">hasClass</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;com.example.MyClass&quot;</span><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></div><h3 id="array-class-extensions" tabindex="-1"><a class="header-anchor" href="#array-class-extensions" aria-hidden="true">#</a> Array Class Extensions</h3><p>In Java, the <code>Class</code> object of an array is a special <code>Class</code> object, and usually we create it as follows.</p><p>For example, create a <code>Class</code> object of <code>java.lang.String[]</code>.</p><blockquote><p>The following example</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;">val</span><span style="color:#ADBAC7;"> arrayClass </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> java.lang.reflect.Array.</span><span style="color:#DCBDFB;">newInstance</span><span style="color:#ADBAC7;">(String::</span><span style="color:#DCBDFB;">class</span><span style="color:#ADBAC7;">.java, </span><span style="color:#6CB6FF;">0</span><span style="color:#ADBAC7;">).javaClass</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>This is very long to write and is not convenient to maintain, so KavaRef provides a way to simplify this process.</p><p>Now, the <code>Class</code> object that creates <code>java.lang.String[]</code> can be written like this.</p><blockquote><p>The following example</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;">val</span><span style="color:#ADBAC7;"> arrayClass </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">ArrayClass</span><span style="color:#ADBAC7;">(String::</span><span style="color:#DCBDFB;">class</span><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></div><h3 id="member-extensions" tabindex="-1"><a class="header-anchor" href="#member-extensions" aria-hidden="true">#</a> Member Extensions</h3><p>KavaRef provides some extension methods to simplify operations on <code>Member</code>.</p><p>You can set its accessibility using the <code>makeAccessible</code> method on any <code>Member</code> object.</p><p>It will take effect if <code>Member</code> is the <code>AccessibleObject</code> type.</p><blockquote><p>The following example</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:#768390;">// Suppose this is your current Member object.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> method: </span><span style="color:#F69D50;">Method</span></span>
<span class="line"><span style="color:#768390;">// Make method is accessible.</span></span>
<span class="line"><span style="color:#ADBAC7;">method.</span><span style="color:#DCBDFB;">makeAccessible</span><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></div><p>Similarly, KavaRef also extends <code>Modifier</code>, and you can directly use <code>Member.isPublic</code> and other methods to judge a <code>Member</code> modifier.</p><h3 id="type-extensions" tabindex="-1"><a class="header-anchor" href="#type-extensions" aria-hidden="true">#</a> Type Extensions</h3><p>When manipulating types or generic types in Java, you usually need to use the <code>Type</code> interface and its subinterface to handle it.</p><p>KavaRef provides some extension methods to simplify operations on <code>Type</code>.</p><p>For example, you can convert a <code>Type</code> that meets the requirements to a <code>Class</code> object.</p><blockquote><p>The following example</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;">val</span><span style="color:#ADBAC7;"> type: </span><span style="color:#F69D50;">Type</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> clazz </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> type.</span><span style="color:#DCBDFB;">toClass</span><span style="color:#ADBAC7;">()</span></span>
<span class="line"><span style="color:#768390;">// You can also use a method with the suffix named OrNull to</span></span>
<span class="line"><span style="color:#768390;">// return null when the conversion fails instead of throwing an exception.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> clazzOrNull </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> type.</span><span style="color:#DCBDFB;">toClassOrNull</span><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></div><p>You can also convert <code>Type</code> that meets the requirements to <code>ParameterizedType</code> object.</p><blockquote><p>The following example</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;">val</span><span style="color:#ADBAC7;"> type: </span><span style="color:#F69D50;">Type</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> parameterizedType </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> type.</span><span style="color:#DCBDFB;">asParameterizedType</span><span style="color:#ADBAC7;">()</span></span>
<span class="line"><span style="color:#768390;">// You can also use a method with the suffix named OrNull to</span></span>
<span class="line"><span style="color:#768390;">// return null when the conversion fails instead of throwing an exception.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> parameterizedTypeOrNull </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> type.</span><span style="color:#DCBDFB;">asParameterizedTypeOrNull</span><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></div><p>You can also use the following method to get the generic parameter array in the superclass, which is often used in some superclass and subclass encapsulation operations.</p><blockquote><p>The following example</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;">val</span><span style="color:#ADBAC7;"> myClass: </span><span style="color:#F69D50;">Class</span><span style="color:#ADBAC7;">&lt;*&gt;</span></span>
<span class="line"><span style="color:#768390;">// Get the generic parameter array of myClass superclass.</span></span>
<span class="line"><span style="color:#768390;">// If the acquisition fails or cannot be retrieved, the empty array will be returned.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> arguments </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> myClass.</span><span style="color:#DCBDFB;">genericSuperclassTypeArguments</span><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></div><h3 id="type-reference-extensions" tabindex="-1"><a class="header-anchor" href="#type-reference-extensions" aria-hidden="true">#</a> Type Reference Extensions</h3><p>In Java, method generics are erased after compilation, and the type obtained at runtime is <code>java.lang.Object</code>.</p>`,72),u=e("code",null,"TypeRef",-1),m={href:"https://github.com/google/gson",target:"_blank",rel:"noopener noreferrer"},h=e("code",null,"TypeToken",-1),v=a(`<p>It is very simple to use, you can use it like this.</p><blockquote><p>The following example</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;">val</span><span style="color:#ADBAC7;"> listStringType </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">typeRef</span><span style="color:#ADBAC7;">&lt;</span><span style="color:#F69D50;">List</span><span style="color:#ADBAC7;">&lt;</span><span style="color:#F69D50;">String</span><span style="color:#ADBAC7;">&gt;&gt;()</span></span>
<span class="line"><span style="color:#768390;">// Get the stored type, which will be List&lt;? extends String&gt;.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> type </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> listStringType.type</span></span>
<span class="line"><span style="color:#768390;">// Get its raw type, which will be List.</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> rawType </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> listStringType.rawType</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></div><p>In scenarios where you need to pass in <code>Type</code> such as when using Gson, you can implement an extension method with <code>reified</code> generics for this purpose.</p><blockquote><p>The following example</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;">val</span><span style="color:#ADBAC7;"> gson </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">Gson</span><span style="color:#ADBAC7;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F47067;">inline</span><span style="color:#ADBAC7;"> </span><span style="color:#F47067;">fun</span><span style="color:#ADBAC7;"> &lt;</span><span style="color:#F69D50;">reified</span><span style="color:#ADBAC7;"> </span><span style="color:#F69D50;">T</span><span style="color:#ADBAC7;"> : </span><span style="color:#F69D50;">Any</span><span style="color:#ADBAC7;">&gt; </span><span style="color:#F69D50;">T</span><span style="color:#ADBAC7;">.</span><span style="color:#DCBDFB;">toJson</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;"> gson.</span><span style="color:#DCBDFB;">toJson</span><span style="color:#ADBAC7;">(</span><span style="color:#6CB6FF;">this</span><span style="color:#ADBAC7;">, </span><span style="color:#DCBDFB;">typeRef</span><span style="color:#ADBAC7;">&lt;</span><span style="color:#F69D50;">T</span><span style="color:#ADBAC7;">&gt;().type)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#768390;">// Usage</span></span>
<span class="line"><span style="color:#F47067;">val</span><span style="color:#ADBAC7;"> json </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">listOf</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;KavaRef&quot;</span><span style="color:#ADBAC7;">, </span><span style="color:#96D0FF;">&quot;is&quot;</span><span style="color:#ADBAC7;">, </span><span style="color:#96D0FF;">&quot;awesome&quot;</span><span style="color:#ADBAC7;">).</span><span style="color:#DCBDFB;">toJson</span><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></div><h3 id="java-wrapper-classes-extensions" tabindex="-1"><a class="header-anchor" href="#java-wrapper-classes-extensions" aria-hidden="true">#</a> Java Wrapper Classes Extensions</h3><p>In Kotlin, you can directly use <code>Boolean::class</code>, <code>Byte::class</code>, etc. to obtain Java&#39;s original types <code>boolean</code> and <code>byte</code> instead of their wrapper classes.</p><p>If you need to get Java wrapper classes, you need to use the complete <code>java.lang.Boolean::class</code>, <code>java.lang.Byte::class</code>, etc. or use <code>Boolean::class.javaObjectType</code>, <code>Byte::class.javaObjectType</code>.</p><p>So, KavaRef provides some type alias to handle Java wrapper classes. Now you only need to prefix <code>J</code> to these types, such as <code>JBoolean::class</code>. It is equivalent to <code>java.lang.Boolean::class</code>, and some types need to be filled in the full name, such as <code>JInteger::class</code>.</p>`,10);function A(C,b){const n=t("ExternalLinkIcon");return p(),c("div",null,[r,e("p",null,[s("You can view the KDoc "),e("a",d,[s("click here"),l(n)]),s(".")]),y,e("p",null,[s("KavaRef provides the "),u,s(" class to wrap your target generics to ensure that you can get the correct generic type at runtime. Its core functionality is referenced from "),e("a",m,[s("Gson"),l(n)]),s("'s "),h,s(".")]),v])}const B=o(i,[["render",A],["__file","kavaref-extension.html.vue"]]);export{B as default};
