class Blufin::ScannerJava

Constants

TYPE_CLASS

TODO NOW - RENAME THESE

TYPE_CLASS_ABSTRACT
TYPE_ENUM
TYPE_INTERFACE

Public Class Methods

scan(path) click to toggle source

Scan Java code. @return void

# 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) click to toggle source

Scans a file. @return void

# 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

Private Class Methods

get_java_class_type() click to toggle source

Gets the Java class TYPE. @return void

# File lib/scan/scanner_java.rb, line 93
def self.get_java_class_type
    if @line =~ /\Apublic class\s/ || @line =~ /\Apublic final class\s/
        return TYPE_CLASS
    elsif @line =~ /\Aclass\s/ || @line =~ /\Afinal class\s/
        raise RuntimeError, "Class in file is not public: #{@line}"
    elsif @line =~ /\Apublic abstract class/
        return TYPE_CLASS_ABSTRACT
    elsif @line =~ /\Apublic enum/
        return TYPE_ENUM
    elsif @line =~ /\Apublic interface/
        return TYPE_INTERFACE
    end
end