简体   繁体   中英

How to generate java's hashcode method in intellij with its members sorted?

I want to generate the the java equals() and hashcode() method in Intellij. I want it to generate the way how eclipse generates. I have created a custom template to do this. But, in the hashcode method generated by intellij, the members are in the order they are listed, not alphabetically. But eclipse sorts them alphabetically when generating equals and hashcode method. I need to achieve this in Intellij. I couldn't find any sort function to use. Any help or pointers will be greatly appreciated. Thank you

This request was already submitted for IntelliJ IDEA and it was declined with the following comment :

We assume that you have ordered your members in a meaningful order within the file, and that preserving this order would be more meaningful than alphabetic sorting.

Based on @CrazyCoder's suggestion , I developed a macro for intellij.

#macro(sort $array)
  #set($size=$array.size())
  #set($index1=0)
  #foreach($element1 in $array)
    #set($index2=0)
    #set($minElement=$element1)
    #set($minElementIndex=$index1)
    #foreach($element2 in $array)
      #if($index2>$index1)
        #if($minElement.name.compareTo($element2.name)>0)
          #set($minElement=$element2)
          #set($minElementIndex=$index2)
        #end
      #end
      #set($index2=$index2+1)
    #end
    #set($temp=$array.get($index1))
    #set($junk=$array.set($index1, $minElement))
    #set($junk=$array.set($minElementIndex, $temp))
    #set($index1=$index1+1)
  #end
#end

Works good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM