jar包冲突解决(conflictResolution)

简介

本工具利用了自定义的classloader,将冲突jar包加载在不同的classloader中,实现jar包无冲突的目的

并将java原有反射调用逻辑进行了封装,实现了更简洁的调用

使用

初始化

继承 ClassProxy 并调用 initClassLoader 初始化类加载器

public class Proxy extends ClassProxy{
    protected  static  CLClassloader classLoader;

    static {
        classLoader=initClassLoader(Proxy.class.getResource("lib"));
    }
    public Proxy(String className) throws EncryptionAndDecryptionException, ClassNotFoundException, InstantiationException, IllegalAccessException, MalformedURLException, URISyntaxException, ConflictResolutionException{
        super(className, classLoader);        
    }
    public Proxy(String className,InputStream in) throws ClassNotFoundException, InstantiationException, IllegalAccessException, URISyntaxException, IOException, ConflictResolutionException {
        super(className,classLoader,in);
    }    
}

加载类

Proxy asymmetricToolProxy=new Proxy("lich.tool.encryptionAndDecryption.core.asymmetric.AsymmetricTool");//加载类

实例化类

默认情况下参数类型会自动识别转换

asymmetricToolProxy.newInstance(o1,o2,.....);

自动识别参数类型在有些情况并不适用,如参数是接口、父类 ,可使用Parameter进行构造

asymmetricToolProxy.newInstance(new Parameter().addParameter(PublicKey.class,publicKey).addParameter(PublicKey.class,publicKey)....);

方法调用

asymmetricToolProxy.exec("verify", new Parameters().addParameter(sign).addParameter(ori).addParameter(PublicKey.class,publicKey).addParameter(algorithm));

获取、设置属性

asymmetricToolProxy.getFieldValue("BC");
asymmetricToolProxy.setFieldValue("BC",BC);

示例

public class KeyPairTool{
    private static Proxy keyPairToolProxy;
    private static void init() throws EncryptionAndDecryptionException {
        try {
            if(keyPairToolProxy==null) {
                keyPairToolProxy=new Proxy("lich.tool.encryptionAndDecryption.core.asymmetric.KeyPairTool");
            }
        } catch (Exception e) {
            throw new EncryptionAndDecryptionException(e);
        }    
    }

    public static KeyPair generateGMKeyPair() throws EncryptionAndDecryptionException {
        try {
            init();
            return (KeyPair)keyPairToolProxy.exec("generateGMKeyPair");
        } catch (Exception e) {
            throw new EncryptionAndDecryptionException(e);
        }
    }
    public static  KeyPair generateRSAKeyPair(int keySize) throws EncryptionAndDecryptionException {
        try {
            init();
            return (KeyPair)keyPairToolProxy.exec("generateRSAKeyPair", new Parameters().addParameter(int.class,keySize));
        } catch (Exception e) {
            throw new EncryptionAndDecryptionException(e);
        }
    }    
}

异常

xxx无法调用无参构造自动转换,请手动转换

在调用exec、newInstance时,如果参数并不在当前ClassLoader内,会在当前ClassLoader调用参数类的空构造函数进行重新实例化,当参数类无空构造时,就会出现如上问题,解决方式如下

Proxy obj=new Proxy("lich.tool.encryptionAndDecryption.ext.xxx");
obj.newInstance(new Parameters().addParameter(int.class,keySize));
baseProxy.exec("setSm1ext",  obj.getClsss(),obj.getObj()));

results matching ""

    No results matching ""