Java CGLib';他真的回来了吗?

Java CGLib';他真的回来了吗?,java,proxy,tostring,cglib,Java,Proxy,Tostring,Cglib,我试图使用CGLib创建自己的延迟加载实现,但我遇到了一些无法解释的奇怪行为 这就是我正在做的。 正在创建代理实例,如下所示: public static <T> T newInstance(Long sourceId, SourceMapper<T> sourceMapper) { Class<?> proxyTargetType = sourceMapper.getType(); //mapper will use provided so

我试图使用CGLib创建自己的延迟加载实现,但我遇到了一些无法解释的奇怪行为

这就是我正在做的。
正在创建代理实例,如下所示:

public static <T> T newInstance(Long sourceId, SourceMapper<T> sourceMapper) {

    Class<?> proxyTargetType = sourceMapper.getType();
    //mapper will use provided sourceId in order to load real object from the DB
    Function<Long, T> mapper = sourceMapper.getMapper();

    return (T) Enhancer.create(proxyTargetType,
                               new DynamicProxy<>(sourceId, mapper));
}
仅当调用其任何方法时,才应加载重载对象:

public Object intercept(Object obj, Method method, Object[] args,
                        MethodProxy methodProxy) throws Throwable {
    T source = this.getSource(); // loads real object using sourceId and mapper
    if(source == null) return null;
    return method.invoke(source, args);
}
如果
this.getSource()
加载某个对象,它将非常有效。

但是如果我们假设,
order.getCustomerTariff()
应该返回
null
this.getSource()
将返回
null

我假设由于某种原因,
toString()
在第(2)行被调用,因此我得到的是
String
null
而不是literal
null
。这就是为什么它不等于比较子句中的文本
null

您认为,有什么方法可以在第(2)行返回一个常规
null
,并在检查过程中收到正确的
false

编辑
被代理的类与此类似:

public class CustomerTariff extends DomainEntity {

    private Customer customer;
    //some other similar fields
    private Tariff tariff;

    public CustomerTariff() {
    }

    public CustomerTariff(Customer customer
                          Tariff tariff) {
        this.customer = customer;
        this.tariff = tariff;
    }

    public CustomerTariff(Long id, Customer customer,
                          Tariff tariff) {
        super(id);
        this.customer = customer;
        this.tariff = tariff;
    } 
    //getters and setters

    @Override
    public String toString() {
    return "CustomerTariff{" +
            "customer=" + customer +
            ", tariff=" + tariff +
            "} " + super.toString();
    }
}

public abstract class DomainEntity {

    private Long id;

    public DomainEntity() {}

    public DomainEntity(Long id) {
       this.id = id;
    }

    @Override
    public String toString() {
       return "DomainEntity{" +
               "id=" + id +
                '}';
       }
    }

我假设您正在从您的拦截器截取
toString
方法,并且您没有得到预期的截取链。指定一个只命中要截取的方法的方法筛选器,您应该会得到预期的结果。

您所做的似乎是正确的。您是否检查了返回值的类型?您是否在拦截器中设置了断点以检查返回的值是否正确?@RafaelWinterhalter从调试会话中我了解到,
Enhancer.create(…)
生成以下值
obj={CustomerTariff$$EnhancerByCGLIB$$d66ba677}“null”
,将其设置为
CustomerTariff
字段。此值与第(2)行的
null
进行比较。我不明白引用的null是什么意思,但问题是
“null”!=null
产生
true
我假设有一个构造函数定义了一个值,该值成为
toString
表示的一部分。我假设您调用构造函数,但不初始化正在读取的某个字段。你能提供被代理的类吗?@RafaelWinterhalter是的,我能提供类。我已编辑了我的问题
LOG.debug("{}", order.getCustomerTariff());          //null    (1)
LOG.debug("{}", order.getCustomerTariff() != null);  //true    (2)
public class CustomerTariff extends DomainEntity {

    private Customer customer;
    //some other similar fields
    private Tariff tariff;

    public CustomerTariff() {
    }

    public CustomerTariff(Customer customer
                          Tariff tariff) {
        this.customer = customer;
        this.tariff = tariff;
    }

    public CustomerTariff(Long id, Customer customer,
                          Tariff tariff) {
        super(id);
        this.customer = customer;
        this.tariff = tariff;
    } 
    //getters and setters

    @Override
    public String toString() {
    return "CustomerTariff{" +
            "customer=" + customer +
            ", tariff=" + tariff +
            "} " + super.toString();
    }
}

public abstract class DomainEntity {

    private Long id;

    public DomainEntity() {}

    public DomainEntity(Long id) {
       this.id = id;
    }

    @Override
    public String toString() {
       return "DomainEntity{" +
               "id=" + id +
                '}';
       }
    }