Skip to content

Commit 370d2f3

Browse files
committed
Remove usage of deprecated APIs
1 parent 2063f5f commit 370d2f3

File tree

8 files changed

+23
-25
lines changed

8 files changed

+23
-25
lines changed

easy-random-bean-validation/src/main/java/org/jeasy/random/validation/SizeAnnotationHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.validation.constraints.Size;
3535
import java.lang.reflect.Array;
3636
import java.lang.reflect.Field;
37+
import java.lang.reflect.InvocationTargetException;
3738
import java.lang.reflect.ParameterizedType;
3839
import java.lang.reflect.Type;
3940
import java.util.Collection;
@@ -119,8 +120,8 @@ public Randomizer<?> getRandomizer(Field field) {
119120
map = (Map<Object, Object>) getEmptyImplementationForMapInterface(fieldType);
120121
} else {
121122
try {
122-
map = (Map<Object, Object>) fieldType.newInstance();
123-
} catch (InstantiationException | IllegalAccessException e) {
123+
map = (Map<Object, Object>) fieldType.getDeclaredConstructor().newInstance();
124+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
124125
if (fieldType.isAssignableFrom(EnumMap.class)) {
125126
if (isParameterizedType(fieldGenericType)) {
126127
Type type = ((ParameterizedType) fieldGenericType).getActualTypeArguments()[0];

easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ void generatedValuesShouldBeValidAccordingToValidationConstraints() {
7777

7878
assertThat(bean.getBirthdayLocalDateTime()).isBefore(LocalDateTime.now());// @Past LocalDateTime birthdayLocalDateTime;
7979

80-
assertThat(bean.getPastOrPresent()).isBeforeOrEqualsTo(new Date());// @PastOrPresent Date pastOrPresent;
80+
assertThat(bean.getPastOrPresent()).isBeforeOrEqualTo(new Date());// @PastOrPresent Date pastOrPresent;
8181

8282
assertThat(bean.getEventDate()).isInTheFuture();// @Future Date eventDate;
8383

8484
assertThat(bean.getEventLocalDateTime()).isAfter(LocalDateTime.now());// @Future LocalDateTime eventLocalDateTime;
8585

86-
assertThat(bean.getFutureOrPresent()).isAfterOrEqualsTo(new Date());// @FutureOrPresent Date eventDate;
86+
assertThat(bean.getFutureOrPresent()).isAfterOrEqualTo(new Date());// @FutureOrPresent Date eventDate;
8787

8888
assertThat(bean.getPositive()).isGreaterThan(0);// @Positive int positive;
8989

@@ -147,13 +147,13 @@ void generatedValuesShouldBeValidAccordingToValidationConstraintsOnMethod() {
147147

148148
assertThat(bean.getBirthdayLocalDateTime()).isBefore(LocalDateTime.now());// @Past LocalDateTime birthdayLocalDateTime;
149149

150-
assertThat(bean.getPastOrPresent()).isBeforeOrEqualsTo(new Date());// @PastOrPresent Date pastOrPresent;
150+
assertThat(bean.getPastOrPresent()).isBeforeOrEqualTo(new Date());// @PastOrPresent Date pastOrPresent;
151151

152152
assertThat(bean.getEventDate()).isInTheFuture();// @Future Date eventDate;
153153

154154
assertThat(bean.getEventLocalDateTime()).isAfter(LocalDateTime.now());// @Future LocalDateTime eventLocalDateTime;
155155

156-
assertThat(bean.getFutureOrPresent()).isAfterOrEqualsTo(new Date());// @FutureOrPresent Date eventDate;
156+
assertThat(bean.getFutureOrPresent()).isAfterOrEqualTo(new Date());// @FutureOrPresent Date eventDate;
157157

158158
assertThat(bean.getPositive()).isGreaterThan(0);// @Positive int positive;
159159

@@ -292,7 +292,7 @@ class Discount {
292292
}
293293

294294
CustomRandomizerRegistry registry = new CustomRandomizerRegistry();
295-
registry.registerRandomizer(BigDecimal.class, new BigDecimalRangeRandomizer(new Double(5d), new Double(10d), Integer.valueOf(3)));
295+
registry.registerRandomizer(BigDecimal.class, new BigDecimalRangeRandomizer(Double.valueOf(5d), Double.valueOf(10d), Integer.valueOf(3)));
296296
registry.registerRandomizer(Integer.class, new IntegerRangeRandomizer(5, 10));
297297
EasyRandomParameters parameters = new EasyRandomParameters()
298298
.randomizerRegistry(registry);

easy-random-core/src/main/java/org/jeasy/random/MapPopulator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.jeasy.random.randomizers.range.IntegerRangeRandomizer;
2828

2929
import java.lang.reflect.Field;
30+
import java.lang.reflect.InvocationTargetException;
3031
import java.lang.reflect.ParameterizedType;
3132
import java.lang.reflect.Type;
3233
import java.util.EnumMap;
@@ -61,8 +62,8 @@ class MapPopulator {
6162
map = (Map<Object, Object>) getEmptyImplementationForMapInterface(fieldType);
6263
} else {
6364
try {
64-
map = (Map<Object, Object>) fieldType.newInstance();
65-
} catch (InstantiationException | IllegalAccessException e) {
65+
map = (Map<Object, Object>) fieldType.getDeclaredConstructor().newInstance();
66+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
6667
// Creating EnumMap with objenesis by-passes the constructor with keyType which leads to CCE at insertion time
6768
if (fieldType.isAssignableFrom(EnumMap.class)) {
6869
if (isParameterizedType(fieldGenericType)) {

easy-random-core/src/main/java/org/jeasy/random/ObjenesisObjectFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ public <T> T createInstance(Class<T> type, RandomizerContext context) {
7272
private <T> T createNewInstance(final Class<T> type) {
7373
try {
7474
Constructor<T> noArgConstructor = type.getDeclaredConstructor();
75-
if (!noArgConstructor.isAccessible()) {
76-
noArgConstructor.setAccessible(true);
77-
}
75+
noArgConstructor.trySetAccessible();
7876
return noArgConstructor.newInstance();
7977
} catch (Exception exception) {
8078
return objenesis.newInstance(type);

easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ public static void setProperty(final Object object, final Field field, final Obj
149149
* @throws IllegalAccessException if the property cannot be set
150150
*/
151151
public static void setFieldValue(final Object object, final Field field, final Object value) throws IllegalAccessException {
152-
boolean access = field.isAccessible();
153-
field.setAccessible(true);
152+
boolean access = field.trySetAccessible();
154153
field.set(object, value);
155154
field.setAccessible(access);
156155
}
@@ -164,8 +163,7 @@ public static void setFieldValue(final Object object, final Field field, final O
164163
* @throws IllegalAccessException if field cannot be accessed
165164
*/
166165
public static Object getFieldValue(final Object object, final Field field) throws IllegalAccessException {
167-
boolean access = field.isAccessible();
168-
field.setAccessible(true);
166+
boolean access = field.trySetAccessible();
169167
Object value = field.get(object);
170168
field.setAccessible(access);
171169
return value;
@@ -492,8 +490,8 @@ public static Collection<?> createEmptyCollectionForType(Class<?> fieldType, int
492490
rejectUnsupportedTypes(fieldType);
493491
Collection<?> collection;
494492
try {
495-
collection = (Collection<?>) fieldType.newInstance();
496-
} catch (InstantiationException | IllegalAccessException e) {
493+
collection = (Collection<?>) fieldType.getDeclaredConstructor().newInstance();
494+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
497495
if (fieldType.equals(ArrayBlockingQueue.class)) {
498496
collection = new ArrayBlockingQueue<>(initialSize);
499497
} else {
@@ -600,8 +598,8 @@ public static <T> Randomizer<T> newInstance(final Class<T> type, final Randomize
600598
return (Randomizer<T>) matchingConstructor.get().newInstance(convertArguments(randomizerArguments));
601599
}
602600
}
603-
return (Randomizer<T>) type.newInstance();
604-
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
601+
return (Randomizer<T>) type.getDeclaredConstructor().newInstance();
602+
} catch (IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e) {
605603
throw new ObjectCreationException(format("Could not create Randomizer of type: %s with constructor arguments: %s", type, Arrays.toString(randomizerArguments)), e);
606604
}
607605
}

easy-random-core/src/test/java/org/jeasy/random/parameters/SeedParameterTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void generatedObjectShouldBeAlwaysTheSameForTheSameSeed() {
5454

5555
// Then
5656
assertThat(actualString).isEqualTo(expectedString);
57-
assertThat(actualPerson).isEqualToIgnoringNullFields(expectedPerson);
57+
assertThat(actualPerson).isEqualByComparingTo(expectedPerson);
5858
assertThat(actualInts).isEqualTo(expectedInts);
5959
}
6060

easy-random-core/src/test/java/org/jeasy/random/randomizers/range/DateRangeRandomizerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void whenSpecifiedMinDateIsNull_thenShouldUseDefaultMinValue() {
7979
Date randomDate = randomizer.getRandomValue();
8080

8181
// Then
82-
assertThat(randomDate).isBeforeOrEqualsTo(maxDate);
82+
assertThat(randomDate).isBeforeOrEqualTo(maxDate);
8383
}
8484

8585
@Test
@@ -91,7 +91,7 @@ void whenSpecifiedMaxDateIsNull_thenShouldUseDefaultMaxValue() {
9191
Date randomDate = randomizer.getRandomValue();
9292

9393
// Then
94-
assertThat(randomDate).isAfterOrEqualsTo(minDate);
94+
assertThat(randomDate).isAfterOrEqualTo(minDate);
9595
}
9696

9797
}

easy-random-core/src/test/java/org/jeasy/random/randomizers/range/SqlDateRangeRandomizerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void whenSpecifiedMinDateIsNull_thenShouldUseDefaultMinValue() {
7979
Date randomDate = randomizer.getRandomValue();
8080

8181
// Then
82-
assertThat(randomDate).isBeforeOrEqualsTo(maxDate);
82+
assertThat(randomDate).isBeforeOrEqualTo(maxDate);
8383
}
8484

8585
@Test
@@ -91,7 +91,7 @@ void whenSpecifiedMaxDateIsNull_thenShouldUseDefaultMaxValue() {
9191
Date randomDate = randomizer.getRandomValue();
9292

9393
// Then
94-
assertThat(randomDate).isAfterOrEqualsTo(minDate);
94+
assertThat(randomDate).isAfterOrEqualTo(minDate);
9595
}
9696

9797
}

0 commit comments

Comments
 (0)