use of com.google.devtools.j2objc.util.Parser in project j2objc by google.
the class CycleFinder method stripIncompatible.
private File stripIncompatible(List<String> sourceFileNames, Parser parser) throws IOException {
File strippedDir = null;
for (int i = 0; i < sourceFileNames.size(); i++) {
String fileName = sourceFileNames.get(i);
RegularInputFile file = new RegularInputFile(fileName);
String source = j2objcOptions.fileUtil().readFile(file);
if (!source.contains("J2ObjCIncompatible")) {
continue;
}
if (strippedDir == null) {
strippedDir = Files.createTempDir();
parser.prependSourcepathEntry(strippedDir.getPath());
}
Parser.ParseResult parseResult = parser.parseWithoutBindings(file, source);
String qualifiedName = parseResult.mainTypeName();
parseResult.stripIncompatibleSource();
String relativePath = qualifiedName.replace('.', File.separatorChar) + ".java";
File strippedFile = new File(strippedDir, relativePath);
Files.createParentDirs(strippedFile);
Files.write(parseResult.getSource(), strippedFile, Charset.forName(options.fileEncoding()));
sourceFileNames.set(i, strippedFile.getPath());
}
return strippedDir;
}
use of com.google.devtools.j2objc.util.Parser in project j2objc by google.
the class TreeShaker method stripIncompatible.
private File stripIncompatible(List<String> sourceFileNames, Parser parser) throws IOException {
File strippedDir = null;
for (int i = 0; i < sourceFileNames.size(); i++) {
String fileName = sourceFileNames.get(i);
RegularInputFile file = new RegularInputFile(fileName);
String source = j2objcOptions.fileUtil().readFile(file);
if (!source.contains("J2ObjCIncompatible")) {
continue;
}
if (strippedDir == null) {
strippedDir = Files.createTempDir();
parser.prependSourcepathEntry(strippedDir.getPath());
}
Parser.ParseResult parseResult = parser.parseWithoutBindings(file, source);
String qualifiedName = parseResult.mainTypeName();
parseResult.stripIncompatibleSource();
String relativePath = qualifiedName.replace('.', File.separatorChar) + ".java";
File strippedFile = new File(strippedDir, relativePath);
Files.createParentDirs(strippedFile);
Files.write(parseResult.getSource(), strippedFile, j2objcOptions.fileUtil().getCharset());
sourceFileNames.set(i, strippedFile.getPath());
}
return strippedDir;
}
use of com.google.devtools.j2objc.util.Parser in project j2objc by google.
the class J2ObjC method run.
/**
* Runs the entire J2ObjC pipeline.
* @param fileArgs the files to process, same format as command-line args to {@link #main}.
*/
public static void run(List<String> fileArgs, Options options) {
File preProcessorTempDir = null;
File strippedSourcesDir = null;
Parser parser = null;
try {
List<ProcessingContext> inputs = Lists.newArrayList();
GenerationBatch batch = new GenerationBatch(options);
batch.processFileArgs(fileArgs);
inputs.addAll(batch.getInputs());
if (ErrorUtil.errorCount() > 0) {
return;
}
parser = createParser(options);
Parser.ProcessingResult processingResult = parser.processAnnotations(fileArgs, inputs);
List<ProcessingContext> generatedInputs = processingResult.getGeneratedSources();
// Ensure all generatedInputs are at end of input list.
inputs.addAll(generatedInputs);
preProcessorTempDir = processingResult.getSourceOutputDirectory();
if (ErrorUtil.errorCount() > 0) {
return;
}
if (preProcessorTempDir != null) {
parser.addSourcepathEntry(preProcessorTempDir.getAbsolutePath());
}
InputFilePreprocessor inputFilePreprocessor = new InputFilePreprocessor(parser);
inputFilePreprocessor.processInputs(inputs);
if (ErrorUtil.errorCount() > 0) {
return;
}
strippedSourcesDir = inputFilePreprocessor.getStrippedSourcesDir();
if (strippedSourcesDir != null) {
parser.prependSourcepathEntry(strippedSourcesDir.getPath());
}
options.getHeaderMap().loadMappings();
TranslationProcessor translationProcessor = new TranslationProcessor(parser, loadDeadCodeMap());
translationProcessor.processInputs(inputs);
translationProcessor.processBuildClosureDependencies();
if (ErrorUtil.errorCount() > 0) {
return;
}
translationProcessor.postProcess();
options.getHeaderMap().printMappings();
} finally {
if (parser != null) {
try {
parser.close();
} catch (IOException e) {
ErrorUtil.error(e.getMessage());
}
}
Set<String> tempDirs = options.fileUtil().getTempDirs();
for (String dir : tempDirs) {
FileUtil.deleteTempDir(new File(dir));
}
FileUtil.deleteTempDir(preProcessorTempDir);
FileUtil.deleteTempDir(strippedSourcesDir);
}
}
use of com.google.devtools.j2objc.util.Parser in project j2objc by google.
the class CycleFinder method createParser.
private Parser createParser() {
Parser parser = Parser.newParser(j2objcOptions);
parser.addSourcepathEntries(Strings.nullToEmpty(options.getSourcepath()));
parser.addClasspathEntries(Strings.nullToEmpty(options.getBootclasspath()));
parser.addClasspathEntries(Strings.nullToEmpty(options.getClasspath()));
return parser;
}
use of com.google.devtools.j2objc.util.Parser in project j2objc by google.
the class CycleFinder method findCycles.
public List<List<Edge>> findCycles() throws IOException {
Parser parser = createParser();
NameList whitelist = NameList.createFromFiles(options.getWhitelistFiles(), options.fileEncoding());
final GraphBuilder graphBuilder = new GraphBuilder(whitelist);
List<String> sourceFiles = options.getSourceFiles();
File strippedDir = stripIncompatible(sourceFiles, parser);
Parser.Handler handler = new Parser.Handler() {
@Override
public void handleParsedUnit(String path, CompilationUnit unit) {
new LambdaTypeElementAdder(unit).run();
new OuterReferenceResolver(unit).run();
graphBuilder.visitAST(unit);
}
};
parser.parseFiles(sourceFiles, handler, options.sourceVersion());
FileUtil.deleteTempDir(strippedDir);
if (ErrorUtil.errorCount() > 0) {
return null;
}
// Construct the graph and find cycles.
ReferenceGraph graph = graphBuilder.constructGraph().getGraph();
for (ReferenceGraph component : graph.getStronglyConnectedComponents(getSeedNodes(graph))) {
handleStronglyConnectedComponent(component);
}
return cycles;
}
Aggregations