>>>>> "T" == Thomas R Corbin <tc / clark.net> writes: T> One thing I'd like to add is the ability to have the java import statements T> sorted by the package names. Also, I'd like to be able to ignore comments T> while scanning the java file. Well, I've very quickly look at it Some remarks T> #class Foo T> # @@foo = "Root" T> # @@bar = 66 T> # T> # cattr_reader :foo, :bar T> #end use instance variable class Foo @foo, @bar = "Root", 66 class << self attr_reader :foo, :bar end end p [Foo.foo, Foo.bar] T> #p [Foo.foo, Foo.bar] #=> ["Root", 66] $byname = false T> class Import [...] T> def <=>( other ) T> self.lineNumber <=> other.lineNumber T> end T> end def select $byname ? @line : @lineNumber end def <=>( other ) self.select <=> other.select end T> class ImportFilter T> @@totalExtraImports = 0 T> @@totalFiles = 0 T> @@totalAffectedFiles = 0 T> cattr_reader( :totalExtraImports, :totalFiles, :totalAffectedFiles ) @totalExtraImports = 0 @totalFiles = 0 @totalAffectedFiles = 0 class << self attr_accessor :totalExtraImports, :totalFiles, :totalAffectedFiles end T> @@totalFiles += 1 type.totalFiles += 1 T> end T> def process() T> File.open( @fileName ).each do |line| IO.foreach( @fileName) do |line| # this is the same T> def filter( line ) T> # T> # Remove any '//' based comments from this line T> # T> # T> # Break the line into separate words and look for things T> # that match import statements. T> # T> words = line.scan( /\w+/ ) T> words.each do |word| T> @hash.delete( word ) T> end T> end def filter( line ) line.sub('//.*', '').scan( /\w+/ ) do |word| @hash.delete( word ) end end T> def saveImport( line ) T> re = /^\s*import\s+(\w*\.)*(\w*)\s*;/ T> match = re.match( line ) T> return false if not match T> # T> # Get the class name. T> # T> className = match[2] T> # T> # Got a match, store it away in the hash table. T> # T> @hash[className] = Import.new( @fileName, @lineCount, line, T> className ) T> return true T> end def saveImport( line ) /^\s*import\s+(\w+\.)*(\w+)\s*;/ =~ line && @hash[$2] = Import.new( @fileName, @lineCount, line, $2) end T> def checkViolation( line ) T> re = /^\s*import\s+(\w*\.)*(\*)\s*;/ T> match = re.match( line ) T> return false if not match T> # T> # Get the class name. T> # T> className = match[2] T> # T> # Make sure this isn't a "*" import with your regexp, className is fatally == '*' T> # T> if className == "*" T> @violations[@violations.length] = Import.new( @fileName, T> @lineCount, T> line, T> className ) T> return true T> end T> end def checkViolation( line ) /^\s*import\s+(\w+\.)*(\*)\s*;/ =~ line && @violations << Import.new( @fileName, @lineCount, line, $2) end T> def reportViolations() T> # If there are no "relics" for this file, nothing to report T> return if @violations.length < 1 T> @violations.each do |entry| T> printf " VIOLATION - line:%d %s", T> entry.lineNumber, T> entry.line T> end T> end def reportViolations() return if @violations.empty? @violations.sort.each do |entry| printf " VIOLATION - line:%d %s", entry.lineNumber, entry.line end end T> def reportResults() T> # If there are no "relics" for this file, nothing to report T> return if @hash.length < 1 T> # Ok, this file is affected. T> @@totalAffectedFiles += 1 T> @@totalExtraImports += @hash.length T> @extraImports = @hash.length T> list = Array.new T> @hash.each do |key, value| T> list.push( value ) T> end T> list.sort!() T> list.each do |entry| T> printf " line:%d %s", entry.lineNumber, entry.line T> end T> end def reportResults() return if @hash.empty? type.totalAffectedFiles += 1 type.totalExtraImports += @hash.length @extraImports = @hash.length @hash.values.sort.each do |entry| printf " line:%d %s", entry.lineNumber, entry.line end end T> def buildLineList() T> list = [] T> @hash.each do |key, value| T> list.push( value.lineNumber ) T> end T> return list T> end def buildLineList() @hash.values.map {|v| v.lineNumber } end T> File.open( @backupName ).each do |line| IO.foreach( @backupName ) do |line| T> lineCount += 1 T> opts = GetoptLong.new( T> ["--find", "-f", GetoptLong::NO_ARGUMENT], T> ["--recurse", "-r", GetoptLong::NO_ARGUMENT], T> ["--modify", "-m", GetoptLong::NO_ARGUMENT], T> ["--verbose", "-v", GetoptLong::NO_ARGUMENT], ["--sortbyname", "-n", GetoptLong::NO_ARGUMENT], T> ["--backup", "-i", GetoptLong::REQUIRED_ARGUMENT] T> ) T> when "--backup" then T> backup = true T> backupExtension = arg when "--sortbyname" then $byname = true T> end T> end Guy Decoux