[Pydev-cvs] org.python.pydev.core/src/org/python/pydev/core REF.java, 1.40, 1.41
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2008-07-06 14:11:33
|
Update of /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27663/src/org/python/pydev/core Modified Files: REF.java Log Message: Changes in REF so that we can get the contents available in a suitable format / Using FastDefinitionsParser for gathering global definitions Index: REF.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/REF.java,v retrieving revision 1.40 retrieving revision 1.41 diff -C2 -d -r1.40 -r1.41 *** REF.java 28 Jun 2008 12:35:47 -0000 1.40 --- REF.java 6 Jul 2008 14:11:40 -0000 1.41 *************** *** 38,45 **** import org.eclipse.core.filebuffers.ITextFileBufferManager; import org.eclipse.core.filebuffers.LocationKind; - import org.eclipse.core.internal.resources.ResourceException; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; --- 38,45 ---- import org.eclipse.core.filebuffers.ITextFileBufferManager; import org.eclipse.core.filebuffers.LocationKind; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; + import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; *************** *** 52,55 **** --- 52,56 ---- import org.eclipse.osgi.service.environment.Constants; import org.python.pydev.core.log.Log; + import org.python.pydev.core.structure.FastStringBuffer; *************** *** 59,62 **** --- 60,67 ---- public class REF { + + /** + * @return true if the passed object has a field with the name passed. + */ public static boolean hasAttr(Object o, String attr){ try { *************** *** 70,74 **** } ! public static Field getAttrFromClass(Class c, String attr){ try { return c.getDeclaredField(attr); --- 75,83 ---- } ! ! /** ! * @return the field from a class that matches the passed attr name (or null if it couldn't be found) ! */ ! public static Field getAttrFromClass(Class<? extends Object> c, String attr){ try { return c.getDeclaredField(attr); *************** *** 79,82 **** --- 88,96 ---- } + + /** + * @return the field from a class that matches the passed attr name (or null if it couldn't be found) + * @see #getAttrObj(Object, String) to get the actual value of the field. + */ public static Field getAttr(Object o, String attr){ try { *************** *** 88,91 **** --- 102,109 ---- } + + /** + * @return the value of some attribute in the given object + */ public static Object getAttrObj(Object o, String attr){ try { *************** *** 109,113 **** try { stream = new FileInputStream(file); ! return getStreamContents(stream, null, null); } catch (Exception e) { throw new RuntimeException(e); --- 127,131 ---- try { stream = new FileInputStream(file); ! return (String) getStreamContents(stream, null, null, String.class); } catch (Exception e) { throw new RuntimeException(e); *************** *** 119,128 **** /** * Get the contents from a given stream. */ ! public static String getStreamContents(InputStream contentStream, String encoding, IProgressMonitor monitor) throws IOException { Reader in= null; try{ final int DEFAULT_FILE_SIZE= 15 * 1024; if (encoding == null){ in= new BufferedReader(new InputStreamReader(contentStream), DEFAULT_FILE_SIZE); --- 137,156 ---- /** * Get the contents from a given stream. + * @param returnType the class that specifies the return type of this method. + * If null, it'll return in the fastest possible way available. + * Valid options are: + * String.class + * IDocument.class + * FastStringBuffer.class + * */ ! private static Object getStreamContents(InputStream contentStream, String encoding, IProgressMonitor monitor, ! Class<? extends Object> returnType) throws IOException { ! Reader in= null; try{ final int DEFAULT_FILE_SIZE= 15 * 1024; + //discover how to actually read the passed input stream. if (encoding == null){ in= new BufferedReader(new InputStreamReader(contentStream), DEFAULT_FILE_SIZE); *************** *** 137,151 **** } ! StringBuffer buffer= new StringBuffer(DEFAULT_FILE_SIZE); char[] readBuffer= new char[2048]; int n= in.read(readBuffer); while (n > 0) { ! if (monitor != null && monitor.isCanceled()) return null; buffer.append(readBuffer, 0, n); ! n= in.read(readBuffer); } ! return buffer.toString(); }finally{ --- 165,197 ---- } ! //fill a buffer with the contents ! FastStringBuffer buffer= new FastStringBuffer(DEFAULT_FILE_SIZE); char[] readBuffer= new char[2048]; int n= in.read(readBuffer); while (n > 0) { ! if (monitor != null && monitor.isCanceled()){ return null; + } buffer.append(readBuffer, 0, n); ! n = in.read(readBuffer); } ! ! ! //return it in the way specified by the user ! if(returnType == null || returnType == FastStringBuffer.class){ ! return buffer; ! ! }else if(returnType == IDocument.class){ ! Document doc = new Document(buffer.toString()); ! return doc; ! ! }else if(returnType == String.class){ ! return buffer.toString(); ! ! }else{ ! throw new RuntimeException("Don't know how to handle return type: "+returnType); ! } ! }finally{ *************** *** 153,156 **** --- 199,204 ---- } } + + /** * @param o the object we want as a string *************** *** 171,174 **** --- 219,225 ---- } + /** + * @return the contents of the passed ByteArrayOutputStream as a byte[] encoded with base64. + */ public static byte[] encodeBase64(ByteArrayOutputStream out) { byte[] byteArray = out.toByteArray(); *************** *** 176,209 **** } public static byte[] encodeBase64(byte[] byteArray) { return Base64.encodeBase64(byteArray); } /** - * * @param persisted the base64 string that should be converted to an object. ! * @param readFromFileMethod should be the calback from the plugin that is calling this function * * * The callback should be something as: ! ! new ICallback<Object, ObjectInputStream>(){ ! ! public Object call(ObjectInputStream arg) { ! try { ! return arg.readObject(); ! } catch (IOException e) { ! throw new RuntimeException(e); ! } catch (ClassNotFoundException e) { ! throw new RuntimeException(e); ! } ! }}; ! * ! * @return ! * @throws IOException ! * @throws ClassNotFoundException */ ! public static Object getStrAsObj(String persisted, ICallback<Object, ObjectInputStream> readFromFileMethod) throws IOException, ClassNotFoundException { InputStream input = new ByteArrayInputStream(decodeBase64(persisted)); Object o = readFromInputStreamAndCloseIt(readFromFileMethod, input); --- 227,264 ---- } + + /** + * @return the contents of the passed byteArray[] as a byte[] encoded with base64. + */ public static byte[] encodeBase64(byte[] byteArray) { return Base64.encodeBase64(byteArray); } + /** * @param persisted the base64 string that should be converted to an object. ! * @param readFromFileMethod should be the calback from the plugin that is calling this function (this is needed ! * because from one plugin we cannot load the contents of objects defined in another plugin) * * * The callback should be something as: ! * ! * new ICallback<Object, ObjectInputStream>(){ ! * ! * public Object call(ObjectInputStream arg) { ! * try { ! * return arg.readObject(); ! * } catch (IOException e) { ! * throw new RuntimeException(e); ! * } catch (ClassNotFoundException e) { ! * throw new RuntimeException(e); ! * } ! * }}; * ! * @return the object that was previously serialized in the passed base64 string. */ ! public static Object getStrAsObj(String persisted, ICallback<Object, ObjectInputStream> readFromFileMethod) ! throws IOException, ClassNotFoundException { ! InputStream input = new ByteArrayInputStream(decodeBase64(persisted)); Object o = readFromInputStreamAndCloseIt(readFromFileMethod, input); *************** *** 212,221 **** /** ! * @param readFromFileMethod ! * @param input ! * @return ! * @throws IOException */ ! public static Object readFromInputStreamAndCloseIt(ICallback<Object, ObjectInputStream> readFromFileMethod, InputStream input) { ObjectInputStream in = null; Object o = null; --- 267,280 ---- /** ! * This method loads the contents of an object that was serialized. ! * ! * @param readFromFileMethod: see {@link #getStrAsObj(String, ICallback)} ! * @param input is the input stream that contains the serialized object ! * ! * @return the object that was previously serialized in the passed input stream. */ ! public static Object readFromInputStreamAndCloseIt(ICallback<Object, ObjectInputStream> readFromFileMethod, ! InputStream input) { ! ObjectInputStream in = null; Object o = null; *************** *** 236,247 **** } public static byte[] decodeBase64(String persisted) { return Base64.decodeBase64(persisted.getBytes()); } - public static void writeStrToFile(String str, String file) { - writeStrToFile(str, new File(file)); - } public static void appendStrToFile(String str, String file) { try { --- 295,310 ---- } + + /** + * Decodes some string that was encoded as base64 + */ public static byte[] decodeBase64(String persisted) { return Base64.decodeBase64(persisted.getBytes()); } + /** + * Appends the contents of the passed string to the given file. + */ public static void appendStrToFile(String str, String file) { try { *************** *** 259,262 **** --- 322,336 ---- } + /** + * Writes the contents of the passed string to the given file. + */ + public static void writeStrToFile(String str, String file) { + writeStrToFile(str, new File(file)); + } + + + /** + * Writes the contents of the passed string to the given file. + */ public static void writeStrToFile(String str, File file) { try { *************** *** 276,281 **** /** ! * @param file ! * @param astManager */ public static void writeToFile(Object o, File file) { --- 350,354 ---- /** ! * Writes the contents of the passed string to the given file. */ public static void writeToFile(Object o, File file) { *************** *** 289,296 **** /** * @param o the object to be written to some stream * @param out the output stream to be used - * - * @throws IOException */ public static void writeToStreamAndCloseIt(Object o, OutputStream out) throws IOException { --- 362,369 ---- /** + * Serializes some object to the given stream + * * @param o the object to be written to some stream * @param out the output stream to be used */ public static void writeToStreamAndCloseIt(Object o, OutputStream out) throws IOException { *************** *** 317,321 **** /** ! * Reads some object from a file * @param file the file from where we should read * @return the object that was read (or null if some error happened while reading) --- 390,398 ---- /** ! * Reads some object from a file (an object that was previously serialized) ! * ! * Important: can only deserialize objects that are defined in this plugin -- ! * see {@link #getStrAsObj(String, ICallback)} if you want to deserialize objects defined in another plugin. ! * * @param file the file from where we should read * @return the object that was read (or null if some error happened while reading) *************** *** 342,346 **** } ! public static String getFileAbsolutePath(String f) { return getFileAbsolutePath(new File(f)); --- 419,429 ---- } ! /** ! * Get the absolute path in the filesystem for the given file. ! * ! * @param f the file we're interested in ! * ! * @return the absolute (canonical) path to the file ! */ public static String getFileAbsolutePath(String f) { return getFileAbsolutePath(new File(f)); *************** *** 348,353 **** /** ! * @param f the file we're interested in ! * @return the absolute (canonical) path to the file */ public static String getFileAbsolutePath(File f) { --- 431,435 ---- /** ! * @see #getFileAbsolutePath(String) */ public static String getFileAbsolutePath(File f) { *************** *** 359,362 **** --- 441,445 ---- } + /** * Calls a method for an object *************** *** 366,369 **** --- 449,454 ---- * @param args the arguments received for the call * @return the return of the method + * + * @throws RuntimeException if the object could not be invoked */ public static Object invoke(Object obj, String name, Object... args) { *************** *** 375,378 **** --- 460,466 ---- + /** + * @see #invoke(Object, String, Object...) + */ public static Object invoke(Object obj, Method m, Object... args) { try { *************** *** 383,400 **** } public static Method findMethod(Object obj, String name, Object... args) { return findMethod(obj.getClass(), name, args); } ! public static Method findMethod(Class class_, String name, Object... args) { try { Method[] methods = class_.getMethods(); for (Method method : methods) { ! Class[] parameterTypes = method.getParameterTypes(); if(method.getName().equals(name) && parameterTypes.length == args.length){ //check the parameters int i = 0; ! for (Class param : parameterTypes) { if(!param.isInstance(args[i])){ continue; --- 471,498 ---- } + + /** + * @return a method that has the given name and arguments + * @throws RuntimeException if the method could not be found + */ public static Method findMethod(Object obj, String name, Object... args) { return findMethod(obj.getClass(), name, args); } + ! /** ! * @return a method that has the given name and arguments ! * @throws RuntimeException if the method could not be found ! */ ! public static Method findMethod(Class<? extends Object> class_, String name, Object... args) { try { Method[] methods = class_.getMethods(); for (Method method : methods) { ! Class<? extends Object>[] parameterTypes = method.getParameterTypes(); if(method.getName().equals(name) && parameterTypes.length == args.length){ //check the parameters int i = 0; ! for (Class<? extends Object> param : parameterTypes) { if(!param.isInstance(args[i])){ continue; *************** *** 409,415 **** throw new RuntimeException(e); } ! throw new RuntimeException("The method with name: "+name+" was not found (or maybe it was found but the parameters didn't match)."); } public static char [] INVALID_FILESYSTEM_CHARS = { '!', '@', '#', '$', '%', '^', '&', '*', --- 507,517 ---- throw new RuntimeException(e); } ! throw new RuntimeException("The method with name: "+name+ ! " was not found (or maybe it was found but the parameters didn't match)."); } + /** + * Characters that files in the filesystem cannot have. + */ public static char [] INVALID_FILESYSTEM_CHARS = { '!', '@', '#', '$', '%', '^', '&', '*', *************** *** 417,422 **** --- 519,531 ---- '.', ' ', '`', '~', '\'', '"', ',', ';'}; + /** + * Determines if we're in tests: When in tests, some warnings may be supressed. + */ public static boolean IN_TESTS = false; + + /** + * @return a valid name for a project so that the returned name can be used to create a file in the filesystem + */ public static String getValidProjectName(IProject project) { String name = project.getName(); *************** *** 430,434 **** /** ! * Makes an equal comparisson taking into account that one of the parameters may be null. */ public static boolean nullEq(Object o1, Object o2){ --- 539,543 ---- /** ! * Makes an equal comparison taking into account that one of the parameters may be null. */ public static boolean nullEq(Object o1, Object o2){ *************** *** 447,459 **** /** * @return a document with the contents from a path within a zip file. */ public static IDocument getDocFromZip(File f, String pathInZip) throws Exception { ZipFile zipFile = new ZipFile(f, ZipFile.OPEN_READ); try { InputStream inputStream = zipFile.getInputStream(zipFile.getEntry(pathInZip)); try { ! Document doc = new Document(REF.getStreamContents(inputStream, null, null)); ! return doc; } finally { inputStream.close(); --- 556,593 ---- /** + * @return a string with the contents from a path within a zip file. + */ + public static String getStringFromZip(File f, String pathInZip) throws Exception { + return (String) getCustomReturnFromZip(f, pathInZip, String.class); + } + + /** * @return a document with the contents from a path within a zip file. */ public static IDocument getDocFromZip(File f, String pathInZip) throws Exception { + return (IDocument) getCustomReturnFromZip(f, pathInZip, IDocument.class); + } + + /** + * @param f the zip file that should be opened + * @param pathInZip the path within the zip file that should be gotten + * @param returnType the class that specifies the return type of this method. + * If null, it'll return in the fastest possible way available. + * Valid options are: + * String.class + * IDocument.class + * FastStringBuffer.class + * + * @return an object with the contents from a path within a zip file, having the return type + * of the object specified by the parameter returnType. + */ + public static Object getCustomReturnFromZip(File f, String pathInZip, Class<? extends Object> returnType) + throws Exception { + ZipFile zipFile = new ZipFile(f, ZipFile.OPEN_READ); try { InputStream inputStream = zipFile.getInputStream(zipFile.getEntry(pathInZip)); try { ! return REF.getStreamContents(inputStream, null, null, returnType); } finally { inputStream.close(); *************** *** 463,499 **** } } /** * @return the document given its 'filesystem' file - * @throws IOException */ public static IDocument getDocFromFile(java.io.File f, boolean loadIfNotInWorkspace) throws IOException { IPath path = Path.fromOSString(getFileAbsolutePath(f)); IDocument doc = getDocFromPath(path); if (doc == null && loadIfNotInWorkspace) { ! return getPythonDocFromFile(f); } return doc; } ! /** ! * @return the document given its 'filesystem' file (checks for the declared python encoding in the file) ! * @throws IOException ! */ ! private static IDocument getPythonDocFromFile(java.io.File f) throws IOException { ! IDocument docFromPath = getDocFromPath(Path.fromOSString(getFileAbsolutePath(f))); ! if(docFromPath != null){ ! return docFromPath; ! } ! ! FileInputStream stream = new FileInputStream(f); ! String fileContents = ""; ! try { ! String encoding = getPythonFileEncoding(f); ! fileContents = getStreamContents(stream, encoding, null); ! } finally { ! try { if(stream != null) stream.close(); } catch (Exception e) {Log.log(e);} ! } ! return new Document(fileContents); ! } --- 597,663 ---- } } + + + /** + * @return a string with the contents of the passed file + */ + public static String getStringFromFile(java.io.File f, boolean loadIfNotInWorkspace) throws IOException { + return (String) getCustomReturnFromFile(f, loadIfNotInWorkspace, String.class); + } + /** * @return the document given its 'filesystem' file */ public static IDocument getDocFromFile(java.io.File f, boolean loadIfNotInWorkspace) throws IOException { + return (IDocument) getCustomReturnFromFile(f, loadIfNotInWorkspace, IDocument.class); + } + + /** + * @param f the file from where we want to get the contents + * @param returnType the class that specifies the return type of this method. + * If null, it'll return in the fastest possible way available. + * Valid options are: + * String.class + * IDocument.class + * FastStringBuffer.class + * + * + * @return an object with the contents from the file, having the return type + * of the object specified by the parameter returnType. + */ + public static Object getCustomReturnFromFile(java.io.File f, boolean loadIfNotInWorkspace, + Class<? extends Object> returnType) throws IOException { + IPath path = Path.fromOSString(getFileAbsolutePath(f)); IDocument doc = getDocFromPath(path); + + if(doc != null){ + if(returnType == null || returnType == IDocument.class){ + return doc; + + }else if(returnType == String.class){ + return doc.get(); + + }else if(returnType == FastStringBuffer.class){ + return new FastStringBuffer(doc.get(), 16); + + }else{ + throw new RuntimeException("Don't know how to treat requested return type: "+returnType); + } + } + if (doc == null && loadIfNotInWorkspace) { ! FileInputStream stream = new FileInputStream(f); ! try { ! String encoding = getPythonFileEncoding(f); ! return getStreamContents(stream, encoding, null, returnType); ! } finally { ! try { if(stream != null) stream.close(); } catch (Exception e) {Log.log(e);} ! } } return doc; } ! *************** *** 502,505 **** --- 666,670 ---- * @return a file buffer to be used. */ + @SuppressWarnings("deprecation") public static ITextFileBuffer getBufferFromPath(IPath path) { //TODO: make this better for 3.3/ 3.2 (and check if behaviour is correct now) *************** *** 563,566 **** --- 728,732 ---- * and if that fails, it creates one reading the file. */ + @SuppressWarnings("restriction") public static IDocument getDocFromResource(IResource resource) { IProject project = resource.getProject(); *************** *** 578,593 **** if(doc == null){ //can this actually happen?... yeap, it can ! InputStream contents = file.getContents(); ! try { ! int i = contents.available(); ! byte b[] = new byte[i]; ! contents.read(b); ! doc = new Document(new String(b)); ! } finally{ ! contents.close(); ! } } return doc; ! }catch(ResourceException e){ //it may stop existing from the initial exists check to the getContents call return null; --- 744,751 ---- if(doc == null){ //can this actually happen?... yeap, it can ! doc = (IDocument) REF.getStreamContents(file.getContents(), null, null, IDocument.class); } return doc; ! }catch(CoreException e){ //it may stop existing from the initial exists check to the getContents call return null; *************** *** 633,639 **** * * Will close the reader. ! * @param fileLocation the file we want to get the encoding from (just passed for giving a better message if it fails -- may be null). */ ! public static String getPythonFileEncoding(Reader inputStreamReader, String fileLocation) throws IllegalCharsetNameException{ String ret = null; BufferedReader reader = new BufferedReader(inputStreamReader); --- 791,800 ---- * * Will close the reader. ! * @param fileLocation the file we want to get the encoding from (just passed for giving a better message ! * if it fails -- may be null). */ ! public static String getPythonFileEncoding(Reader inputStreamReader, String fileLocation) ! throws IllegalCharsetNameException{ ! String ret = null; BufferedReader reader = new BufferedReader(inputStreamReader); *************** *** 674,678 **** } ! StringBuffer buffer = new StringBuffer(); while(lEnc.length() > 0 && ((c = lEnc.charAt(0)) != ' ' || c == '-' || c == '*')) { --- 835,839 ---- } ! FastStringBuffer buffer = new FastStringBuffer(); while(lEnc.length() > 0 && ((c = lEnc.charAt(0)) != ' ' || c == '-' || c == '*')) { |