1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.castor.xmlctf.compiler;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.StringWriter;
19 import java.io.Writer;
20 import java.lang.reflect.Method;
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import javax.tools.JavaCompiler;
27 import javax.tools.JavaFileObject;
28 import javax.tools.StandardJavaFileManager;
29 import javax.tools.ToolProvider;
30
31 import org.castor.xmlctf.XMLTestCase;
32 import org.castor.xmlctf.util.FileServices;
33
34
35
36
37
38
39
40
41
42 public class OracleJavaCompiler implements Compiler {
43
44 private static final Set<String> IGNORE_DIRS = new HashSet<String>();
45
46
47 private static final float JAVA_VERSION =
48 Float.parseFloat(System.getProperty("java.specification.version"));
49
50 private String _javaVersion = null;
51
52 private final File _baseDirectory;
53 private final File _outputDirectory;
54
55 static {
56 IGNORE_DIRS.add(FileServices.CVS);
57 IGNORE_DIRS.add(FileServices.SVN);
58 IGNORE_DIRS.add(FileServices.GIT);
59 }
60
61
62
63
64
65
66 public OracleJavaCompiler(File baseDirectory) {
67 if (baseDirectory == null) {
68 throw new IllegalArgumentException("'baseDirectory' must not be null.");
69 }
70 _baseDirectory = baseDirectory;
71 _outputDirectory = baseDirectory;
72 }
73
74
75
76
77
78
79 public void setJavaSourceVersion(float javaSourceVersion) {
80 float srcVersion = javaSourceVersion;
81 if (javaSourceVersion >= 5F && javaSourceVersion < 10F) {
82 srcVersion = 1.0F + (javaSourceVersion / 10F);
83 }
84 _javaVersion = "" + srcVersion;
85 }
86
87
88
89
90
91 public void compileDirectory() {
92 List<File> filesList = findSourceFiles(_baseDirectory);
93 if (!filesList.isEmpty()) {
94
95
96
97 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
98 StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
99
100 Iterable<? extends JavaFileObject> compilationUnits =
101 fileManager.getJavaFileObjectsFromFiles(filesList);
102 Iterable<String> compileOptions = getCompileArguments(_baseDirectory, _outputDirectory);
103
104
105 Writer logger = new StringWriter();
106 JavaCompiler.CompilationTask task =
107 compiler.getTask(logger, fileManager, null, compileOptions, null, compilationUnits);
108 boolean ok = task.call();
109 try {
110 fileManager.close();
111 } catch (IOException ioe) {
112
113 ioe.printStackTrace(System.err);
114 }
115
116 System.out.println(logger.toString());
117
118 if (ok == false) {
119 throw new IllegalStateException("Failed to call compile method.");
120 }
121
122 } else {
123 throw new CompilationException("No files to compile: " + _baseDirectory);
124 }
125 }
126
127
128
129
130
131
132
133
134 private List<String> getCompileArguments(final File srcDir, final File destDir) {
135 List<String> args = new ArrayList<String>();
136
137 args.add("-g");
138 if (JAVA_VERSION == 1.5F || JAVA_VERSION == 1.6F || JAVA_VERSION == 1.7F) {
139 args.add("-Xlint:unchecked");
140 }
141 if (XMLTestCase._verbose) {
142 args.add("-verbose");
143 } else {
144 args.add("-nowarn");
145 args.add("-Xmaxwarns");
146 args.add("0");
147 args.add("-Xmaxerrs");
148 args.add("5");
149 }
150 if (_javaVersion != null) {
151 args.add("-source");
152 args.add(_javaVersion);
153 }
154 args.add("-classpath");
155 String classPathOverriden = System.getProperty("xmlctf.classpath.override");
156 if (classPathOverriden != null) {
157 args.add(classPathOverriden + ";" + destDir.getAbsolutePath());
158 } else {
159 args.add(System.getProperty("java.class.path") + ";" + destDir.getAbsolutePath());
160 }
161 args.add("-d");
162 args.add(destDir.getAbsolutePath());
163 args.add("-sourcepath");
164 args.add(srcDir.getAbsolutePath());
165 args.add("-g");
166 return args;
167 }
168
169
170
171
172
173
174
175 private List<File> findSourceFiles(File sourceDirectory) {
176 List<File> files = new ArrayList<File>();
177
178 File[] entries = sourceDirectory.listFiles();
179 for (int i = 0; i < entries.length; i++) {
180 File entry = entries[i];
181 if (entry.getName().endsWith(".java")) {
182 files.add(entry);
183 } else if (entry.isDirectory() && !IGNORE_DIRS.contains(entry.getName())) {
184 files.addAll(findSourceFiles(entry));
185 }
186 }
187
188 return files;
189 }
190
191 }