11package tech .hiddenproject .aide .example ;
22
3+ import java .lang .reflect .Constructor ;
4+ import java .lang .reflect .Method ;
5+ import java .util .Arrays ;
6+ import java .util .stream .Collectors ;
37import tech .hiddenproject .aide .reflection .LambdaWrapper ;
48import tech .hiddenproject .aide .reflection .LambdaWrapperHolder ;
9+ import tech .hiddenproject .aide .reflection .MethodHolder ;
10+ import tech .hiddenproject .aide .reflection .WrapperHolder ;
511import tech .hiddenproject .aide .reflection .annotation .ExactInvoker ;
12+ import tech .hiddenproject .aide .reflection .matcher .ArgumentMatcherHolder ;
13+ import tech .hiddenproject .aide .reflection .signature .LambdaMetadata ;
14+ import tech .hiddenproject .aide .reflection .signature .MatcherSignature ;
15+ import tech .hiddenproject .aide .reflection .signature .MethodSignature ;
16+ import tech .hiddenproject .aide .reflection .util .ReflectionUtil ;
617
718/**
819 * @author Danila Rassokhin
@@ -11,38 +22,122 @@ public class ReflectionExample {
1122
1223
1324 public ReflectionExample () {
14- LambdaWrapperHolder lambdaWrapperHolder = LambdaWrapperHolder .INSTANCE ;
25+ // Create holder with default LambdaWrapper initialization
26+ LambdaWrapperHolder lambdaWrapperHolder = LambdaWrapperHolder .DEFAULT ;
27+ // Load all annotated methods from TestInterface
1528 lambdaWrapperHolder .add (TestInterface .class );
16-
29+ // Create caller
1730 TestClass caller = new TestClass ();
1831
19- LambdaWrapper getter = lambdaWrapperHolder .wrap (caller , "get" );
20-
32+ // Find getter
33+ Method getMethod = ReflectionUtil .getMethod (TestClass .class , "get" );
34+ // Wrap getter
35+ WrapperHolder <LambdaWrapper > getterHolder = lambdaWrapperHolder .wrap (getMethod );
36+ LambdaWrapper getter = getterHolder .getWrapper ();
37+ // Invoke getter
2138 Long id = getter .get (caller );
2239 System .out .println ("Getter result: " + id );
2340
24- LambdaWrapper setter = lambdaWrapperHolder .wrap (caller , "set" , String .class );
25-
41+ // Get setter
42+ Method setMethod = ReflectionUtil .getMethod (TestClass .class , "set" , String .class );
43+ // Wrap setter
44+ WrapperHolder <LambdaWrapper > setterHolder = lambdaWrapperHolder .wrap (setMethod );
45+ LambdaWrapper setter = setterHolder .getWrapper ();
46+ // Invoke setter
2647 setter .set (caller , "Hello" );
2748
28- LambdaWrapper function = lambdaWrapperHolder .wrap (caller , "apply" , String .class );
29-
49+ // Find apply method
50+ Method functionMethod = ReflectionUtil .getMethod (TestClass .class , "apply" , String .class );
51+ // Wrap apply method
52+ WrapperHolder <LambdaWrapper > functionHolder = lambdaWrapperHolder .wrap (functionMethod );
53+ LambdaWrapper function = functionHolder .getWrapper ();
54+ // Invoke apply method and get result
3055 int res = function .apply (caller , "Hi" );
3156 System .out .println ("Function result: " + res );
3257
33- TestInterface testInterface = lambdaWrapperHolder .wrapExact (caller , "special" , int .class );
34- String special = testInterface .special (caller , 1 );
58+ // Find special method
59+ Method specialMethod = ReflectionUtil .getMethod (TestClass .class , "exact" , int .class );
60+ // Wrap special method with exact same wrapper from TestInterface
61+ WrapperHolder <TestInterface > specialHolder = lambdaWrapperHolder .wrapExact (
62+ specialMethod , TestInterface .class );
63+ TestInterface testInterface = specialHolder .getWrapper ();
64+ // Invoke special method and get result
65+ String special = testInterface .exact (caller , 1 );
3566 System .out .println ("Special result: " + special );
67+
68+ // Get method with var args
69+ Method varArgsMethod = ReflectionUtil .getMethod (TestClass .class , "varargs" , Object [].class );
70+ // Wrap method with var args
71+ WrapperHolder <LambdaWrapper > varArgsHolder = lambdaWrapperHolder .wrap (varArgsMethod );
72+ LambdaWrapper varargs = varArgsHolder .getWrapper ();
73+ // Invoke method and get result
74+ Class <?>[] varArgsTypes = varargs .apply (caller , new Object []{"Hello" , 1 });
75+ System .out .println ("Var args result: " + Arrays .toString (varArgsTypes ));
76+
77+ // Find constructor of TestClass
78+ Constructor <TestClass > constructorMethod = ReflectionUtil .getConstructor (TestClass .class );
79+ // Wrap constructor
80+ MethodHolder <LambdaWrapper , ReflectionExample , TestClass > constructor =
81+ lambdaWrapperHolder .wrapSafe (constructorMethod );
82+ // Call constructor
83+ caller = constructor .invokeStatic ();
84+
85+ // Wrap special method
86+ MethodHolder <LambdaWrapper , TestClass , String > methodHolder = lambdaWrapperHolder .wrapSafe (
87+ specialMethod );
88+ // Invoke special method
89+ String r = methodHolder .invoke (caller , 1 );
90+
91+ // Wrap setter
92+ MethodHolder <LambdaWrapper , TestClass , Void > setHolder = lambdaWrapperHolder .wrapSafe (
93+ setMethod );
94+ // Invoke setter
95+ setHolder .invoke (caller , "hello" );
96+
97+ // Find static method
98+ Method staticMethod = ReflectionUtil .getMethod (TestClass .class , "staticMethod" , String .class );
99+ // Wrap static method
100+ MethodHolder <LambdaWrapper , TestClass , Integer > staticHolder = lambdaWrapperHolder .wrapSafe (
101+ staticMethod );
102+ // Invoke static method without caller
103+ int staticResult = staticHolder .invokeStatic ("Hello" );
104+ System .out .println ("Static result: " + staticResult );
105+
106+ // Create metadata for wrapper lambda
107+ Method testMethod = ReflectionUtil .getMethod (
108+ TestInterface .class , "exact" , Object .class , int .class );
109+ LambdaMetadata testWrapper = LambdaMetadata .from (testMethod );
110+ // Wrap special method with your own lambda metadata
111+ MethodHolder <TestInterface , TestClass , String > testHolder = lambdaWrapperHolder .wrapSafe (
112+ specialMethod , testWrapper );
113+ // Create new ArgumentMatcher for wrapper
114+ MethodSignature methodSignature = MethodSignature .fromWrapper (testMethod );
115+ MatcherSignature <TestInterface > matcherSignature = new MatcherSignature <>(
116+ TestInterface .class , methodSignature );
117+ ArgumentMatcherHolder .INSTANCE .addMatcher (
118+ matcherSignature , (holder , original , args ) -> holder .getWrapper ()
119+ .exact (args [0 ], (int ) args [1 ]));
120+ // Invoke special method with custom argument matcher
121+ testHolder .invoke (caller , 1 );
36122 }
37123
38124 public interface TestInterface {
39125
40126 @ ExactInvoker
41- String special (Object caller , int id );
127+ String exact (Object caller , int id );
42128 }
43129
44130 public static class TestClass {
45131
132+ public TestClass () {
133+ System .out .println ("Test class constructor invoked!" );
134+ }
135+
136+ public static int staticMethod (String s ) {
137+ System .out .println ("Static invoked" );
138+ return s .length ();
139+ }
140+
46141 public Long get () {
47142 System .out .println ("Getter invoked" );
48143 return 1L ;
@@ -57,9 +152,14 @@ public int apply(String arg) {
57152 return arg .length ();
58153 }
59154
60- public String special (int id ) {
155+ public String exact (int id ) {
61156 System .out .println ("Special invoked: " + id );
62157 return String .valueOf (id );
63158 }
159+
160+ public Class <?>[] varargs (Object ... args ) {
161+ return Arrays .stream (args ).map (Object ::getClass ).collect (Collectors .toList ())
162+ .toArray (new Class []{});
163+ }
64164 }
65165}
0 commit comments