Skip to content

Commit 336f469

Browse files
committed
Ensure that private class methods are private
Previously, a number of class methods were incorrectly scoped by using the instance `private` method. As pointed out in the issue, this doesn't actually hide them. This change uses the class-reopen style to actually scope them. [#59657214]
1 parent 2bd01bd commit 336f469

26 files changed

Lines changed: 497 additions & 453 deletions

.idea/dictionaries/bhale.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/extending-repositories.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ The main class used when dealing with a repository is [`JavaBuildpack::Repositor
3030
# @param [Block, nil] version_validator an optional version validation block
3131
# @return [JavaBuildpack::Util::TokenizedVersion] the chosen version of the file
3232
# @return [String] the URI of the chosen version of the file
33-
def self.find_item(configuration, &version_validator)
33+
def find_item(configuration, &version_validator)
3434
```
3535

3636
Usage of the class might look like the following:

docs/security.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Security
2-
In addition to security considerations associated with containers, frameworks, and JREs the
3-
following points pertain to the security of the buildpack itself.
2+
In addition to security considerations associated with containers, frameworks, and JREs the following points pertain to the security of the buildpack itself.
43

54
## Buildpack Forks
6-
If you fork the Java buildpack, it is important to keep the fork up to date with the
7-
original repository. This will ensure that your fork runs with any security fixes that may be necessary.
5+
If you fork the Java buildpack, it is important to keep the fork up to date with the original repository. This will ensure that your fork runs with any security fixes that may be necessary.
86

97
## Security and Logs
108
See [Sensitive Information in Logs][].

lib/java_buildpack/buildpack.rb

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,6 @@ module JavaBuildpack
3333
# Encapsulates the detection, compile, and release functionality for Java application
3434
class Buildpack
3535

36-
# Main entry to the buildpack. Initializes the buildpack and all of its dependencies and yields a new instance
37-
# to any given block. Any exceptions thrown as part of the buildpack setup or execution are handled
38-
#
39-
# @param [String] app_dir the path of the application directory
40-
# @param [String] message an error message with an insert for the reason for failure
41-
# @yield [Buildpack] the buildpack to work with
42-
# @return [Object] the return value from the given block
43-
def self.with_buildpack(app_dir, message)
44-
app_dir = Pathname.new(File.expand_path(app_dir))
45-
Logging::LoggerFactory.setup app_dir
46-
application = Component::Application.new(app_dir)
47-
48-
yield new(app_dir, application) if block_given?
49-
rescue => e
50-
logger = Logging::LoggerFactory.get_logger Buildpack
51-
52-
logger.error { message % e.inspect }
53-
logger.debug { "Exception #{e.inspect} backtrace:\n#{e.backtrace.join("\n")}" }
54-
abort e.message
55-
end
56-
5736
# Iterates over all of the components to detect if this buildpack can be used to run an application
5837
#
5938
# @return [Array<String>] An array of strings that identify the components and versions that will be used to run
@@ -187,6 +166,31 @@ def tag_detection(type, components, unique)
187166
tags
188167
end
189168

169+
class << self
170+
171+
# Main entry to the buildpack. Initializes the buildpack and all of its dependencies and yields a new instance
172+
# to any given block. Any exceptions thrown as part of the buildpack setup or execution are handled
173+
#
174+
# @param [String] app_dir the path of the application directory
175+
# @param [String] message an error message with an insert for the reason for failure
176+
# @yield [Buildpack] the buildpack to work with
177+
# @return [Object] the return value from the given block
178+
def with_buildpack(app_dir, message)
179+
app_dir = Pathname.new(File.expand_path(app_dir))
180+
Logging::LoggerFactory.setup app_dir
181+
application = Component::Application.new(app_dir)
182+
183+
yield new(app_dir, application) if block_given?
184+
rescue => e
185+
logger = Logging::LoggerFactory.get_logger Buildpack
186+
187+
logger.error { message % e.inspect }
188+
logger.debug { "Exception #{e.inspect} backtrace:\n#{e.backtrace.join("\n")}" }
189+
abort e.message
190+
end
191+
192+
end
193+
190194
end
191195

192196
end

lib/java_buildpack/jre/memory/memory_bucket.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class MemoryBucket
4444
# @param [MemoryRange, nil] range a user-specified range for the memory bucket or nil if the user did not specify a
4545
# range
4646
def initialize(name, weighting, range)
47-
@name = MemoryBucket.validate_name name
47+
@name = validate_name name
4848
@weighting = validate_weighting weighting
4949
@range = range ? validate_memory_range(range) : nil
5050
logger = JavaBuildpack::Logging::LoggerFactory.get_logger MemoryBucket
@@ -55,13 +55,13 @@ def initialize(name, weighting, range)
5555

5656
private
5757

58-
def self.validate_name(name)
58+
def validate_name(name)
5959
fail "Invalid MemoryBucket name '#{name}'" if name.nil? || name.to_s.size == 0
6060
name
6161
end
6262

6363
def validate_weighting(weighting)
64-
fail diagnose_weighting(weighting, 'not numeric') unless MemoryBucket.is_numeric weighting
64+
fail diagnose_weighting(weighting, 'not numeric') unless is_numeric weighting
6565
fail diagnose_weighting(weighting, 'negative') if weighting < 0
6666
weighting
6767
end
@@ -70,7 +70,7 @@ def diagnose_weighting(weighting, reason)
7070
"Invalid weighting '#{@weighting}' for #{identify} : #{reason}"
7171
end
7272

73-
def self.is_numeric(w)
73+
def is_numeric(w)
7474
Float(w) rescue false
7575
end
7676

lib/java_buildpack/jre/memory/memory_limit.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ module JavaBuildpack::Jre
2222
# A utility for handling Java memory settings.
2323
class MemoryLimit
2424

25-
# Returns the application's memory limit.
26-
#
27-
# @return [MemorySize, nil] the application's memory limit or nil if no memory limit has been provided
28-
def self.memory_limit
29-
memory_limit = ENV['MEMORY_LIMIT']
30-
return nil unless memory_limit
31-
memory_limit_size = MemorySize.new(memory_limit)
32-
fail "Invalid negative $MEMORY_LIMIT #{memory_limit}" if memory_limit_size < 0
33-
memory_limit_size
25+
private_class_method :new
26+
27+
class << self
28+
29+
# Returns the application's memory limit.
30+
#
31+
# @return [MemorySize, nil] the application's memory limit or nil if no memory limit has been provided
32+
def memory_limit
33+
memory_limit = ENV['MEMORY_LIMIT']
34+
return nil unless memory_limit
35+
memory_limit_size = MemorySize.new(memory_limit)
36+
fail "Invalid negative $MEMORY_LIMIT #{memory_limit}" if memory_limit_size < 0
37+
memory_limit_size
38+
end
39+
3440
end
3541

3642
end

lib/java_buildpack/jre/memory/memory_range.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ class MemoryRange
3636
def initialize(value, ceiling = nil)
3737
if value.is_a? String
3838
fail "Invalid combination of parameter types #{value.class} and #{ceiling.class}" unless ceiling.nil?
39-
lower_bound, upper_bound = MemoryRange.get_bounds(value)
39+
lower_bound, upper_bound = get_bounds(value)
4040
@floor = create_memory_size lower_bound
4141
@ceiling = upper_bound ? create_memory_size(upper_bound) : nil
4242
else
43-
MemoryRange.validate_memory_size value
44-
MemoryRange.validate_memory_size ceiling unless ceiling.nil?
43+
validate_memory_size value
44+
validate_memory_size ceiling unless ceiling.nil?
4545
@floor = value
4646
@ceiling = ceiling
4747
end
@@ -113,7 +113,7 @@ def to_s
113113

114114
RANGE_SEPARATOR = '..'
115115

116-
def self.get_bounds(range)
116+
def get_bounds(range)
117117
if range.index(RANGE_SEPARATOR)
118118
lower_bound, upper_bound = range.split(RANGE_SEPARATOR)
119119
lower_bound = '0' if lower_bound.nil? || lower_bound == ''
@@ -127,7 +127,7 @@ def create_memory_size(size)
127127
MemorySize.new(size)
128128
end
129129

130-
def self.validate_memory_size(size)
130+
def validate_memory_size(size)
131131
fail "Invalid MemorySize parameter of type #{size.class}" unless size.is_a? MemorySize
132132
end
133133

lib/java_buildpack/jre/memory/memory_size.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def initialize(size)
3232
fail "Invalid memory size '#{size}'" if !size || size.length < 2
3333
unit = size[-1]
3434
v = size[0..-2]
35-
fail "Invalid memory size '#{size}'" unless MemorySize.is_integer v
35+
fail "Invalid memory size '#{size}'" unless is_integer v
3636
v = size.to_i
3737

3838
# Store the number of bytes.
@@ -101,7 +101,7 @@ def +(other)
101101
# @return [MemorySize] the result
102102
def *(other)
103103
fail "Cannot multiply a Memory size by an instance of #{other.class}" unless other.is_a? Numeric
104-
MemorySize.from_numeric((@bytes * other).round)
104+
from_numeric((@bytes * other).round)
105105
end
106106

107107
# Subtract a memory size from this memory size.
@@ -121,7 +121,7 @@ def -(other)
121121
# @return [MemorySize, Numeric] the result
122122
def /(other)
123123
return @bytes / other.bytes.to_f if other.is_a? MemorySize
124-
return MemorySize.from_numeric((@bytes / other.to_f).round) if other.is_a? Numeric
124+
return from_numeric((@bytes / other.to_f).round) if other.is_a? Numeric
125125
fail "Cannot divide a MemorySize by an instance of #{other.class}"
126126
end
127127

@@ -137,24 +137,24 @@ def /(other)
137137

138138
def memory_size_operation(other)
139139
fail "Invalid parameter: instance of #{other.class} is not a MemorySize" unless other.is_a? MemorySize
140-
MemorySize.from_numeric(yield @bytes, other.bytes)
140+
from_numeric(yield @bytes, other.bytes)
141141
end
142142

143-
def self.is_integer(v)
143+
def is_integer(v)
144144
f = Float(v)
145145
f && f.floor == f
146146
rescue
147147
false
148148
end
149149

150-
def self.from_numeric(n)
150+
def from_numeric(n)
151151
MemorySize.new("#{n.to_s}B")
152152
end
153153

154154
public
155155

156156
# Zero byte memory size
157-
ZERO = from_numeric 0
157+
ZERO = MemorySize.new('0B')
158158

159159
end
160160

0 commit comments

Comments
 (0)