forked from j-easy/easy-random
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See [issue](j-easy#425) Pass the actual type along with a field that will be used to pick correct randomizer. Consider the following example: ``` abstract class Base<T> { T t; } class Concrete extends Base<String> {} ``` If we only rely on `Field.getType()`, property `Concrete.t` would be randomized as `Object` and then cause a runtime exception when used. Java reflection API provides a way to get the actual type, in this case `String`. This information is passed in `GenericField` so that the correct randomizer can be picked later.
- Loading branch information
Olli Kuonanoja
committed
Aug 17, 2020
1 parent
1598695
commit c376ec5
Showing
10 changed files
with
337 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
easy-random-core/src/main/java/org/jeasy/random/util/GenericField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2020, Mahmoud Ben Hassine ([email protected]) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.jeasy.random.util; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Support type variables for {@link Field} | ||
* | ||
* When using type variables, {@link Field#getType()} will return the lower bound class. | ||
* However, in case the generic field is part of a base class, the actual type is sometimes | ||
* known. This class wraps {@link Field} and contains the actual type as well. | ||
* | ||
*/ | ||
public class GenericField { | ||
private final Field field; | ||
private final Class<?> type; | ||
|
||
public GenericField(Field field, Class<?> type) { | ||
this.field = field; | ||
this.type = type; | ||
} | ||
|
||
public GenericField(Field field) { | ||
this(field, field.getType()); | ||
} | ||
|
||
/** | ||
* @return The wrapped {@link Field} | ||
*/ | ||
public Field getField() { | ||
return field; | ||
} | ||
|
||
/** | ||
* @return The type of the field, possibly a resolved type variable | ||
*/ | ||
public Class<?> getType() { | ||
return type; | ||
} | ||
|
||
public String getName() { | ||
return field.getName(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
GenericField that = (GenericField) o; | ||
return Objects.equals(field, that.field) && | ||
Objects.equals(type, that.type); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(field, type); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "GenericField{" + | ||
"field=" + field + | ||
", type=" + type + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.