Various code simplification/fixes and a lot of typo/grammar fixes (may brake some stuff)
This commit is contained in:
@@ -37,7 +37,7 @@ public class Reflect {
|
||||
|
||||
/**
|
||||
* Wraps the class of the provided object into a {@link ReflectClass}.
|
||||
* @param instance the object wrom which to get the class using {@link Object#getClass()}.
|
||||
* @param instance the object from which to get the class using {@link Object#getClass()}.
|
||||
* @return a {@link ReflectClass} wrapping the provided object’s class.
|
||||
* @throws IllegalArgumentException if {@code instance} is null.
|
||||
*/
|
||||
|
@@ -57,7 +57,7 @@ public class ReflectClass<T> {
|
||||
* @param name the method name.
|
||||
* @param paramTypes the types of the method parameters.
|
||||
* @return a {@link ReflectMethod} wrapping the requested {@link Method}.
|
||||
* @throws NoSuchMethodException if the requested method doesn’t exists in the wrapped class.
|
||||
* @throws NoSuchMethodException if the requested method doesn't exist in the wrapped class.
|
||||
*/
|
||||
public ReflectMethod<T> method(String name, Class<?>... paramTypes) throws NoSuchMethodException {
|
||||
return method(new MethodIdentifier(name, paramTypes), false);
|
||||
@@ -69,7 +69,7 @@ public class ReflectClass<T> {
|
||||
* @param name the method name.
|
||||
* @param paramTypes the types of the method parameters.
|
||||
* @return a {@link ReflectMethod} wrapping the requested {@link Method}.
|
||||
* @throws NoSuchMethodException if the requested method doesn’t exists in the wrapped class.
|
||||
* @throws NoSuchMethodException if the requested method doesn't exist in the wrapped class.
|
||||
*/
|
||||
public ReflectMethod<T> filteredMethod(String name, Class<?>... paramTypes) throws NoSuchMethodException {
|
||||
return method(new MethodIdentifier(name, paramTypes), true);
|
||||
@@ -93,7 +93,7 @@ public class ReflectClass<T> {
|
||||
* Provides a {@link ReflectConstructor} wrapping the requested {@link Constructor}.
|
||||
* @param paramTypes the types of the constructor parameters.
|
||||
* @return a {@link ReflectConstructor} wrapping the requested {@link Constructor}.
|
||||
* @throws NoSuchMethodException if the requested constructor doesn’t exists in the wrapped class.
|
||||
* @throws NoSuchMethodException if the requested constructor doesn't exist in the wrapped class.
|
||||
*/
|
||||
public ReflectConstructor<T> constructor(Class<?>... paramTypes) throws NoSuchMethodException {
|
||||
return constructor(new ConstructorIdentifier(paramTypes), false);
|
||||
@@ -104,7 +104,7 @@ public class ReflectClass<T> {
|
||||
* filtering in the {@link Class} implementation.
|
||||
* @param paramTypes the types of the constructor parameters.
|
||||
* @return a {@link ReflectConstructor} wrapping the requested {@link Constructor}.
|
||||
* @throws NoSuchMethodException if the requested constructor doesn’t exists in the wrapped class.
|
||||
* @throws NoSuchMethodException if the requested constructor doesn't exist in the wrapped class.
|
||||
*/
|
||||
public ReflectConstructor<T> filteredConstructor(Class<?>... paramTypes) throws NoSuchMethodException {
|
||||
return constructor(new ConstructorIdentifier(paramTypes), true);
|
||||
@@ -128,7 +128,7 @@ public class ReflectClass<T> {
|
||||
* Provides a {@link ReflectField} wrapping the requested {@link Field}.
|
||||
* @param name the name of the field.
|
||||
* @return a {@link ReflectField} wrapping the requested {@link Field}.
|
||||
* @throws NoSuchFieldException if the requested field doesn’t exists in the wrapped class.
|
||||
* @throws NoSuchFieldException if the requested field doesn't exist in the wrapped class.
|
||||
*/
|
||||
public ReflectField<T> field(String name) throws NoSuchFieldException {
|
||||
return field0(name, false);
|
||||
@@ -139,10 +139,11 @@ public class ReflectClass<T> {
|
||||
* {@link Class} implementation.
|
||||
* @param name the name of the field.
|
||||
* @return a {@link ReflectField} wrapping the requested {@link Field}.
|
||||
* @throws NoSuchFieldException if the requested field doesn’t exists in the wrapped class.
|
||||
* @throws NoSuchFieldException if the requested field doesn't exist in the wrapped class.
|
||||
* @deprecated on Java 17, does not work due to module encapsulation, it is impossible to bypass the Java reflection
|
||||
* API internal filtering.
|
||||
*/
|
||||
@SuppressWarnings("DeprecatedIsStillUsed")
|
||||
@Deprecated(since = "Java 17")
|
||||
public ReflectField<T> filteredField(String name) throws NoSuchFieldException {
|
||||
return field0(name, true);
|
||||
|
@@ -61,7 +61,7 @@ public final class ReflectConstructor<T> extends ReflectMember<T, ConstructorIde
|
||||
* @throws InvocationTargetException if the called constructor throws an exception.
|
||||
* @see Constructor#newInstance(Object...)
|
||||
*/
|
||||
public T instanciate(Object... values) throws InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||
public T instantiate(Object... values) throws InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||
return get().newInstance(values);
|
||||
}
|
||||
|
||||
|
@@ -9,7 +9,7 @@ import java.lang.reflect.Modifier;
|
||||
*/
|
||||
public final class ReflectField<T> extends ReflectMember<T, String, Field, NoSuchFieldException> {
|
||||
|
||||
/* Those fields are used to modify the value of a static variable. Depending of the current Java version,
|
||||
/* Those fields are used to modify the value of a static variable. Depending on the current Java version,
|
||||
* one of them will be used for this purpose.
|
||||
*/
|
||||
private static sun.misc.Unsafe sunMiscUnsafeInstance;
|
||||
@@ -130,7 +130,7 @@ public final class ReflectField<T> extends ReflectMember<T, String, Field, NoSuc
|
||||
sunMiscUnsafeInstance.putObject(instance, offset, value);
|
||||
}
|
||||
} else { // Java < 16
|
||||
// change the modifier in the Field instance so the method #set(instance, value) doesn’t throw an exception
|
||||
// change the modifier in the Field instance so the method #set(instance, value) doesn't throw an exception
|
||||
try {
|
||||
modifiersFieldInFieldClass.set(f, realModifiers & ~Modifier.FINAL);
|
||||
f.set(instance, value);
|
||||
|
@@ -1,30 +1,18 @@
|
||||
package fr.pandacube.lib.reflect.wrapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import fr.pandacube.lib.util.MappedListView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A wrapper for a list of wrapped object. It is an extension of {@link MappedListView} that is used to transparently
|
||||
* wrap/unwrap the elements of the backend list.
|
||||
* @param <W> the type of the reflect wrapper for the elements of this list.
|
||||
* @param <W> the type of the reflection wrapper for the elements of this list.
|
||||
*/
|
||||
public class ReflectListWrapper<W extends ReflectWrapperI> extends MappedListView<Object, W> implements ReflectWrapperTypedI<List<Object>> {
|
||||
|
||||
private final Class<W> expectedWrapperClass;
|
||||
|
||||
/* package */ ReflectListWrapper(Class<W> expectedWrapperClass) {
|
||||
this(ArrayList::new, expectedWrapperClass);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
/* package */ ReflectListWrapper(Supplier<List<?>> listCreator, Class<W> expectedWrapperClass) {
|
||||
this((List<Object>) (listCreator == null ? new ArrayList<>() : listCreator.get()), expectedWrapperClass);
|
||||
}
|
||||
|
||||
/* package */ ReflectListWrapper(List<Object> wrappedList, Class<W> expectedWrapperClass) {
|
||||
super(wrappedList, el -> ReflectWrapper.wrap(el, expectedWrapperClass), ReflectWrapper::unwrap);
|
||||
this.expectedWrapperClass = expectedWrapperClass;
|
||||
@@ -45,4 +33,17 @@ public class ReflectListWrapper<W extends ReflectWrapperI> extends MappedListVie
|
||||
public List<W> subList(int fromIndex, int toIndex) {
|
||||
return new ReflectListWrapper<>(backend.subList(fromIndex, toIndex), expectedWrapperClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof List l && backend.equals(l instanceof ReflectListWrapper<?> rw ? rw.backend : l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return backend.hashCode();
|
||||
}
|
||||
}
|
||||
|
@@ -11,7 +11,7 @@ import java.util.Objects;
|
||||
import static fr.pandacube.lib.util.ThrowableUtil.wrapEx;
|
||||
|
||||
/**
|
||||
* Superclass of all reflect wrapper objects.
|
||||
* Superclass of all reflection wrapper objects.
|
||||
*/
|
||||
public abstract class ReflectWrapper implements ReflectWrapperI {
|
||||
|
||||
@@ -20,7 +20,7 @@ public abstract class ReflectWrapper implements ReflectWrapperI {
|
||||
|
||||
/**
|
||||
* Unwraps the object from the provided reflect wrapper.
|
||||
* @param wr the reflect wrapper from which to get the object.
|
||||
* @param wr the reflection wrapper from which to get the object.
|
||||
* @return the object from the provided reflect wrapper.
|
||||
*/
|
||||
public static Object unwrap(ReflectWrapperI wr) {
|
||||
@@ -29,7 +29,7 @@ public abstract class ReflectWrapper implements ReflectWrapperI {
|
||||
|
||||
/**
|
||||
* Unwraps the object from the provided reflect wrapper.
|
||||
* @param wr the reflect wrapper from which to get the object.
|
||||
* @param wr the reflection wrapper from which to get the object.
|
||||
* @param <T> the type of the wrapped object.
|
||||
* @return the object from the provided reflect wrapper.
|
||||
*/
|
||||
@@ -38,11 +38,11 @@ public abstract class ReflectWrapper implements ReflectWrapperI {
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the provided runtime object into a reflect wrapper.
|
||||
* If a wrapper instance is already known, it will return it instead of instanciating a new one.
|
||||
* Wraps the provided runtime object into a reflection wrapper.
|
||||
* If a wrapper instance is already known, it will return it instead of instantiating a new one.
|
||||
* It is better to call {@link #wrap(Object, Class)} if you know the type of wrapper needed.
|
||||
* @param runtimeObj the object to wrap.
|
||||
* @return the reflect wrapper wrapping the provided object.
|
||||
* @return the reflection wrapper wrapping the provided object.
|
||||
* @throws ClassCastException if the runtime class of the object is not handled by the expected wrapper class or its
|
||||
* subclasses.
|
||||
* @throws IllegalArgumentException if the runtime class of the object is not handled by any of the registered
|
||||
@@ -53,14 +53,14 @@ public abstract class ReflectWrapper implements ReflectWrapperI {
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the provided runtime object (with has a known type) into a reflect wrapper.
|
||||
* If a wrapper instance is already known, it will return it instead of instanciating a new one.
|
||||
* Wraps the provided runtime object (with has a known type) into a reflection wrapper.
|
||||
* If a wrapper instance is already known, it will return it instead of instantiating a new one.
|
||||
* It is better to call {@link #wrap(Object, Class)} if you know the type of wrapper needed.
|
||||
* @param runtimeObj the object to wrap.
|
||||
* @param expectedWrapperClass the reflect wrapper class expected to be returned.
|
||||
* @param <W> the type of the reflect wrapper.
|
||||
* @param expectedWrapperClass the reflection wrapper class expected to be returned.
|
||||
* @param <W> the type of the reflection wrapper.
|
||||
* @param <T> the type of the wrapped object.
|
||||
* @return the reflect wrapper wrapping the provided object.
|
||||
* @return the reflection wrapper wrapping the provided object.
|
||||
* @throws ClassCastException if the runtime class of the object is not handled by the expected wrapper class or its
|
||||
* subclasses.
|
||||
* @throws IllegalArgumentException if the runtime class of the object is not handled by any of the registered
|
||||
@@ -71,13 +71,13 @@ public abstract class ReflectWrapper implements ReflectWrapperI {
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the provided runtime object into a reflect wrapper.
|
||||
* If a wrapper instance is already known, it will return it instead of instanciating a new one.
|
||||
* It is better to call {@link #wrap(Object, Class)} if you know the type of wrapper needed.
|
||||
* Wraps the provided runtime object into a reflection wrapper.
|
||||
* If a wrapper instance is already known, it will return it instead of instantiating a new one.
|
||||
* It is better to call {@link #wrap(Object)} if you don't know the type of wrapper needed.
|
||||
* @param runtimeObj the object to wrap.
|
||||
* @param expectedWrapperClass the reflect wrapper class expected to be returned.
|
||||
* @param <W> the type of the reflect wrapper.
|
||||
* @return the reflect wrapper wrapping the provided object.
|
||||
* @param expectedWrapperClass the reflection wrapper class expected to be returned.
|
||||
* @param <W> the type of the reflection wrapper.
|
||||
* @return the reflection wrapper wrapping the provided object.
|
||||
* @throws ClassCastException if the runtime class of the object is not handled by the expected wrapper class or its
|
||||
* subclasses.
|
||||
* @throws IllegalArgumentException if the runtime class of the object is not handled by any of the registered
|
||||
@@ -117,9 +117,9 @@ public abstract class ReflectWrapper implements ReflectWrapperI {
|
||||
}
|
||||
ReflectConstructor<? extends ReflectWrapperI> constructor = WrapperRegistry.getWrapperConstructorOfWrapperClass(wrapperClass);
|
||||
if (constructor == null) {
|
||||
throw new IllegalStateException("Unable to find a constructor to instanciate " + wrapperClass + " to wrap an instance of " + runtimeObj);
|
||||
throw new IllegalStateException("Unable to find a constructor to instantiate " + wrapperClass + " to wrap an instance of " + runtimeObj);
|
||||
}
|
||||
ReflectWrapperI wrapper = wrapEx(() -> constructor.instanciate(runtimeObj));
|
||||
ReflectWrapperI wrapper = wrapEx(() -> constructor.instantiate(runtimeObj));
|
||||
// added to cache by constructor
|
||||
@SuppressWarnings("unchecked")
|
||||
W wr = (W) wrapper;
|
||||
@@ -128,11 +128,11 @@ public abstract class ReflectWrapper implements ReflectWrapperI {
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the provided runtime list into a reflect list wrapper.
|
||||
* Wraps the provided runtime list into a reflection list wrapper.
|
||||
* @param runtimeList the list of runtime object to wrap.
|
||||
* @param expectedWrapperClass the wrapper class of the objects in this list.
|
||||
* @param <W> the type of reflect wrapper for the objects in this list.
|
||||
* @return a reflect list wrapper wrapping the provided list.
|
||||
* @return a reflection list wrapper wrapping the provided list.
|
||||
*/
|
||||
public static <W extends ReflectWrapperI> ReflectListWrapper<W> wrapList(List<Object> runtimeList, Class<W> expectedWrapperClass) {
|
||||
return new ReflectListWrapper<>(runtimeList, expectedWrapperClass);
|
||||
@@ -152,7 +152,7 @@ public abstract class ReflectWrapper implements ReflectWrapperI {
|
||||
private final Object reflectObject;
|
||||
|
||||
/**
|
||||
* Instanciate this Reflect Wrapper with the provided object.
|
||||
* Instantiate this reflection wrapper with the provided object.
|
||||
* Any subclasses should not make their constructor public since the instanciation is managed by {@link #wrap(Object, Class) wrap(...)}.
|
||||
* @param obj the object to wrap. It must be an instance of the {@link #__getRuntimeClass() runtime class} of this
|
||||
* wrapper class.
|
||||
|
@@ -7,7 +7,7 @@ package fr.pandacube.lib.reflect.wrapper;
|
||||
public abstract class ReflectWrapperTyped<T> extends ReflectWrapper implements ReflectWrapperTypedI<T> {
|
||||
|
||||
/**
|
||||
* Instanciate this Reflect Wrapper with the provided object.
|
||||
* Instantiate this reflection Wrapper with the provided object.
|
||||
* Any subclasses should not make their constructor public since the instanciation is managed by {@link #wrap(Object, Class) wrap(...)}.
|
||||
* @param obj the object to wrap. It must be an instance of the {@link #__getRuntimeClass() runtime class} of this
|
||||
* wrapper class.
|
||||
|
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Set of class allowing applications to implement almost transparent reflection classes.
|
||||
* The goal it to implement the class, methods and fields that the application have only access through reflection, and
|
||||
* reflection call when these implementation are called.
|
||||
* reflection call when these implementations are called.
|
||||
* Each of those reflection classes must extend {@link fr.pandacube.lib.reflect.wrapper.ReflectWrapper} (or, if it’s
|
||||
* an interface, must extend {@link fr.pandacube.lib.reflect.wrapper.ReflectWrapperI}). The implemented class wraps
|
||||
* the reflected object and redirects the method calls to them using reflection.
|
||||
|
Reference in New Issue
Block a user