KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > util > JDTCompiler


1 package spoon.support.util;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.FileInputStream JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.eclipse.jdt.internal.compiler.ClassFile;
10 import org.eclipse.jdt.internal.compiler.CompilationResult;
11 import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
12 import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
13 import org.eclipse.jdt.internal.compiler.batch.FileSystem;
14 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
15 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
16 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
17 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
18
19 public class JDTCompiler implements ICompilerRequestor {
20
21     public static ICompilationUnit getUnit(String JavaDoc name, File JavaDoc file)
22             throws Exception JavaDoc {
23
24         String JavaDoc[] tmp = name.split("[.]");
25         char[][] pack = new char[tmp.length - 1][];
26
27         for (int i = 0; i < tmp.length - 1; i++) {
28             pack[i] = tmp[i].toCharArray();
29         }
30
31         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
32         FileInputStream JavaDoc in = new FileInputStream JavaDoc(file);
33         byte[] buffer = new byte[512];
34         int read;
35         while ((read = in.read(buffer, 0, 512)) >= 0)
36             out.write(buffer, 0, read);
37
38         ICompilationUnit unit = null;
39
40         unit = new BasicCompilationUnit(out.toString().toCharArray(), pack,
41                 file.getName());
42
43         out.close();
44         in.close();
45         return unit;
46     }
47
48     CompilerOptions compilerOption;
49
50     List JavaDoc<ClassFile> classFiles = new ArrayList JavaDoc<ClassFile>();
51
52     public void acceptResult(CompilationResult result) {
53         if (result.hasErrors()) {
54             System.err.println(result);
55         }
56
57         for (ClassFile f : result.getClassFiles()) {
58             classFiles.add(f);
59         }
60     }
61
62     public List JavaDoc<ClassFile> compile(ICompilationUnit[] units) {
63         org.eclipse.jdt.internal.compiler.Compiler compiler = new org.eclipse.jdt.internal.compiler.Compiler(
64                 getLibraryAccess(), getHandlingPolicy(), getCompilerOption(),
65                 this, new DefaultProblemFactory());
66         compiler.compile(units);
67         return classFiles;
68     }
69
70     public CompilerOptions getCompilerOption() {
71         if (compilerOption == null) {
72             compilerOption = new CompilerOptions();
73             compilerOption.sourceLevel = ClassFileConstants.JDK1_5;
74             compilerOption.suppressWarnings = true;
75         }
76         return compilerOption;
77     }
78
79     private IErrorHandlingPolicy getHandlingPolicy() {
80         return new IErrorHandlingPolicy() {
81             public boolean proceedOnErrors() {
82                 return true; // stop if there are some errors
83
}
84
85             public boolean stopOnFirstError() {
86                 return false;
87             }
88         };
89     }
90
91     FileSystem getLibraryAccess() {
92         String JavaDoc bootpath = System.getProperty("sun.boot.class.path");
93         String JavaDoc classpath = System.getProperty("java.class.path");
94         List JavaDoc<String JavaDoc> lst = new ArrayList JavaDoc<String JavaDoc>();
95         for (String JavaDoc s : bootpath.split(File.pathSeparator)) {
96             File JavaDoc f = new File JavaDoc(s);
97             if (f.exists()) {
98                 lst.add(f.getAbsolutePath());
99             }
100         }
101         for (String JavaDoc s : classpath.split(File.pathSeparator)) {
102             File JavaDoc f = new File JavaDoc(s);
103             if (f.exists()) {
104                 lst.add(f.getAbsolutePath());
105             }
106         }
107         return new FileSystem(lst.toArray(new String JavaDoc[0]), new String JavaDoc[0], System
108                 .getProperty("file.encoding"));
109     }
110
111     public List JavaDoc<ClassFile> getClassFiles() {
112         return classFiles;
113     }
114
115 }
116
Popular Tags