mirror of
https://github.com/elastic/logstash.git
synced 2025-04-25 07:07:54 -04:00
fix for Java 8 Map merge method conflict
typo DRYied code, comments, cleanup added spec specs DRYing also test HashMap, refactor with shared_example fixes logstash-plugins/logstash-filter-multiline#10
This commit is contained in:
parent
ae17b4160d
commit
cbb225db35
2 changed files with 53 additions and 9 deletions
|
@ -24,24 +24,39 @@ class Hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# map_mixin to patch LinkedHashMap and HashMap. it must be done directly on the classes,
|
||||||
|
# using a module mixin does not work, and injecting in the Map interface does not work either
|
||||||
|
# but injecting in the class works.
|
||||||
|
|
||||||
# this is a temporary fix to solve a bug in JRuby where classes implementing the Map interface, like LinkedHashMap
|
map_mixin = lambda do
|
||||||
# have a bug in the has_key? method that is implemented in the Enumerable module that is somehow mixed in the Map interface.
|
# this is a temporary fix to solve a bug in JRuby where classes implementing the Map interface, like LinkedHashMap
|
||||||
# this bug makes has_key? (and all its aliases) return false for a key that has a nil value.
|
# have a bug in the has_key? method that is implemented in the Enumerable module that is somehow mixed in the Map interface.
|
||||||
# Only LinkedHashMap is patched here because patching the Map interface is not working.
|
# this bug makes has_key? (and all its aliases) return false for a key that has a nil value.
|
||||||
# TODO find proper fix, and submit upstream
|
# Only LinkedHashMap is patched here because patching the Map interface is not working.
|
||||||
# releavant JRuby files:
|
# TODO find proper fix, and submit upstream
|
||||||
# https://github.com/jruby/jruby/blob/master/core/src/main/ruby/jruby/java/java_ext/java.util.rb
|
# releavant JRuby files:
|
||||||
# https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/java/proxies/MapJavaProxy.java
|
# https://github.com/jruby/jruby/blob/master/core/src/main/ruby/jruby/java/java_ext/java.util.rb
|
||||||
class Java::JavaUtil::LinkedHashMap
|
# https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/java/proxies/MapJavaProxy.java
|
||||||
def has_key?(key)
|
def has_key?(key)
|
||||||
self.containsKey(key)
|
self.containsKey(key)
|
||||||
end
|
end
|
||||||
alias_method :include?, :has_key?
|
alias_method :include?, :has_key?
|
||||||
alias_method :member?, :has_key?
|
alias_method :member?, :has_key?
|
||||||
alias_method :key?, :has_key?
|
alias_method :key?, :has_key?
|
||||||
|
|
||||||
|
# Java 8 Map implements a merge method with a different signature from
|
||||||
|
# the Ruby Hash#merge. see https://github.com/jruby/jruby/issues/1249
|
||||||
|
# this can be removed when fixed upstream
|
||||||
|
if ENV_JAVA['java.specification.version'] >= '1.8'
|
||||||
|
def merge(other)
|
||||||
|
dup.merge!(other)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Java::JavaUtil::LinkedHashMap.module_exec(&map_mixin)
|
||||||
|
Java::JavaUtil::HashMap.module_exec(&map_mixin)
|
||||||
|
|
||||||
module java::util::Map
|
module java::util::Map
|
||||||
# have Map objects like LinkedHashMap objects report is_a?(Array) == true
|
# have Map objects like LinkedHashMap objects report is_a?(Array) == true
|
||||||
def is_a?(clazz)
|
def is_a?(clazz)
|
||||||
|
|
|
@ -50,6 +50,35 @@ describe "Java integration" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "Java::JavaUtil::Map" do
|
||||||
|
# this is to test the Java 8 Map interface change for the merge method
|
||||||
|
|
||||||
|
let(:merger){{:a => 1, :b => 2}}
|
||||||
|
let(:mergee){{:b => 3, :c => 4}}
|
||||||
|
|
||||||
|
shared_examples "map merge" do
|
||||||
|
it "should support merging" do
|
||||||
|
expect(subject.merge(mergee)).to eq({:a => 1, :b => 3, :c => 4})
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return a new hash and not change original hash" do
|
||||||
|
expect{subject.merge(mergee)}.to_not change{subject}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with Java::JavaUtil::LinkedHashMap" do
|
||||||
|
it_behaves_like "map merge" do
|
||||||
|
subject{Java::JavaUtil::LinkedHashMap.new(merger)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with Java::JavaUtil::HashMap" do
|
||||||
|
it_behaves_like "map merge" do
|
||||||
|
subject{Java::JavaUtil::HashMap.new(merger)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "Java::JavaUtil::Collection" do
|
context "Java::JavaUtil::Collection" do
|
||||||
subject{Java::JavaUtil::ArrayList.new(initial_array)}
|
subject{Java::JavaUtil::ArrayList.new(initial_array)}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue