2017-02-11 22:02:38 +00:00
|
|
|
" Author: farenjihn <farenjihn@gmail.com>, w0rp <devw0rp@gmail.com>
|
2017-02-11 21:29:48 +00:00
|
|
|
" Description: Lints java files using javac
|
|
|
|
|
2017-04-25 21:32:48 +00:00
|
|
|
let s:classpath_sep = has('unix') ? ':' : ';'
|
|
|
|
|
2018-04-08 19:35:06 +00:00
|
|
|
call ale#Set('java_javac_executable', 'javac')
|
|
|
|
call ale#Set('java_javac_options', '')
|
|
|
|
call ale#Set('java_javac_classpath', '')
|
2020-05-15 02:48:03 +00:00
|
|
|
call ale#Set('java_javac_sourcepath', [])
|
2017-02-11 21:29:48 +00:00
|
|
|
|
2019-04-07 13:58:06 +00:00
|
|
|
function! ale_linters#java#javac#RunWithImportPaths(buffer) abort
|
|
|
|
let l:command = ''
|
2017-04-25 21:32:48 +00:00
|
|
|
let l:pom_path = ale#path#FindNearestFile(a:buffer, 'pom.xml')
|
|
|
|
|
|
|
|
if !empty(l:pom_path) && executable('mvn')
|
2019-04-07 13:58:06 +00:00
|
|
|
let l:command = ale#path#CdString(fnamemodify(l:pom_path, ':h'))
|
2017-04-25 21:32:48 +00:00
|
|
|
\ . 'mvn dependency:build-classpath'
|
|
|
|
endif
|
|
|
|
|
2019-04-07 13:58:06 +00:00
|
|
|
" Try to use Gradle if Maven isn't available.
|
|
|
|
if empty(l:command)
|
|
|
|
let l:command = ale#gradle#BuildClasspathCommand(a:buffer)
|
|
|
|
endif
|
2018-09-04 15:51:18 +00:00
|
|
|
|
2019-06-03 19:30:18 +00:00
|
|
|
" Try to use Ant if Gradle and Maven aren't available
|
|
|
|
if empty(l:command)
|
|
|
|
let l:command = ale#ant#BuildClasspathCommand(a:buffer)
|
|
|
|
endif
|
|
|
|
|
2019-04-07 13:58:06 +00:00
|
|
|
if empty(l:command)
|
|
|
|
return ale_linters#java#javac#GetCommand(a:buffer, [], {})
|
2017-08-29 22:39:23 +00:00
|
|
|
endif
|
|
|
|
|
2019-04-07 13:58:06 +00:00
|
|
|
return ale#command#Run(
|
|
|
|
\ a:buffer,
|
|
|
|
\ l:command,
|
|
|
|
\ function('ale_linters#java#javac#GetCommand')
|
|
|
|
\)
|
2017-04-25 21:32:48 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:BuildClassPathOption(buffer, import_paths) abort
|
|
|
|
" Filter out lines like [INFO], etc.
|
|
|
|
let l:class_paths = filter(a:import_paths[:], 'v:val !~# ''[''')
|
|
|
|
call extend(
|
|
|
|
\ l:class_paths,
|
|
|
|
\ split(ale#Var(a:buffer, 'java_javac_classpath'), s:classpath_sep),
|
|
|
|
\)
|
|
|
|
|
|
|
|
return !empty(l:class_paths)
|
2017-05-12 08:20:16 +00:00
|
|
|
\ ? '-cp ' . ale#Escape(join(l:class_paths, s:classpath_sep))
|
2017-02-11 22:02:38 +00:00
|
|
|
\ : ''
|
2017-04-25 21:32:48 +00:00
|
|
|
endfunction
|
|
|
|
|
2019-04-07 13:58:06 +00:00
|
|
|
function! ale_linters#java#javac#GetCommand(buffer, import_paths, meta) abort
|
2017-04-25 21:32:48 +00:00
|
|
|
let l:cp_option = s:BuildClassPathOption(a:buffer, a:import_paths)
|
|
|
|
let l:sp_option = ''
|
|
|
|
|
|
|
|
" Find the src directory, for files in this project.
|
|
|
|
let l:src_dir = ale#path#FindNearestDirectory(a:buffer, 'src/main/java')
|
2017-11-20 16:42:43 +00:00
|
|
|
let l:sp_dirs = []
|
2017-04-25 21:32:48 +00:00
|
|
|
|
|
|
|
if !empty(l:src_dir)
|
2017-11-20 16:42:43 +00:00
|
|
|
call add(l:sp_dirs, l:src_dir)
|
|
|
|
|
|
|
|
" Automatically include the jaxb directory too, if it's there.
|
|
|
|
let l:jaxb_dir = fnamemodify(l:src_dir, ':h:h')
|
|
|
|
\ . (has('win32') ? '\jaxb\' : '/jaxb/')
|
|
|
|
|
|
|
|
if isdirectory(l:jaxb_dir)
|
|
|
|
call add(l:sp_dirs, l:jaxb_dir)
|
|
|
|
endif
|
2018-12-20 22:37:31 +00:00
|
|
|
endif
|
2017-11-20 16:42:43 +00:00
|
|
|
|
2018-12-20 22:37:31 +00:00
|
|
|
" Automatically include the test directory, but only for test code.
|
|
|
|
if expand('#' . a:buffer . ':p') =~? '\vsrc[/\\]test[/\\]java'
|
|
|
|
let l:test_dir = ale#path#FindNearestDirectory(a:buffer, 'src/test/java')
|
2017-11-20 16:42:43 +00:00
|
|
|
|
2018-12-20 22:37:31 +00:00
|
|
|
if isdirectory(l:test_dir)
|
|
|
|
call add(l:sp_dirs, l:test_dir)
|
2017-11-20 16:42:43 +00:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2020-05-18 09:35:16 +00:00
|
|
|
let l:source_paths = []
|
|
|
|
let l:source_path = ale#Var(a:buffer, 'java_javac_sourcepath')
|
2020-05-15 03:19:06 +00:00
|
|
|
|
2020-05-18 09:35:16 +00:00
|
|
|
if !empty(l:source_path) && type(l:source_path) is v:t_string
|
|
|
|
let l:source_paths = split(l:source_path, s:classpath_sep)
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !empty(l:source_path) && type(l:source_path) is v:t_list
|
|
|
|
let l:source_paths = l:source_path
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !empty(l:source_paths)
|
|
|
|
for l:path in l:source_paths
|
|
|
|
let l:sp_path = ale#path#FindNearestDirectory(a:buffer, l:path)
|
2020-05-15 03:19:06 +00:00
|
|
|
|
2020-05-15 02:48:03 +00:00
|
|
|
if !empty(l:sp_path)
|
|
|
|
call add(l:sp_dirs, l:sp_path)
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
|
2017-11-20 16:42:43 +00:00
|
|
|
if !empty(l:sp_dirs)
|
|
|
|
let l:sp_option = '-sourcepath '
|
|
|
|
\ . ale#Escape(join(l:sp_dirs, s:classpath_sep))
|
2017-04-25 21:32:48 +00:00
|
|
|
endif
|
2017-02-11 22:02:38 +00:00
|
|
|
|
2017-03-31 19:14:53 +00:00
|
|
|
" Create .class files in a temporary directory, which we will delete later.
|
2019-01-26 19:33:52 +00:00
|
|
|
let l:class_file_directory = ale#command#CreateDirectory(a:buffer)
|
2017-03-31 19:14:53 +00:00
|
|
|
|
2017-11-05 15:33:31 +00:00
|
|
|
" Always run javac from the directory the file is in, so we can resolve
|
|
|
|
" relative paths correctly.
|
|
|
|
return ale#path#BufferCdString(a:buffer)
|
2018-08-02 22:44:12 +00:00
|
|
|
\ . '%e -Xlint'
|
2018-07-15 17:24:53 +00:00
|
|
|
\ . ale#Pad(l:cp_option)
|
|
|
|
\ . ale#Pad(l:sp_option)
|
2017-05-12 08:20:16 +00:00
|
|
|
\ . ' -d ' . ale#Escape(l:class_file_directory)
|
2018-07-15 17:24:53 +00:00
|
|
|
\ . ale#Pad(ale#Var(a:buffer, 'java_javac_options'))
|
2017-02-11 22:02:38 +00:00
|
|
|
\ . ' %t'
|
|
|
|
endfunction
|
2017-02-11 21:29:48 +00:00
|
|
|
|
|
|
|
function! ale_linters#java#javac#Handle(buffer, lines) abort
|
|
|
|
" Look for lines like the following.
|
|
|
|
"
|
|
|
|
" Main.java:13: warning: [deprecation] donaught() in Testclass has been deprecated
|
|
|
|
" Main.java:16: error: ';' expected
|
2017-11-05 15:33:31 +00:00
|
|
|
let l:directory = expand('#' . a:buffer . ':p:h')
|
2018-12-09 17:22:50 +00:00
|
|
|
let l:pattern = '\v^(.*):(\d+): (.{-1,}):(.+)$'
|
2017-06-19 10:45:09 +00:00
|
|
|
let l:col_pattern = '\v^(\s*\^)$'
|
2017-05-04 22:34:52 +00:00
|
|
|
let l:symbol_pattern = '\v^ +symbol: *(class|method) +([^ ]+)'
|
2017-02-11 21:29:48 +00:00
|
|
|
let l:output = []
|
|
|
|
|
2017-06-19 10:45:09 +00:00
|
|
|
for l:match in ale#util#GetMatches(a:lines, [l:pattern, l:col_pattern, l:symbol_pattern])
|
|
|
|
if empty(l:match[2]) && empty(l:match[3])
|
2017-11-05 15:33:31 +00:00
|
|
|
let l:output[-1].col = len(l:match[1])
|
2017-06-19 10:45:09 +00:00
|
|
|
elseif empty(l:match[3])
|
2017-05-04 22:19:58 +00:00
|
|
|
" Add symbols to 'cannot find symbol' errors.
|
2017-08-08 07:39:13 +00:00
|
|
|
if l:output[-1].text is# 'error: cannot find symbol'
|
2017-05-04 22:19:58 +00:00
|
|
|
let l:output[-1].text .= ': ' . l:match[2]
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
call add(l:output, {
|
2017-11-05 15:33:31 +00:00
|
|
|
\ 'filename': ale#path#GetAbsPath(l:directory, l:match[1]),
|
|
|
|
\ 'lnum': l:match[2] + 0,
|
|
|
|
\ 'text': l:match[3] . ':' . l:match[4],
|
|
|
|
\ 'type': l:match[3] is# 'error' ? 'E' : 'W',
|
2017-05-04 22:19:58 +00:00
|
|
|
\})
|
|
|
|
endif
|
2017-02-11 21:29:48 +00:00
|
|
|
endfor
|
|
|
|
|
|
|
|
return l:output
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call ale#linter#Define('java', {
|
|
|
|
\ 'name': 'javac',
|
2019-02-22 18:05:04 +00:00
|
|
|
\ 'executable': {b -> ale#Var(b, 'java_javac_executable')},
|
2019-04-07 13:58:06 +00:00
|
|
|
\ 'command': function('ale_linters#java#javac#RunWithImportPaths'),
|
|
|
|
\ 'output_stream': 'stderr',
|
2017-02-11 21:29:48 +00:00
|
|
|
\ 'callback': 'ale_linters#java#javac#Handle',
|
|
|
|
\})
|