1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
" Default pre- and post-compiler actions and commands for SpotBugs
" Maintainers: @konfekt and @zzzyxwvut
" Last Change: 2024 Dec 08
let s:save_cpo = &cpo
set cpo&vim
" Look for the setting of "g:spotbugs#state" in "ftplugin/java.vim".
let s:state = get(g:, 'spotbugs#state', {})
let s:commands = get(s:state, 'commands', {})
let s:compiler = get(s:state, 'compiler', '')
let s:readable = filereadable($VIMRUNTIME . '/compiler/' . s:compiler . '.vim')
if has_key(s:commands, 'DefaultPreCompilerCommand')
let g:SpotBugsPreCompilerCommand = s:commands.DefaultPreCompilerCommand
else
function! s:DefaultPreCompilerCommand(arguments) abort
execute 'make ' . a:arguments
cc
endfunction
let g:SpotBugsPreCompilerCommand = function('s:DefaultPreCompilerCommand')
endif
if has_key(s:commands, 'DefaultPreCompilerTestCommand')
let g:SpotBugsPreCompilerTestCommand = s:commands.DefaultPreCompilerTestCommand
else
function! s:DefaultPreCompilerTestCommand(arguments) abort
execute 'make ' . a:arguments
cc
endfunction
let g:SpotBugsPreCompilerTestCommand = function('s:DefaultPreCompilerTestCommand')
endif
if has_key(s:commands, 'DefaultPostCompilerCommand')
let g:SpotBugsPostCompilerCommand = s:commands.DefaultPostCompilerCommand
else
function! s:DefaultPostCompilerCommand(arguments) abort
execute 'make ' . a:arguments
endfunction
let g:SpotBugsPostCompilerCommand = function('s:DefaultPostCompilerCommand')
endif
if v:version > 900 || has('nvim')
function! spotbugs#DeleteClassFiles() abort
if !exists('b:spotbugs_class_files')
return
endif
for pathname in b:spotbugs_class_files
let classname = pathname =~# "^'.\\+\\.class'$"
\ ? eval(pathname)
\ : pathname
if classname =~# '\.class$' && filereadable(classname)
" Since v9.0.0795.
let octad = readblob(classname, 0, 8)
" Test the magic number and the major version number (45 for v1.0).
" Since v9.0.2027.
if len(octad) == 8 && octad[0 : 3] == 0zcafe.babe &&
" Nvim: no << operator
"\ or((octad[6] << 8), octad[7]) >= 45
\ or((octad[6] * 256), octad[7]) >= 45
echomsg printf('Deleting %s: %d', classname, delete(classname))
endif
endif
endfor
let b:spotbugs_class_files = []
endfunction
else
function! s:DeleteClassFilesWithNewLineCodes(classname) abort
" The distribution of "0a"s in class file versions 2560 and 2570:
"
" 0zca.fe.ba.be.00.00.0a.00 0zca.fe.ba.be.00.00.0a.0a
" 0zca.fe.ba.be.00.0a.0a.00 0zca.fe.ba.be.00.0a.0a.0a
" 0zca.fe.ba.be.0a.00.0a.00 0zca.fe.ba.be.0a.00.0a.0a
" 0zca.fe.ba.be.0a.0a.0a.00 0zca.fe.ba.be.0a.0a.0a.0a
let numbers = [0, 0, 0, 0, 0, 0, 0, 0]
let offset = 0
let lines = readfile(a:classname, 'b', 4)
" Track NL byte counts to handle files of less than 8 bytes.
let nl_cnt = len(lines)
" Track non-NL byte counts for "0zca.fe.ba.be.0a.0a.0a.0a".
let non_nl_cnt = 0
for line in lines
for idx in range(strlen(line))
" Remap NLs to Nuls.
let numbers[offset] = (line[idx] == "\n") ? 0 : char2nr(line[idx]) % 256
let non_nl_cnt += 1
let offset += 1
if offset > 7
break
endif
endfor
let nl_cnt -= 1
if offset > 7 || (nl_cnt < 1 && non_nl_cnt > 4)
break
endif
" Reclaim NLs.
let numbers[offset] = 10
let offset += 1
if offset > 7
break
endif
endfor
" Test the magic number and the major version number (45 for v1.0).
if offset > 7 && numbers[0] == 0xca && numbers[1] == 0xfe &&
\ numbers[2] == 0xba && numbers[3] == 0xbe &&
\ (numbers[6] * 256 + numbers[7]) >= 45
echomsg printf('Deleting %s: %d', a:classname, delete(a:classname))
endif
endfunction
function! spotbugs#DeleteClassFiles() abort
if !exists('b:spotbugs_class_files')
return
endif
let encoding = &encoding
try
set encoding=latin1
for pathname in b:spotbugs_class_files
let classname = pathname =~# "^'.\\+\\.class'$"
\ ? eval(pathname)
\ : pathname
if classname =~# '\.class$' && filereadable(classname)
let line = get(readfile(classname, 'b', 1), 0, '')
let length = strlen(line)
" Test the magic number and the major version number (45 for v1.0).
if length > 3 && line[0 : 3] == "\xca\xfe\xba\xbe"
if length > 7 && ((line[6] == "\n" ? 0 : char2nr(line[6]) % 256) * 256 +
\ (line[7] == "\n" ? 0 : char2nr(line[7]) % 256)) >= 45
echomsg printf('Deleting %s: %d', classname, delete(classname))
else
call s:DeleteClassFilesWithNewLineCodes(classname)
endif
endif
endif
endfor
finally
let &encoding = encoding
endtry
let b:spotbugs_class_files = []
endfunction
endif
function! spotbugs#DefaultPostCompilerAction() abort
" Since v7.4.191.
call call(g:SpotBugsPostCompilerCommand, ['%:S'])
endfunction
if s:readable && s:compiler ==# 'maven' && executable('mvn')
function! spotbugs#DefaultPreCompilerAction() abort
call spotbugs#DeleteClassFiles()
compiler maven
call call(g:SpotBugsPreCompilerCommand, ['compile'])
endfunction
function! spotbugs#DefaultPreCompilerTestAction() abort
call spotbugs#DeleteClassFiles()
compiler maven
call call(g:SpotBugsPreCompilerTestCommand, ['test-compile'])
endfunction
function! spotbugs#DefaultProperties() abort
return {
\ 'PreCompilerAction':
\ function('spotbugs#DefaultPreCompilerAction'),
\ 'PreCompilerTestAction':
\ function('spotbugs#DefaultPreCompilerTestAction'),
\ 'PostCompilerAction':
\ function('spotbugs#DefaultPostCompilerAction'),
\ 'sourceDirPath': ['src/main/java'],
\ 'classDirPath': ['target/classes'],
\ 'testSourceDirPath': ['src/test/java'],
\ 'testClassDirPath': ['target/test-classes'],
\ }
endfunction
unlet s:readable s:compiler
elseif s:readable && s:compiler ==# 'ant' && executable('ant')
function! spotbugs#DefaultPreCompilerAction() abort
call spotbugs#DeleteClassFiles()
compiler ant
call call(g:SpotBugsPreCompilerCommand, ['compile'])
endfunction
function! spotbugs#DefaultPreCompilerTestAction() abort
call spotbugs#DeleteClassFiles()
compiler ant
call call(g:SpotBugsPreCompilerTestCommand, ['compile-test'])
endfunction
function! spotbugs#DefaultProperties() abort
return {
\ 'PreCompilerAction':
\ function('spotbugs#DefaultPreCompilerAction'),
\ 'PreCompilerTestAction':
\ function('spotbugs#DefaultPreCompilerTestAction'),
\ 'PostCompilerAction':
\ function('spotbugs#DefaultPostCompilerAction'),
\ 'sourceDirPath': ['src'],
\ 'classDirPath': ['build/classes'],
\ 'testSourceDirPath': ['test'],
\ 'testClassDirPath': ['build/test/classes'],
\ }
endfunction
unlet s:readable s:compiler
elseif s:readable && s:compiler ==# 'javac' && executable('javac')
let s:filename = tempname()
function! spotbugs#DefaultPreCompilerAction() abort
call spotbugs#DeleteClassFiles()
compiler javac
if get(b:, 'javac_makeprg_params', get(g:, 'javac_makeprg_params', '')) =~ '\s@\S'
" Only read options and filenames from @options [@sources ...] and do
" not update these files when filelists change.
call call(g:SpotBugsPreCompilerCommand, [''])
else
" Collect filenames so that Javac can figure out what to compile.
let filelist = []
for arg_num in range(argc(-1))
let arg_name = argv(arg_num)
if arg_name =~# '\.java\=$'
call add(filelist, fnamemodify(arg_name, ':p:S'))
endif
endfor
for buf_num in range(1, bufnr('$'))
if !buflisted(buf_num)
continue
endif
let buf_name = bufname(buf_num)
if buf_name =~# '\.java\=$'
let buf_name = fnamemodify(buf_name, ':p:S')
if index(filelist, buf_name) < 0
call add(filelist, buf_name)
endif
endif
endfor
noautocmd call writefile(filelist, s:filename)
call call(g:SpotBugsPreCompilerCommand, [shellescape('@' . s:filename)])
endif
endfunction
function! spotbugs#DefaultPreCompilerTestAction() abort
call spotbugs#DefaultPreCompilerAction()
endfunction
function! spotbugs#DefaultProperties() abort
return {
\ 'PreCompilerAction':
\ function('spotbugs#DefaultPreCompilerAction'),
\ 'PostCompilerAction':
\ function('spotbugs#DefaultPostCompilerAction'),
\ }
endfunction
unlet s:readable s:compiler g:SpotBugsPreCompilerTestCommand
delfunction! s:DefaultPreCompilerTestCommand
else
function! spotbugs#DefaultPreCompilerAction() abort
echomsg printf('Not supported: "%s"', s:compiler)
endfunction
function! spotbugs#DefaultPreCompilerTestAction() abort
call spotbugs#DefaultPreCompilerAction()
endfunction
function! spotbugs#DefaultProperties() abort
return {}
endfunction
" XXX: Keep "s:compiler" around for "spotbugs#DefaultPreCompilerAction()",
" "s:DefaultPostCompilerCommand" -- "spotbugs#DefaultPostCompilerAction()".
unlet s:readable g:SpotBugsPreCompilerCommand g:SpotBugsPreCompilerTestCommand
delfunction! s:DefaultPreCompilerCommand
delfunction! s:DefaultPreCompilerTestCommand
endif
function! s:DefineBufferAutocmd(event, ...) abort
if !exists('#java_spotbugs#User')
return 1
endif
for l:event in insert(copy(a:000), a:event)
if l:event != 'User'
execute printf('silent! autocmd! java_spotbugs %s <buffer>', l:event)
execute printf('autocmd java_spotbugs %s <buffer> doautocmd User', l:event)
endif
endfor
return 0
endfunction
function! s:RemoveBufferAutocmd(event, ...) abort
if !exists('#java_spotbugs')
return 1
endif
for l:event in insert(copy(a:000), a:event)
if l:event != 'User'
execute printf('silent! autocmd! java_spotbugs %s <buffer>', l:event)
endif
endfor
return 0
endfunction
" Documented in ":help compiler-spotbugs".
command! -bar -nargs=+ -complete=event SpotBugsDefineBufferAutocmd
\ call s:DefineBufferAutocmd(<f-args>)
command! -bar -nargs=+ -complete=event SpotBugsRemoveBufferAutocmd
\ call s:RemoveBufferAutocmd(<f-args>)
let &cpo = s:save_cpo
unlet s:commands s:state s:save_cpo
" vim: set foldmethod=syntax shiftwidth=2 expandtab:
|