📄 classpath.jrag
字号:
protected String fullName; long age; Program program; protected PathPart() { } protected boolean isSource; protected String fileSuffix() { return isSource ? ".java" : ".class"; } public static PathPart createSourcePath(String fileName, Program program) { PathPart p = createPathPart(fileName); if(p != null) { p.isSource = true; p.program = program; } return p; } public static PathPart createClassPath(String fileName, Program program) { PathPart p = createPathPart(fileName); if(p != null) { p.isSource = false; p.program = program; } return p; } private static PathPart createPathPart(String s) { try { File f = new File(s); if(f.isDirectory()) return new FolderPart(f); else if(f.isFile()) return new ZipFilePart(new ZipFile(f)); } catch (IOException e) { // error in path } return null; } // is there a package with the specified name on this path part public boolean hasPackage(String name) { return false; } // select a compilation unit from a canonical name // returns true of the compilation unit exists on this path public boolean selectCompilationUnit(String canonicalName) throws IOException { return false; } // load the return currently selected compilation unit public CompilationUnit getCompilationUnit() { long startTime = System.currentTimeMillis(); if(!isSource) { try { if(Program.verbose()) System.out.print("Loading .class file: " + fullName + " "); CompilationUnit u = program.bytecodeReader.read(is, fullName, program); //CompilationUnit u = new bytecode.Parser(is, fullName).parse(null, null, program); u.setPathName(pathName); u.setRelativeName(relativeName); u.setFromSource(false); is.close(); is = null; if(Program.verbose()) System.out.println("from " + pathName + " in " + (System.currentTimeMillis() - startTime) + " ms"); return u; } catch (Exception e) { throw new Error("Error loading " + fullName, e); } } else { try { if(Program.verbose()) System.out.print("Loading .java file: " + fullName + " "); CompilationUnit u = program.javaParser.parse(is, fullName); is.close(); is = null; u.setPathName(pathName); u.setRelativeName(relativeName); u.setFromSource(true); if(Program.verbose()) System.out.println("in " + (System.currentTimeMillis() - startTime) + " ms"); return u; } catch (Exception e) { System.err.println("Unexpected error of kind " + e.getClass().getName()); throw new Error(fullName + ": " + e.getMessage(), e); } } } } // load files from a folder class FolderPart extends PathPart { private HashMap map = new HashMap(); private File folder; public FolderPart(File folder) { this.folder = folder; } public boolean hasPackage(String name) { return filesInPackage(name) != null; } public boolean hasCompilationUnit(String canonicalName) { int index = canonicalName.lastIndexOf('.'); String packageName = index == -1 ? "" : canonicalName.substring(0, index); String typeName = canonicalName.substring(index + 1, canonicalName.length()); Collection c = filesInPackage(packageName); boolean result = c != null && c.contains(typeName + fileSuffix()); return result; } private Collection filesInPackage(String packageName) { if(!map.containsKey(packageName)) { File f = new File(folder, packageName.replace('.', File.separatorChar)); Collection c = Collections.EMPTY_LIST; if(f.exists() && f.isDirectory()) { String[] files = f.list(); if(files.length > 0) { c = new HashSet(); for(int i = 0; i < files.length; i++) c.add(files[i]); } } else c = null; map.put(packageName, c); } return (Collection)map.get(packageName); } public boolean selectCompilationUnit(String canonicalName) throws IOException { if(hasCompilationUnit(canonicalName)) { String fileName = canonicalName.replace('.', File.separatorChar); File classFile = new File(folder, fileName + fileSuffix()); if(classFile.isFile()) { is = new FileInputStream(classFile); age = classFile.lastModified(); pathName = classFile.getAbsolutePath(); relativeName = fileName + fileSuffix(); fullName = canonicalName; return true; } } return false; } } // load files in a zip file class ZipFilePart extends PathPart { private HashSet set = new HashSet(); private ZipFile file; public boolean hasPackage(String name) { return set.contains(name); } public ZipFilePart(ZipFile file) { this.file = file; // process all entries in the zip file for (Enumeration e = file.entries() ; e.hasMoreElements() ;) { ZipEntry entry = (ZipEntry)e.nextElement(); String pathName = new File(entry.getName()).getParent(); if(pathName != null) pathName = pathName.replace(File.separatorChar, '.'); if(!set.contains(pathName)) { int pos = 0; while(pathName != null && -1 != (pos = pathName.indexOf('.', pos + 1))) { String n = pathName.substring(0, pos); if(!set.contains(n)) { set.add(n); } } set.add(pathName); } set.add(entry.getName()); } } public boolean selectCompilationUnit(String canonicalName) throws IOException { String name = canonicalName.replace('.', '/'); // ZipFiles do always use '/' as separator name = name + fileSuffix(); if(set.contains(name)) { ZipEntry zipEntry = file.getEntry(name); if(zipEntry != null && !zipEntry.isDirectory()) { is = file.getInputStream(zipEntry); age = zipEntry.getTime(); pathName = file.getName(); relativeName = name + fileSuffix(); fullName = canonicalName; return true; } } return false; } } // load files specified explicitly (on the command line) class FileNamesPart extends PathPart { private HashMap sourceFiles = new HashMap(); private HashSet packages = new HashSet(); public FileNamesPart(Program p) { isSource = true; program = p; } public boolean hasPackage(String name) { return packages.contains(name); } public boolean isEmpty() { return sourceFiles.isEmpty(); } public Collection keySet() { return sourceFiles.keySet(); } public boolean selectCompilationUnit(String canonicalName) throws IOException { if(sourceFiles.containsKey(canonicalName)) { String f = (String)sourceFiles.get(canonicalName); File classFile = new File(f); if(classFile.isFile()) { is = new FileInputStream(classFile); pathName = classFile.getAbsolutePath(); // TODO: check me""; relativeName = f; fullName = canonicalName; sourceFiles.remove(canonicalName); return true; } } return false; } public void addSourceFile(String name) { try { File classFile = new File(name); if(classFile.isFile()) { is = new FileInputStream(classFile); this.pathName = classFile.getAbsolutePath(); relativeName = name; fullName = name; // is this ok CompilationUnit u = getCompilationUnit(); if(u != null) { String packageName = u.getPackageDecl(); if(packageName != null && !packages.contains(packageName)) { packages.add(packageName); int pos = 0; while(packageName != null && -1 != (pos = packageName.indexOf('.', pos + 1))) { String n = packageName.substring(0, pos); if(!packages.contains(n)) packages.add(n); } } program.addCompilationUnit(u); } } } catch (IOException e) { } } } // remove user defined classes from this program but keep library classes public void Program.simpleReset() { lookupType_String_String_values = new HashMap(); hasPackage_String_values = new HashMap(); List list = new List(); for(int i = 0; i < getNumCompilationUnit(); i++) { CompilationUnit unit = getCompilationUnit(i); if(!unit.fromSource()) { list.add(unit); } } setCompilationUnitList(list); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -