Class: JavaTools::Javac

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*source_files) ⇒ Javac

Returns a new instance of Javac.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/java_tools/javac.rb', line 38

def initialize( *source_files )
  @source_files = source_files || [ ]
  
  @source_path          = [ ]
  @destination          = 'build'
  @class_path           = [ ]
  @deprecation_warnings = true
  @warnings             = true
  @encoding             = nil
  @verbose              = false
end

Instance Attribute Details

#class_pathObject

The compilation class path



23
24
25
# File 'lib/java_tools/javac.rb', line 23

def class_path
  @class_path
end

#deprecation_warningsObject

Show deprecation warnings, true by default



26
27
28
# File 'lib/java_tools/javac.rb', line 26

def deprecation_warnings
  @deprecation_warnings
end

#destinationObject

The directory where the generated class files should be written, defaults to “build”



20
21
22
# File 'lib/java_tools/javac.rb', line 20

def destination
  @destination
end

#encodingObject

The encoding of the source files



32
33
34
# File 'lib/java_tools/javac.rb', line 32

def encoding
  @encoding
end

#source_filesObject

The files to compile.



14
15
16
# File 'lib/java_tools/javac.rb', line 14

def source_files
  @source_files
end

#source_pathObject

Additional directories where source files can be found.



17
18
19
# File 'lib/java_tools/javac.rb', line 17

def source_path
  @source_path
end

#verboseObject

Whether or not to print the equivalent command string to the output (see #execute)



35
36
37
# File 'lib/java_tools/javac.rb', line 35

def verbose
  @verbose
end

#warningsObject

Generate warnings, true by default



29
30
31
# File 'lib/java_tools/javac.rb', line 29

def warnings
  @warnings
end

Instance Method Details

#command_argsObject

:nodoc:



50
51
52
53
54
55
56
57
58
59
# File 'lib/java_tools/javac.rb', line 50

def command_args # :nodoc:
  args = [ ]
  args << '-sourcepath' << @source_path.join(':') unless @source_path.empty?
  args << '-d' << @destination unless (@destination.nil? || @destination =~ /^\s*$/)
  args << '-classpath' << @class_path.join(':') unless @class_path.empty?
  args << '-deprecation' unless @deprecation_warnings
  args << '-nowarn' unless @warnings
  args << '-encoding' << @encoding if @encoding
  args + @source_files
end

#command_stringObject

:nodoc:



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/java_tools/javac.rb', line 61

def command_string # :nodoc:
  args = [ ]
  args << '-sourcepath' << @source_path.join(':') unless @source_path.empty?
  args << '-d' << @destination unless (@destination.nil? || @destination =~ /^\s*$/)
  args << '-classpath' << @class_path.join(':') unless @class_path.empty?
  args << '-deprecation' unless @deprecation_warnings
  args << '-nowarn' unless @warnings
  args << '-encoding' << @encoding if @encoding

  "javac #{args.join(' ')}"
end

#execute(io = $stderr) ⇒ Object

Run javac. If #verbose is true the equivalent command string for the javac command will be printed to the stream passed as io (or $stdout by default)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/java_tools/javac.rb', line 76

def execute( io = $stderr )
  output_writer = StringWriter.new
  
  args = command_args.to_java(java.lang.String)
  
  result = com.sun.tools.javac.Main.compile(args, PrintWriter.new(output_writer))
  
  io.puts command_string if @verbose
  
  output_str = output_writer.to_s
  
  io.puts output_str if output_str !~ /^\s*$/
  
  if 0 == result
    true
  else
    false
  end
end