diff --git a/easy-random-bean-validation/pom.xml b/easy-random-bean-validation/pom.xml index 0589f093..b25477bb 100644 --- a/easy-random-bean-validation/pom.xml +++ b/easy-random-bean-validation/pom.xml @@ -4,7 +4,7 @@ io.github.dvgaba easy-random - 7.1.0 + 7.1.1-SNAPSHOT 4.0.0 easy-random-bean-validation diff --git a/easy-random-core/pom.xml b/easy-random-core/pom.xml index dc20d918..1f1ef0c5 100644 --- a/easy-random-core/pom.xml +++ b/easy-random-core/pom.xml @@ -4,7 +4,7 @@ io.github.dvgaba easy-random - 7.1.0 + 7.1.1-SNAPSHOT 4.0.0 easy-random-core diff --git a/easy-random-core/src/main/java/org/jeasy/random/ObjenesisObjectFactory.java b/easy-random-core/src/main/java/org/jeasy/random/ObjenesisObjectFactory.java index 1ae274f6..b26a79b6 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/ObjenesisObjectFactory.java +++ b/easy-random-core/src/main/java/org/jeasy/random/ObjenesisObjectFactory.java @@ -75,7 +75,11 @@ public T createInstance(Class type, RandomizerContext context) { private T createNewInstance(final Class type) { try { Constructor noArgConstructor = type.getDeclaredConstructor(); - noArgConstructor.trySetAccessible(); + try { + noArgConstructor.trySetAccessible(); + } catch(NoSuchMethodError e) { + noArgConstructor.setAccessible(true); + } return noArgConstructor.newInstance(); } catch (Exception exception) { return objenesis.newInstance(type); diff --git a/easy-random-core/src/main/java/org/jeasy/random/randomizers/registry/InternalRandomizerRegistry.java b/easy-random-core/src/main/java/org/jeasy/random/randomizers/registry/InternalRandomizerRegistry.java index 46a8d6cd..332c3a03 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/randomizers/registry/InternalRandomizerRegistry.java +++ b/easy-random-core/src/main/java/org/jeasy/random/randomizers/registry/InternalRandomizerRegistry.java @@ -23,14 +23,13 @@ */ package org.jeasy.random.randomizers.registry; -import static java.sql.Date.valueOf; - import java.lang.reflect.Field; import java.math.BigDecimal; import java.math.BigInteger; import java.net.URI; import java.net.URL; import java.nio.charset.Charset; +import java.time.LocalDate; import java.util.Calendar; import java.util.Date; import java.util.HashMap; @@ -132,4 +131,8 @@ public Randomizer getRandomizer(final Field field) { public Randomizer getRandomizer(Class type) { return randomizers.get(type); } + + private static Date valueOf(LocalDate date) { + return new Date(date.getYear() - 1900, date.getMonthValue() -1, date.getDayOfMonth()); + } } diff --git a/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java b/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java index 990b2da0..1b42926d 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java +++ b/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java @@ -34,6 +34,7 @@ import java.util.concurrent.*; import java.util.function.Supplier; import java.util.stream.Stream; + import org.jeasy.random.ObjectCreationException; import org.jeasy.random.annotation.RandomizerArgument; import org.jeasy.random.api.Randomizer; @@ -42,20 +43,22 @@ /** * Reflection utility methods. * - * This class is intended for internal use only. All public methods - * (except {@link ReflectionUtils#asRandomizer(java.util.function.Supplier)} - * might change between minor versions without notice. + * This class is intended for internal use only. All public methods + * (except {@link ReflectionUtils#asRandomizer(java.util.function.Supplier)} + * might change between minor versions without notice. * * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) */ public final class ReflectionUtils { - private ReflectionUtils() {} + private ReflectionUtils() { + } /** * Create a dynamic proxy that adapts the given {@link Supplier} to a {@link Randomizer}. + * * @param supplier to adapt - * @param target type + * @param target type * @return the proxy randomizer */ @SuppressWarnings("unchecked") @@ -80,9 +83,9 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg } return (Randomizer) Proxy.newProxyInstance( - Randomizer.class.getClassLoader(), - new Class[] { Randomizer.class }, - new RandomizerProxy(supplier) + Randomizer.class.getClassLoader(), + new Class[]{Randomizer.class}, + new RandomizerProxy(supplier) ); } @@ -124,7 +127,7 @@ public static List getInheritedFields(Class type) { * @throws IllegalAccessException if the property cannot be set */ public static void setProperty(final Object object, final Field field, final Object value) - throws IllegalAccessException, InvocationTargetException { + throws IllegalAccessException, InvocationTargetException { try { Optional setter = getWriteMethod(field); if (setter.isPresent()) { @@ -147,7 +150,7 @@ public static void setProperty(final Object object, final Field field, final Obj * @throws IllegalAccessException if the property cannot be set */ public static void setFieldValue(final Object object, final Field field, final Object value) - throws IllegalAccessException { + throws IllegalAccessException { boolean access = field.trySetAccessible(); field.set(object, value); field.setAccessible(access); @@ -162,10 +165,15 @@ public static void setFieldValue(final Object object, final Field field, final O * @throws IllegalAccessException if field cannot be accessed */ public static Object getFieldValue(final Object object, final Field field) throws IllegalAccessException { - boolean access = field.trySetAccessible(); - Object value = field.get(object); - field.setAccessible(access); - return value; + try { + boolean access = field.trySetAccessible(); + Object value = field.get(object); + field.setAccessible(access); + return value; + } catch (NoSuchMethodError e) { + field.setAccessible(true); + return field.get(object); + } } /** @@ -193,7 +201,7 @@ public static Class getWrapperType(Class primitiveType) { * @throws IllegalAccessException if field cannot be accessed */ public static boolean isPrimitiveFieldWithDefaultValue(final Object object, final Field field) - throws IllegalAccessException { + throws IllegalAccessException { Class fieldType = field.getType(); if (!fieldType.isPrimitive()) { return false; @@ -329,10 +337,10 @@ public static boolean isPopulatable(final Type type) { */ public static boolean isIntrospectable(final Class type) { return ( - !isEnumType(type) && - !isArrayType(type) && - !(isCollectionType(type) && isJdkBuiltIn(type)) && - !(isMapType(type) && isJdkBuiltIn(type)) + !isEnumType(type) && + !isArrayType(type) && + !(isCollectionType(type) && isJdkBuiltIn(type)) && + !(isMapType(type) && isJdkBuiltIn(type)) ); } @@ -421,11 +429,11 @@ public static List> filterSameParameterizedTypes(final List> t for (Class currentConcreteType : types) { List actualTypeArguments = getActualTypeArgumentsOfGenericInterfaces(currentConcreteType); typesWithSameParameterizedTypes.addAll( - actualTypeArguments - .stream() - .filter(currentTypeArguments -> Arrays.equals(fieldArugmentTypes, currentTypeArguments)) - .map(currentTypeArguments -> currentConcreteType) - .toList() + actualTypeArguments + .stream() + .filter(currentTypeArguments -> Arrays.equals(fieldArugmentTypes, currentTypeArguments)) + .map(currentTypeArguments -> currentConcreteType) + .toList() ); } return typesWithSameParameterizedTypes; @@ -436,29 +444,29 @@ public static List> filterSameParameterizedTypes(final List> t /** * Looks for given annotationType on given field or read method for field. * - * @param field field to check + * @param field field to check * @param annotationType Type of annotation you're looking for. - * @param the actual type of annotation + * @param the actual type of annotation * @return given annotation if field or read method has this annotation or null. */ public static T getAnnotation(Field field, Class annotationType) { return field.getAnnotation(annotationType) == null - ? getAnnotationFromReadMethod(getReadMethod(field).orElse(null), annotationType) - : field.getAnnotation(annotationType); + ? getAnnotationFromReadMethod(getReadMethod(field).orElse(null), annotationType) + : field.getAnnotation(annotationType); } /** * Checks if field or corresponding read method is annotated with given annotationType. * - * @param field Field to check + * @param field Field to check * @param annotationType Annotation you're looking for. * @return true if field or read method it annotated with given annotationType or false. */ public static boolean isAnnotationPresent(Field field, Class annotationType) { final Optional readMethod = getReadMethod(field); return ( - field.isAnnotationPresent(annotationType) || - (readMethod.isPresent() && readMethod.get().isAnnotationPresent(annotationType)) + field.isAnnotationPresent(annotationType) || + (readMethod.isPresent() && readMethod.get().isAnnotationPresent(annotationType)) ); } @@ -494,7 +502,8 @@ public static Collection getEmptyImplementationForCollectionInterface(final C /** * Create an empty collection for the given type. - * @param fieldType for which an empty collection should we created + * + * @param fieldType for which an empty collection should we created * @param initialSize initial size of the collection * @return empty collection */ @@ -504,7 +513,7 @@ public static Collection createEmptyCollectionForType(Class fieldType, int try { collection = (Collection) fieldType.getDeclaredConstructor().newInstance(); } catch ( - InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e + InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e ) { if (fieldType.equals(ArrayBlockingQueue.class)) { collection = new ArrayBlockingQueue<>(initialSize); @@ -517,6 +526,7 @@ public static Collection createEmptyCollectionForType(Class fieldType, int /** * Return an empty implementation for the given {@link Map} interface. + * * @param mapInterface for which an empty implementation should be returned * @return empty implementation for the given {@link Map} interface. */ @@ -557,6 +567,7 @@ public static Optional getWriteMethod(Field field) { /** * Get the read method for given field. + * * @param field field to get the read method for. * @return Optional of read method or empty if field has no read method */ @@ -605,27 +616,27 @@ public static Randomizer newInstance(final Class type, final Randomize try { if (notEmpty(randomizerArguments)) { Optional> matchingConstructor = Stream - .of(type.getConstructors()) - .filter(constructor -> - hasSameArgumentNumber(constructor, randomizerArguments) && - hasSameArgumentTypes(constructor, randomizerArguments) - ) - .findFirst(); + .of(type.getConstructors()) + .filter(constructor -> + hasSameArgumentNumber(constructor, randomizerArguments) && + hasSameArgumentTypes(constructor, randomizerArguments) + ) + .findFirst(); if (matchingConstructor.isPresent()) { return (Randomizer) matchingConstructor.get().newInstance(convertArguments(randomizerArguments)); } } return (Randomizer) type.getDeclaredConstructor().newInstance(); } catch ( - IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e + IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e ) { throw new ObjectCreationException( - format( - "Could not create Randomizer of type: %s with constructor arguments: %s", - type, - Arrays.toString(randomizerArguments) - ), - e + format( + "Could not create Randomizer of type: %s with constructor arguments: %s", + type, + Arrays.toString(randomizerArguments) + ), + e ); } } @@ -635,15 +646,15 @@ private static boolean notEmpty(final RandomizerArgument[] randomizerArguments) } private static boolean hasSameArgumentNumber( - final Constructor constructor, - final RandomizerArgument[] randomizerArguments + final Constructor constructor, + final RandomizerArgument[] randomizerArguments ) { return constructor.getParameterCount() == randomizerArguments.length; } private static boolean hasSameArgumentTypes( - final Constructor constructor, - final RandomizerArgument[] randomizerArguments + final Constructor constructor, + final RandomizerArgument[] randomizerArguments ) { Class[] constructorParameterTypes = constructor.getParameterTypes(); for (int i = 0; i < randomizerArguments.length; i++) { diff --git a/easy-random-protobuf/pom.xml b/easy-random-protobuf/pom.xml index 73f74167..d8e393ae 100644 --- a/easy-random-protobuf/pom.xml +++ b/easy-random-protobuf/pom.xml @@ -4,7 +4,7 @@ io.github.dvgaba easy-random - 7.1.0 + 7.1.1-SNAPSHOT 4.0.0 easy-random-protobuf diff --git a/easy-random-randomizers/pom.xml b/easy-random-randomizers/pom.xml index afc05fd6..8392928c 100644 --- a/easy-random-randomizers/pom.xml +++ b/easy-random-randomizers/pom.xml @@ -4,7 +4,7 @@ io.github.dvgaba easy-random - 7.1.0 + 7.1.1-SNAPSHOT 4.0.0 easy-random-randomizers diff --git a/pom.xml b/pom.xml index 6d42bf7c..22198bdb 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ 4.0.0 io.github.dvgaba easy-random - 7.1.0 + 7.1.1-SNAPSHOT pom