Class: Blufin::ScannerJava

Inherits:
Object
  • Object
show all
Defined in:
lib/scan/scanner_java.rb

Constant Summary collapse

TYPE_CLASS =

TODO NOW - RENAME THESE

'type_class'
TYPE_CLASS_ABSTRACT =
'type_class_abstract'
TYPE_ENUM =
'type_enum'
TYPE_INTERFACE =
'type_interface'

Class Method Summary collapse

Class Method Details

.scan(path) ⇒ Object

Scan Java code.

Returns:

  • void


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/scan/scanner_java.rb', line 13

def self.scan(path)

    # Check path exists.
    Blufin::Terminal::error("Path does not exist: #{Blufin::Terminal::format_invalid(path)}") unless Blufin::Files::path_exists(path)

    @data   = {}
    @errors = []

    # Get all file(s) in path.
    Blufin::Files::get_files_in_dir(path, 'java').each do |file|
        data, errors = scan_file(file)
        @data[file]  = data
        @errors.concat(errors)
    end

    return @data, @errors

end

.scan_file(file) ⇒ Object

Scans a file.

Returns:

  • void

Raises:

  • (RuntimeError)

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
# File 'lib/scan/scanner_java.rb', line 34

def self.scan_file(file)
    raise RuntimeError, "File not found: #{file}" unless Blufin::Files::file_exists(file)
    data         = {}
    errors       = []
    fs           = file.split('/')
    data[:class] = fs[fs.length - 1].gsub(/\.java\s*$/i, '')
    @line_number = 0

    Blufin::Files::read_file(file).each do |line|

        begin

            @line        = line.gsub("\n", '')
            @line_number += 1

            # Get the type of class this is.
            class_type   = get_java_class_type
            unless class_type.nil?
                data[:class_type] = class_type
                next
            end


            if @line =~ /^\s*@RestController\s*$/
                data[:controller] = true
                next
            elsif @line =~ /^\s*@RequestMapping\(".+"\)$/
                rm = @line.gsub(/^\s*@RequestMapping\("/, '').gsub(/"\)$/, '')
                # Skip the custom 404 error handler mapping.
                next if rm == '${server.error.path:${error.path:/error}}'
                raise RuntimeError, 'Cannot have annotation @RequestMapping before (or without) @RestController.' unless data[:controller]

                # TODO NOW - Continue here.
                # TODO NOW - Need an example of every possible end-point in TestController.

                data[:request_mapping] = rm

            elsif !data[:class_type].nil? && @line =~ /^\s*.*\s+(public|protected|private)\s+[A-Za-z]+\s+[A-Za-z0-9]+\s*\(.*\)\s*\{?\}?\s*$/

                # TODO - REMOVE
                # puts "\x1B[38;5;154m#{@line}\x1B[0m"

            end

        rescue RuntimeError => e
            errors << Blufin::ScannerError.new(file, @line, @line_number, e.message)
            next
        end

    end

    return data, errors

end