Skip to content

Commit 09d0790

Browse files
[API Change] Added generics, ClassFinder and Enums
API Change: The BetterReflectionClass now uses generics to assign a class type Enums: Added support for enum classes through the EnumBetterReflectionClass, with the following methods + #getEnumConstants() + contains(String) + getIfPresent(String) + getOrNull(String) + getOrDefault(String, T) + toString(String) + toString() Added a ClassFinder: find and iterate through a java program's classes (both IDE runtime and packed jar) This class deprecates BetterReflectionUtils#getClassesInPackage(String) Added unit tests for the ClassFinder class
1 parent 240815d commit 09d0790

22 files changed

+904
-159
lines changed

README.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
# Java Better-Reflection
2+
23
## Faster Reflections [![Build Status](https://api.travis-ci.com/OxideWaveLength/Java-BetterReflection.svg?branch=main)](https://travis-ci.com/OxideWaveLength/Java-BetterReflection)
34

45
BetterReflection is a Java library that caches reflection to achieve lower access time.
56

6-
> Note: *BetterReflection is essentially caching reflections. There might be better alternatives for what you are trying to achieve.*
7+
> Note: *BetterReflection is essentially caching reflections. There might be better alternatives for what you are trying
8+
to achieve.*
79

810
#### This library has been created for Java 1.8, results may vary with different Java versions.
911

1012
## Installation
11-
Every update has been packed into a Jar and released on GitHub. This library is also being hosted on Maven Central, starting with version 1.0
13+
14+
Every update has been packed into a Jar and released on GitHub. This library is also being hosted on Maven Central,
15+
starting with version 1.0
16+
1217
```xml
1318
<dependency>
1419
<groupId>top.wavelength</groupId>
1520
<artifactId>Java-BetterReflection</artifactId>
1621
<version>LATEST_VERSION</version>
1722
</dependency>
1823
```
19-
Note: the package used to be me.wavelength.betterreflection up until version 1.0, where it's been renamed to top.wavelength.betterreflection
24+
25+
Note: the package used to be me.wavelength.betterreflection up until version 1.0, where it's been renamed to
26+
top.wavelength.betterreflection
2027

2128
## Usage
2229

23-
There are no dependencies needed to use this library. Once you've downloaded it and set it up in your IDE you can start using it.
30+
There are no dependencies needed to use this library. Once you've downloaded it and set it up in your IDE you can start
31+
using it.
2432

2533
You can temporarily cache a class by simply creating a new instance of BetterReflectionClass
2634

2735
```Java
28-
BetterReflectionClass ClassName = new BetterReflectionClass(ClassName.class); // You can also provide a string, containing the package and the name in this format: "com.example.Class".
36+
BetterReflectionClass<ClassName> className = new BetterReflectionClass<?>(ClassName.class); // You can also provide a string, containing the package and the name in this format: "com.example.Class".
2937
```
3038

3139
## Something more efficient...
32-
Temporarily caching a class, though, might be worse than using normal reflections, because the BetterReflection class caches everything as soon as instantiated.
33-
To achieve actually better results than normal reflections you must actually permanently cache said classes.
3440

41+
Temporarily caching a class, though, might be worse than using normal reflections, because the BetterReflection class
42+
caches everything as soon as instantiated.
43+
To achieve actually better results than normal reflections you must actually permanently cache said classes.
3544

3645
### Using the BetterReflection class
3746

3847
The BetterReflection class is a class that keeps in an arraylist the cached classes
3948

4049
#### Create an instance of BetterReflection inside of your main class
50+
4151
```Java
4252
private BetterReflection betterReflection;
4353

@@ -51,19 +61,22 @@ public BetterReflection getBetterReflection() {
5161
```
5262

5363
#### Get the class from the BetterReflection instance you've just created
64+
5465
```Java
5566
public class MyClass {
5667
...
57-
BetterReflectionClass ClassNeeded = exampleClass.getBetterReflection().getBetterReflectionClass(ClassNeeded.class);
68+
BetterReflectionClass<ClassNeeded> classNeeded = exampleClass.getBetterReflection().getBetterReflectionClass(ClassNeeded.class);
5869
...
5970
}
6071
```
6172

