Skip to content

Commit

Permalink
Make Options non-static
Browse files Browse the repository at this point in the history
	Change on 2016/12/20 by zgao <zgao@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=142566606
  • Loading branch information
zhouyanggao authored and tomball committed Dec 28, 2016
1 parent 6bd42d4 commit 230a25b
Show file tree
Hide file tree
Showing 81 changed files with 569 additions and 580 deletions.
Expand Up @@ -43,6 +43,7 @@
public class CycleFinder { public class CycleFinder {


private final Options options; private final Options options;
private final com.google.devtools.j2objc.Options j2objcOptions;
private final NameList blacklist; private final NameList blacklist;
private final List<List<Edge>> cycles = new ArrayList<>(); private final List<List<Edge>> cycles = new ArrayList<>();


Expand All @@ -56,15 +57,17 @@ public class CycleFinder {


public CycleFinder(Options options) throws IOException { public CycleFinder(Options options) throws IOException {
this.options = options; this.options = options;
com.google.devtools.j2objc.Options.load(new String[] { j2objcOptions = new com.google.devtools.j2objc.Options();

j2objcOptions.load(new String[] {
"-encoding", options.fileEncoding(), "-encoding", options.fileEncoding(),
"-source", options.sourceVersion().flag() "-source", options.sourceVersion().flag()
}); });
blacklist = getBlacklist(); blacklist = getBlacklist();
} }


private static Parser createParser(Options options) { private Parser createParser() {
Parser parser = Parser.newParser(); Parser parser = Parser.newParser(j2objcOptions);
parser.addSourcepathEntries(Strings.nullToEmpty(options.getSourcepath())); parser.addSourcepathEntries(Strings.nullToEmpty(options.getSourcepath()));
parser.addClasspathEntries(Strings.nullToEmpty(options.getBootclasspath())); parser.addClasspathEntries(Strings.nullToEmpty(options.getBootclasspath()));
parser.addClasspathEntries(Strings.nullToEmpty(options.getClasspath())); parser.addClasspathEntries(Strings.nullToEmpty(options.getClasspath()));
Expand Down Expand Up @@ -106,7 +109,7 @@ private File stripIncompatible(
for (int i = 0; i < sourceFileNames.size(); i++) { for (int i = 0; i < sourceFileNames.size(); i++) {
String fileName = sourceFileNames.get(i); String fileName = sourceFileNames.get(i);
RegularInputFile file = new RegularInputFile(fileName); RegularInputFile file = new RegularInputFile(fileName);
String source = FileUtil.readFile(file); String source = FileUtil.readFile(file, j2objcOptions.getCharset());
if (!source.contains("J2ObjCIncompatible")) { if (!source.contains("J2ObjCIncompatible")) {
continue; continue;
} }
Expand All @@ -127,7 +130,7 @@ private File stripIncompatible(
} }


public List<List<Edge>> findCycles() throws IOException { public List<List<Edge>> findCycles() throws IOException {
Parser parser = createParser(options); Parser parser = createParser();
NameList whitelist = NameList whitelist =
NameList.createFromFiles(options.getWhitelistFiles(), options.fileEncoding()); NameList.createFromFiles(options.getWhitelistFiles(), options.fileEncoding());
final GraphBuilder graphBuilder = new GraphBuilder(whitelist); final GraphBuilder graphBuilder = new GraphBuilder(whitelist);
Expand Down
38 changes: 20 additions & 18 deletions translator/src/main/java/com/google/devtools/j2objc/J2ObjC.java
Expand Up @@ -58,9 +58,9 @@ public static String getFileHeader(String sourceFileName) {
return UnicodeUtils.format(Options.getFileHeader(), sourceFileName); return UnicodeUtils.format(Options.getFileHeader(), sourceFileName);
} }


private static void checkErrors() { private static void checkErrors(boolean treatWarningsAsErrors) {
int errors = ErrorUtil.errorCount(); int errors = ErrorUtil.errorCount();
if (Options.treatWarningsAsErrors()) { if (treatWarningsAsErrors) {
errors += ErrorUtil.warningCount(); errors += ErrorUtil.warningCount();
} }
if (errors > 0) { if (errors > 0) {
Expand All @@ -69,14 +69,14 @@ private static void checkErrors() {
} }


@VisibleForTesting @VisibleForTesting
public static Parser createParser() { public static Parser createParser(Options options) {
Parser parser = Parser.newParser(); Parser parser = Parser.newParser(options);
parser.addClasspathEntries(Options.getClassPathEntries()); parser.addClasspathEntries(options.getClassPathEntries());
parser.addClasspathEntries(Options.getBootClasspath()); parser.addClasspathEntries(Options.getBootClasspath());
parser.addSourcepathEntries(Options.getSourcePathEntries()); parser.addSourcepathEntries(options.getSourcePathEntries());
parser.setIncludeRunningVMBootclasspath(false); parser.setIncludeRunningVMBootclasspath(false);
parser.setEncoding(Options.fileEncoding()); parser.setEncoding(options.fileEncoding());
parser.setEnableDocComments(Options.docCommentsEnabled()); parser.setEnableDocComments(options.docCommentsEnabled());
return parser; return parser;
} }


Expand All @@ -88,21 +88,21 @@ private static CodeReferenceMap loadDeadCodeMap() {
* Runs the entire J2ObjC pipeline. * Runs the entire J2ObjC pipeline.
* @param fileArgs the files to process, same format as command-line args to {@link #main}. * @param fileArgs the files to process, same format as command-line args to {@link #main}.
*/ */
public static void run(List<String> fileArgs) { public static void run(List<String> fileArgs, Options options) {
File preProcessorTempDir = null; File preProcessorTempDir = null;
File strippedSourcesDir = null; File strippedSourcesDir = null;
try { try {
Parser parser = createParser(); Parser parser = createParser(options);


List<ProcessingContext> inputs = Lists.newArrayList(); List<ProcessingContext> inputs = Lists.newArrayList();
GenerationBatch batch = new GenerationBatch(); GenerationBatch batch = new GenerationBatch(options);
batch.processFileArgs(fileArgs); batch.processFileArgs(fileArgs);
inputs.addAll(batch.getInputs()); inputs.addAll(batch.getInputs());
if (ErrorUtil.errorCount() > 0) { if (ErrorUtil.errorCount() > 0) {
return; return;
} }


AnnotationPreProcessor preProcessor = new AnnotationPreProcessor(); AnnotationPreProcessor preProcessor = new AnnotationPreProcessor(options);
List<ProcessingContext> generatedInputs = preProcessor.process(fileArgs, inputs); List<ProcessingContext> generatedInputs = preProcessor.process(fileArgs, inputs);
inputs.addAll(generatedInputs); // Ensure all generatedInputs are at end of input list. inputs.addAll(generatedInputs); // Ensure all generatedInputs are at end of input list.
preProcessorTempDir = preProcessor.getTemporaryDirectory(); preProcessorTempDir = preProcessor.getTemporaryDirectory();
Expand All @@ -123,7 +123,7 @@ public static void run(List<String> fileArgs) {
parser.prependSourcepathEntry(strippedSourcesDir.getPath()); parser.prependSourcepathEntry(strippedSourcesDir.getPath());
} }


Options.getHeaderMap().loadMappings(); options.getHeaderMap().loadMappings();
TranslationProcessor translationProcessor = TranslationProcessor translationProcessor =
new TranslationProcessor(parser, loadDeadCodeMap()); new TranslationProcessor(parser, loadDeadCodeMap());
translationProcessor.processInputs(inputs); translationProcessor.processInputs(inputs);
Expand All @@ -133,7 +133,7 @@ public static void run(List<String> fileArgs) {
} }
translationProcessor.postProcess(); translationProcessor.postProcess();


Options.getHeaderMap().printMappings(); options.getHeaderMap().printMappings();
} finally { } finally {
FileUtil.deleteTempDir(preProcessorTempDir); FileUtil.deleteTempDir(preProcessorTempDir);
FileUtil.deleteTempDir(strippedSourcesDir); FileUtil.deleteTempDir(strippedSourcesDir);
Expand All @@ -153,8 +153,10 @@ public static void main(String[] args) {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();


String[] files = null; String[] files = null;
Options options = new Options();

try { try {
files = Options.load(args); files = options.load(args);
if (files.length == 0) { if (files.length == 0) {
Options.usage("no source files"); Options.usage("no source files");
} }
Expand All @@ -163,14 +165,14 @@ public static void main(String[] args) {
System.exit(1); System.exit(1);
} }


run(Arrays.asList(files)); run(Arrays.asList(files), options);


TimingLevel timingLevel = Options.timingLevel(); TimingLevel timingLevel = options.timingLevel();
if (timingLevel == TimingLevel.TOTAL || timingLevel == TimingLevel.ALL) { if (timingLevel == TimingLevel.TOTAL || timingLevel == TimingLevel.ALL) {
System.out.printf("j2objc execution time: %d ms\n", System.currentTimeMillis() - startTime); System.out.printf("j2objc execution time: %d ms\n", System.currentTimeMillis() - startTime);
} }


// Run last, since it calls System.exit() with the number of errors. // Run last, since it calls System.exit() with the number of errors.
checkErrors(); checkErrors(options.treatWarningsAsErrors());
} }
} }

0 comments on commit 230a25b

Please sign in to comment.