Avoid NPE when getting CodeSource

CodeSource can be null - add a check to avoid NPE. I found this issue while profiling my application - looks like this code throws NPE sometimes. This check avoid this overhead.