Skip to content

Commit

Permalink
Moves logic for determining "outputPath" and "name" fields into Gener…
Browse files Browse the repository at this point in the history
…ationUnit so that the logic is in a single place and setters aren't required.

	Change on 2015/04/23 by kstanger <kstanger@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=91885304
  • Loading branch information
kstanger authored and tomball committed Apr 24, 2015
1 parent caf6b08 commit 964f438
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 102 deletions.
1 change: 1 addition & 0 deletions translator/Makefile
Expand Up @@ -159,6 +159,7 @@ JAVA_SOURCES = \
ast/VariableDeclarationStatement.java \
ast/WhileStatement.java \
gen/AbstractSourceGenerator.java \
gen/GenerationUnit.java \
gen/JavadocGenerator.java \
gen/LiteralGenerator.java \
gen/MetadataGenerator.java \
Expand Down
Expand Up @@ -15,12 +15,10 @@
package com.google.devtools.j2objc;

import com.google.common.base.Preconditions;
import com.google.devtools.j2objc.ast.PackageDeclaration;
import com.google.devtools.j2objc.ast.TreeConverter;
import com.google.devtools.j2objc.file.InputFile;
import com.google.devtools.j2objc.gen.GenerationUnit;
import com.google.devtools.j2objc.types.Types;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.FileUtil;
import com.google.devtools.j2objc.util.JdtParser;
import com.google.devtools.j2objc.util.NameTable;
Expand Down Expand Up @@ -147,10 +145,6 @@ protected void processSource(InputFile file, GenerationUnit generationUnit) {
processCompilationUnit(generationUnit, compilationUnit, file);
}

private boolean isFullyParsed(GenerationUnit unit) {
return unit.getCompilationUnits().size() == unit.getInputFiles().size();
}

/**
* Callback invoked when a file is parsed.
*/
Expand All @@ -163,16 +157,7 @@ protected void processCompilationUnit(
= TreeConverter.convertCompilationUnit(unit, file, source, nameTable);
genUnit.addCompilationUnit(translatedUnit);

if (isFullyParsed(genUnit)) {
ensureOutputPath(genUnit);
if (genUnit.getName() == null) {
// We infer names from the AST. If size > 1 we shouldn't reach here.
assert genUnit.getCompilationUnits().size() == 1;
genUnit.setName(
NameTable.camelCaseQualifiedName(
NameTable.getMainTypeFullName(translatedUnit)));
}

if (genUnit.isFullyParsed()) {
logger.finest("Processing compiled unit " + genUnit.getName()
+ " of size " + genUnit.getCompilationUnits().size());
processCompiledGenerationUnit(genUnit);
Expand All @@ -186,36 +171,6 @@ protected void processCompilationUnit(

protected abstract void processCompiledGenerationUnit(GenerationUnit unit);

/**
* Sets the output path if there isn't one already.
* For example, foo/bar/Mumble.java translates to $(OUTPUT_DIR)/foo/bar/Mumble.
* If --no-package-directories is specified, though, the output file is $(OUTPUT_DIR)/Mumble.
* <p>
* Note: class names are still camel-cased to avoid name collisions.
*/
static void ensureOutputPath(GenerationUnit unit) {
String result = unit.getOutputPath();
if (result == null) {
// We can only infer the output path if there's one compilation unit.
assert unit.getCompilationUnits().size() == 1;
com.google.devtools.j2objc.ast.CompilationUnit node = unit.getCompilationUnits().get(0);
PackageDeclaration pkg = node.getPackage();
if (Options.usePackageDirectories() && !pkg.isDefaultPackage()) {
result = pkg.getName().getFullyQualifiedName().replace('.', File.separatorChar);
result += File.separatorChar + node.getMainTypeName();
} else {
result = node.getMainTypeName();
}

// Make sure the name is legal...
if (node.getMainTypeName().equals(NameTable.PACKAGE_INFO_MAIN_TYPE)) {
result = result.replace(NameTable.PACKAGE_INFO_MAIN_TYPE, NameTable.PACKAGE_INFO_FILE_NAME);
}
}

unit.setOutputPath(result);
}

protected TimeTracker getTicker(String name) {
if (logger.isLoggable(Level.FINEST)) {
return TimeTracker.start(name);
Expand Down
Expand Up @@ -24,15 +24,12 @@
import com.google.devtools.j2objc.gen.GenerationUnit;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.FileUtil;
import com.google.devtools.j2objc.util.NameTable;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -70,10 +67,6 @@ public static GenerationBatch fromUnit(CompilationUnit node, String name) {
assert newBatch.units.size() == 1; // One input file -> one GenerationUnit.
GenerationUnit unit = newBatch.units.get(0);
unit.addCompilationUnit(node);
FileProcessor.ensureOutputPath(unit);
if (unit.getName() == null) {
unit.setName(NameTable.camelCaseQualifiedName(NameTable.getMainTypeFullName(node)));
}
return newBatch;
}

Expand Down Expand Up @@ -172,7 +165,7 @@ private void processJarFile(String filename) {
zfile.close(); // Also closes input stream.
}
if (Options.combineSourceJars()) {
addCombinedJar(filename, inputFiles);
units.add(GenerationUnit.newCombinedJarUnit(filename, inputFiles));
} else {
for (InputFile file : inputFiles) {
addSource(file);
Expand All @@ -186,33 +179,12 @@ private void processJarFile(String filename) {
}
}

private void addCombinedJar(String filename, List<InputFile> inputFiles) {
String outputPath = filename;
if (outputPath.lastIndexOf("/") < outputPath.lastIndexOf(".")) {
outputPath = outputPath.substring(0, outputPath.lastIndexOf("."));
}
GenerationUnit unit = new GenerationUnit(filename);
unit.setOutputPath(outputPath);
unit.setName(NameTable.camelCasePath(outputPath));
for (InputFile file : inputFiles) {
unit.addInputFile(file);
}
units.add(unit);
}

/**
* Adds the given InputFile to this GenerationBatch,
* creating GenerationUnits and inferring unit names/output paths as necessary.
*/
protected void addSource(InputFile file) {
GenerationUnit unit = new GenerationUnit(file.getPath());
unit.addInputFile(file);
if (Options.useSourceDirectories()) {
String outputPath = file.getUnitName();
outputPath = outputPath.substring(0, outputPath.lastIndexOf(".java"));
unit.setOutputPath(outputPath);
}
units.add(unit);
public void addSource(InputFile file) {
units.add(GenerationUnit.newSingleFileUnit(file));
}

@VisibleForTesting
Expand Down
Expand Up @@ -138,9 +138,7 @@ public void processBatch(GenerationBatch units) {
continue;
}

GenerationUnit genUnit = new GenerationUnit(file.getPath());
genUnit.addInputFile(file);
processGenerationUnit(genUnit);
processGenerationUnit(GenerationUnit.newSingleFileUnit(file));
// Possible for a batch to add to pending files.
if (pendingFiles.isEmpty()) {
processBatch();
Expand Down
Expand Up @@ -13,12 +13,17 @@
*/
package com.google.devtools.j2objc.gen;

import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.ast.PackageDeclaration;
import com.google.devtools.j2objc.file.InputFile;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2objc.util.UnicodeUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Nullable;
Expand All @@ -39,10 +44,34 @@ public class GenerationUnit {
private final String sourceName;
private final List<String> errors = new ArrayList<String>();

public GenerationUnit(String sourceName) {
private GenerationUnit(String sourceName) {
this.sourceName = sourceName;
}

public static GenerationUnit newSingleFileUnit(InputFile file) {
GenerationUnit unit = new GenerationUnit(file.getPath());
unit.inputFiles.add(file);
if (Options.useSourceDirectories()) {
String outputPath = file.getUnitName();
outputPath = outputPath.substring(0, outputPath.lastIndexOf(".java"));
unit.outputPath = outputPath;
}
return unit;
}

public static GenerationUnit newCombinedJarUnit(
String filename, Collection<? extends InputFile> inputFiles) {
String outputPath = filename;
if (outputPath.lastIndexOf(File.separatorChar) < outputPath.lastIndexOf(".")) {
outputPath = outputPath.substring(0, outputPath.lastIndexOf("."));
}
GenerationUnit unit = new GenerationUnit(filename);
unit.outputPath = outputPath;
unit.name = UnicodeUtils.asValidObjcIdentifier(NameTable.camelCasePath(outputPath));
unit.inputFiles.addAll(inputFiles);
return unit;
}

/**
* Gets the 'source name' of this GenerationUnit. Might not be a .java file,
* but if given, should probably be an actual file somewhere, like a .jar.
Expand All @@ -60,18 +89,6 @@ public String getName() {
return name;
}

/**
* Sets the name of this GenerationUnit after munging it into a valid Objective-C identifier.
*/
public void setName(String name) {
this.name = UnicodeUtils.asValidObjcIdentifier(name);
}

public void addInputFile(InputFile file) {
assert compilationUnits.isEmpty(); // Probably shouldn't be adding files when this isn't empty.
inputFiles.add(file);
}

public List<InputFile> getInputFiles() {
return inputFiles;
}
Expand All @@ -89,6 +106,38 @@ public List<CompilationUnit> getCompilationUnits() {
public void addCompilationUnit(CompilationUnit unit) {
assert compilationUnits.size() < inputFiles.size();
compilationUnits.add(unit);

if (name == null) {
assert inputFiles.size() == 1;
name = UnicodeUtils.asValidObjcIdentifier(NameTable.getMainTypeFullName(unit));
}
if (outputPath == null) {
// We can only infer the output path if there's one compilation unit.
assert inputFiles.size() == 1;
outputPath = getDefaultOutputPath(unit);
}
}

public boolean isFullyParsed() {
return compilationUnits.size() == inputFiles.size();
}

/**
* Gets the output path if there isn't one already.
* For example, foo/bar/Mumble.java translates to $(OUTPUT_DIR)/foo/bar/Mumble.
* If --no-package-directories is specified, though, the output file is $(OUTPUT_DIR)/Mumble.
*/
private static String getDefaultOutputPath(CompilationUnit unit) {
String path = unit.getMainTypeName();
if (path.equals(NameTable.PACKAGE_INFO_MAIN_TYPE)) {
path = NameTable.PACKAGE_INFO_FILE_NAME;
}
PackageDeclaration pkg = unit.getPackage();
if (Options.usePackageDirectories() && !pkg.isDefaultPackage()) {
path = pkg.getName().getFullyQualifiedName().replace('.', File.separatorChar)
+ File.separatorChar + path;
}
return path;
}

/**
Expand All @@ -106,10 +155,6 @@ public String getOutputPath() {
return outputPath;
}

public void setOutputPath(String outputPath) {
this.outputPath = outputPath;
}

/**
* A unit is broken if we couldn't read all of its input files.
* We mark it as broken so some processors may decide to halt for broken GenerationUnits.
Expand Down
Expand Up @@ -316,12 +316,11 @@ protected String translateSourceFile(String source, String typeName, String file

protected String translateCombinedFiles(String outputPath, String extension, String... sources)
throws IOException {
GenerationUnit unit = new GenerationUnit(outputPath + ".testfile");
unit.setOutputPath(outputPath);
unit.setName(NameTable.camelCasePath(outputPath));
List<RegularInputFile> inputFiles = Lists.newArrayList();
for (String sourceFile : sources) {
unit.addInputFile(new RegularInputFile(tempDir + "/" + sourceFile, sourceFile));
inputFiles.add(new RegularInputFile(tempDir + "/" + sourceFile, sourceFile));
}
GenerationUnit unit = GenerationUnit.newCombinedJarUnit(outputPath + ".testfile", inputFiles);
GenerationBatch batch = new GenerationBatch();
batch.addGenerationUnit(unit);
parser.setEnableDocComments(Options.docCommentsEnabled());
Expand Down
Expand Up @@ -73,7 +73,7 @@ public void testUnicodeHeaderGuardTranslation() throws IOException {
// Egyptian heiroglyph letters outside UCS-2, requiring two UTF-16 chars
translation = translateSourceFile(
"public class egyptian\uD80C\uDC00 {}", "egyptian\uD80C\uDC00", "egyptian\uD80C\uDC00.h");
assertTranslation(translation, "#ifndef _Egyptian\uD80C\uDC00_H");
assertTranslation(translation, "#ifndef _egyptian\uD80C\uDC00_H");
}

public void testDeprecatedTypeNameTranslation() throws IOException {
Expand Down

0 comments on commit 964f438

Please sign in to comment.