public JavaCompiler() {
super(ONE_OUTPUT_FILE_PER_INPUT_FILE, ".java", ".class", null);
}
public List<CompilerError> compile(CompilerConfiguration configuration) throws CompilerException {
BundleResolver stateController = new BundleResolver(getLogger());
getLogger().info("Invoking Tuscany Eclipse JDT compiler");
List<URL> urls = new ArrayList<URL>();
try {
for (String entry : (List<String>)configuration.getClasspathEntries()) {
urls.add(new File(entry).toURI().toURL());
}
} catch (MalformedURLException e) {
throw new CompilerException(e.getMessage(), e);
}
ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
// Determine compiler configuration
Map<String, String> settings = new HashMap<String, String>();
String sourceVersion = configuration.getSourceVersion();
if (sourceVersion != null && sourceVersion.length() != 0) {
settings.put(OPTION_Source, sourceVersion);
}
String targetVersion = configuration.getTargetVersion();
if (targetVersion != null && targetVersion.length() != 0) {
settings.put(OPTION_TargetPlatform, targetVersion);
}
settings.put(OPTION_LineNumberAttribute, GENERATE);
settings.put(OPTION_SourceFileAttribute, GENERATE);
if (configuration.isDebug()) {
settings.put(OPTION_LocalVariableAttribute, GENERATE);
}
if (configuration.getSourceEncoding() != null && !(configuration.getSourceEncoding().length() == 0)) {
settings.put(OPTION_Encoding, configuration.getSourceEncoding());
}
if (configuration.isShowDeprecation()) {
settings.put(OPTION_ReportDeprecation, WARNING);
} else {
settings.put(OPTION_ReportDeprecation, IGNORE);
}
// Create a compiler
List<CompilerError> compilerErrors = new ArrayList<CompilerError>();
INameEnvironment nameEnvironment =
new ClassLoaderNameEnvironment(classLoader, configuration.getSourceLocations());
ICompilerRequestor requestor =
new CompilerRequestor(configuration.getOutputLocation(), configuration.isShowWarnings(), compilerErrors);
Compiler compiler =
new Compiler(nameEnvironment, proceedWithAllProblems(), new CompilerOptions(settings), requestor,
new DefaultProblemFactory(Locale.getDefault()));
// Create compilation units for the source files
List<FileCompilationUnit> compilationUnits = new ArrayList<FileCompilationUnit>();
// Go over the input source locations
List<String> sourceLocations = (List<String>)configuration.getSourceLocations();
for (String sourceLocation : sourceLocations) {
// Exclude nested source locations
List<String> excludeLocations = new ArrayList<String>();
for (String nestedLocation : sourceLocations) {
if (nestedLocation != sourceLocation && nestedLocation.startsWith(sourceLocation)) {
excludeLocations.add(nestedLocation);
}
}
// List source files in each source location
for (String sourceFile : (Set<String>)getSourceFilesForSourceRoot(configuration, sourceLocation)) {
// Exclude files from excluded nested locations
boolean excluded = false;
for (String excludeLocation : excludeLocations) {
if (sourceFile.startsWith(excludeLocation)) {
excluded = true;
}
}
if (!excluded) {
// Create a compilation unit for the source file
FileCompilationUnit compilationUnit =
new FileCompilationUnit(sourceFile, makeClassName(sourceFile, sourceLocation));
compilationUnits.add(compilationUnit);
}
}
}
// Compile all the compilation units
getLogger().info("Compiling " + compilationUnits.size() + " to " + configuration.getOutputLocation());
compiler.compile((ICompilationUnit[])compilationUnits.toArray(new ICompilationUnit[compilationUnits.size()]));
// getLogger().info(configuration.getCustomCompilerArguments().toString());
boolean osgi = "true".equals(configuration.getCustomCompilerArguments().get("-osgi"));
File proj = new File(configuration.getOutputLocation());
String symbol = null;
try {
// Don't trigger the resolution for test-compile
if (!"test-classes".equals(proj.getName())) {
symbol = BundleUtil.getBundleSymbolicName(proj);
}
} catch (IOException e1) {
symbol = null;
}
if (osgi && symbol != null) {
getLogger().info("Resolving OSGi bundle: "+symbol);
for (String entry : (List<String>)configuration.getClasspathEntries()) {
try {
File cp = new File(entry);
if (cp.exists()) {
stateController.addBundle(cp);
}
} catch (BundleException e) {
getLogger().error(e.getMessage(), e);
}
}
stateController.resolveState();
BundleDescription b = stateController.getBundleDescription(proj);
if (b != null) {
try {
stateController.assertResolved(b);
getLogger().info("OSGi bundle is resolved: " + b.getSymbolicName());
} catch (BundleException e) {
stateController.analyzeErrors(b);
if(getLogger().isDebugEnabled()) {
getLogger().debug(stateController.reportErrors(b));
}
// FIXME: For now, only a warning is reported
// throw new CompilerException(e.getMessage(), e);
}
}