- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如果该类具有每个字段的默认构造函数和 setter ,OpenCSV 会愉快地将记录转换为对象。但是,我希望为其生成对象的类是使用最终字段、私有(private)构造函数和生成器定义的。例如,如果我想创建一个 X
类型的对象,其中 X
定义为
public class X {
private final String val;
private X(final String val) { this.val = val; }
public Builder builder() { return new Builder(); }
public static class Builder {
private String val;
public Builder withVal(final String val) { this.val = val; return this; }
public X build() { return new X(val); }
}
}
我已经看到了 com.opencsv.bean.MappingStrategy
接口(interface),想知道是否可以以某种方式应用它,但我还没有找到解决方案。有什么想法吗?
最佳答案
com.opencsv.bean.MappingStrategy
看起来是处理这个问题的好方法,但由于您有最终属性和私有(private)构造函数,因此您需要从 java 反射系统获得一些帮助。
OpenCSV 的内置映射策略继承自 AbstractMappingStrategy
类,使用无参数默认构造函数构造 bean,然后查找 setter 方法来填充值。
可以准备一个 MappingStrategy
实现,该实现将找到一个合适的构造函数,其参数与 CSV 文件中映射列的顺序和类型相匹配,并使用它来构造 bean。即使构造函数是私有(private)的或 protected ,也可以通过反射系统访问它。
例如以下 CSV:
number;string
1;abcde
2;ghijk
可以映射到以下类:
public class Something {
@CsvBindByName
private final int number;
@CsvBindByName
private final String string;
public Something(int number, String string) {
this.number = number;
this.string = string;
}
// ... getters, equals, toString etc. omitted
}
使用以下 CvsToBean 实例:
CsvToBean<Something> beanLoader = new CsvToBeanBuilder<Something>(reader)
.withType(Something.class)
.withSeparator(';')
.withMappingStrategy(new ImmutableClassMappingStrategy<>(Something.class))
.build();
List<Something> result = beanLoader.parse();
<小时/>
ImmutableClassMappingStrategy
的完整代码:
import com.opencsv.bean.AbstractBeanField;
import com.opencsv.bean.BeanField;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import com.opencsv.exceptions.CsvConstraintViolationException;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
import java.beans.IntrospectionException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* A {@link com.opencsv.bean.MappingStrategy} implementation which allows to construct immutable beans containing
* final fields.
*
* It tries to find a constructor with order and types of arguments same as the CSV lines and construct the bean using
* this constructor. If not found it tries to use the default constructor.
*
* @param <T> Type of the bean to be returned
*/
public class ImmutableClassMappingStrategy<T> extends HeaderColumnNameMappingStrategy<T> {
/**
* Constructor
*
* @param type Type of the bean which will be returned
*/
public ColumnMappingStrategy(Class<T> type) {
setType(type);
}
@Override
public T populateNewBean(String[] line) throws InstantiationException, IllegalAccessException, IntrospectionException, InvocationTargetException, CsvRequiredFieldEmptyException, CsvDataTypeMismatchException, CsvConstraintViolationException {
verifyLineLength(line.length);
try {
// try constructing the bean using explicit constructor
return constructUsingConstructorWithArguments(line);
} catch (NoSuchMethodException e) {
// fallback to default constructor
return super.populateNewBean(line);
}
}
/**
* Tries constructing the bean using a constructor with parameters for all matching CSV columns
*
* @param line A line of input
*
* @return
* @throws NoSuchMethodException in case no suitable constructor is found
*/
private T constructUsingConstructorWithArguments(String[] line) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor<? extends T> constructor = findSuitableConstructor(line.length);
// in case of private or protected constructor, try to set it to be accessible
if (!constructor.canAccess(null)) {
constructor.setAccessible(true);
}
Object[] arguments = prepareArguments(line);
return constructor.newInstance(arguments);
}
/**
* Tries to find a suitable constructor with exact number and types of parameters in order defined in the CSV file
*
* @param columns Number of columns in the CSV file
* @return Constructor reflection
* @throws NoSuchMethodException in case no such constructor exists
*/
private Constructor<? extends T> findSuitableConstructor(int columns) throws NoSuchMethodException {
Class<?>[] types = new Class<?>[columns];
for (int col = 0; col < columns; col++) {
BeanField<T> field = findField(col);
Class<?> type = field.getField().getType();
types[col] = type;
}
return type.getDeclaredConstructor(types);
}
/**
* Prepare arguments with correct types to be used in the constructor
*
* @param line A line of input
* @return Array of correctly typed argument values
*/
private Object[] prepareArguments(String[] line) {
Object[] arguments = new Object[line.length];
for (int col = 0; col < line.length; col++) {
arguments[col] = prepareArgument(col, line[col], findField(col));
}
return arguments;
}
/**
* Prepare a single argument with correct type
*
* @param col Column index
* @param value Column value
* @param beanField Field with
* @return
*/
private Object prepareArgument(int col, String value, BeanField<T> beanField) {
Field field = beanField.getField();
// empty value for primitive type would be converted to null which would throw an NPE
if ("".equals(value) && field.getType().isPrimitive()) {
throw new IllegalArgumentException(String.format("Null value for primitive field '%s'", headerIndex.getByPosition(col)));
}
try {
// reflectively access the convert method, as it's protected in AbstractBeanField class
Method convert = AbstractBeanField.class.getDeclaredMethod("convert", String.class);
convert.setAccessible(true);
return convert.invoke(beanField, value);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException(String.format("Unable to convert bean field '%s'", headerIndex.getByPosition(col)), e);
}
}
}
<小时/>
另一种方法可能是将 CSV 列映射到构建器本身,然后构建不可变类。
关于java - 使用 OpenCSV,我们如何将记录映射到使用 Builder 而不是 setter 的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53667832/
因此,我需要获取在为其赋值时调用的 setter 的名称。像这样: var b = {}; var a = { set hey(value) { b[] = value; } } 我希望 se
我是 Android 编程的新手(~ 2 个月)有必要为几十个不同的变量设置 getter 吗? 例如—— //Yes I realise that this isn't 'dozens' publi
import Control.Lens import Control.Lens.TH data Foo = Foo { _bar, _baz :: Int } makeLenses ''
我有点困惑:我可以覆盖 setter/getter 但仍然使用 super setter/getter 吗?如果是 - 怎么办? 用例: class A { void set value(num
我有一个接收消息的应用程序。消息中存在可编辑的字段。当字段更改时,应将其保存到数据库中。不幸的是,setter 仅在 setter 的范围内更改给定字段的值。知道为什么会发生这种情况吗?这是 gett
C# 中有没有一种方法可以让 setter 从“某物”继承,这样每次为特定基类及其继承者调用 setter 时,我都可以运行一些代码? 我想做的是在我的基类上有一个名为 IsValid 的 bool
可能是一个我无法解决的非常简单的问题 - 我从 C# 开始,需要使用 getter/setter 方法向数组添加值,例如: public partial class Form1 : Form {
这两个属性实现有什么区别? public override string A { get { return "s"; } set { } } public override strin
是否可以使用 abc.abstractproperty 创建一个具体的 getter 但将 setter 抽象为每个继承类的不同。我为每个子类处理不同的 val 设置。 例如。 @abstractpr
我在某处看到类似下面的内容,想知道它是什么意思。我知道他们是getter和setter,但是想知道为什么字符串Type是这样定义的。谢谢你帮助我。 public string Type { get;
Public class Example { private int number; public Example(int number){ this.number =
假设我有这样的代码: public response MyMethod(Request req) { String id = req.getFirst().geId(); } 我已经模拟了主对
允许这样做: public int Age { get; set; } 但是应用程序是否为变量创建/分配空间?我经常这样做 private int age = 0; public int Age {
我有一个条件,我构造字符串 (finalValue) 的方式是基于我在输入中获得的非空值的数量。所以我想知道是否可以用一个不同的参数为字符串 (finalValue) 重载 setter 方法并根据我
例如,在这段代码中 var o = { set a(value) {this.b = value}, get a() {return this.b} } 是否有可能获得对 o.a 的 sett
我一直在努力了解 getter 和 setter,但没有深入了解。我读过 JavaScript Getters and Setters和 Defining Getters and Setters只是没
我想在我的类中添加一个 getter 和 setter。然而,setter 应该接收一个 querySelector,但 getter 返回一个新类型 pageSections。 我的问题是 gett
使用有什么好处: private var _someProp:String; public function set someProp(value:String):void { _somePr
当从域类调用它时,我想在我的 setter 中执行一些操作,而不是从 hibernate 中调用它时。此外,我正在使用 session 工厂,因此我无法使用 @PostLoad 来触发标志! 有人对此
人员类别: public class Person { private String firstName; private String lastName; public Pe
我是一名优秀的程序员,十分优秀!