Skip to content

Instantly share code, notes, and snippets.

@kaansoral
Created July 6, 2015 05:55
Show Gist options
  • Save kaansoral/aed8c5d56f262ed1d63a to your computer and use it in GitHub Desktop.
Save kaansoral/aed8c5d56f262ed1d63a to your computer and use it in GitHub Desktop.
Finds Java Imports - Python Snippet
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment