javaparse 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ module JavaParse
2
+
3
+ class JavaFiles
4
+
5
+ def initialize(*directories)
6
+ @java_units = []
7
+ paths = directories.map { |directory|
8
+ Dir.glob("#{directory}/**/*.java")
9
+ }.flatten
10
+ paths.uniq!
11
+ paths.each { |path|
12
+ begin
13
+ @java_units.push(JavaUnit.new(path))
14
+ rescue
15
+ nil
16
+ end
17
+ }
18
+ end
19
+
20
+ def count(type = :files)
21
+ return @java_units.inject(0) { |sum, java_unit| sum + java_unit.send(type)} if [:bloc, :cloc, :loc].include? type.to_sym
22
+ return @java_units.size if type == :files
23
+ raise "Do not know how to count type #{type}"
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -1,22 +1,25 @@
1
- module JavaParse
1
+ module JavaParse
2
+
3
+ JAVA_COMMENTS_RE = /^\s*\/\/|^\s*\/\*|^\s*\*/
2
4
 
3
5
  class JavaUnit
4
6
 
5
- attr_reader :body, :head
7
+ attr_reader :body, :head, :loc, :bloc, :cloc
6
8
 
7
9
  def initialize(java_file_path)
8
10
  @file_path = java_file_path
9
11
  @unit_name = File.basename(java_file_path, ".java")
10
- @content = File.open(java_file_path) { |file| file.read }
12
+ @content = File.open(@file_path) { |file| file.read }
11
13
  validate_unit
12
14
  @head, @body = partition_unit
15
+ count_lines
13
16
  end
14
17
 
15
18
  def method_blocks
16
19
  return @body.split("}")[0...-1] if (clazz? or enum?)
17
20
  return @body.split(";")[0...-1] if (interface?)
18
21
  end
19
-
22
+
20
23
  def clazz?
21
24
  unit_declaration_line.include? 'class'
22
25
  end
@@ -29,12 +32,26 @@ module JavaParse
29
32
  unit_declaration_line.include? 'enum'
30
33
  end
31
34
 
32
- private
35
+ private
33
36
 
34
37
  def validate_unit
35
38
  raise RuntimeError, "Mismatch between filename and declared name for #{@file_path}" unless unit_declaration_line
36
39
  end
37
40
 
41
+ def count_lines
42
+ @loc, @bloc, @cloc = 0, 0, 0
43
+ @content.each_line { |line|
44
+ if line.strip.empty?
45
+ @bloc += 1
46
+ elsif JAVA_COMMENTS_RE.match(line)
47
+ @cloc += 1
48
+ else
49
+ @loc += 1
50
+ end
51
+ }
52
+ [@loc, @cloc, @bloc]
53
+ end
54
+
38
55
  def unit_declaration_line
39
56
  declaration_line_match = /^.*(?<=class|enum|interface)\s*#{@unit_name}.*$/.match(@content)
40
57
  declaration_line_match[0] unless declaration_line_match.nil?
data/lib/javaparse.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  $LOAD_PATH.unshift File.dirname(__FILE__)
2
2
 
3
3
  require 'javaparse/java_unit.rb'
4
- require 'javaparse/java_section.rb'
4
+ require 'javaparse/java_section.rb'
5
+ require 'javaparse/java_files.rb'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: javaparse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-22 00:00:00.000000000 Z
12
+ date: 2012-07-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -20,6 +20,7 @@ files:
20
20
  - lib/javaparse.rb
21
21
  - lib/javaparse/java_section.rb
22
22
  - lib/javaparse/java_unit.rb
23
+ - lib/javaparse/java_files.rb
23
24
  homepage:
24
25
  licenses: []
25
26
  post_install_message:
@@ -43,5 +44,5 @@ rubyforge_project: javaparse
43
44
  rubygems_version: 1.8.24
44
45
  signing_key:
45
46
  specification_version: 3
46
- summary: Get info from Java files with Ruby
47
+ summary: Get info from Java files
47
48
  test_files: []