com.baidu.jprotobuf 使用远程类编码和解码

背景

因业务需要,我们某个业务需要从远程拿到一批二进制数据,然后将这批数据转为java对象进行处理
数据是通过jprotobuf 将java对象编码后得到的
本地没有这个编码、解码的规则,需要从远程拿取class文件,然后在根据动态加载这个class文件,再根据这个class文件将数据流转为可用的java对象

碰到的问题

在 执行 ProtobufProxy.create(schoolClass); 的时候报错

代码:

   public void protobufProxyTest2() throws Exception {
        File file1 =  new File("D:\\code\\untitled\\class");
        URLClassLoader classloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
        add.setAccessible(true);
        add.invoke(classloader, new Object[] { file1.toURI().toURL() });

        Class<?> schoolClass = classloader.loadClass("School");
        Codec schoolCodec = ProtobufProxy.create(schoolClass);
    }

报错:

java.lang.IllegalStateException: Compilation failed. class: School$$JProtoBufClass, diagnostics: [School$$JProtoBufClass.java:18: 警告: Can't initialize javac processor due to (most likely) a class loader problem: java.lang.NoClassDefFoundError: com/sun/tools/javac/processing/JavacProcessingEnvironment
public class School$$JProtoBufClass implements com.baidu.bjf.remoting.protobuf.Codec<School>{
       ^
  	//此处删除了很多栈xinxi
  	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
  	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
, School$$JProtoBufClass.java:18: 错误: 找不到符号
public class School$$JProtoBufClass implements com.baidu.bjf.remoting.protobuf.Codec<School>{
                                                                                     ^
  符号:School, School$$JProtoBufClass.java:21: 错误: 找不到符号
    public byte[] encode(School t) throws IOException {
                         ^
  符号:School
  位置:School$$JProtoBufClass, School$$JProtoBufClass.java:29: 错误: 找不到符号
    public School decode(byte[] bb) throws IOException {
           ^
  符号:School
  位置:School$$JProtoBufClass, School$$JProtoBufClass.java:34: 错误: 找不到符号
    public int size(School t) throws IOException {
                    ^
  符号:School
  位置:School$$JProtoBufClass, School$$JProtoBufClass.java:61: 错误: 找不到符号
    public void doWriteTo(School t, CodedOutputStream output)
                          ^
  符号:School
  位置:School$$JProtoBufClass, School$$JProtoBufClass.java:90: 错误: 找不到符号
    public void writeTo(School t, CodedOutputStream output)
                        ^
  符号:School
  位置:School$$JProtoBufClass, School$$JProtoBufClass.java:95: 错误: 找不到符号
public School readFrom(CodedInputStream input) throws IOException {
           ^
  符号:School
  位置:School$$JProtoBufClass]

	at com.baidu.bjf.remoting.protobuf.utils.compiler.JdkCompiler.doCompile(JdkCompiler.java:202)
	at com.baidu.bjf.remoting.protobuf.utils.compiler.AbstractCompiler.compile(AbstractCompiler.java:43)
	at com.baidu.bjf.remoting.protobuf.ProtobufProxy.doCreate(ProtobufProxy.java:395)
	at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:297)
	at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:262)
	at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:234)
	at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:193)
	at Test2.protobufProxyTest2(Test2.java:71)


追了一波源码,发现调了JavaCompiler进行编译class文件(可以理解为执行javac命令)

位置: JdkCompiler-146行

在这里插入图片描述

解决方案 (不想看debug过程直接点这里)

研究了一番,猜测是因为项目启动时没指定此类所在目录,于是在项目启动时执行下面的方法

addClassPath为下载的class文件所在目录

  public static void putClasspath(String addClassPath){
        String property = System.getProperty("java.class.path");
        System.setProperty("java.class.path", property +";"+addClassPath+";");
        System.out.println(System.getProperty("java.class.path"));
    }

完美解决!!

测试代码:

@Test
    public void protobufProxyTest() throws Exception {
        // 下载类的目录
        putClasspath("D:\\code\\untitled\\class");
        File file1 =  new File("D:\\code\\untitled\\class");
        //初始化URLClassLoader 用来动态加载下载下来的类
        URLClassLoader classloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
        add.setAccessible(true);
        add.invoke(classloader, new Object[] { file1.toURI().toURL() });


        School2 school = new School2();
        school.setSchoolLocation("schoolLocation");
        school.setExtend(11111111L);
        //为了方便测试,直接创建和下载的类结构一致的类School2
        Codec<School2> tCodec = ProtobufProxy.create(School2.class);
        byte[] encode = tCodec.encode(school);
        School2 encode1 = tCodec.decode(encode);
        System.out.println(encode1);

		// 动态加载远程下载的类
        Class<?> schoolClass = classloader.loadClass("School");
        Codec schoolCodec = ProtobufProxy.create(schoolClass);
        //解码为java对象
        Object decode = schoolCodec.decode(encode);
        System.out.println(decode);
    }


    public static void putClasspath(String addClassPath){
        String property = System.getProperty("java.class.path");
        System.setProperty("java.class.path", property +";"+addClassPath+";");
        System.out.println(System.getProperty("java.class.path"));
    }

输出日志
在这里插入图片描述
测试demo地址
https://gitee.com/ayezs/protobuf-proxy-test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值