use of com.google.devtools.j2objc.file.RegularInputFile in project j2objc by google.
the class JdtParser method parseFiles.
@Override
public void parseFiles(Collection<String> paths, final Handler handler, SourceVersion sourceVersion) {
ASTParser parser = newASTParser(true, sourceVersion);
FileASTRequestor astRequestor = new FileASTRequestor() {
@Override
public void acceptAST(String sourceFilePath, CompilationUnit ast) {
logger.fine("acceptAST: " + sourceFilePath);
if (checkCompilationErrors(sourceFilePath, ast)) {
RegularInputFile file = new RegularInputFile(sourceFilePath);
try {
String source = options.fileUtil().readFile(file);
ParserEnvironment parserEnv = new JdtParserEnvironment(ast.getAST());
TranslationEnvironment env = new TranslationEnvironment(options, parserEnv);
com.google.devtools.j2objc.ast.CompilationUnit unit = TreeConverter.convertCompilationUnit(env, ast, sourceFilePath, FileUtil.getMainTypeName(file), source);
handler.handleParsedUnit(sourceFilePath, unit);
} catch (IOException e) {
ErrorUtil.error("Error reading file " + file.getOriginalLocation() + ": " + e.getMessage());
}
}
}
};
// JDT fails to resolve all secondary bindings unless there are the same
// number of "binding key" strings as source files. It doesn't appear to
// matter what the binding key strings should be (as long as they're non-
// null), so the paths array is reused.
String[] pathsArray = paths.toArray(new String[paths.size()]);
parser.createASTs(pathsArray, getEncodings(pathsArray.length), pathsArray, astRequestor, null);
}
use of com.google.devtools.j2objc.file.RegularInputFile in project j2objc by google.
the class GenerationBatch method processJavaFile.
private void processJavaFile(String filename) {
InputFile inputFile;
try {
inputFile = new RegularInputFile(filename, filename);
if (!inputFile.exists()) {
// Convert to a qualified name and search on the sourcepath.
String qualifiedName = filename.substring(0, filename.length() - 5).replace(File.separatorChar, '.');
inputFile = options.fileUtil().findOnSourcePath(qualifiedName);
if (inputFile == null) {
ErrorUtil.error("No such file: " + filename);
return;
}
}
} catch (IOException e) {
ErrorUtil.warning(e.getMessage());
return;
}
addSource(inputFile);
}
use of com.google.devtools.j2objc.file.RegularInputFile in project j2objc by google.
the class GenerationBatch method processJarFile.
private void processJarFile(String filename) {
File f = findJarFile(filename);
if (f == null) {
ErrorUtil.error("No such file: " + filename);
return;
}
// don't support Java-like source paths.
if (options.emitLineDirectives()) {
ErrorUtil.warning("source debugging of jar files is not supported: " + filename);
}
GenerationUnit combinedUnit = null;
if (options.getHeaderMap().combineSourceJars()) {
combinedUnit = GenerationUnit.newCombinedJarUnit(filename, options);
}
try {
ZipFile zfile = new ZipFile(f);
try {
Enumeration<? extends ZipEntry> enumerator = zfile.entries();
File tempDir = FileUtil.createTempDir(J2OBJC_TEMP_DIR_PREFIX);
String tempDirPath = tempDir.getAbsolutePath();
options.fileUtil().addTempDir(tempDirPath);
options.fileUtil().appendSourcePath(tempDirPath);
while (enumerator.hasMoreElements()) {
ZipEntry entry = enumerator.nextElement();
String internalPath = entry.getName();
if (internalPath.endsWith(".java")) {
// Extract JAR file to a temporary directory
File outputFile = options.fileUtil().extractZipEntry(tempDir, zfile, entry);
InputFile newFile = new RegularInputFile(outputFile.getAbsolutePath(), internalPath);
if (combinedUnit != null) {
inputs.add(new ProcessingContext(newFile, combinedUnit));
} else {
addExtractedJarSource(newFile, filename, internalPath);
}
}
}
} finally {
// Also closes input stream.
zfile.close();
}
} catch (ZipException e) {
// Also catches JarExceptions
logger.fine(e.getMessage());
ErrorUtil.error("Error reading file " + filename + " as a zip or jar file.");
} catch (IOException e) {
ErrorUtil.error(e.getMessage());
}
}
use of com.google.devtools.j2objc.file.RegularInputFile in project j2objc by google.
the class JdtParser method parse.
@Override
public com.google.devtools.j2objc.ast.CompilationUnit parse(String mainTypeName, String path, String source) {
int errors = ErrorUtil.errorCount();
org.eclipse.jdt.core.dom.CompilationUnit unit = parse(path, source, true);
if (ErrorUtil.errorCount() > errors) {
return null;
}
if (mainTypeName == null) {
RegularInputFile file = new RegularInputFile(path);
mainTypeName = FileUtil.getQualifiedMainTypeName(file, unit);
}
ParserEnvironment parserEnv = new JdtParserEnvironment(unit.getAST());
TranslationEnvironment env = new TranslationEnvironment(options, parserEnv);
return TreeConverter.convertCompilationUnit(env, unit, path, mainTypeName, source);
}
use of com.google.devtools.j2objc.file.RegularInputFile in project j2objc by google.
the class InputFilePreprocessor method processRegularSource.
private void processRegularSource(ProcessingContext input) throws IOException {
InputFile file = input.getFile();
String source = options.fileUtil().readFile(file);
boolean shouldMapHeaders = options.getHeaderMap().useSourceDirectories();
boolean doIncompatibleStripping = source.contains("J2ObjCIncompatible");
if (!(shouldMapHeaders || doIncompatibleStripping)) {
// No need to parse.
return;
}
Parser.ParseResult parseResult = parser.parseWithoutBindings(file, source);
if (parseResult == null) {
// The parser found and reported one or more errors.
return;
}
String qualifiedName = parseResult.mainTypeName();
if (shouldMapHeaders) {
options.getHeaderMap().put(qualifiedName, input.getGenerationUnit().getOutputPath() + ".h");
}
if (doIncompatibleStripping) {
parseResult.stripIncompatibleSource();
File strippedDir = getCreatedStrippedSourcesDir();
String relativePath = qualifiedName.replace('.', File.separatorChar) + ".java";
File strippedFile = new File(strippedDir, relativePath);
Files.createParentDirs(strippedFile);
Files.write(parseResult.getSource(), strippedFile, options.fileUtil().getCharset());
input.setFile(new RegularInputFile(strippedFile.getPath(), relativePath));
}
}
Aggregations