| Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
|---|---|---|---|---|
| lib/buildr/java/rjb.rb | 154 | 60 | 26.62%
|
41.67%
|
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.
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 'rjb' |
18 |
19 |
20 # Equivalent to Java system properties. For example: |
21 # ENV_JAVA['java.version'] |
22 # ENV_JAVA['java.class.version'] |
23 ENV_JAVA = {} |
24 |
25 |
26 # Buildr runs along side a JVM, using either RJB or JRuby. The Java module allows |
27 # you to access Java classes and create Java objects. |
28 # |
29 # Java classes are accessed as static methods on the Java module, for example: |
30 # str = Java.java.lang.String.new('hai!') |
31 # str.toUpperCase |
32 # => 'HAI!' |
33 # Java.java.lang.String.isInstance(str) |
34 # => true |
35 # Java.com.sun.tools.javac.Main.compile(args) |
36 # |
37 # The classpath attribute allows Buildr to add JARs and directories to the classpath, |
38 # for example, we use it to load Ant and various Ant tasks, code generators, test |
39 # frameworks, and so forth. |
40 # |
41 # When using an artifact specification, Buildr will automatically download and |
42 # install the artifact before adding it to the classpath. |
43 # |
44 # For example, Ant is loaded as follows: |
45 # Java.classpath << 'org.apache.ant:ant:jar:1.7.0' |
46 # |
47 # Artifacts can only be downloaded after the Buildfile has loaded, giving it |
48 # a chance to specify which remote repositories to use, so adding to classpath |
49 # does not by itself load any libraries. You must call Java.load before accessing |
50 # any Java classes to give Buildr a chance to load the libraries specified in the |
51 # classpath. |
52 # |
53 # When building an extension, make sure to follow these rules: |
54 # 1. Add to the classpath when the extension is loaded (i.e. in module or class |
55 # definition), so the first call to Java.load anywhere in the code will include |
56 # the libraries you specify. |
57 # 2. Call Java.load once before accessing any Java classes, allowing Buildr to |
58 # set up the classpath. |
59 # 3. Only call Java.load when invoked, otherwise you may end up loading the JVM |
60 # with a partial classpath, or before all remote repositories are listed. |
61 # 4. Check on a clean build with empty local repository. |
62 module Java |
63 |
64 module Package #:nodoc: |
65 |
66 def method_missing(sym, *args, &block) |
67 raise ArgumentError, 'No arguments expected' unless args.empty? |
68 name = "#{@name}.#{sym}" |
69 return ::Rjb.import(name) if sym.to_s =~ /^[[:upper:]]/ |
70 ::Java.send :__package__, name |
71 end |
72 |
73 end |
74 |
75 # On OS X we know where the default JDK is. We can try to guess for other OS. |
76 # We set JAVA_HOME early so we can use it without calling Java.load first. |
77 ENV['JAVA_HOME'] ||= '/System/Library/Frameworks/JavaVM.framework/Home' if Config::CONFIG['host_os'] =~ /darwin/i |
78 |
79 class << self |
80 |
81 # Returns the classpath, an array listing directories, JAR files and |
82 # artifacts. Use when loading the extension to add any additional |
83 # libraries used by that extension. |
84 # |
85 # For example, Ant is loaded as follows: |
86 # Java.classpath << 'org.apache.ant:ant:jar:1.7.0' |
87 def classpath |
88 @classpath ||= [] |
89 end |
90 |
91 # Most platforms requires tools.jar to be on the classpath, tools.jar contains the |
92 # Java compiler (OS X and AIX are two exceptions we know about, may be more). |
93 # Guess where tools.jar is from JAVA_HOME, which hopefully points to the JDK, |
94 # but maybe the JRE. Return nil if not found. |
95 def tools_jar #:nodoc: |
96 @tools_jar ||= begin |
97 home = ENV['JAVA_HOME'] or fail 'Are we forgetting something? JAVA_HOME not set.' |
98 ['lib/tools.jar', '../lib/tools.jar'].map { |path| File.expand_path(path, home) }. |
99 find { |path| File.exist?(path) } |
100 end |
101 end |
102 |
103 # Loads the JVM and all the libraries listed on the classpath. Call this |
104 # method before accessing any Java class, but only call it from methods |
105 # used in the build, giving the Buildfile a chance to load all extensions |
106 # that append to the classpath and specify which remote repositories to use. |
107 def load |
108 return self if @loaded |
109 classpath << tools_jar if tools_jar |
110 |
111 classpath.map! { |path| Proc === path ? path.call : path } |
112 cp = Buildr.artifacts(classpath).map(&:to_s).each { |path| file(path).invoke } |
113 java_opts = (ENV['JAVA_OPTS'] || ENV['JAVA_OPTIONS']).to_s.split |
114 ::Rjb.load cp.join(File::PATH_SEPARATOR), java_opts |
115 |
116 props = ::Rjb.import('java.lang.System').getProperties |
117 enum = props.propertyNames |
118 while enum.hasMoreElements |
119 name = enum.nextElement.toString |
120 ENV_JAVA[name] = props.getProperty(name) |
121 end |
122 @loaded = true |
123 self |
124 end |
125 |
126 def method_missing(sym, *args, &block) #:nodoc: |
127 raise ArgumentError, 'No arguments expected' unless args.empty? |
128 Java.load # need to load RJB's classpath now! |
129 name = sym.to_s |
130 return ::Rjb.import(name) if name =~ /^[[:upper:]]/ |
131 __package__ name |
132 end |
133 |
134 private |
135 |
136 def __package__(name) #:nodoc: |
137 Module.new.tap do |m| |
138 m.extend Package |
139 m.instance_variable_set :@name, name |
140 end |
141 end |
142 |
143 end |
144 |
145 end |
146 |
147 |
148 class Array |
149 # Converts a Ruby array into a typed Java array, argument specifies the element type. |
150 # This is necessary for JRuby and causes no harm on RJB. |
151 def to_java(cls) |
152 map { |item| cls.new(item) } |
153 end |
154 end |
Generated on 2011-07-06 23:35:37 -0700 with rcov 0.9.8