Skip to content

Commit

Permalink
Code refactor (generics, SuppressWarnings...)
Browse files Browse the repository at this point in the history
Signed-off-by: Radek Felcman <[email protected]>
  • Loading branch information
rfelcman committed Jun 11, 2024
1 parent faebf1d commit 898a7a8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public class RecordInstantiationPolicyTest {

@Test
public void newRecordTest() {
Class clazz = NestedDetailRecord.class;
Class<NestedDetailRecord> clazz = NestedDetailRecord.class;
//Values order is important. It must be same as order of Record attributes.
List values = new ArrayList<>();
List<Object> values = new ArrayList<>();
values.add(FLOAT_ATTR);
values.add(INSTANT_ATTR);
RecordInstantiationPolicy recordInstantiationPolicy = new RecordInstantiationPolicy(clazz);
RecordInstantiationPolicy<NestedDetailRecord> recordInstantiationPolicy = new RecordInstantiationPolicy<>(clazz);
recordInstantiationPolicy.setValues(values);
NestedDetailRecord nestedDetailRecord = (NestedDetailRecord) recordInstantiationPolicy.buildNewInstance();
assertEquals(FLOAT_ATTR, nestedDetailRecord.floatAttribute(), 0);
Expand All @@ -45,12 +45,12 @@ public void newRecordTest() {

@Test
public void newRecordWithNullTest() {
Class clazz = NestedDetailRecord.class;
Class<NestedDetailRecord> clazz = NestedDetailRecord.class;
//Values order is important. It must be same as order of Record attributes.
List values = new ArrayList<>();
List<Object> values = new ArrayList<>();
values.add(FLOAT_ATTR);
values.add(null);
RecordInstantiationPolicy recordInstantiationPolicy = new RecordInstantiationPolicy(clazz);
RecordInstantiationPolicy<NestedDetailRecord> recordInstantiationPolicy = new RecordInstantiationPolicy<>(clazz);
recordInstantiationPolicy.setValues(values);
NestedDetailRecord nestedDetailRecord = (NestedDetailRecord) recordInstantiationPolicy.buildNewInstance();
assertEquals(FLOAT_ATTR, nestedDetailRecord.floatAttribute(), 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public boolean buildsNewInstance() {
private <R extends Record> R cloneRecord(R template) {
try {
ArrayList<Class<?>> types = new ArrayList<>();
ArrayList values = new ArrayList<>();
ArrayList<Object> values = new ArrayList<>();
for (RecordComponent component : template.getClass().getRecordComponents()) {
types.add(component.getType());
Object value = component.getAccessor().invoke(template);
Expand All @@ -56,6 +56,7 @@ private <R extends Record> R cloneRecord(R template) {
values.add(value);
}
Constructor<? extends Record> canonical = template.getClass().getDeclaredConstructor(types.toArray(Class[]::new));
@SuppressWarnings("unchecked")
var result = (R) canonical.newInstance(values.toArray(Object[]::new));
return result;
} catch (ReflectiveOperationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ protected CoreObjectBuilder() {
*/
public abstract Object buildNewInstance();

/**
* Return a new {@code java.lang.Record} instance.
* As this kind of class is immutable all values must be passed during creation as constructor parameters.
*/
public abstract Object buildNewRecordInstance(Class<Record> clazz, List<MAPPING> mappings, ABSTRACT_RECORD databaseRow, ABSTRACT_SESSION session);

/**
* Create a new row/record for the object builder.
* This allows subclasses to define different record types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,6 @@ public Object buildNewInstance() {
* Return a new {@code java.lang.Record} instance.
* As this kind of class is immutable all values must be passed during creation as constructor parameters.
*/
@Override
public Object buildNewRecordInstance(Class<Record> clazz, List<DatabaseMapping> mappings, AbstractRecord databaseRow, AbstractSession session) {
class TypeValue {
public TypeValue(Class<?> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
/**
* <b>Purpose</b>: Allows customization of how an {@code java.lang.Record} is created/instantiated.<p>
*/
public class RecordInstantiationPolicy extends InstantiationPolicy {
public class RecordInstantiationPolicy<T extends Record> extends InstantiationPolicy {

/**
* Record class
*/
private Class clazz;
private Class<T> clazz;

/**
* Values passed to the record constructor
*/
private List values;
private List<?> values;

/**
* Default constructor
Expand All @@ -45,9 +45,10 @@ public RecordInstantiationPolicy() {

/**
* Constructor
*
* @param clazz Record class
*/
public RecordInstantiationPolicy(Class clazz) {
public RecordInstantiationPolicy(Class<T> clazz) {
this();
this.clazz = clazz;
}
Expand All @@ -64,24 +65,26 @@ public Object buildNewInstance() throws DescriptorException {
} else {
return newRecord(clazz, values);
}
} catch (Throwable t) {
throw t;
}
finally {
} finally {
values = null;
}
}

private <R extends Record> R newRecord(Class<R> clazz, List values) {
@Override
public void useFactoryInstantiationPolicy(String factoryClassName, String methodName) {
throw new UnsupportedOperationException();
}

private T newRecord(Class<T> clazz, List<?> values) {
List<Class<?>> types = new ArrayList<>();
RecordComponent[] recordComponents = clazz.getRecordComponents();
for (int i = 0; i < recordComponents.length; i++) {
RecordComponent component = recordComponents[i];
types.add(component.getType());
for (RecordComponent recordComponent: recordComponents) {
types.add(recordComponent.getType());
}
try {
Constructor<? extends Record> canonical = clazz.getDeclaredConstructor(types.toArray(Class[]::new));
var result = (R) canonical.newInstance(values.toArray(Object[]::new));
@SuppressWarnings("unchecked")
var result = (T) canonical.newInstance(values.toArray(Object[]::new));
return result;
} catch (ReflectiveOperationException e) {
throw new RuntimeException("New Record creation failed: " + e, e);
Expand All @@ -90,9 +93,10 @@ private <R extends Record> R newRecord(Class<R> clazz, List values) {

/**
* Values passed to the record constructor
* @param values
*
* @param values values which are passed to the new record
*/
public void setValues(List values) {
public void setValues(List<?> values) {
this.values = values;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ public Object buildNewInstance() {
return this.descriptor.getInstantiationPolicy().buildNewInstance();
}

@Override
public Object buildNewRecordInstance(Class<Record> clazz, List<CoreMapping> coreMappings, CoreAbstractRecord databaseRow, CoreAbstractSession session) {
return null;
}

@Override
public XMLRecord buildRow(XMLRecord record, Object object,
CoreAbstractSession session, Marshaller marshaller,
Expand Down

0 comments on commit 898a7a8

Please sign in to comment.