Java 惰性加载程序,cglib的替代品?

Java 惰性加载程序,cglib的替代品?,java,javassist,cglib,Java,Javassist,Cglib,使用cglib,我可以使用以下代码创建延迟实例化的BigInteger: Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(BigInteger.class); enhancer.setCallback( new LazyLoader() { @Override public Object loadObject() throws Exception {

使用cglib,我可以使用以下代码创建延迟实例化的BigInteger:

Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(BigInteger.class);
enhancer.setCallback(
        new LazyLoader() {

            @Override
            public Object loadObject() throws Exception {
                // expensive computation here
                long totalCount = getTotalCount(totalCountExecutors); 
                return BigInteger.valueOf(totalCount);
            }
        });
totalCount =
        (BigInteger)
                enhancer.create(new Class[] {String.class}, new Object[] {"0"});
然而,上面提到的Java 11发出了警告,而且似乎还没有兼容的版本,所以我想迁移到其他库。
ByteBuddy是。。。大规模的,并希望避免如此大的依赖。是否可以使用javassist(或任何其他“轻”库)创建延迟加载代理?

您可以使用javassist库()

例如,Hibernate正在大量使用Javassist代理来管理延迟加载:


您可能会从中得到启发?

使用Byte Buddy会很容易,但如果您必须使用“轻量级”(我的连接在不到100毫秒的时间内下载3 MB),您可以这样做:

ProxyFactory factory = new ProxyFactory();
factory .setSuperclass(BigInteger.class);
factory .setFilter(new MethodFilter() {
  @Override
  public boolean isHandled(Method m) {
     return !m.getName().equals("finalize");
  }
});

Class<?> clazz = factory.createClass();
MethodHandler handler = new MethodHandler() {

  private volatile Object delegate;

  @Override
  public Object invoke(Object self, Method method, Method proceed,
                       Object[] args) throws Throwable {
     Object delegate = this.delegate;
     if (delegate == null) {
       synchronized (this) {
         delegate = this.delegate;
         if (delegate == null) {
           this.delegate = delegate = loadObject();
         }
       }
     }
     return method.invoke(delegate, args);
 }

  private Object loadObject() throws Exception {
    // expensive computation here
    long totalCount = getTotalCount(totalCountExecutors); 
    return BigInteger.valueOf(totalCount);
  }
};

BigInteger instance = (BigInteger) clazz.newInstance();
((Proxy) instance).setHandler(handler);
ProxyFactory=newproxyFactory();
factory.setSuperclass(BigInteger.class);
setFilter(新的MethodFilter(){
@凌驾
公共布尔值isHandled(方法m){
return!m.getName().equals(“finalize”);
}
});
Class clazz=factory.createClass();
MethodHandler=newMethodHandler(){
私有易失性对象委托;
@凌驾
公共对象调用(对象自身、方法、方法继续、,
对象[]args)抛出可丢弃的{
对象委托=this.delegate;
if(委托==null){
已同步(此){
delegate=this.delegate;
if(委托==null){
this.delegate=delegate=loadObject();
}
}
}
返回方法.invoke(委托,参数);
}
私有对象loadObject()引发异常{
//这里的计算很昂贵
long totalCount=getTotalCount(totalCountExecutors);
返回BigInteger.valueOf(totalCount);
}
};
BigInteger实例=(BigInteger)clazz.newInstance();
((代理)实例);

但是请注意,Javassist在模块系统中面临着自己的问题,将来可能需要再次迁移。

这是完全没有第三方库的实现,正如@zapl所建议的。我花了3分钟在Eclispe中创建了这个:我创建了一个类存根,然后使用“生成委托方法…”和“生成getter和setter…”。所有荣誉归于@zapl

import java.math.BigInteger;

public class MyBigInt extends BigInteger {

    private BigInteger delegate;

    public MyBigInt() {
        super("0");
    }

    private BigInteger getDelegate() {
        if (delegate == null) {
            delegate = computeMeHere();
        }
        return delegate;
    }

    public byte byteValue() {
        return getDelegate().byteValue();
    }

    public short shortValue() {
        return getDelegate().shortValue();
    }

    public BigInteger nextProbablePrime() {
        return getDelegate().nextProbablePrime();
    }

    public BigInteger add(BigInteger val) {
        return getDelegate().add(val);
    }

    public BigInteger subtract(BigInteger val) {
        return getDelegate().subtract(val);
    }

    public BigInteger multiply(BigInteger val) {
        return getDelegate().multiply(val);
    }

    public BigInteger divide(BigInteger val) {
        return getDelegate().divide(val);
    }

    public BigInteger[] divideAndRemainder(BigInteger val) {
        return getDelegate().divideAndRemainder(val);
    }

    public BigInteger remainder(BigInteger val) {
        return getDelegate().remainder(val);
    }

    public BigInteger pow(int exponent) {
        return getDelegate().pow(exponent);
    }

    public BigInteger gcd(BigInteger val) {
        return getDelegate().gcd(val);
    }

    public BigInteger abs() {
        return getDelegate().abs();
    }

    public BigInteger negate() {
        return getDelegate().negate();
    }

    public int signum() {
        return getDelegate().signum();
    }

    public BigInteger mod(BigInteger m) {
        return getDelegate().mod(m);
    }

    public BigInteger modPow(BigInteger exponent, BigInteger m) {
        return getDelegate().modPow(exponent, m);
    }

    public BigInteger modInverse(BigInteger m) {
        return getDelegate().modInverse(m);
    }

    public BigInteger shiftLeft(int n) {
        return getDelegate().shiftLeft(n);
    }

    public BigInteger shiftRight(int n) {
        return getDelegate().shiftRight(n);
    }

    public BigInteger and(BigInteger val) {
        return getDelegate().and(val);
    }

    public BigInteger or(BigInteger val) {
        return getDelegate().or(val);
    }

    public BigInteger xor(BigInteger val) {
        return getDelegate().xor(val);
    }

    public BigInteger not() {
        return getDelegate().not();
    }

    public BigInteger andNot(BigInteger val) {
        return getDelegate().andNot(val);
    }

    public boolean testBit(int n) {
        return getDelegate().testBit(n);
    }

    public BigInteger setBit(int n) {
        return getDelegate().setBit(n);
    }

    public BigInteger clearBit(int n) {
        return getDelegate().clearBit(n);
    }

    public BigInteger flipBit(int n) {
        return getDelegate().flipBit(n);
    }

    public int getLowestSetBit() {
        return getDelegate().getLowestSetBit();
    }

    public int bitLength() {
        return getDelegate().bitLength();
    }

    public int bitCount() {
        return getDelegate().bitCount();
    }

    public boolean isProbablePrime(int certainty) {
        return getDelegate().isProbablePrime(certainty);
    }

    public int compareTo(BigInteger val) {
        return getDelegate().compareTo(val);
    }

    public boolean equals(Object x) {
        return getDelegate().equals(x);
    }

    public BigInteger min(BigInteger val) {
        return getDelegate().min(val);
    }

    public BigInteger max(BigInteger val) {
        return getDelegate().max(val);
    }

    public int hashCode() {
        return getDelegate().hashCode();
    }

    public String toString(int radix) {
        return getDelegate().toString(radix);
    }

    public String toString() {
        return getDelegate().toString();
    }

    public byte[] toByteArray() {
        return getDelegate().toByteArray();
    }

    public int intValue() {
        return getDelegate().intValue();
    }

    public long longValue() {
        return getDelegate().longValue();
    }

    public float floatValue() {
        return getDelegate().floatValue();
    }

    public double doubleValue() {
        return getDelegate().doubleValue();
    }

    public long longValueExact() {
        return getDelegate().longValueExact();
    }

    public int intValueExact() {
        return getDelegate().intValueExact();
    }

    public short shortValueExact() {
        return getDelegate().shortValueExact();
    }

    public byte byteValueExact() {
        return getDelegate().byteValueExact();
    }
}

你为什么要这么做?您可以在不使用代理和类加载的情况下懒洋洋地创建对象,特别是在您有lambda的情况下,例如@zapl可能是因为它们需要将某种类型的懒洋洋加载的对象传递给库。@ohlec ok,但代理并不是用普通代码无法重新创建的魔法(除非它们执行编译器不允许的操作),例如,我有一个BigDecimal,它的计算成本很高,并且不是代码中的所有子序列步骤都会使用(取决于太多的因素)。BigDecimal没有无参数构造函数,因此公共代理不是选项。哦,但我看到你不是在使用Java代理,而是在生成一个子类。。。可以工作。@zapl基本上是一个手动创建的代理。当然,这是可能的,但它需要大量的样板文件。您可能了解一些东西,但代码似乎严重依赖Hibernate,我在类路径中没有这样的代码。我的意思是:不要这样使用它,而是看看Hibernate是如何做的(使用javassist),并尝试做同样的事情(当然不要嵌入Hibernate)…我真正的意思是,100个奖励点数应该比一个可能有用的通用指针更值钱;-)仅供参考:Hibernate使用Byte Buddy已经有一段时间了。最后,这是我想要的最好的匹配,没有额外的库负担。