org.fife.rsta.ac.java.JarManager Maven / Gradle / Ivy
/* * 03/21/2010 * * Copyright (C) 2010 Robert Futrell * robert_futrell at users.sourceforge.net * http://fifesoft.com/rsyntaxtextarea * * This library is distributed under a modified BSD license. See the included * RSTALanguageSupport.License.txt file for details. */ package org.fife.rsta.ac.java; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; import org.fife.rsta.ac.java.classreader.ClassFile; import org.fife.rsta.ac.java.rjc.ast.ImportDeclaration; import org.fife.ui.autocomplete.CompletionProvider; /** * Manages a list of jars and gets completions from them. This can be shared * amongst multiple {@link JavaCompletionProvider} instances. * * @author Robert Futrell * @version 1.0 */ public class JarManager { /** * Jars to get completions from. */ private List jars; /** * Whether to check datestamps on jars/directories when completion * information is requested. */ private static boolean checkModified; /** * Constructor. */ public JarManager() { jars = new ArrayList(); setCheckModifiedDatestamps(true); } /** * Adds completions matching the specified text to a list. * * @param p The parent completion provider. * @param text The text to match. * @param addTo The list to add completion choices to. */ public void addCompletions(CompletionProvider p, String text, Set addTo) { /* * The commented-out code below is probably replaced by the rest of the code * in this method... TODO: Verify me!!! * // Add any completions matching the text for each jar we know about String[] pkgNames = Util.splitOnChar(text, '.'); for (int i=0; i
, then * the current JVM's main JRE jar (rt.jar, or classes.jar on OS X) * will be added. If this jar has already been added, adding it * again will do nothing (except possibly update its attached source * location). * @return Whether this jar was added (e.g. it wasn't already loaded, or * it has a new source path). * @throws IOException If an IO error occurs. * @see #getJars() * @see #removeJar(File) */ public boolean addJar(JarInfo info) throws IOException { // First see if this jar is already on the "build path." for (int i=0; i-1) { String[] pkgNames = Util.splitOnChar(text, '.'); for (int i=0; i null null if there are none. */ public List getClassesWithUnqualifiedName(String name, List importDeclarations) { // Might be more than one class/interface/enum with the same name. List result = null; // Loop through all of our imports. for (int i=0; i See if package contains a class with this name if (idec.isWildcard()) { String qualified = idec.getName(); qualified = qualified.substring(0, qualified.indexOf('*')); qualified += name; ClassFile entry = getClassEntry(qualified); if (entry!=null) { if (result==null) { result = new ArrayList(1); // Usually small } result.add(entry); } } // Not wildcard => fully-qualified class/interface name else { String name2 = idec.getName(); String unqualifiedName2 = name2.substring(name2.lastIndexOf('.')+1); if (unqualifiedName2.equals(name)) { ClassFile entry = getClassEntry(name2); if (entry!=null) { // Should always be true if (result==null) { result = new ArrayList(1); // Usually small } result.add(entry); } else { System.err.println("ERROR: Class not found! - " + name2); } } } } } // Also check java.lang String qualified = "java.lang." + name; ClassFile entry = getClassEntry(qualified); if (entry!=null) { if (result==null) { result = new ArrayList(1); // Usually small } result.add(entry); } return result; } /** * * @param pkgName A package name. * @return A list of all classes in that package. */ public List getClassesInPackage(String pkgName, boolean inPkg) { List list = new ArrayList(); String[] pkgs = Util.splitOnChar(pkgName, '.'); for (int i=0; i JarInfo in * this list will have no effect on this completion provider; in * order to do that, you must re-add the jar via * {@link #addJar(JarInfo)}. If there are no jars on the * "build path," this will be an empty list. * @see #addJar(JarInfo) */ public List getJars() { List jarList = new ArrayList(jars.size()); for (Iterator i=jars.iterator(); i.hasNext(); ) { JarReader reader = (JarReader)i.next(); jarList.add(reader.getJarInfo().clone()); } return jarList; } public SortedMap getPackageEntry(String pkgName) { String[] pkgs = Util.splitOnChar(pkgName, '.'); SortedMap map = new TreeMap(); for (int i=0; i false * if the jar was not on the build path. * @see #addJar(JarInfo) * @see #getJars() */ public boolean removeJar(File jar) { for (Iterator i=jars.iterator(); i.hasNext(); ) { JarReader reader = (JarReader)i.next(); JarInfo info = reader.getJarInfo(); File jar2 = info.getJarFile(); if (jar.equals(jar2)) { i.remove(); return true; } } return false; } /** * Sets whether the "last modified" time stamp on jars and class * directories should be checked whenever completions are requested, and * if the jar/directory has been modified since the last time, reload any * cached class file data. This allows for code completion to update * whenever dependencies are rebuilt, but has the side effect of increased * file I/O. By default this option is enabled; if you somehow find the * file I/O to be a bottleneck (perhaps accessing jars over a slow NFS * mount), you can disable this option. * * @param check Whether to check if any jars/directories have been * modified since the last access, and clear any cached completion * information if so. * @see #getCheckModifiedDatestamps() */ public static void setCheckModifiedDatestamps(boolean check) { checkModified = check; } }