1 19 20 package org.netbeans.modules.tasklist.providers; 21 22 import java.io.BufferedInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.io.Reader ; 26 import java.lang.reflect.InvocationTargetException ; 27 import org.openide.filesystems.FileObject; 28 import org.openide.ErrorManager; 29 import java.lang.reflect.Method ; 30 import org.openide.loaders.DataObject; 31 import org.openide.loaders.DataObjectNotFoundException; 32 import org.openide.util.Lookup; 33 34 35 40 final class JavaSuggestionContext { 41 private static boolean lookupAttempted = false; 42 private static Method org_netbeans_modules_java_Util_getFileEncoding; 43 private static Method org_netbeans_modules_java_Util_getContent; 44 45 50 private static boolean findMethods() { 51 if (!lookupAttempted) { 52 lookupAttempted = true; 53 ClassLoader systemClassLoader = 54 (ClassLoader ) Lookup.getDefault().lookup(ClassLoader .class); 55 try { 56 Class c = systemClassLoader. 57 loadClass("org.netbeans.modules.java.Util"); org_netbeans_modules_java_Util_getFileEncoding = 59 c.getMethod("getFileEncoding", new Class [] {FileObject.class}); 60 org_netbeans_modules_java_Util_getContent = 61 c.getMethod("getContent", new Class [] {FileObject.class, Boolean.TYPE, Boolean.TYPE, String .class}); 63 } catch (Exception e) { 64 System.out.println("org.netbeans.modules.tasklist.providers" + ".JavaSuggestionContext: Methods not found"); } 68 } 69 70 return org_netbeans_modules_java_Util_getFileEncoding != null && 71 org_netbeans_modules_java_Util_getContent != null; 72 } 73 74 77 static String getContent(FileObject fo) { 78 String result = null; if (!findMethods()) { 80 try { 81 char[] buf = new char[1024*64]; 82 StringBuffer sb = new StringBuffer (); 83 Reader r = new InputStreamReader (new BufferedInputStream (fo.getInputStream())); 84 int len; 85 try { 86 while (true) { 87 len = r.read(buf); 88 if (len == -1) break; 89 sb.append(buf, 0, len); 90 } 91 result = sb.toString(); 92 } finally { 93 r.close(); 94 } 95 } catch (IOException e) { 96 ErrorManager.getDefault().notify(e); 97 } 98 99 } else { 106 try { 107 String encoding = (String ) org_netbeans_modules_java_Util_getFileEncoding. 108 invoke(null, new Object [] {fo}); 109 110 char[] source = (char[]) org_netbeans_modules_java_Util_getContent.invoke( 111 null, 112 new Object [] {fo, Boolean.FALSE, Boolean.TRUE, encoding}); 113 result = new String (source); 114 } catch (InvocationTargetException e) { 115 ErrorManager.getDefault().notify(e); 116 } catch (IllegalArgumentException e) { 117 ErrorManager.getDefault().notify(e); 118 } catch (IllegalAccessException e) { 119 ErrorManager.getDefault().notify(e); 120 } 121 } 122 return result; 123 } 124 } 125 | Popular Tags |