Created
January 30, 2012 08:49
-
-
Save gavinHuang/1703407 to your computer and use it in GitHub Desktop.
why there are differences between class files that manually created and created by eclipse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.util.Arrays; | |
import javax.tools.JavaCompiler; | |
import javax.tools.JavaCompiler.CompilationTask; | |
import javax.tools.JavaFileObject; | |
import javax.tools.SimpleJavaFileObject; | |
import javax.tools.StandardJavaFileManager; | |
import javax.tools.ToolProvider; | |
public class CompilerTest { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
CompilerTest ct = new CompilerTest(); | |
ct.testCompiler(); | |
} | |
public void testCompiler(){ | |
//get source code from java file | |
try { | |
//FileInputStream fis = new FileInputStream("source.java"); | |
FileReader fr = new FileReader("./src/Source.java"); | |
BufferedReader br = new BufferedReader(fr); | |
StringBuffer sb = new StringBuffer(); | |
String line = br.readLine(); | |
while(line != null){ | |
sb.append(line); | |
line = br.readLine(); | |
} | |
line = sb.toString(); | |
//compiler | |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); | |
//1) javaFileManager | |
StandardJavaFileManager javaFileManager = compiler.getStandardFileManager(null, null, null); | |
//2)javaFileObject | |
URI sourceUri = URI.create("string:///Source"+JavaFileObject.Kind.SOURCE.extension); | |
CompilerTest.StringJavaFileObject fileObject = new CompilerTest.StringJavaFileObject(sourceUri, JavaFileObject.Kind.SOURCE); | |
fileObject.setContent(line); | |
//put javaFileObject into list | |
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(fileObject); | |
//put them into task | |
CompilationTask task = compiler.getTask(null, javaFileManager, null, null, null, fileObjects); | |
boolean result = task.call(); | |
if (result) System.out.println("compile successfully!"); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
static class StringJavaFileObject extends SimpleJavaFileObject{ | |
private String content = null; | |
protected StringJavaFileObject(URI uri, Kind kind) { | |
super(uri, kind); | |
} | |
public void setContent(String content){ | |
this.content = content; | |
} | |
public CharSequence getCharContent(boolean | |
ignoreEncodingErrors) throws IOException { | |
return content; | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compiled from Source.java (version 1.6 : 50.0, super bit) | |
public class Source { | |
// Method descriptor #6 ()V | |
// Stack: 1, Locals: 1 | |
public Source(); | |
0 aload_0 [this] | |
1 invokespecial java.lang.Object() [8] | |
4 return | |
Line numbers: | |
[pc: 0, line: 2] | |
Local variable table: | |
[pc: 0, pc: 5] local: this index: 0 type: Source | |
// Method descriptor #15 ([Ljava/lang/String;)V | |
// Stack: 2, Locals: 1 | |
public static void main(java.lang.String[] args); | |
0 getstatic java.lang.System.out : java.io.PrintStream [16] | |
3 ldc <String "Here's what you want."> [22] | |
5 invokevirtual java.io.PrintStream.println(java.lang.String) : void [24] | |
8 return | |
Line numbers: | |
[pc: 0, line: 5] | |
[pc: 8, line: 6] | |
Local variable table: | |
[pc: 0, pc: 9] local: args index: 0 type: java.lang.String[] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compiled from Source.java from StringJavaFileObject (version 1.6 : 50.0, super bit) | |
public class Source { | |
// Method descriptor #8 ()V | |
// Stack: 1, Locals: 1 | |
public Source(); | |
0 aload_0 [this] | |
1 invokespecial java.lang.Object() [1] | |
4 return | |
Line numbers: | |
[pc: 0, line: 1] | |
// Method descriptor #12 ([Ljava/lang/String;)V | |
// Stack: 2, Locals: 1 | |
public static void main(java.lang.String[] arg0); | |
0 getstatic java.lang.System.out : java.io.PrintStream [2] | |
3 ldc <String "Here's what you want."> [3] | |
5 invokevirtual java.io.PrintStream.println(java.lang.String) : void [4] | |
8 return | |
Line numbers: | |
[pc: 0, line: 1] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Source { | |
public static void main(String[] args) { | |
System.out.println("Here's what you want."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment