#!/usr/bin/rubyrequire'find'require'colorize'require"fileutils"# extract arguements from command linedirToSearch=ARGV[0]keywordToSearch=ARGV[1].to_s.stripmatchedResultFile=ARGV[2]puts"dirToSearch=#{dirToSearch};keywordToSearch=#{keywordToSearch}, matchedResultFile=#{matchedResultFile}"# Eagerly create the result file so that user could use tools like `tail -f ` to observer the resultFileUtils.touch(matchedResultFile)puts"result will be outputted to #{matchedResultFile}"# Helper method to append content(each line) to the filedefappendLineContentToFile(lineContent,filePath)File.open(filePath,'a')do|file|file.puts"#{lineContent}"endend# write matched class file path along with bytecode content to the output file.defwriteResultInformation(classFilePath,byteCodeContent,outputFile)appendLineContentToFile(classFilePath,outputFile)# leave blank linesappendLineContentToFile("",outputFile)appendLineContentToFile("",outputFile)appendLineContentToFile("",outputFile)appendLineContentToFile(byteCodeContent,outputFile)# leave blank linesappendLineContentToFile("",outputFile)appendLineContentToFile("",outputFile)appendLineContentToFile("",outputFile)endFind.find(dirToSearch).select{|f|f.end_with?".class"}.each{|f|puts"checking #{f}"byteCodeContent=`javap -c #{f}`contains=byteCodeContent.include?keywordToSearchresultMessage=""ifcontainsresultMessage="#{f} contains #{keywordToSearch}".greenwriteResultInformation(f,byteCodeContent,matchedResultFile)elseresultMessage="#{f} does NOT contains #{keywordToSearch}".redendputsresultMessage}
catsample_intrinsics.txt./out/production/BytecodeSample/MainKt.classCompiledfrom"Main.kt"publicfinalclassMainKt{publicstaticfinalvoidmain();Code:0:ldc#11// String Hello2:invokestatic#17// Method StringExtKt.toConsole:(Ljava/lang/String;)V5:iconst_16:invokestatic#23// Method IntExtKt.increase:(I)I9:pop10:new#25// class Book13:dup14:invokespecial#28// Method Book."<init>":()V17:getfield#32// Field Book.name:Ljava/lang/String;20:dup21:ifnonnull2724:invokestatic#37// Method kotlin/jvm/internal/Intrinsics.throwNpe:()V27:invokevirtual#43// Method java/lang/String.toString:()Ljava/lang/String;30:pop31:returnpublicstaticvoidmain(java.lang.String[]);Code:0:invokestatic#9// Method main:()V3:return}./out/production/BytecodeSample/StringExtKt.classCompiledfrom"StringExt.kt"publicfinalclassStringExtKt{publicstaticfinalvoidtoConsole(java.lang.String);Code:0:aload_01:ldc#9// String $this$toConsole3:invokestatic#15// Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Object;Ljava/lang/String;)V6:iconst_07:istore_18:getstatic#21// Field java/lang/System.out:Ljava/io/PrintStream;11:aload_012:invokevirtual#27// Method java/io/PrintStream.println:(Ljava/lang/Object;)V15:return}
问题排查
123
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- colorize (LoadError) from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Users/androidyue/Documents/OneDrive/scripts//searchBytecode.rb:3:in `<main>'
需手动安装ruby gems依赖
1234567
➜ gem install colorize
YAML safe loading is not available. Please upgrade psych to a version that supports safe loading (>= 2.0).
Fetching: colorize-0.8.1.gem (100%)Successfully installed colorize-0.8.1
Parsing documentation for colorize-0.8.1
Installing ri documentation for colorize-0.8.1
1 gem installed