-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ignore annotation / shuffle things around -- WIP
- Loading branch information
Showing
20 changed files
with
235 additions
and
562 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ch.jalu.configme.beanmapper; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface Ignore { | ||
} |
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
63 changes: 63 additions & 0 deletions
63
src/main/java/ch/jalu/configme/beanmapper/instantiation/BeanZeroArgConstrInstantiation.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,63 @@ | ||
package ch.jalu.configme.beanmapper.instantiation; | ||
|
||
import ch.jalu.configme.beanmapper.propertydescription.BeanPropertyDescription; | ||
import ch.jalu.configme.beanmapper.propertydescription.FieldProperty; | ||
import ch.jalu.configme.exception.ConfigMeException; | ||
import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
final class BeanZeroArgConstrInstantiation implements BeanInstantiation { | ||
|
||
private final Constructor<?> zeroArgsConstructor; | ||
private final List<FieldProperty> properties; | ||
|
||
public BeanZeroArgConstrInstantiation(@NotNull Constructor<?> zeroArgsConstructor, | ||
@NotNull List<FieldProperty> properties) { | ||
this.zeroArgsConstructor = zeroArgsConstructor; | ||
this.properties = properties; | ||
} | ||
|
||
@Override | ||
public @NotNull List<BeanPropertyDescription> getProperties() { | ||
return Collections.unmodifiableList(properties); | ||
} | ||
|
||
@Override | ||
public @Nullable Object create(@NotNull List<Object> propertyValues, | ||
@NotNull ConvertErrorRecorder errorRecorder) { | ||
final Object bean; | ||
try { | ||
bean = zeroArgsConstructor.newInstance(); | ||
} catch (ReflectiveOperationException e) { | ||
throw new ConfigMeException("Failed to call constructor for " | ||
+ zeroArgsConstructor.getDeclaringClass()); | ||
} | ||
|
||
if (propertyValues.size() != properties.size()) { | ||
throw new ConfigMeException("Invalid property values, " + propertyValues.size() + " were given, but " | ||
+ zeroArgsConstructor.getDeclaringClass() + " has " + properties.size() + " properties"); | ||
} | ||
|
||
Iterator<FieldProperty> propIt = properties.iterator(); | ||
Iterator<Object> valuesIt = propertyValues.iterator(); | ||
while (propIt.hasNext() && valuesIt.hasNext()) { | ||
FieldProperty property = propIt.next(); | ||
Object value = valuesIt.next(); | ||
if (value == null) { | ||
if (property.getValue(bean) == null) { | ||
return null; // No default value on field, return null -> no bean with a null value | ||
} | ||
errorRecorder.setHasError("Fallback to default value"); | ||
} else { | ||
property.setValue(bean, value); | ||
} | ||
} | ||
return bean; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/ch/jalu/configme/beanmapper/instantiation/RecordInstantiation.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,48 @@ | ||
package ch.jalu.configme.beanmapper.instantiation; | ||
|
||
import ch.jalu.configme.beanmapper.propertydescription.BeanPropertyDescription; | ||
import ch.jalu.configme.beanmapper.propertydescription.FieldProperty; | ||
import ch.jalu.configme.exception.ConfigMeException; | ||
import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
final class RecordInstantiation implements BeanInstantiation { | ||
|
||
private final Constructor<?> canonicalConstructor; | ||
private final List<FieldProperty> properties; | ||
|
||
public RecordInstantiation(@NotNull Class<?> clazz, @NotNull List<FieldProperty> properties) { | ||
this.properties = properties; | ||
Class<?>[] recordTypes = properties.stream().map(FieldProperty::getType).toArray(Class[]::new); | ||
this.canonicalConstructor = BeanInspector.getConstructor(clazz, recordTypes) | ||
.orElseThrow(() -> new ConfigMeException("Could not get canonical constructor of " + clazz)); | ||
} | ||
|
||
@Override | ||
public @NotNull List<BeanPropertyDescription> getProperties() { | ||
return Collections.unmodifiableList(properties); | ||
} | ||
|
||
@Override | ||
public @Nullable Object create(@NotNull List<Object> propertyValues, | ||
@NotNull ConvertErrorRecorder errorRecorder) { | ||
if (propertyValues.stream().anyMatch(Objects::isNull)) { | ||
return null; // No support for null values in records | ||
} | ||
|
||
Object[] properties = propertyValues.toArray(); | ||
try { | ||
return canonicalConstructor.newInstance(properties); | ||
} catch (IllegalArgumentException | ReflectiveOperationException e) { | ||
// TODO: Separate clause for InvocationTargetException? | ||
throw new ConfigMeException("Error calling record constructor for " | ||
+ canonicalConstructor.getDeclaringClass(), e); | ||
} | ||
} | ||
} |
Oops, something went wrong.