Skip to content

Instantly share code, notes, and snippets.

@kaansoral
Created July 6, 2015 05:55

Revisions

  1. kaansoral created this gist Jul 6, 2015.
    45 changes: 45 additions & 0 deletions find_java_import.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    import os,zipfile,sys

    #simplified: https://github.com/MDeiml/SublimeJavaImports/blob/master/JavaAddImport.py
    #NEWIDEA: an exhaustive search option that searches the files [06/07/15]

    paths=[
    "/Users/kaan/.cordova/lib/android/cordova/master/framework/src/",
    "/usr/local/var/lib/android-sdk/platforms/android-22/android.jar",
    "/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/src.zip"
    ]

    def get_classes_list(path):
    if path.endswith(".zip") or path.endswith(".jar"):
    zipF = zipfile.ZipFile(path, "r")
    classesList = zipF.namelist()
    zipF.close()
    return classesList
    else:
    classesList = []
    for root, dirs, files in os.walk(path):
    for filename in files:
    classesList.append((root+"/"+filename)[len(path):])
    return classesList

    def find_import(className):
    classesList = []
    for path in paths:
    classesList = classesList + get_classes_list(path)

    results = []
    for name in classesList:
    if name.endswith("/"+className+".java") or name.endswith("\\"+className+".java") or name.endswith("/"+className+".class") or name.endswith("\\"+className+".class"):
    result = name.replace("/",".").replace("\\",".").replace(".java","").replace(".class", "")
    if result.startswith("."):
    result = result[1:]
    results.append(result)

    if len(results):
    for result in results:
    print result
    else:
    print "There is no such class: "+className

    for name in sys.argv:
    if not name.endswith(".py"): find_import(name)