Answer the question
In order to leave comments, you need to log in
How to get class field parameters without using reflection?
Hello everyone, there are the following requirements for the method architecture:
Implement the fillAllFields and getAllFields methods in all device classes in such a way that first the fields of the AbstractDevice class go in the array, then the child classes. When implementing, do not forget that you can call parent implementations of the same method. Implement the fillAllFields and getAllFields methods so that they do not need to be changed if the number or composition of fields in the parent or child classes changes, and even if the parent class is replaced with another one.
Everything would be fine, but it is undesirable to use reflection here, which would greatly simplify this matter. I read that you can somehow use Generics, but I can not figure out how to tie it here.
import java.util.List;
public interface FillableEntity {
void fillAllFields(List<Field> fields);
List<Field> getAllFields();
class Field {
Class type;
Object value;
public Field(Class type, Object value) {
this.type = type;
this.value = value;
}
public Class getType() {
return type;
}
public void setType(Class type) {
this.type = type;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
}
import java.util.Date;
public interface Device extends FillableEntity {
int getIn();
void setIn(int in);
String getType();
void setType(String type);
String getManufacturer();
void setManufacturer(String manufacturer);
String getModel();
void setModel(String model);
Date getProductionDate();
void setProductionDate(Date productionDate);
}
public abstract class AbstractDevice implements Device {
protected int in = 0;
protected String type;
protected String manufacturer;
protected String model;
protected Date productionDate;
private static final Logger LOG = Logger
.getLogger(AbstractDevice.class.getName());
/*
Геттеры и сеттеры дальше идут для них
*/
}
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Battery<T> extends AbstractDevice implements Device {
protected int chargeVolume;
public int getChargeVolume() {
return this.chargeVolume;
}
public void setChargeVolume(int chargeVolume) {
this.chargeVolume = chargeVolume;
}
@Override
public void fillAllFields(List<Field> fields) {
Field f[] = new Field[fields.size()];
for (int i = 0; i < fields.size(); i++) {
f[i] = fields.get(i);
}
super.setIn(Integer.parseInt(f[0].getValue().toString()));
super.setType(null);
super.setManufacturer(String.valueOf(f[2].getValue()));
super.setModel(String.valueOf(f[3].getValue()));
super.setProductionDate((Date) f[4].getValue());
this.chargeVolume = (int) f[5].getValue();
}
@Override
public List<Field> getAllFields() {
List<Field> listField = new ArrayList<>();
listField.add(new Field(Integer.class, super.getIn()));
listField.add(new Field(String.class, super.getType()));
listField.add(new Field(String.class, super.getManufacturer()));
listField.add(new Field(String.class, super.getModel()));
listField.add(new Field(Date.class, super.getProductionDate()));
listField.add(new Field(Integer.class, this.chargeVolume));
Battery<T> battery = new Battery<T>();
return listField;
}
}
Answer the question
In order to leave comments, you need to log in
I suspect that you can do it like this - you can call super.fillAllFields in the fillAllFields method. Then call super.getAllFields().getSize(), and fill in your fields starting from the position where the parent's fields ended.
The getAllFields method is even simpler, you need to call super.getAllFields() and add your fields to the end of this array.
This seems to work for any hierarchy.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question