62-
> Note: *There are some cons to using this method. RAM usage could be somewhat high and once there are many classes looping through a list containing all of them might get expensive*
73+
> Note: *There are some cons to using this method. RAM usage could be somewhat high and once there are many classes
74+
looping through a list containing all of them might get expensive*
6375

6476
### Alternative way
6577

6678
#### Creating a class containing public static final fields
79+
6780
```Java
6881
public class ListOfMyClasses {
6982
...
@@ -73,6 +86,7 @@ public class ListOfMyClasses {
7386
```
7487

7588
#### Accessing it from anywhere
89+
7690
```Java
7791
import static ListOfMyClasses.*;
7892

@@ -98,18 +112,27 @@ public class MyClass {
98112
There are several ways you can contribute.
99113

100114
### Tests
101-
There are never enough tests! You can create a new test anytime you want to, but your test must meet specific requirements.
115+
116+
There are never enough tests! You can create a new test anytime you want to, but your test must meet specific
117+
requirements.
118+
102119
- Your test must extend the Test class
103120
- Your test must be added to the correct array inside of the Tester class
104121
- Your test must use the Timer class to return the time passed
105122
- Your test must tackle somewhat real scenarios
106123
- Your test must have a good English description
107124

108125
### Proper Benchmarks
109-
If you have some spare time and are generous enough, feel free to implement some proper benchmark and create a pull request. OpenJDK JHM is suggested, but if you're into some other benchmarking libraries feel free to use them instead
126+
127+
If you have some spare time and are generous enough, feel free to implement some proper benchmark and create a pull
128+
request. OpenJDK JHM is suggested, but if you're into some other benchmarking libraries feel free to use them instead
110129

111130
### BetterReflection itself
112-
If you spot issues in the code, think there are better ways to achieve something or think that there are methods that should be added, feel free to add them.
131+
132+
If you spot issues in the code, think there are better ways to achieve something or think that there are methods that
133+
should be added, feel free to add them.
113134

114135
### Issues
115-
Of course, contributing is not limited to code, you can also open issues to report bugs, performance issues or request features.
136+
137+
Of course, contributing is not limited to code, you can also open issues to report bugs, performance issues or request
138+
features.

build.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<project name="Java-BetterReflection" basedir="./Java-BetterReflection" default="main">
2-
<property name="dir.src" value="src"/>
3-
<property name="dir.build" value="build"/>
2+
<property name="dir.src" value="src"/>
3+
<property name="dir.build" value="build"/>
44
<property name="dir.classes" value="${dir.build}/classes"/>
5-
<property name="dir.jar" value="${dir.build}/jar"/>
5+
<property name="dir.jar" value="${dir.build}/jar"/>
66

7-
<property name="main-class" value="me.wavelength.BetterReflection"/>
7+
<property name="main-class" value="me.wavelength.BetterReflection"/>
88

99
<target name="clean">
1010
<delete dir="${dir.build}"/>

pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
<groupId>top.wavelength</groupId>
3131
<artifactId>Java-BetterReflection</artifactId>
32-
<version>1.0</version>
32+
<version>1.1</version>
3333

3434
<properties>
3535
<maven.compiler.source>1.8</maven.compiler.source>
@@ -46,6 +46,24 @@
4646
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
4747
</repository>
4848
</distributionManagement>
49+
50+
<dependencies>
51+
<dependency>
52+
<groupId>org.junit.jupiter</groupId>
53+
<artifactId>junit-jupiter-api</artifactId>
54+
<version>5.10.0</version>
55+
<scope>test</scope>
56+
</dependency>
57+
58+
<!-- JUnit Jupiter Engine -->
59+
<dependency>
60+
<groupId>org.junit.jupiter</groupId>
61+
<artifactId>junit-jupiter-engine</artifactId>
62+
<version>5.10.0</version>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
66+
4967
<build>
5068
<plugins>
5169
<plugin>

src/main/java/top/wavelength/betterreflection/BetterReflection.java

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,35 @@
1717
**/
1818
public class BetterReflection {
1919

20-
private final List<BetterReflectionClass> betterReflectionClasses;
20+
private final List<BetterReflectionClass<?>> betterReflectionClasses;
2121

22-
public static final BetterReflectionClass FIELD = new BetterReflectionClass(Field.class);
22+
/**
23+
* @since 1.1
24+
*/
25+
public static final BetterReflection INSTANCE = new BetterReflection();
26+
27+
public static final BetterReflectionClass<Field> FIELD = new BetterReflectionClass<>(Field.class);
2328

24-
private static final Map<Class<?>, BetterReflectionClass> PRIMITIVES = new HashMap<>();
29+
private static final Map<Class<?>, BetterReflectionClass<?>> PRIMITIVES = new HashMap<>();
2530

2631
static {
27-
PRIMITIVES.put(Short.class, new BetterReflectionClass(Short.class));
28-
PRIMITIVES.put(Byte.class, new BetterReflectionClass(Byte.class));
29-
PRIMITIVES.put(Double.class, new BetterReflectionClass(Double.class));
30-
PRIMITIVES.put(Integer.class, new BetterReflectionClass(Integer.class));
31-
PRIMITIVES.put(Float.class, new BetterReflectionClass(Float.class));
32-
PRIMITIVES.put(Boolean.class, new BetterReflectionClass(Boolean.class));
33-
PRIMITIVES.put(Long.class, new BetterReflectionClass(Long.class));
34-
PRIMITIVES.put(Void.class, new BetterReflectionClass(Void.class));
35-
36-
PRIMITIVES.put(short.class, new BetterReflectionClass(short.class));
37-
PRIMITIVES.put(byte.class, new BetterReflectionClass(byte.class));
38-
PRIMITIVES.put(double.class, new BetterReflectionClass(double.class));
39-
PRIMITIVES.put(int.class, new BetterReflectionClass(int.class));
40-
PRIMITIVES.put(float.class, new BetterReflectionClass(float.class));
41-
PRIMITIVES.put(boolean.class, new BetterReflectionClass(boolean.class));
42-
PRIMITIVES.put(long.class, new BetterReflectionClass(long.class));
43-
PRIMITIVES.put(void.class, new BetterReflectionClass(void.class));
32+
PRIMITIVES.put(Short.class, new BetterReflectionClass<>(Short.class));
33+
PRIMITIVES.put(Byte.class, new BetterReflectionClass<>(Byte.class));
34+
PRIMITIVES.put(Double.class, new BetterReflectionClass<>(Double.class));
35+
PRIMITIVES.put(Integer.class, new BetterReflectionClass<>(Integer.class));
36+
PRIMITIVES.put(Float.class, new BetterReflectionClass<>(Float.class));
37+
PRIMITIVES.put(Boolean.class, new BetterReflectionClass<>(Boolean.class));
38+
PRIMITIVES.put(Long.class, new BetterReflectionClass<>(Long.class));
39+
PRIMITIVES.put(Void.class, new BetterReflectionClass<>(Void.class));
40+
41+
PRIMITIVES.put(short.class, new BetterReflectionClass<>(short.class));
42+
PRIMITIVES.put(byte.class, new BetterReflectionClass<>(byte.class));
43+
PRIMITIVES.put(double.class, new BetterReflectionClass<>(double.class));
44+
PRIMITIVES.put(int.class, new BetterReflectionClass<>(int.class));
45+
PRIMITIVES.put(float.class, new BetterReflectionClass<>(float.class));
46+
PRIMITIVES.put(boolean.class, new BetterReflectionClass<>(boolean.class));
47+
PRIMITIVES.put(long.class, new BetterReflectionClass<>(long.class));
48+
PRIMITIVES.put(void.class, new BetterReflectionClass<>(void.class));
4449
}
4550

4651
/**
@@ -55,37 +60,42 @@ public BetterReflection() {
5560
/**
5661
* @return a copy of the cached classes
5762
*/
58-
public List<BetterReflectionClass> getBetterReflectionClasses() {
63+
public List<BetterReflectionClass<?>> getBetterReflectionClasses() {
5964
return new ArrayList<>(betterReflectionClasses);
6065
}
6166

62-
public BetterReflectionClass getBetterReflectionClass(String name) {
63-
for (BetterReflectionClass betterReflectionClass : getBetterReflectionClasses())
67+
public BetterReflectionClass<?> getBetterReflectionClass(String name) {
68+
for (BetterReflectionClass<?> betterReflectionClass : getBetterReflectionClasses())
6469
if (betterReflectionClass.getClasz().getCanonicalName() != null && betterReflectionClass.getClasz().getCanonicalName().equals(name))
6570
return betterReflectionClass;
6671

67-
BetterReflectionClass betterReflectionClass = BetterReflectionClass.forName(name);
72+
BetterReflectionClass<?> betterReflectionClass = BetterReflectionClass.forName(name);
6873
if (betterReflectionClass == null)
6974
return null;
7075
this.betterReflectionClasses.add(betterReflectionClass);
7176
return betterReflectionClass;
7277
}
7378

74-
public BetterReflectionClass getBetterReflectionClass(Class<?> clasz) {
75-
for (BetterReflectionClass betterReflectionClass : getBetterReflectionClasses())
79+
@SuppressWarnings("unchecked")
80+
public <T> BetterReflectionClass<T> getBetterReflectionClass(Class<T> clasz) {
81+
for (BetterReflectionClass<?> betterReflectionClass : getBetterReflectionClasses())
7682
if (betterReflectionClass.getClasz().equals(clasz))
77-
return betterReflectionClass;
83+
return (BetterReflectionClass<T>) betterReflectionClass;
7884

79-
BetterReflectionClass betterReflectionClass = new BetterReflectionClass(clasz);
85+
BetterReflectionClass<?> betterReflectionClass;
86+
if (clasz.isEnum())
87+
betterReflectionClass = new EnumBetterReflectionClass<>((Class<Enum<?>>) clasz);
88+
else
89+
betterReflectionClass = new BetterReflectionClass<>(clasz);
8090
this.betterReflectionClasses.add(betterReflectionClass);
81-
return betterReflectionClass;
91+
return (BetterReflectionClass<T>) betterReflectionClass;
8292
}
8393

84-
public static Map<Class<?>, BetterReflectionClass> getPrimitives() {
94+
public static Map<Class<?>, BetterReflectionClass<?>> getPrimitives() {
8595
return PRIMITIVES;
8696
}
8797

88-
public static Object getFieldValue(BetterReflectionClass betterReflectionClass, Object instance, String fieldName) throws IllegalAccessException {
98+
public static Object getFieldValue(BetterReflectionClass<?> betterReflectionClass, Object instance, String fieldName) throws IllegalAccessException {
8999
return betterReflectionClass.getDeclaredField(fieldName).get(instance);
90100
}
91101

@@ -102,7 +112,7 @@ public static FutureTask<String> getLatestVersion() {
102112
encoding = encoding == null ? "UTF-8" : encoding.substring(encoding.indexOf("charset=") + 8);
103113
ByteArrayOutputStream baos = new ByteArrayOutputStream();
104114
byte[] buf = new byte[8192];
105-
int len = 0;
115+
int len;
106116
while ((len = in.read(buf)) != -1)
107117
baos.write(buf, 0, len);
108118
String body = baos.toString(encoding);
@@ -145,7 +155,7 @@ public static FutureTask<Boolean> isUpToDate() {
145155
* @since 0.4
146156
*/
147157
public static String getVersion() {
148-
return "1.0";
158+
return "1.1";
149159
}
150160

151161
/**
@@ -175,8 +185,8 @@ public static int[] versionToInts(String version) {
175185
}
176186

177187

178-
public static final BetterReflectionClass JAVA_CLASS = new BetterReflectionClass(Class.class);
188+
public static final BetterReflectionClass<?> JAVA_CLASS = new BetterReflectionClass<>(Class.class);
179189

180-
public static final BetterReflectionClass UNSAFE_CLASS = new BetterReflectionClass(Unsafe.class);
190+
public static final BetterReflectionClass<Unsafe> UNSAFE_CLASS = new BetterReflectionClass<>(Unsafe.class);
181191

182192
}

0 commit comments

Comments
 (0)