Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix java8 lambda #68

Merged
merged 1 commit into from
Jul 13, 2016
Merged

fix java8 lambda #68

merged 1 commit into from
Jul 13, 2016

Conversation

wuwen5
Copy link
Collaborator

@wuwen5 wuwen5 commented Jul 12, 2016

java8中,如果使用含有lambda语法会有异常:

七月 12, 2016 9:48:01 下午 com.alibaba.ttl.threadpool.agent.TtlTransformer transform
严重: Fail to transform class null, cause: null
七月 12, 2016 9:48:01 下午 com.alibaba.ttl.threadpool.agent.TtlTransformer transform
严重: Fail to transform class null, cause: null
七月 12, 2016 9:48:01 下午 com.alibaba.ttl.threadpool.agent.TtlTransformer transform
严重: Fail to transform class null, cause: null
七月 12, 2016 9:48:01 下午 com.alibaba.ttl.threadpool.agent.TtlTransformer transform
严重: Fail to transform class null, cause: null
七月 12, 2016 9:48:01 下午 com.alibaba.ttl.threadpool.agent.TtlTransformer transform
严重: Fail to transform class null, cause: null
七月 12, 2016 9:48:01 下午 com.alibaba.ttl.threadpool.agent.TtlTransformer transform
严重: Fail to transform class null, cause: null
七月 12, 2016 9:48:01 下午 com.alibaba.ttl.threadpool.agent.TtlTransformer transform
严重: Fail to transform class null, cause: null
七月 12, 2016 9:48:01 下午 com.alibaba.ttl.threadpool.agent.TtlTransformer transform
严重: Fail to transform class null, cause: null
七月 12, 2016 9:48:01 下午 com.alibaba.ttl.threadpool.agent.TtlTransformer transform
严重: Fail to transform class null, cause: null
七月 12, 2016 9:48:01 下午 com.alibaba.ttl.threadpool.agent.TtlTransformer transform
严重: Fail to transform class null, cause: null

分析发现仅在环境中使用了lambda的时候, classFile是空的,导致空指针异常。

public byte[] transform(ClassLoader loader, String classFile, Class<?> classBeingRedefined,
                            ProtectionDomain protectionDomain, byte[] classFileBuffer) 

我把classFileBuffer 写到文件然后查看发现是这样的:

javap 96fcce0c-0b37-4228-8da6-9bf45b59f94f
警告: 二进制文件96fcce0c-0b37-4228-8da6-9bf45b59f94f包含java.lang.invoke.LambdaForm$MH
Compiled from "LambdaForm$MH"
final class java.lang.invoke.LambdaForm$MH {
  static java.lang.Object linkToTargetMethod(java.lang.Object);
}

javap 3e8da79c-ece4-4699-bc16-25427040b04f
警告: 二进制文件3e8da79c-ece4-4699-bc16-25427040b04f包含com.ofpay.ofdc.Demo$$Lambda$1
final class com.ofpay.ofdc.Demo$$Lambda$1 implements java.lang.Runnable {
  public void run();
}

@CLAassistant
Copy link

CLAassistant commented Jul 12, 2016

CLA assistant check
All committers have signed the CLA.

@codecov-io
Copy link

codecov-io commented Jul 12, 2016

Current coverage is 84.39%

Merging #68 into master will not change coverage

@@             master        #68   diff @@
==========================================
  Files             7          7          
  Lines           173        173          
  Methods           0          0          
  Messages          0          0          
  Branches         27         27          
==========================================
  Hits            146        146          
  Misses           17         17          
  Partials         10         10          

Powered by Codecov. Last updated by 6bb5991...88d4786

@oldratlee
Copy link
Member

oldratlee commented Jul 13, 2016

@wuwen5 非常感谢 问题汇报和解决。

我看一下 ,如果OK,我就发一个新版本出来 😄

@oldratlee oldratlee merged commit 2c30e20 into alibaba:master Jul 13, 2016
@oldratlee
Copy link
Member

oldratlee commented Jul 13, 2016

@wuwen5

java8中,如果使用含有lambda语法会有异常:

能给一下 『使用含有lambda语法』的代码, 方便复现问题后确认方案是有效解决的。

@wuwen5
Copy link
Collaborator Author

wuwen5 commented Jul 13, 2016

这是本地验证的时候的测试代码.

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        executorService.execute(() -> System.out.println("++++"));
    }
}

同样是jdk 8
如果这么写也不会出现classFile空值的情况

executorService.execute(new Runnable() {
    @Override
    public void run() {
        System.out.println("---run test----")
    }
});

oldratlee pushed a commit that referenced this pull request Jul 13, 2016
Merge pull request #68 from wuwen5/fix-java8-lambda
oldratlee pushed a commit that referenced this pull request Jul 13, 2016
Merge pull request #68 from wuwen5/fix-java8-lambda
oldratlee pushed a commit that referenced this pull request Jul 13, 2016
Merge pull request #68 from wuwen5/fix-java8-lambda
@oldratlee oldratlee modified the milestone: 2.0.2 Jul 13, 2016
@oldratlee
Copy link
Member

oldratlee commented Jul 13, 2016

@wuwen5 已Release新版本 2.0.2
https://github.com/alibaba/transmittable-thread-local/releases/tag/v2.0.2

@wuwen5
Copy link
Collaborator Author

wuwen5 commented Jul 15, 2016

@oldratlee 有个疑问请教下,我看代码transform的返回值改了,将不需要转换的class改成返回空数组了。byte[] EMPTY_BYTE_ARRAY = {};

但是我很好奇ClassFileTransformer API的注释写的 不转换是返回 null,jvm这块处理空数组和null会是一样么?

@return  a well-formed class file buffer (the result of the transform),
                or <code>null</code> if no transform is performed.

@oldratlee
Copy link
Member

oldratlee commented Jul 15, 2016

用 empty array 来自代码质量改进的一个pull request #56
你可以从commit log( 4a5b292 )里查到。

我看持续集成多jdk版本都是通过的,就接纳了这个pr。

@haogrgr
Copy link

haogrgr commented Aug 8, 2016

Lambda是运行时生成的字节码, new Runnable是编译期的.

@oldratlee
Copy link
Member

oldratlee commented Aug 8, 2016

@haogrgr 因为运行时生成字节码所以没class file。Got!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants