C0 code coverage information

Generated on Wed Oct 07 08:34:01 -0700 2009 with rcov 0.8.2.1


Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
Name Total lines Lines of code Total coverage Code coverage
lib/buildr/java/compiler.rb 349 194
92.0%  
86.6%  
  1 # Licensed to the Apache Software Foundation (ASF) under one or more
  2 # contributor license agreements.  See the NOTICE file distributed with this
  3 # work for additional information regarding copyright ownership.  The ASF
  4 # licenses this file to you under the Apache License, Version 2.0 (the
  5 # "License"); you may not use this file except in compliance with the License.
  6 # You may obtain a copy of the License at
  7 #
  8 #    http://www.apache.org/licenses/LICENSE-2.0
  9 #
 10 # Unless required by applicable law or agreed to in writing, software
 11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 13 # License for the specific language governing permissions and limitations under
 14 # the License.
 15 
 16 
 17 require 'buildr/core/project'
 18 require 'buildr/core/common'
 19 require 'buildr/core/compile'
 20 require 'buildr/packaging'
 21 
 22 
 23 module Buildr
 24   module Compiler
 25 
 26     # Javac compiler:
 27     #   compile.using(:javac)
 28     # Used by default if .java files are found in the src/main/java directory (or src/test/java)
 29     # and sets the target directory to target/classes (or target/test/classes).
 30     #
 31     # Accepts the following options:
 32     # * :warnings    -- Issue warnings when compiling.  True when running in verbose mode.
 33     # * :debug       -- Generates bytecode with debugging information.  Set from the debug
 34     # environment variable/global option.
 35     # * :deprecation -- If true, shows deprecation messages.  False by default.
 36     # * :source      -- Source code compatibility.
 37     # * :target      -- Bytecode compatibility.
 38     # * :lint        -- Lint option is one of true, false (default), name (e.g. 'cast') or array.
 39     # * :other       -- Array of options passed to the compiler 
 40     # (e.g. ['-implicit:none', '-encoding', 'iso-8859-1'])
 41     class Javac < Base
 42 
 43       OPTIONS = [:warnings, :debug, :deprecation, :source, :target, :lint, :other]
 44 
 45       specify :language=>:java, :target=>'classes', :target_ext=>'class', :packaging=>:jar
 46 
 47       def initialize(project, options) #:nodoc:
 48         super
 49         options[:debug] = Buildr.options.debug if options[:debug].nil?
 50         options[:warnings] = verbose if options[:warnings].nil?
 51         options[:deprecation] ||= false
 52         options[:lint] ||= false
 53       end
 54 
 55       def compile(sources, target, dependencies) #:nodoc:
 56         check_options options, OPTIONS
 57         cmd_args = []
 58         # tools.jar contains the Java compiler.
 59         dependencies << Java.tools_jar if Java.tools_jar
 60         cmd_args << '-classpath' << dependencies.join(File::PATH_SEPARATOR) unless dependencies.empty?
 61         source_paths = sources.select { |source| File.directory?(source) }
 62         cmd_args << '-sourcepath' << source_paths.join(File::PATH_SEPARATOR) unless source_paths.empty?
 63         cmd_args << '-d' << File.expand_path(target)
 64         cmd_args += javac_args
 65         cmd_args += files_from_sources(sources)
 66         unless Buildr.application.options.dryrun
 67           trace((['javac'] + cmd_args).join(' '))
 68           Java.load
 69           Java.com.sun.tools.javac.Main.compile(cmd_args.to_java(Java.java.lang.String)) == 0 or
 70             fail 'Failed to compile, see errors above'
 71         end
 72       end
 73 
 74     private
 75 
 76       def javac_args #:nodoc:
 77         args = []  
 78         args << '-nowarn' unless options[:warnings]
 79         args << '-verbose' if Buildr.application.options.trace
 80         args << '-g' if options[:debug]
 81         args << '-deprecation' if options[:deprecation]
 82         args << '-source' << options[:source].to_s if options[:source]
 83         args << '-target' << options[:target].to_s if options[:target]
 84         case options[:lint]
 85           when Array  then args << "-Xlint:#{options[:lint].join(',')}"
 86           when String then args << "-Xlint:#{options[:lint]}"
 87           when true   then args << '-Xlint'
 88         end
 89         args + Array(options[:other])
 90       end
 91 
 92     end
 93     
 94   end
 95 
 96 
 97   # Methods added to Project for creating JavaDoc documentation.
 98   module Javadoc
 99 
100     # A convenient task for creating Javadocs from the project's compile task. Minimizes all
101     # the hard work to calling #from and #using.
102     #
103     # For example:
104     #   javadoc.from(projects('myapp:foo', 'myapp:bar')).using(:windowtitle=>'My App')
105     # Or, short and sweet:
106     #   desc 'My App'
107     #   define 'myapp' do
108     #     . . .
109     #     javadoc projects('myapp:foo', 'myapp:bar')
110     #   end
111     class JavadocTask < Rake::Task
112 
113       def initialize(*args) #:nodoc:
114         super
115         @options = {}
116         @classpath = []
117         @sourcepath = []
118         @files = FileList[]
119         enhance do |task|
120           rm_rf target.to_s
121           generate source_files, File.expand_path(target.to_s), options.merge(:classpath=>classpath, :sourcepath=>sourcepath)
122           touch target.to_s
123         end
124       end
125 
126       # The target directory for the generated Javadoc files.
127       attr_reader :target
128 
129       # :call-seq:
130       #   into(path) => self
131       #
132       # Sets the target directory and returns self. This will also set the Javadoc task
133       # as a prerequisite to a file task on the target directory.
134       #
135       # For example:
136       #   package :zip, :classifier=>'docs', :include=>javadoc.target
137       def into(path)
138         @target = file(path.to_s).enhance([self]) unless @target && @target.to_s == path.to_s
139         self
140       end
141 
142       # :call-seq:
143       #   include(*files) => self
144       #
145       # Includes additional source files and directories when generating the documentation
146       # and returns self. When specifying a directory, includes all .java files in that directory.
147       def include(*files)
148         @files.include *files.flatten.compact
149         self
150       end
151 
152       # :call-seq:
153       #   exclude(*files) => self
154       #
155       # Excludes source files and directories from generating the documentation.
156       def exclude(*files)
157         @files.exclude *files
158         self
159       end
160 
161       # Classpath dependencies.
162       attr_accessor :classpath
163 
164       # :call-seq:
165       #   with(*artifacts) => self
166       #
167       # Adds files and artifacts as classpath dependencies, and returns self.
168       def with(*specs)
169         @classpath |= Buildr.artifacts(specs.flatten).uniq
170         self
171       end
172 
173       # Additional sourcepaths that are not part of the documented files.
174       attr_accessor :sourcepath
175         
176       # Returns the Javadoc options.
177       attr_reader :options
178 
179       # :call-seq:
180       #   using(options) => self
181       #
182       # Sets the Javadoc options from a hash and returns self.
183       #
184       # For example:
185       #   javadoc.using :windowtitle=>'My application'
186       def using(*args)
187         args.pop.each { |key, value| @options[key.to_sym] = value } if Hash === args.last
188         args.each { |key| @options[key.to_sym] = true }
189         self
190       end
191 
192       # :call-seq:
193       #   from(*sources) => self
194       #
195       # Includes files, directories and projects in the Javadoc documentation and returns self.
196       #
197       # You can call this method with Java source files and directories containing Java source files
198       # to include these files in the Javadoc documentation, similar to #include. You can also call
199       # this method with projects. When called with a project, it includes all the source files compiled
200       # by that project and classpath dependencies used when compiling.
201       #
202       # For example:
203       #   javadoc.from projects('myapp:foo', 'myapp:bar')
204       def from(*sources)
205         sources.flatten.each do |source|
206           case source
207           when Project
208             self.enhance source.prerequisites
209             self.include source.compile.sources
210             self.with source.compile.dependencies 
211           when Rake::Task, String
212             self.include source
213           else
214             fail "Don't know how to generate Javadocs from #{source || 'nil'}"
215           end
216         end
217         self
218       end
219 
220       def prerequisites() #:nodoc:
221         super + @files + classpath + sourcepath
222       end
223 
224       def source_files() #:nodoc:
225         @source_files ||= @files.map(&:to_s).
226           map { |file| File.directory?(file) ? FileList[File.join(file, "**/*.java")] : file }.
227           flatten.reject { |file| @files.exclude?(file) }
228       end
229 
230       def needed?() #:nodoc:
231         return false if source_files.empty?
232         return true unless File.exist?(target.to_s)
233         source_files.map { |src| File.stat(src.to_s).mtime }.max > File.stat(target.to_s).mtime
234       end
235 
236     private
237 
238       def generate(sources, target, options = {})
239         cmd_args = [ '-d', target, Buildr.application.options.trace ? '-verbose' : '-quiet' ]
240         options.reject { |key, value| [:sourcepath, :classpath].include?(key) }.
241           each { |key, value| value.invoke if value.respond_to?(:invoke) }.
242           each do |key, value|
243             case value
244             when true, nil
245               cmd_args << "-#{key}"
246             when false
247               cmd_args << "-no#{key}"
248             when Hash
249               value.each { |k,v| cmd_args << "-#{key}" << k.to_s << v.to_s }
250             else
251               cmd_args += Array(value).map { |item| ["-#{key}", item.to_s] }.flatten
252             end
253           end
254         [:sourcepath, :classpath].each do |option|
255           Array(options[option]).flatten.tap do |paths|
256             cmd_args << "-#{option}" << paths.flatten.map(&:to_s).join(File::PATH_SEPARATOR) unless paths.empty?
257           end
258         end
259         cmd_args += sources.flatten.uniq
260         unless Buildr.application.options.dryrun
261           info "Generating Javadoc for #{name}"
262           trace (['javadoc'] + cmd_args).join(' ')
263           Java.load
264           Java.com.sun.tools.javadoc.Main.execute(cmd_args.to_java(Java.java.lang.String)) == 0 or
265             fail 'Failed to generate Javadocs, see errors above'
266         end
267       end
268 
269     end
270 
271 
272     include Extension
273 
274     first_time do
275       desc 'Create the Javadocs for this project'
276       Project.local_task('javadoc')
277     end
278 
279     before_define do |project|
280       JavadocTask.define_task('javadoc').tap do |javadoc|
281         javadoc.into project.path_to(:target, :javadoc)
282         javadoc.using :windowtitle=>project.comment || project.name
283       end
284     end
285 
286     after_define do |project|
287       project.javadoc.from project
288     end
289 
290     # :call-seq:
291     #   javadoc(*sources) => JavadocTask
292     #
293     # This method returns the project's Javadoc task. It also accepts a list of source files,
294     # directories and projects to include when generating the Javadocs.
295     #
296     # By default the Javadoc task uses all the source directories from compile.sources and generates
297     # Javadocs in the target/javadoc directory. This method accepts sources and adds them by calling
298     # JavadocsTask#from.
299     #
300     # For example, if you want to generate Javadocs for a given project that includes all source files
301     # in two of its sub-projects:
302     #   javadoc projects('myapp:foo', 'myapp:bar').using(:windowtitle=>'Docs for foo and bar')
303     def javadoc(*sources, &block)
304       task('javadoc').from(*sources).enhance &block
305     end
306 
307   end
308 
309 
310   # Methods added to Project to support the Java Annotation Processor.
311   module Apt
312 
313     # :call-seq:
314     #   apt(*sources) => task
315     #
316     # Returns a task that will use Java#apt to generate source files in target/generated/apt,
317     # from all the source directories passed as arguments. Uses the compile.sources list if
318     # on arguments supplied.
319     #
320     # For example:
321     #
322     def apt(*sources)
323       sources = compile.sources if sources.empty?
324       file(path_to(:target, 'generated/apt')=>sources) do |task|
325         cmd_args = [ Buildr.application.options.trace ? '-verbose' : '-nowarn' ]
326         cmd_args << '-nocompile' << '-s' << task.name
327         cmd_args << '-source' << compile.options.source if compile.options.source
328         classpath = Buildr.artifacts(compile.dependencies).map(&:to_s).each { |t| task(t).invoke }
329         cmd_args << '-classpath' << classpath.join(File::PATH_SEPARATOR) unless classpath.empty?
330         cmd_args += (sources.map(&:to_s) - [task.name]).
331           map { |file| File.directory?(file) ? FileList["#{file}/**/*.java"] : file }.flatten
332         unless Buildr.application.options.dryrun
333           info 'Running apt'
334           trace (['apt'] + cmd_args).join(' ')
335           Java.com.sun.tools.apt.Main.process(cmd_args.to_java(Java.java.lang.String)) == 0 or
336             fail 'Failed to process annotations, see errors above'
337         end
338       end
339     end
340 
341   end
342 
343 end
344 
345 Buildr::Compiler << Buildr::Compiler::Javac
346 class Buildr::Project
347   include Buildr::Javadoc
348   include Buildr::Apt
349 end

Generated using the rcov code coverage analysis tool for Ruby version 0.8.2.1.

Valid XHTML 1.0! Valid CSS!