mirror of
https://github.com/HighCapable/KavaRef.git
synced 2025-09-08 11:34:14 +08:00
Initial commit
This commit is contained in:
28
samples/kavaref-demo/build.gradle.kts
Normal file
28
samples/kavaref-demo/build.gradle.kts
Normal file
@@ -0,0 +1,28 @@
|
||||
plugins {
|
||||
autowire(libs.plugins.kotlin.jvm)
|
||||
}
|
||||
|
||||
group = property.project.samples.kavaref.demo.groupName
|
||||
version = property.project.samples.kavaref.demo.version
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(17)
|
||||
sourceSets.all { languageSettings { languageVersion = "2.0" } }
|
||||
compilerOptions {
|
||||
freeCompilerArgs = listOf(
|
||||
"-Xno-param-assertions",
|
||||
"-Xno-call-assertions",
|
||||
"-Xno-receiver-assertions"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(projects.kavarefCore)
|
||||
implementation(projects.kavarefExtension)
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* KavaRef - A modernizing Java Reflection with Kotlin.
|
||||
* Copyright (C) 2019 HighCapable
|
||||
* https://github.com/HighCapable/KavaRef
|
||||
*
|
||||
* Apache License Version 2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is created by fankes on 2025/5/16.
|
||||
*/
|
||||
package com.highcapable.kavaref.demo;
|
||||
|
||||
import com.highcapable.kavaref.KavaRef;
|
||||
import com.highcapable.kavaref.demo.test.Test;
|
||||
import com.highcapable.kavaref.runtime.KavaRefRuntime;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Enable KavaRef debug level logging.
|
||||
KavaRef.setLogLevel(KavaRefRuntime.LogLevel.DEBUG);
|
||||
// Resolve the Test class using KavaRef.
|
||||
var memberScope = KavaRef.resolveClass(Test.class);
|
||||
// Create an instance of the Test class using the resolved class.
|
||||
var test = memberScope.constructor()
|
||||
.emptyParameters()
|
||||
.build()
|
||||
.get(0)
|
||||
.create();
|
||||
// Invoke a method, modify a field, and retrieve a value using the resolved class.
|
||||
memberScope.method()
|
||||
.name("test")
|
||||
.parameters(String.class)
|
||||
.build()
|
||||
.get(0)
|
||||
.of(test)
|
||||
.invoke("reflection test");
|
||||
// Modify the field 'myTest' and retrieve its value using the resolved class.
|
||||
memberScope.field()
|
||||
.name("myTest")
|
||||
.type(String.class)
|
||||
.build()
|
||||
.get(0)
|
||||
.of(test)
|
||||
.set("Hello modified reflection test");
|
||||
// Retrieve the value of the field 'myTest' using the resolved class.
|
||||
var testString = (String) memberScope.method()
|
||||
.name("getTest")
|
||||
.emptyParameters()
|
||||
.build()
|
||||
.get(0)
|
||||
.of(test)
|
||||
.invoke();
|
||||
// Print the value of the field 'myTest'.
|
||||
System.out.println(testString);
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* KavaRef - A modernizing Java Reflection with Kotlin.
|
||||
* Copyright (C) 2019 HighCapable
|
||||
* https://github.com/HighCapable/KavaRef
|
||||
*
|
||||
* Apache License Version 2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is created by fankes on 2025/5/24.
|
||||
*/
|
||||
package com.highcapable.kavaref.demo.test;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
public class Test {
|
||||
|
||||
private String myTest = "Hello private Test";
|
||||
|
||||
private Test() {
|
||||
System.out.println("Create private Test");
|
||||
}
|
||||
|
||||
private void test(String myTest) {
|
||||
System.out.println("Hello private " + myTest);
|
||||
}
|
||||
|
||||
public Test(String aa,Boolean bb,boolean cc){
|
||||
System.out.println("Create Test with parameters: " + aa + ", " + bb + ", " + cc);
|
||||
}
|
||||
|
||||
private String getTest() {
|
||||
return this.myTest;
|
||||
}
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* KavaRef - A modernizing Java Reflection with Kotlin.
|
||||
* Copyright (C) 2019 HighCapable
|
||||
* https://github.com/HighCapable/KavaRef
|
||||
*
|
||||
* Apache License Version 2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is created by fankes on 2025/5/10.
|
||||
*/
|
||||
package com.highcapable.kavaref.demo
|
||||
|
||||
import com.highcapable.kavaref.KavaRef
|
||||
import com.highcapable.kavaref.KavaRef.Companion.resolve
|
||||
import com.highcapable.kavaref.demo.test.Test
|
||||
import com.highcapable.kavaref.runtime.KavaRefRuntime
|
||||
|
||||
fun main() {
|
||||
// Enable KavaRef debug level logging.
|
||||
KavaRef.logLevel = KavaRefRuntime.LogLevel.DEBUG
|
||||
// Resolve the Test class using KavaRef.
|
||||
// Create an instance of the Test class using the resolved class.
|
||||
val test = Test::class.resolve()
|
||||
.firstConstructor {
|
||||
emptyParameters()
|
||||
}.create()
|
||||
// Invoke a method, modify a field, and retrieve a value using the resolved class.
|
||||
// (1) Call from Class.
|
||||
Test::class.resolve()
|
||||
.firstMethod {
|
||||
name = "test"
|
||||
parameters(String::class)
|
||||
}.of(test).invoke("reflection test")
|
||||
// (2) Call from Object.
|
||||
test.resolve()
|
||||
.firstMethod {
|
||||
name = "test"
|
||||
parameters(String::class)
|
||||
}.invoke("reflection test")
|
||||
// Modify the field 'myTest' and retrieve its value using the resolved class.
|
||||
// (1) Call from Class.
|
||||
Test::class.resolve()
|
||||
.firstField {
|
||||
name = "myTest"
|
||||
type = String::class
|
||||
}.of(test).set("Hello modified reflection test")
|
||||
// (2) Call from Object.
|
||||
test.resolve()
|
||||
.firstField {
|
||||
name = "myTest"
|
||||
type = String::class
|
||||
}.set("Hello modified reflection test")
|
||||
// Retrieve the value of the field 'myTest' using the resolved class.
|
||||
// (1) Call from Class.
|
||||
val testString1 = Test::class.resolve()
|
||||
.firstMethod {
|
||||
name = "getTest"
|
||||
emptyParameters()
|
||||
}.of(test).invoke<String>()
|
||||
// (2) Call from Object.
|
||||
val testString2 = test.resolve()
|
||||
.firstMethod {
|
||||
name = "getTest"
|
||||
emptyParameters()
|
||||
}.invoke<String>()
|
||||
// Print the value of the field 'myTest'.
|
||||
println(testString1)
|
||||
println(testString2)
|
||||
}
|
Reference in New Issue
Block a user