Class: JavaClass

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

Overview

for parsing Java .java files

Direct Known Subclasses

Android::JavaClass

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ JavaClass

Returns a new instance of JavaClass.



6
7
8
# File 'lib/javaclass.rb', line 6

def initialize file_path
  @file_path = file_path
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



4
5
6
# File 'lib/javaclass.rb', line 4

def file_path
  @file_path
end

Class Method Details

.parse(java_file) ⇒ Object



46
47
48
# File 'lib/javaclass.rb', line 46

def self.parse java_file
  JavaClass.new java_file
end

Instance Method Details

#class_nameObject Also known as: name



10
11
12
13
14
15
16
17
18
# File 'lib/javaclass.rb', line 10

def class_name
  match = /public .*class (\w+)/.match(source_sans_comments)
  if match
    match.captures.first
  else
    match = /class (\w+)/.match(source_sans_comments)
    match.captures.first if match
  end
end

#package_nameObject Also known as: package



21
22
23
# File 'lib/javaclass.rb', line 21

def package_name
  /package (.*);/.match(source_sans_comments).captures.first
end

#source_codeObject Also known as: source



33
34
35
# File 'lib/javaclass.rb', line 33

def source_code
  File.read file_path
end

#source_code_without_commentsObject Also known as: source_without_comments, source_sans_comments



38
39
40
41
42
# File 'lib/javaclass.rb', line 38

def source_code_without_comments
  # regular expression to find all comments, from: http://ostermiller.org/findcomment.html
  regular_expression = Regexp.new('(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)')
  source.gsub(regular_expression, '')
end

#super_classObject Also known as: superclass, parent



26
27
28
29
# File 'lib/javaclass.rb', line 26

def super_class
  match = /public .*class \w+ extends (\w+)/.match(source_sans_comments)
  if match then match.captures.first else nil end
end