Java 如何进行空检查并以通用方式返回值
我有一长串的对象映射要从JAXB自动生成的类中进行Java 如何进行空检查并以通用方式返回值,java,Java,我有一长串的对象映射要从JAXB自动生成的类中进行 customer.setCustomerId(rentalCustomer.getCustomerid().getValue())); customer.setCustomerName(rentalCustomer.getTradingname().getValue()); customer.setVatNumber(rentalSearchCustomer.getVatNumber().getValue()); .... ....
customer.setCustomerId(rentalCustomer.getCustomerid().getValue()));
customer.setCustomerName(rentalCustomer.getTradingname().getValue());
customer.setVatNumber(rentalSearchCustomer.getVatNumber().getValue());
....
....
基本上,我需要对所有字段进行空检查:
getValue(RentalCustomerIDType idType){
if(idType != null){
return idType.getValue();
}
else {
return "";
}
}
问题是这些类型太多了,它们都有不同的类型:RentalCustomerIDType、TradingType、VatNumberType……等等
通过创建一个泛型方法,对所有进行空检查并返回正确的值,是否有一种优雅的方法可以做到这一点?也许可以使用Java函数库?据我所知,您不希望更改现有的自动生成类。您可以做的是创建一个
customerRapper
类,当null
设置为字段时,该类包装Customer
,并插入默认值。这就是代码中的想法:
公共类CustomerRapper(){
私人最终客户;
公共客户说话人(客户){
this.customer=customer;
}
public void setCustomerId(字符串id){
this.customer.setCustomerId(id==null?“:id);
}
//在此处插入其他方法。
}
这就是你想要的。但有一个警告。每种类型都必须实现提供getValue()
方法的相同接口。否则,您可能需要反射才能获得您所怀疑的方法。但这是一个为子孙后代提供的解决方案
setType(rentalSearchCustomer::getCustomerid,
customer::setCustomerId);
setType(rentalSearchCustomer::getTradingname,
customer::setCustomerName);
setType(rentalSearchCustomer::getVatNumber,
customer::setVatNumber);
System.out.println(customer);
public static <T extends GetValue> void setType(Supplier<T> sup,
Consumer<String> con) {
if (sup.get() == null) {
con.accept("");
} else {
con.accept(sup.get().getValue());
}
}
interface GetValue {
public String getValue();
}
setType(rentalSearchCustomer::getCustomerid,
customer::setCustomerId);
setType(rentalSearchCustomer::getTradingname,
客户::设置客户名称);
setType(rentalSearchCustomer::getVatNumber,
客户::setVatNumber);
系统输出打印项次(客户);
公共静态无效设置类型(供应商支持,
消费者(con){
if(sup.get()==null){
同意接受(“”);
}否则{
con.accept(sup.get().getValue());
}
}
接口GetValue{
公共字符串getValue();
}
是否可以在生成类时对其使用反射,并通过向字段分配非空值来消除所有空值
他们说(回答的那个人)他们强烈反对为此目的使用反射。。。但是无聊的。我已经做了,而且很有效。我想你应该用类似的东西。在这里,我将ResponseUserDto作为我的Pojo类,对其属性进行空检查
private ResponseUserDto getValidNotNullPropertyObject(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
Map < String, Object > result = new HashMap<>();
for (PropertyDescriptor property: src.getPropertyDescriptors()) {
if (src.getPropertyValue(property.getName()) == null) {
/* if(property.getPropertyType() == ?) {
//maybe do somethig here
}*/
result.put(property.getName(), ""); // this is start
} else {
result.put(property.getName(), src.getPropertyValue(property.getName()));
}
}
final ObjectMapper mapper = new ObjectMapper(); // Jackson's ObjectMapper
final ResponseUserDto finalResult = mapper.convertValue(result, ResponseUserDto.class);
return finalResult;
}
您可以使用泛型方法声明
getvaluefromalobjects
方法,然后使用反射调用getValue
方法
public static <T> String getValueFromAllObjects(T t) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if(t != null){
return (String) t.getClass().getDeclaredMethod("getValue").invoke(t);
}
else {
return "";
}
}
公共静态字符串GetValueFromAllObject(T T)引发IllegalAccessException、InvocationTargetException、NoSuchMethodException{
如果(t!=null){
return(String)t.getClass().getDeclaredMethod(“getValue”).invoke(t);
}
否则{
返回“”;
}
}
有关反射备选方案,请参阅。我刚刚在上面的代码中内联了一个示例,如果它的使用是一个选项,那么它可能是面向方面编程的一个例子:
- 在AspectJ中使用注释(请参见有关注释的
)
- 或者(我没有检查这个)Spring AOP(已关闭,但有一个答案)
idType的。getValue()
始终是字符串吗?或者它也可能是其他类型的吗?好的,那么我们只需要找到一些关于所有这些方法的共同点。这些类型都是扩展某个类还是实现某个接口?所有的方法都命名为getValue()
?否则我们只需要写一个长长的switch案例陈述。@Spring我建议不要对那些试图帮助你的人大喊大叫,也不要为了帮助你而提出澄清问题。好吧,有点困惑。所有getValue()方法是否都返回字符串?如果没有,他们会返回什么。还有什么是包含getValue()方法的类的示例。@Spring我才明白问题是什么。对不起,我的坚持。但这是一个有趣的问题,所以我将继续研究它。但我有一个简单的问题?您是否为每个具有getValue()方法的类型编写了类?或者每种类型都实现了一些指定getValue
方法的接口吗?OP说,请注意getValue()是特定于类型的。因此getValue()返回不同的类型。每种类型都需要这些方法中的一种。您不能将字符串分配给所有类型。我假设它是字符串,正如他在注释[here]()中为idType
澄清的那样。还有其他类型。@WJS getValue()函数中返回空字符串“”意味着String@Spring不用担心,如果它有助于论证“支持”反射的理由,那么JAXB会大量使用它,DI框架(如Spring)也是如此
public static <T> String getValueFromAllObjects(T t) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if(t != null){
return (String) t.getClass().getDeclaredMethod("getValue").invoke(t);
}
else {
return "";
}
}