0

I am trying to create a subclass in jruby which overrides the keys method from the super java class properties cause i want to sort the entries in the properties class by the keys:

class SortedProperties < java.util.Properties  
 def keys
  keysEnum = super().keys()
  keyList = java.util.Vector.new
  keysEnum.elements.each do |element|
    puts element.to_s
    keyList.add(element.to_java(:String))
  end
  java.util.Collections.sort(keyList)
  puts keyList.elements().to_s
  return keyList.elements()
 end
end

I am doing definitly something wrong with the super statement but i don't know what. Searched a lot but can't find anything that points me to the correct call of the method keys() in the super class properties.

1 Answer 1

1

you need make your brain "forget" the Java style super syntax and simply do it the Ruby way e.g.

class SortedProperties < java.util.Properties
 def keys
  keyList = java.util.Vector.new
  super.each do |element|
    keyList.add(element.to_java(:String))
  end
  java.util.Collections.sort(keyList)
  return keyList.elements()
 end
end

props = java.util.Properties.new
props.setProperty 'bbb', 'B'
props.setProperty 'aaa', 'A'
props.setProperty 'ccc', 'C'

props.store java.lang.System.out, ' raw-properties'

props = SortedProperties.new
props.setProperty 'bbb', 'B'
props.setProperty 'aaa', 'A'
props.setProperty 'ccc', 'C'

props.store java.lang.System.out, ' sorted-properties'

calling super already returns a result and it might be already Ruby converted by JRuby, even if it is not Java Enumeration/Collection are all Ruby Enumerable (or at least have a each method).

Sign up to request clarification or add additional context in comments.

3 Comments

thanks! this is exactly what i am searching for. another short question...do you know what i need to do to store comments with an leading "#" character in my properties? these will be ignored by the default java properties and will be removed
I do not - would ask a separate (likely Java only) question for that ... sure someone will confirm if that's possible. but otherwise I would likely just use "pure" (J)Ruby to generate .properties files ... as it seems you're wasting time hacking java.util.Properties
Yes i think you are right. Using the JRuby properties files would be more efficient. Thanks for you help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.