Skip to content

Commit

Permalink
Added -source flag to cycle_finder, with shared flag parsing and vali…
Browse files Browse the repository at this point in the history
…dation with j2objc.

	Change on 2016/02/26 by tball <tball@google.com>
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=115691788
  • Loading branch information
tomball committed Mar 7, 2016
1 parent 11639f7 commit e066584
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 38 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ 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[] { "-encoding", options.fileEncoding() }); com.google.devtools.j2objc.Options.load(new String[] {
"-encoding", options.fileEncoding(),
"-source", options.sourceVersion().flag()
});
} }


private static JdtParser createParser(Options options) { private static JdtParser createParser(Options options) {
Expand Down Expand Up @@ -141,7 +144,7 @@ public void handleParsedUnit(String path, org.eclipse.jdt.core.dom.CompilationUn
outerResolver.run(unit); outerResolver.run(unit);
} }
}; };
parser.parseFiles(sourceFiles, handler); parser.parseFiles(sourceFiles, handler, options.sourceVersion());


FileUtil.deleteTempDir(strippedDir); FileUtil.deleteTempDir(strippedDir);


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import com.google.devtools.j2objc.util.SourceVersion;
import com.google.devtools.j2objc.util.Version; import com.google.devtools.j2objc.util.Version;


import java.io.BufferedReader; import java.io.BufferedReader;
Expand Down Expand Up @@ -57,6 +58,7 @@ class Options {
private List<String> blacklistFiles = Lists.newArrayList(); private List<String> blacklistFiles = Lists.newArrayList();
private List<String> sourceFiles = Lists.newArrayList(); private List<String> sourceFiles = Lists.newArrayList();
private String fileEncoding = System.getProperty("file.encoding", "UTF-8"); private String fileEncoding = System.getProperty("file.encoding", "UTF-8");
private SourceVersion sourceVersion = SourceVersion.JAVA_7;


public List<String> getSourceFiles() { public List<String> getSourceFiles() {
return sourceFiles; return sourceFiles;
Expand Down Expand Up @@ -115,6 +117,10 @@ public String fileEncoding() {
return fileEncoding; return fileEncoding;
} }


public SourceVersion sourceVersion() {
return sourceVersion;
}

public static void usage(String invalidUseMsg) { public static void usage(String invalidUseMsg) {
System.err.println("cycle_finder: " + invalidUseMsg); System.err.println("cycle_finder: " + invalidUseMsg);
System.err.println(usageMessage); System.err.println(usageMessage);
Expand Down Expand Up @@ -170,6 +176,15 @@ public static Options parse(String[] args) throws IOException {
usage("-encoding requires an argument"); usage("-encoding requires an argument");
} }
options.fileEncoding = args[nArg]; options.fileEncoding = args[nArg];
} else if (arg.equals("-source")) {
if (++nArg == args.length) {
usage("-source requires an argument");
}
try {
options.sourceVersion = SourceVersion.parse(args[nArg]);
} catch (IllegalArgumentException e) {
usage("invalid source release: " + args[nArg]);
}
} else if (arg.equals("-version")) { } else if (arg.equals("-version")) {
version(); version();
} else if (arg.startsWith("-h") || arg.equals("--help")) { } else if (arg.startsWith("-h") || arg.equals("--help")) {
Expand Down
2 changes: 1 addition & 1 deletion jre_emul/Makefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ objc_sources_manifest: $(OBJC_SOURCES_MANIFEST)
all_objc_sources_manifests: $(OBJC_SOURCES_MANIFESTS) all_objc_sources_manifests: $(OBJC_SOURCES_MANIFESTS)


find_cycles: cycle_finder_dist $(JAVA_SOURCES_MANIFEST) find_cycles: cycle_finder_dist $(JAVA_SOURCES_MANIFEST)
$(DIST_DIR)/cycle_finder -w cycle_whitelist.txt `cat $(JAVA_SOURCES_MANIFEST)` $(DIST_DIR)/cycle_finder -source 1.8 -w cycle_whitelist.txt `cat $(JAVA_SOURCES_MANIFEST)`


licenses: $(JRE_EMUL_LICENSES) licenses: $(JRE_EMUL_LICENSES)
@: @:
Expand Down
1 change: 1 addition & 0 deletions translator/Makefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ JAVA_SOURCES = \
util/NameTable.java \ util/NameTable.java \
util/PackagePrefixes.java \ util/PackagePrefixes.java \
util/ProGuardUsageParser.java \ util/ProGuardUsageParser.java \
util/SourceVersion.java \
util/TimeTracker.java \ util/TimeTracker.java \
util/TranslationUtil.java \ util/TranslationUtil.java \
util/UnicodeUtils.java \ util/UnicodeUtils.java \
Expand Down
28 changes: 10 additions & 18 deletions translator/src/main/java/com/google/devtools/j2objc/Options.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import com.google.devtools.j2objc.util.ErrorUtil; import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.FileUtil; import com.google.devtools.j2objc.util.FileUtil;
import com.google.devtools.j2objc.util.HeaderMap; import com.google.devtools.j2objc.util.HeaderMap;
import com.google.devtools.j2objc.util.PackagePrefixes; import com.google.devtools.j2objc.util.PackagePrefixes;
import com.google.devtools.j2objc.util.SourceVersion;
import com.google.devtools.j2objc.util.Version; import com.google.devtools.j2objc.util.Version;


import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaCore;
Expand All @@ -44,7 +44,6 @@
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
Expand Down Expand Up @@ -98,14 +97,11 @@ public class Options {


private PackagePrefixes packagePrefixes = new PackagePrefixes(); private PackagePrefixes packagePrefixes = new PackagePrefixes();


private static final Set<String> VALID_JAVA_VERSIONS = ImmutableSet.of("1.8", "1.7", "1.6",
"1.5");

// TODO(kirbs): Uncomment following lines and lines in OptionsTest when we enable automatic // TODO(kirbs): Uncomment following lines and lines in OptionsTest when we enable automatic
// version detection. Currently this is breaking pulse builds using 64 bit Java 8, and upgrading // version detection. Currently this is breaking pulse builds using 64 bit Java 8, and upgrading
// to Eclipse 4.5 is gated by bytecode errors in compiling junit. I won't have time to do a // to Eclipse 4.5 is gated by bytecode errors in compiling junit. I won't have time to do a
// more in depth root cause analysis on this. // more in depth root cause analysis on this.
private String sourceVersion = "1.7"; private SourceVersion sourceVersion = SourceVersion.JAVA_7;
// // The default source version number if not passed with -source is determined from the system // // The default source version number if not passed with -source is determined from the system
// // properties of the running java version after parsing the argument list. // // properties of the running java version after parsing the argument list.
// private String sourceVersion = null; // private String sourceVersion = null;
Expand Down Expand Up @@ -519,13 +515,9 @@ else if (arg.equals("--final-methods-as-functions")
usage("-source requires an argument"); usage("-source requires an argument");
} }
// Handle aliasing of version numbers as supported by javac. // Handle aliasing of version numbers as supported by javac.
if (args[nArg].length() == 1){ try {
sourceVersion = "1." + args[nArg]; sourceVersion = SourceVersion.parse(args[nArg]);
} else { } catch (IllegalArgumentException e) {
sourceVersion = args[nArg];
}
// Make sure that we were passed a valid release version.
if (!VALID_JAVA_VERSIONS.contains(sourceVersion)) {
usage("invalid source release: " + args[nArg]); usage("invalid source release: " + args[nArg]);
} }
} else if (arg.equals("-target")) { } else if (arg.equals("-target")) {
Expand Down Expand Up @@ -562,13 +554,12 @@ else if (arg.equals("--final-methods-as-functions")


// Pull source version from system properties if it is not passed with -source flag. // Pull source version from system properties if it is not passed with -source flag.
if (sourceVersion == null) { if (sourceVersion == null) {
sourceVersion = System.getProperty("java.version").substring(0, 3); sourceVersion = SourceVersion.parse(System.getProperty("java.version").substring(0, 3));
} }


// Java 6 had a 1G max heap limit, removed in Java 7. // Java 6 had a 1G max heap limit, removed in Java 7.
boolean java7orHigher = sourceVersion.charAt(2) >= '7';
if (batchTranslateMaximum == -1) { // Not set by flag. if (batchTranslateMaximum == -1) { // Not set by flag.
batchTranslateMaximum = java7orHigher ? 300 : 0; batchTranslateMaximum = SourceVersion.java7Minimum(sourceVersion) ? 300 : 0;
} }


int nFiles = args.length - nArg; int nFiles = args.length - nArg;
Expand Down Expand Up @@ -935,13 +926,14 @@ public static boolean shouldMapHeaders() {
return useSourceDirectories() || combineSourceJars() || includeGeneratedSources(); return useSourceDirectories() || combineSourceJars() || includeGeneratedSources();
} }


public static String getSourceVersion(){ public static SourceVersion getSourceVersion(){
return instance.sourceVersion; return instance.sourceVersion;
} }


// TODO(kirbs): Remove when Java 8 is fully supported. // TODO(kirbs): Remove when Java 8 is fully supported.
public static boolean isJava8Translator() { public static boolean isJava8Translator() {
return instance.forceIncompleteJava8Support && instance.sourceVersion.equals("1.8"); return instance.forceIncompleteJava8Support
&& SourceVersion.java8Minimum(instance.sourceVersion);
} }


public static boolean staticAccessorMethods() { public static boolean staticAccessorMethods() {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public List<ProcessingContext> process(Iterable<String> fileArgs,
compileArgs.add("-encoding"); compileArgs.add("-encoding");
compileArgs.add(Options.getCharset().name()); compileArgs.add(Options.getCharset().name());
compileArgs.add("-source"); compileArgs.add("-source");
compileArgs.add(Options.getSourceVersion()); compileArgs.add(Options.getSourceVersion().flag());
compileArgs.add("-s"); compileArgs.add("-s");
compileArgs.add(tmpDirPath); compileArgs.add(tmpDirPath);
compileArgs.add("-d"); compileArgs.add("-d");
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void handleParsedUnit(String path, CompilationUnit unit) {
} }
}; };
logger.finest("Processing batch of size " + batchInputs.size()); logger.finest("Processing batch of size " + batchInputs.size());
parser.parseFiles(paths, handler); parser.parseFiles(paths, handler, Options.getSourceVersion());


// Any remaining files in batchFiles has some kind of error. // Any remaining files in batchFiles has some kind of error.
for (ProcessingContext input : batchInputs) { for (ProcessingContext input : batchInputs) {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public class JdtParser {


private static final Logger logger = Logger.getLogger(JdtParser.class.getName()); private static final Logger logger = Logger.getLogger(JdtParser.class.getName());


private Map<String, String> compilerOptions = initCompilerOptions(); private Map<String, String> compilerOptions = initCompilerOptions(Options.getSourceVersion());
private List<String> classpathEntries = Lists.newArrayList(); private List<String> classpathEntries = Lists.newArrayList();
private List<String> sourcepathEntries = Lists.newArrayList(); private List<String> sourcepathEntries = Lists.newArrayList();
private String encoding = null; private String encoding = null;
private boolean includeRunningVMBootclasspath = true; private boolean includeRunningVMBootclasspath = true;


private static Map<String, String> initCompilerOptions() { private static Map<String, String> initCompilerOptions(SourceVersion sourceVersion) {
Map<String, String> compilerOptions = Maps.newHashMap(); Map<String, String> compilerOptions = Maps.newHashMap();
String version = Options.getSourceVersion(); String version = sourceVersion.flag();
compilerOptions.put(org.eclipse.jdt.core.JavaCore.COMPILER_SOURCE, version); compilerOptions.put(org.eclipse.jdt.core.JavaCore.COMPILER_SOURCE, version);
compilerOptions.put(org.eclipse.jdt.core.JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, version); compilerOptions.put(org.eclipse.jdt.core.JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, version);
compilerOptions.put(org.eclipse.jdt.core.JavaCore.COMPILER_COMPLIANCE, version); compilerOptions.put(org.eclipse.jdt.core.JavaCore.COMPILER_COMPLIANCE, version);
Expand Down Expand Up @@ -135,7 +135,7 @@ public CompilationUnit parseWithBindings(String unitName, String source) {
} }


private CompilationUnit parse(String unitName, String source, boolean resolveBindings) { private CompilationUnit parse(String unitName, String source, boolean resolveBindings) {
ASTParser parser = newASTParser(resolveBindings); ASTParser parser = newASTParser(resolveBindings, Options.getSourceVersion());
parser.setUnitName(unitName); parser.setUnitName(unitName);
parser.setSource(source.toCharArray()); parser.setSource(source.toCharArray());
CompilationUnit unit = (CompilationUnit) parser.createAST(null); CompilationUnit unit = (CompilationUnit) parser.createAST(null);
Expand All @@ -154,8 +154,9 @@ public interface Handler {
public void handleParsedUnit(String path, CompilationUnit unit); public void handleParsedUnit(String path, CompilationUnit unit);
} }


public void parseFiles(Collection<String> paths, final Handler handler) { public void parseFiles(Collection<String> paths, final Handler handler,
ASTParser parser = newASTParser(true); SourceVersion sourceVersion) {
ASTParser parser = newASTParser(true, sourceVersion);
FileASTRequestor astRequestor = new FileASTRequestor() { FileASTRequestor astRequestor = new FileASTRequestor() {
@Override @Override
public void acceptAST(String sourceFilePath, CompilationUnit ast) { public void acceptAST(String sourceFilePath, CompilationUnit ast) {
Expand All @@ -174,9 +175,9 @@ public void acceptAST(String sourceFilePath, CompilationUnit ast) {
} }


@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private ASTParser newASTParser(boolean resolveBindings) { private ASTParser newASTParser(boolean resolveBindings, SourceVersion sourceVersion) {
ASTParser parser; ASTParser parser;
if (Options.isJava8Translator()) { if (SourceVersion.java8Minimum(sourceVersion)) {
parser = ASTParser.newParser(AST.JLS8); parser = ASTParser.newParser(AST.JLS8);
} else { } else {
parser = ASTParser.newParser(AST.JLS4); // Java 7 parser = ASTParser.newParser(AST.JLS4); // Java 7
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.devtools.j2objc.util;

/**
* Supported Java versions, used by the -source and -target flags.
*/
public enum SourceVersion {

JAVA_8(8, "1.8"), JAVA_7(7, "1.7"), JAVA_6(6, "1.6"), JAVA_5(5, "1.5");

private final int version;
private final String flag;

SourceVersion(int version, String flag) {
this.flag = flag;
this.version = version;
}

public int version() {
return version;
}

public String flag() {
return flag;
}

public static SourceVersion parse(String flag) {
String fullFlag = flag.length() == 1 ? "1." + flag : flag;
for (SourceVersion sv : values()) {
if (sv.flag.equals(fullFlag)) {
return sv;
}
}
throw new IllegalArgumentException(flag);
}

public static boolean java7Minimum(SourceVersion sourceVersion) {
return sourceVersion.version >= 7;
}

public static boolean java8Minimum(SourceVersion sourceVersion) {
return sourceVersion.version >= 8;
}

@Override
public String toString() {
return flag;
}
}
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@


package com.google.devtools.j2objc; package com.google.devtools.j2objc;


import com.google.devtools.j2objc.util.SourceVersion;

import java.io.IOException; import java.io.IOException;


/** /**
Expand All @@ -38,39 +40,39 @@ public void testSourceVersionFlags() throws IOException {
// System.setProperty("java.version", "1.8.0_45"); // System.setProperty("java.version", "1.8.0_45");
// Options.reset(); // Options.reset();
// Options.load(new String[] {}); // Options.load(new String[] {});
// assertEquals("1.8", Options.getSourceVersion()); // assertEquals(SourceVersion.JAVA_8, Options.getSourceVersion());
// //
// System.setProperty("java.version", "1.6.0"); // System.setProperty("java.version", "1.6.0");
// Options.reset(); // Options.reset();
// Options.load(new String[] {}); // Options.load(new String[] {});
// assertEquals("1.6", Options.getSourceVersion()); // assertEquals(SourceVersion.JAVA_6, Options.getSourceVersion());
// //
// System.setProperty("java.version", "1.7"); // System.setProperty("java.version", "1.7");
// Options.reset(); // Options.reset();
// Options.load(new String[] {}); // Options.load(new String[] {});
// assertEquals("1.7", Options.getSourceVersion()); // assertEquals(SourceVersion.JAVA_7, Options.getSourceVersion());
// //
// // Reset the java.version property to prevent any unexpected jvm behavior after testing. // // Reset the java.version property to prevent any unexpected jvm behavior after testing.
// System.setProperty("java.version", javaVersion); // System.setProperty("java.version", javaVersion);


String[] argsJavaSource = "-source 1.6".split(" "); String[] argsJavaSource = "-source 1.6".split(" ");
Options.load(argsJavaSource); Options.load(argsJavaSource);
assertEquals("1.6", Options.getSourceVersion()); assertEquals(SourceVersion.JAVA_6, Options.getSourceVersion());


argsJavaSource = "-source 1.7".split(" "); argsJavaSource = "-source 1.7".split(" ");
Options.load(argsJavaSource); Options.load(argsJavaSource);
assertEquals("1.7", Options.getSourceVersion()); assertEquals(SourceVersion.JAVA_7, Options.getSourceVersion());


argsJavaSource = "-source 1.8".split(" "); argsJavaSource = "-source 1.8".split(" ");
Options.load(argsJavaSource); Options.load(argsJavaSource);
assertEquals("1.8", Options.getSourceVersion()); assertEquals(SourceVersion.JAVA_8, Options.getSourceVersion());
} }


public void testSourceVersionFlagAliases() throws IOException { public void testSourceVersionFlagAliases() throws IOException {
// Check that version aliases work correctly. // Check that version aliases work correctly.
String[] argsJavaSource = "-source 8".split(" "); String[] argsJavaSource = "-source 8".split(" ");
Options.load(argsJavaSource); Options.load(argsJavaSource);
assertEquals("1.8", Options.getSourceVersion()); assertEquals(SourceVersion.JAVA_8, Options.getSourceVersion());
} }


public void testTargetVersionFlags() throws IOException { public void testTargetVersionFlags() throws IOException {
Expand Down

0 comments on commit e066584

Please sign in to comment.