Skip to content

Commit 62f2821

Browse files
authored
Merge pull request #6 from CrissNamon/dev
1.1
2 parents ac1faea + 413f54d commit 62f2821

36 files changed

+1120
-277
lines changed

.github/workflows/maven.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ jobs:
2020
runs-on: ubuntu-latest
2121

2222
steps:
23-
- uses: actions/checkout@v3
24-
- name: Set up JDK 8
25-
uses: actions/setup-java@v3
26-
with:
27-
java-version: '8'
28-
distribution: 'temurin'
29-
cache: maven
30-
- name: Build with Maven
31-
run: mvn -B test --file pom.xml
23+
- uses: actions/checkout@v3
24+
- name: Set up JDK 8
25+
uses: actions/setup-java@v3
26+
with:
27+
java-version: '8'
28+
distribution: 'temurin'
29+
cache: maven
30+
- name: Build with Maven
31+
run: mvn -B test --file pom.xml

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![Build](https://github.com/CrissNamon/aide/actions/workflows/maven.yml/badge.svg)](https://github.com/CrissNamon/aide/actions/workflows/maven.yml)
44

55
Aide is a set of useful utils to simplify your code
6+
67
## Content
78

89
* Reflection
@@ -13,6 +14,7 @@ Aide is a set of useful utils to simplify your code
1314
## Use
1415

1516
Artifact ids:
17+
1618
- all -> aide-all
1719
- reflection -> aide-reflection
1820
- optional -> aide-optional
@@ -23,14 +25,14 @@ Artifact ids:
2325
<dependency>
2426
<groupId>tech.hiddenproject</groupId>
2527
<artifactId>aide-all</artifactId>
26-
<version>1.0</version>
28+
<version>1.1</version>
2729
</dependency>
2830
```
2931

3032
### Gradle
3133

3234
```groovy
33-
implementation 'tech:hiddenproject:aide-all:1.0'
35+
implementation 'tech:hiddenproject:aide-all:1.1'
3436
```
3537

3638
## Resources
@@ -39,13 +41,14 @@ ___
3941

4042
* Learn more at Aide [Wiki](https://github.com/CrissNamon/aide/wiki)
4143
* Look at some examples
42-
in [example](https://github.com/CrissNamon/aide/tree/main/aide-all/src/main/java/tech/hiddenproject/aide/example) package
44+
in [example](https://github.com/CrissNamon/aide/tree/main/aide-all/src/main/java/tech/hiddenproject/aide/example)
45+
package
4346

4447
## Dependencies and source
4548

4649
___
4750

48-
Aide has no dependencies and use only Java 8.</p>
51+
Aide has no dependencies and use only Java 8.</p>
4952

5053
## Repository info
5154

aide-all/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<dependency>
88
<artifactId>aide-optional</artifactId>
99
<groupId>tech.hiddenproject</groupId>
10-
<version>1.0</version>
10+
<version>1.1</version>
1111
</dependency>
1212
<dependency>
1313
<artifactId>aide-reflection</artifactId>
1414
<groupId>tech.hiddenproject</groupId>
15-
<version>1.0</version>
15+
<version>1.1</version>
1616
</dependency>
1717
</dependencies>
1818

@@ -21,7 +21,7 @@
2121
<parent>
2222
<artifactId>aide</artifactId>
2323
<groupId>tech.hiddenproject</groupId>
24-
<version>1.0</version>
24+
<version>1.1</version>
2525
</parent>
2626

2727
<properties>
Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
package 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;
37
import tech.hiddenproject.aide.reflection.LambdaWrapper;
48
import tech.hiddenproject.aide.reflection.LambdaWrapperHolder;
9+
import tech.hiddenproject.aide.reflection.MethodHolder;
10+
import tech.hiddenproject.aide.reflection.WrapperHolder;
511
import 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
}

aide-optional/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
<parent>
99
<artifactId>aide</artifactId>
1010
<groupId>tech.hiddenproject</groupId>
11-
<version>1.0</version>
11+
<version>1.1</version>
1212
</parent>
1313

14-
<version>1.0</version>
15-
1614
<properties>
1715
<maven.compiler.source>8</maven.compiler.source>
1816
<maven.compiler.target>8</maven.compiler.target>
1917
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2018
</properties>
2119

20+
<version>1.1</version>
21+
2222
</project>

aide-optional/src/main/java/tech/hiddenproject/aide/optional/Action.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tech.hiddenproject.aide.optional;
22

33
/**
4+
* No args version of {@link java.util.function.Consumer}.
5+
*
46
* @author Danila Rassokhin
57
*/
68
public interface Action {

aide-optional/src/main/java/tech/hiddenproject/aide/optional/BooleanOptional.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void ifFalseThen(Action action) {
7070
* If value is true then throws exception.
7171
*
7272
* @param throwableSupplier {@link Supplier} for {@link Throwable}
73-
* @param <X> {@link Throwable} type
73+
* @param <X> {@link Throwable} type
7474
* @throws X if value is true
7575
*/
7676
public <X extends Throwable> void ifTrueThrow(Supplier<X> throwableSupplier) throws X {
@@ -83,7 +83,7 @@ public <X extends Throwable> void ifTrueThrow(Supplier<X> throwableSupplier) thr
8383
* If value is false then throws exception.
8484
*
8585
* @param throwableSupplier {@link Supplier} for {@link Throwable}
86-
* @param <X> {@link Throwable} type
86+
* @param <X> {@link Throwable} type
8787
* @throws X if false is true
8888
*/
8989
public <X extends Throwable> void ifFalseThrow(Supplier<X> throwableSupplier) throws X {

aide-optional/src/main/java/tech/hiddenproject/aide/optional/IfTrueConditional.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.LinkedHashMap;
44
import java.util.Map;
5+
import java.util.Map.Entry;
56
import java.util.Optional;
67
import java.util.function.Predicate;
78
import java.util.function.Supplier;
@@ -66,7 +67,7 @@ public ConditionalThen<T> ifTrue(boolean condition) {
6667
* @return Result of conditional
6768
*/
6869
public <D> D orElse(D defaultValue) {
69-
return get().map(value -> (D) value).orElse(defaultValue);
70+
return get().map(value -> (D) value.getValue().get()).orElse(defaultValue);
7071
}
7172

7273
/**
@@ -77,7 +78,7 @@ public <D> D orElse(D defaultValue) {
7778
* @return Result of conditional
7879
*/
7980
public <D> D orElseGet(Supplier<D> defaultValue) {
80-
return get().map(value -> (D) value).orElseGet(defaultValue);
81+
return get().map(value -> (D) value.getValue().get()).orElseGet(defaultValue);
8182
}
8283

8384
/**
@@ -90,15 +91,15 @@ public <D> D orElseGet(Supplier<D> defaultValue) {
9091
* @throws X if all branches failed
9192
*/
9293
public <X extends Throwable, D> D orElseThrows(Supplier<? extends X> exceptionSupplier) throws X {
93-
return get().map(value -> (D) value).orElseThrow(exceptionSupplier);
94+
return get().map(value -> (D) value.getValue().get()).orElseThrow(exceptionSupplier);
9495
}
9596

9697
/**
9798
* @return Result of conditional as {@link Optional}
9899
*/
99-
private Optional<T> get() {
100-
return conditions.entrySet().stream().filter(entry -> entry.getKey().test())
101-
.map(entry -> entry.getValue().get())
100+
private Optional<Entry<BooleanAction, Supplier<T>>> get() {
101+
return conditions.entrySet().stream()
102+
.filter(entry -> entry.getKey().test())
102103
.findFirst();
103104
}
104105

aide-optional/src/main/java/tech/hiddenproject/aide/optional/ObjectUtils.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ObjectUtils {
1414
* Checks if null objects count is greater than maxCount.
1515
*
1616
* @param maxCount Max count of null objects
17-
* @param objects Objects to check
17+
* @param objects Objects to check
1818
* @return true if null objects count is greater than maxCount
1919
*/
2020
public static boolean isMoreThanNull(int maxCount, Object... objects) {
@@ -33,4 +33,25 @@ public static boolean isMoreThanNull(Object... objects) {
3333
return isMoreThanNull(1, objects);
3434
}
3535

36+
/**
37+
* Checks if object classes equals.
38+
*
39+
* @param o1 {@link Object}
40+
* @param o2 {@link Object}
41+
* @return true if o1.getClass.equals(o2.getClass())
42+
*/
43+
public static boolean equalsClass(Object o1, Object o2) {
44+
return o1.getClass().equals(o2.getClass());
45+
}
46+
47+
/**
48+
* Checks if object is instance of class.
49+
*
50+
* @param o {@link Object}
51+
* @param c {@link Class}
52+
* @return true if o.getClass().equals(c)
53+
*/
54+
public static boolean isInstanceOf(Object o, Class<?> c) {
55+
return o.getClass().equals(c);
56+
}
3657
}

0 commit comments

Comments
 (0)