1 19 20 package org.netbeans.modules.java.source.script; 21 22 import com.sun.tools.javac.util.DefaultFileManager; 23 import com.sun.tools.javac.main.*; 24 import com.sun.tools.javac.util.*; 25 26 import java.io.*; 27 import javax.tools.JavaFileManager; 28 import javax.tools.JavaFileObject; 29 30 66 public class PluginCompiler { 67 68 private static final File generatedCodeDir; 69 private final ClassLoader classloader = new ClassLoader (getClass().getClassLoader()) { 70 public Class findClass(String name) throws ClassNotFoundException { 71 try { 72 FileInputStream in = new FileInputStream(new File(generatedCodeDir, name + ".class")); 73 int len0 = in.available(); 74 byte[] b = new byte[len0]; 75 int len1 = in.read(b); 76 in.close(); 77 if (len1 != len0) 78 throw new ClassNotFoundException ("read failure"); 79 return defineClass(name, b, 0, b.length); 80 } catch(Throwable t) { 81 ClassNotFoundException cnf; 82 if (t instanceof ClassNotFoundException ) 83 cnf = (ClassNotFoundException ) t; 84 else 85 cnf = new ClassNotFoundException ("load failure", t); 86 throw cnf; 87 } 88 } 89 }; 90 91 static { 92 File cb = new File(System.getProperty("user.home"), ".jackpot"); 93 if (!cb.exists() && !cb.mkdirs()) { 94 File tmp = null; 95 try { 96 tmp = File.createTempFile("HOHO", null); 97 cb = new File(tmp.getParentFile(), 98 "PluginCache-"+System.getProperty("user.name","user")); 99 } 100 catch(IOException ioe) { 101 cb = new File("/tmp/AuxPluginCache"); 102 } 103 finally { 104 if (tmp != null) 105 tmp.delete(); 106 } 107 cb.mkdirs(); 108 } 109 generatedCodeDir = cb; 110 } 111 112 private String className; 113 private File source; 114 private File genJava; 115 private LineWriter out; 116 private static int seq = 0; 117 public boolean needsGeneration(String s, long lastModified, boolean force) { 118 int src = 0; 119 int dst = 0; 120 char [] nbuf; 121 if (s != null) { 122 source = new File(s); 123 int limit = s.length(); 124 nbuf = new char[(limit < 2 ? 10 : limit) + 6]; 125 while (src < limit) { 126 char c = s.charAt(src++); 127 if (dst > 0) { 128 if (Character.isJavaIdentifierPart(c)) 129 nbuf[dst++] = c; 130 else if (nbuf[dst - 1] != '_') 131 nbuf[dst++] = '_'; 132 } else if (Character.isJavaIdentifierStart(c)) 133 nbuf[dst++] = c; 134 } 135 while (dst > 0 && nbuf[dst - 1] == '_') 136 dst--; 137 } else { 138 source = null; 139 nbuf = new char[12]; 140 } 141 if (dst == 0) { 142 nbuf[dst++] = 'C'; 143 int ls = seq++; 144 do { 145 nbuf[dst++] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef".charAt(ls & 31); 146 ls >>>= 5; 147 } while (ls != 0); 148 } 149 className = new String (nbuf, 0, dst); 150 String javaName = new String (nbuf, 0, addExt(nbuf, dst, ".java")); 151 genJava = new File(generatedCodeDir, javaName); 152 File genClass = new File(generatedCodeDir, javaName.replace(".java", ".class")); 153 out = null; 154 if(force) return true; 155 long sm = lastModified > 0 ? lastModified : source != null ? source.lastModified() : 0; 156 return sm <= 0 || sm >= genClass.lastModified(); 157 } 158 private static int addExt(char[] buf, int st, String ext) { 159 int limit = ext.length(); 160 for (int i = 0; i < limit; i++) 161 buf[st + i] = ext.charAt(i); 162 return st + limit; 163 } 164 public void startGeneration() 165 throws IOException 166 { 167 out = new LineWriter(new FileWriter(genJava)); 168 } 169 public void write(String s) 170 throws IOException 171 { 172 out.write(s); 173 } 174 public void write(int i) throws IOException { 175 if(i<0) { 176 out.write('-'); 177 i = -i; 178 } 179 if(i>=10) write(i/10); 180 out.write((char)('0'+i%10)); 181 } 182 public void write(long i) throws IOException { 183 if(i<0) { 184 out.write('-'); 185 i = -i; 186 } 187 if(i>=10) write(i/10); 188 out.write((char)('0'+i%10)); 189 } 190 191 public void writeClassName() 192 throws IOException 193 { 194 out.write(className); 195 } 196 public void write(char c) 197 throws IOException 198 { 199 out.write(c); 200 } 201 private static final char[] hexChars = "0123456789ABCDEF".toCharArray(); 202 private static final char backslash = '\\'; 203 private static void oneChar(Writer out, char c) 204 throws IOException 205 { 206 if (c < 040 || c == '\'' || c >= 127 && c <= 0377) { 207 out.write(backslash); 208 out.write(hexChars[(c >>> 6) & 7]); 209 out.write(hexChars[(c >>> 3) & 7]); 210 out.write(hexChars[(c >>> 0) & 7]); 211 } else if (c == '"') { 212 out.write('\\'); 213 out.write('"'); 214 } else if (c < 127) 215 out.write(c); 216 else { 217 out.write(backslash); 218 out.write('u'); 219 out.write(hexChars[(c >>> 12) & 0xF]); 220 out.write(hexChars[(c >>> 8) & 0xF]); 221 out.write(hexChars[(c >>> 4) & 0xF]); 222 out.write(hexChars[(c >>> 0) & 0xF]); 223 } 224 } 225 public void writeQuoted(String s) 226 throws IOException 227 { 228 Writer out = this.out; 229 if (s == null) 230 out.write("null"); 231 else { 232 int limit = s.length(); 233 out.write('"'); 234 for (int i = 0; i < limit; i++) 235 oneChar(out, s.charAt(i)); 236 out.write('"'); 237 } 238 } 239 public void writeCapitalizedIdent(String s) 240 throws IOException 241 { 242 Writer out = this.out; 243 if (s == null) 244 out.write("NULL"); 245 else { 246 int limit = s.length(); 247 boolean start = true; 248 for (int i = 0; i < limit; i++) { 249 char c = s.charAt(i); 250 if(Character.isJavaIdentifierPart(c)) { 251 if(start && Character.isLowerCase(c)) 252 c = Character.toLowerCase(c); 253 out.write(c); 254 start = false; 255 } else start=true; 256 } 257 } 258 } 259 public void writeQuoted(char c) 260 throws IOException 261 { 262 out.write('\''); 263 oneChar(out, c); 264 out.write('\''); 265 } 266 public Writer getWriter() { return out; } 267 268 public int getCurrentLineNumber() { 269 return out.getLineNumber(); 270 } 271 272 289 290 ScriptParser.ScriptLog log; 291 292 public Class loadClass(String javacpath) throws IOException { 293 if (out != null) { 294 out.close(); 295 Context context = new Context(); 296 DefaultFileManager.preRegister(context); 297 log = new ScriptParser.ScriptLog(context); 298 Options options = Options.instance(context); 299 String gcd = generatedCodeDir.toString(); 300 options.put("-source", "1.5"); 301 options.put("-target", "1.5"); 302 options.put("-d", gcd); 303 options.put("-sourcepath", gcd); 304 options.put("-g", "-g"); 305 if (javacpath != null) 306 options.put("-Xbootclasspath/p:", javacpath); 307 DefaultFileManager fileManager = new DefaultFileManager(context, true, null); 308 JavaFileObject fileobject = fileManager.getFileForInput(genJava.toString()); 309 List<JavaFileObject> filenames = List.of(fileobject); 310 JavaCompiler comp = new JavaCompiler(context); 311 log.useSource(fileobject); 312 try { 313 comp.compile(filenames); 314 if(hasErrors()) 315 throw new IOException("Compilation errors"); 316 } catch(Throwable t) { 317 IOException ioe = new IOException("Class " + className + " compilation exception"); 318 ioe.initCause(t); 319 throw ioe; 320 } 321 } 322 try { 323 return classloader.loadClass(className); 324 } catch(ClassNotFoundException cnf) { 325 IOException ioe = new IOException("Class " + className + " not found"); 326 ioe.initCause(cnf); 327 throw ioe; 328 } 329 } 330 public boolean hasErrors() { return log!=null && log.hasErrors(); } 331 public String getErrors() { return log==null ? null : log.getErrors(); } 332 333 336 private static class LineWriter extends BufferedWriter { 337 int line = 0; 338 LineWriter(Writer out) { 339 super(out); 340 } 341 public void write(String s, int off, int len) throws IOException { 342 write(s.toCharArray(), off, len); 343 } 344 public void write(char[] cbuf, int off, int len) throws IOException { 345 super.write(cbuf, off, len); 346 for (int i = off; i < len; i++) 347 if (cbuf[i] == '\n') 348 line++; 349 } 350 public void write(int c) throws IOException { 351 super.write(c); 352 if (c == '\n') 353 line++; 354 } 355 public void newLine() throws IOException { 356 super.newLine(); 357 line++; 358 } 359 int getLineNumber() { 360 return line; 361 } 362 } 363 } 364 | Popular Tags |