diff --git a/src/main/java/me/mrnavastar/r/R.java b/src/main/java/me/mrnavastar/r/R.java index 156df77..bf40905 100644 --- a/src/main/java/me/mrnavastar/r/R.java +++ b/src/main/java/me/mrnavastar/r/R.java @@ -8,15 +8,26 @@ public class R { private final Object instance; + private final Class clazz; public R(Object instance) { this.instance = instance; + clazz = instance.getClass(); + } + + public R(Class clazz) { + instance = null; + this.clazz = clazz; } public static R of(Object instance) { return new R(instance); } + public static R of(Class clazz) { + return new R(clazz); + } + // Search super classes for field private Field findField(Class clazz, String name) throws NoSuchFieldException { if (clazz == null) throw new NoSuchFieldException(); @@ -47,7 +58,7 @@ private Method findMethod(Class clazz, String name, Class[] argTypes) thro public T get(String name, Class type) { try { - return type.cast(findField(instance.getClass(), name).get(instance)); + return type.cast(findField(clazz, name).get(instance)); } catch (IllegalAccessException | NoSuchFieldException e) { throw new RuntimeException(e); } @@ -55,7 +66,7 @@ public T get(String name, Class type) { public void set(String name, Object value) { try { - findField(instance.getClass(), name).set(instance, value); + findField(clazz, name).set(instance, value); } catch (NoSuchFieldException | IllegalAccessException e) { throw new RuntimeException(e); } @@ -64,7 +75,7 @@ public void set(String name, Object value) { public T call(String name, Class returnType, Object... args) { try { Class[] classes = Arrays.stream(args).map(Object::getClass).toArray(Class[]::new); - Object returnVal = findMethod(instance.getClass(), name, classes).invoke(instance, args); + Object returnVal = findMethod(clazz, name, classes).invoke(instance, args); if (returnVal == null || returnType == null) return null; return returnType.cast(returnVal); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {