Skip to content

Commit

Permalink
Better object reflections
Browse files Browse the repository at this point in the history
  • Loading branch information
HttpMarco committed Feb 22, 2024
1 parent 2f45161 commit 6385840
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 30 deletions.
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ allprojects {


group = "dev.httpmarco"
version = "1.0.15-SNAPSHOT"
version = "1.0.16-SNAPSHOT"

repositories {
mavenCentral()
Expand All @@ -20,6 +20,9 @@ allprojects {
dependencies {
implementation(rootProject.libs.lombok)
annotationProcessor(rootProject.libs.lombok)

implementation(rootProject.libs.annotations)
annotationProcessor(rootProject.libs.annotations)
}

tasks.withType<JavaCompile> {
Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

nexusPublish = "2.0.0-rc-2"

annotations = "24.1.0"
lombok = "1.18.30"

gson = "2.10.1"

[libraries]
lombok = { group = "org.projectlombok", name="lombok", version.ref = "lombok" }

annotations = { group = "org.jetbrains", name="annotations", version.ref = "annotations" }
gson = { group = "com.google.code.gson", name="gson", version.ref = "gson" }

[plugins]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package dev.httpmarco.osgan.reflections.allocator;
package dev.httpmarco.osgan.reflections;

import sun.misc.Unsafe;

import java.lang.reflect.InvocationTargetException;

public final class ReflectionClassAllocater {
final class Allocator {

public static final Unsafe unsafe;
private static final Unsafe unsafe;

static {
try {
Expand All @@ -18,6 +18,7 @@ public final class ReflectionClassAllocater {
}
}

@SuppressWarnings("unchecked")
public static <T> T allocate(Class<T> tClass) {
try {
return (T) unsafe.allocateInstance(tClass);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,80 @@
package dev.httpmarco.osgan.reflections;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

public final class Reflections {
@RequiredArgsConstructor
public class Reflections<T> {

private final Class<T> clazz;
private @Nullable T value;

public static <D> Reflections<D> of(Class<D> clazz) {
return new Reflections<>(clazz);
}

public Reflections<T> withValue(Object value) {
this.value = clazz.cast(value);
return this;
}

@SneakyThrows
public static void modifyField(Field field, Object object, Object value) {
public Field field(String id) {
var field = this.clazz.getDeclaredField(id);
field.setAccessible(true);
field.set(object, value);
return field;
}

@SneakyThrows
public Method method(String id) {
var method = this.clazz.getDeclaredMethod(id);
method.setAccessible(true);
return method;
}

@SneakyThrows
public static Object getField(Field field, Object object) {
public T newInstanceWithNoArgs() {
return this.clazz.getConstructor().newInstance();
}

public T allocate() {
return Allocator.allocate(clazz);
}

@SneakyThrows
@SuppressWarnings("unchecked")
public T value(Field field) {
field.setAccessible(true);
return field.get(object);
return (T) field.get(this.value);
}

public static <T> T newInstance(Class<T> clazz, Object... objects) {
try {
return clazz.getConstructor(Arrays.stream(objects).map(Object::getClass).toArray(Class[]::new)).newInstance(objects);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
@SneakyThrows
public T value(String fieldId) {
return this.value(field(fieldId));
}

public static Object getField(String id, Object value) {
try {
return getField(value.getClass().getDeclaredField(id), value);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
@SneakyThrows
public void modify(Field field, Object value) {
field.setAccessible(true);
field.set(this.value, value);
}

public static void callMethod(Method method, Object object, Object... args) {
try {
method.invoke(object, args);
} catch (Exception e) {
throw new RuntimeException(e);
}
@SneakyThrows
public void modify(String fieldId, Object value) {
this.modify(field(fieldId), value);
}

@SneakyThrows
public void applyMethod(Method method, Object... args) {
method.invoke(value, args);
}

@SneakyThrows
public void applyMethod(String methodId, Object... args) {
method(methodId).invoke(value, args);
}
}

0 comments on commit 6385840

Please sign in to comment.