Falls es nicht zuviel wird, die Methode sieht so aus:
Code:
private void controlLogging() {
if ( this.logged ) {
System.out.println("[Compiling "+ name + " with Logger]");
try {
String filename = this.name + ".java";
File f = new File(filename);
if ( f.isFile() ) {
FileInputStream fis = new FileInputStream(f); /** Creates file instance to work on. */
long size = f.length(); /** Size of the file. */
byte[] content = new byte[(int)size]; /** Buffer to store the file-content. */
long i = 0;
/** Read the file content into the buffer. */
if ( ( i = fis.read(content)) != size ) {
System.err.println("Size mismatch");
System.exit(1);
}
fis.close(); /** Close the FileInputStream. */
/** Create temporary copy of the file. */
File ftmp = new File(f.getCanonicalFile()+".tmp");
f.renameTo(ftmp);
File ftmp2 = new File(this.name+".class");
if ( ftmp2.exists() ) {
ftmp2.delete();
}
/** Converts byte-file-content into string. */
String contentConverted = new String(content);
/**
* To split the lines.
*/
StringTokenizer st = new StringTokenizer(contentConverted,"\n",true);
String[] stArray = new String[st.countTokens()]; /** To store the lines. */
int counter = 0;
while ( st.hasMoreTokens() ) {
stArray[counter] = st.nextToken();
if ( stArray[counter].startsWith("// LOGGER ") ) {
/**
* If LOGGER entry was found it will be replaced.
*/
String message = stArray[counter].substring("// LOGGER ".length()+1,stArray[counter].length());
String m = "";
for ( int counter2 = 0; counter2 < message.length(); counter2++ ) {
if ( message.charAt(counter2) != '"' ) {
m += message.charAt(counter2);
} else {
break;
}
}
message = m;
stArray[counter] = "Logger.log(\""+message+"\");";
}
counter++;
}
/** Get the modified file-content. */
String con = "";
for ( counter = 0; counter < stArray.length; counter++ ) {
con += stArray[counter];
}
FileOutputStream fos = new FileOutputStream(f); /** To write the file back. */
fos.write(con.getBytes());
fos.close();
// Runtime.getRuntime().exec("javac " + f.getAbsolutePath()); /** Compiling the file. */
Compiler.compileClasses(f.getAbsolutePath());
ftmp.renameTo(f); /** Recreate the original file. */
} else {
System.err.println("Argument is no File, exit.");
System.exit(1);
}
} catch (Exception e) {
System.err.println(e);
}
}
}
Wie du vielleicht sieht
Code:
File ftmp2 = new File(this.name+".class");
if ( ftmp2.exists() ) {
ftmp2.delete();
}
lösche ich das class-File extra, damit die Datei sicher neu kompiliert wird!
Lesezeichen