Skip to content

Commit

Permalink
Brings all header mapping logic into one place with a new HeaderMap c…
Browse files Browse the repository at this point in the history
…lass.

	Change on 2015/05/04 by kstanger <kstanger@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=92721258
  • Loading branch information
kstanger authored and tomball committed May 12, 2015
1 parent 7206acf commit 806de7c
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 188 deletions.
1 change: 1 addition & 0 deletions translator/Makefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ JAVA_SOURCES = \
util/DeadCodeMap.java \ util/DeadCodeMap.java \
util/ErrorUtil.java \ util/ErrorUtil.java \
util/FileUtil.java \ util/FileUtil.java \
util/HeaderMap.java \
util/JdtParser.java \ util/JdtParser.java \
util/NameTable.java \ util/NameTable.java \
util/ProGuardUsageParser.java \ util/ProGuardUsageParser.java \
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static void run(List<String> fileArgs) {
parser.prependSourcepathEntry(strippedSourcesDir.getPath()); parser.prependSourcepathEntry(strippedSourcesDir.getPath());
} }


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

Options.getHeaderMap().printMappings();
} finally { } finally {
FileUtil.deleteTempDir(preProcessorTempDir); FileUtil.deleteTempDir(preProcessorTempDir);
FileUtil.deleteTempDir(strippedSourcesDir); FileUtil.deleteTempDir(strippedSourcesDir);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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 java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
Expand Down Expand Up @@ -65,8 +66,7 @@ public class Options {
private boolean emitLineDirectives = false; private boolean emitLineDirectives = false;
private boolean warningsAsErrors = false; private boolean warningsAsErrors = false;
private boolean deprecatedDeclarations = false; private boolean deprecatedDeclarations = false;
// Keys are class names, values are header paths (with a .h). private HeaderMap headerMap = new HeaderMap();
private Map<String, String> headerMappings = Maps.newLinkedHashMap();
private File outputHeaderMappingFile = null; private File outputHeaderMappingFile = null;
private Map<String, String> classMappings = Maps.newLinkedHashMap(); private Map<String, String> classMappings = Maps.newLinkedHashMap();
private Map<String, String> methodMappings = Maps.newLinkedHashMap(); private Map<String, String> methodMappings = Maps.newLinkedHashMap();
Expand Down Expand Up @@ -566,8 +566,8 @@ public static Map<String, String> getMethodMappings() {
return instance.methodMappings; return instance.methodMappings;
} }


public static Map<String, String> getHeaderMappings() { public static HeaderMap getHeaderMap() {
return instance.headerMappings; return instance.headerMap;
} }


@Nullable @Nullable
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ private void processRegularSource(ProcessingContext input) throws IOException {
} }
String qualifiedName = FileUtil.getQualifiedMainTypeName(file, compilationUnit); String qualifiedName = FileUtil.getQualifiedMainTypeName(file, compilationUnit);
if (Options.shouldMapHeaders()) { if (Options.shouldMapHeaders()) {
Options.getHeaderMappings().put( Options.getHeaderMap().put(qualifiedName, input.getGenerationUnit().getOutputPath() + ".h");
qualifiedName, input.getGenerationUnit().getOutputPath() + ".h");
} }
if (doIncompatibleStripping) { if (doIncompatibleStripping) {
String newSource = J2ObjCIncompatibleStripper.strip(source, compilationUnit); String newSource = J2ObjCIncompatibleStripper.strip(source, compilationUnit);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,21 +56,11 @@
import com.google.devtools.j2objc.types.Import; import com.google.devtools.j2objc.types.Import;
import com.google.devtools.j2objc.util.DeadCodeMap; import com.google.devtools.j2objc.util.DeadCodeMap;
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.JdtParser; import com.google.devtools.j2objc.util.JdtParser;
import com.google.devtools.j2objc.util.TimeTracker; import com.google.devtools.j2objc.util.TimeTracker;


import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.ITypeBinding;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
Expand Down Expand Up @@ -310,8 +300,6 @@ protected void handleError(ProcessingContext input) {
} }


public void postProcess() { public void postProcess() {
printHeaderMappings();

if (logger.isLoggable(Level.INFO)) { if (logger.isLoggable(Level.INFO)) {
int nFiles = processedCount; int nFiles = processedCount;
System.out.println(String.format( System.out.println(String.format(
Expand Down Expand Up @@ -350,59 +338,4 @@ private TimeTracker getTicker(String name) {
return TimeTracker.noop(); return TimeTracker.noop();
} }
} }

@VisibleForTesting
public static void printHeaderMappings() {
if (Options.getOutputHeaderMappingFile() != null) {
Map<String, String> headerMappings = Options.getHeaderMappings();
File outputMappingFile = Options.getOutputHeaderMappingFile();

try {
if (!outputMappingFile.exists()) {
outputMappingFile.getParentFile().mkdirs();
outputMappingFile.createNewFile();
}
PrintWriter writer = new PrintWriter(outputMappingFile);

for (String className : headerMappings.keySet()) {
writer.println(String.format("%s=%s", className, headerMappings.get(className)));
}

writer.close();
} catch (IOException e) {
ErrorUtil.error(e.getMessage());
}
}
}

public static void loadHeaderMappings() {
Map<String, String> headerMappings = Options.getHeaderMappings();

List<String> headerMappingFiles = Options.getHeaderMappingFiles();
List<Properties> headerMappingProps = new ArrayList<Properties>();

try {
if (headerMappingFiles == null) {
try {
headerMappingProps.add(FileUtil.loadProperties(Options.DEFAULT_HEADER_MAPPING_FILE));
} catch (FileNotFoundException e) {
// Don't fail if mappings aren't configured and the default mapping is absent.
}
} else {
for (String resourceName : headerMappingFiles) {
headerMappingProps.add(FileUtil.loadProperties(resourceName));
}
}
} catch (IOException e) {
ErrorUtil.error(e.getMessage());
}

for (Properties mappings : headerMappingProps) {
Enumeration<?> keyIterator = mappings.propertyNames();
while (keyIterator.hasMoreElements()) {
String key = (String) keyIterator.nextElement();
headerMappings.put(key, mappings.getProperty(key));
}
}
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.devtools.j2objc.Options; import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.ast.CompilationUnit; import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.NameTable; import com.google.devtools.j2objc.util.NameTable;


import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.ITypeBinding;
Expand All @@ -45,46 +44,6 @@ public class Import implements Comparable<Import> {
private final String mainTypeName; private final String mainTypeName;
private final String importFileName; private final String importFileName;


/**
* Public packages included by the j2objc libraries. This list is necessary so
* that when package directories are suppressed, the platform headers can still
* be found.
*/
// TODO(tball): move this list to a distributed file, perhaps generated by build.
private static final Set<String> PLATFORM_PACKAGES = Sets.newHashSet(new String[] {
"android",
"com.android.internal.util",
"com.google.android",
"com.google.common",
"com.google.common.annotations",
"com.google.common.base",
"com.google.common.cache",
"com.google.common.collect",
"com.google.common.hash",
"com.google.common.io",
"com.google.common.math",
"com.google.common.net",
"com.google.common.primitives",
"com.google.common.util",
"com.google.j2objc",
"com.google.protobuf",
"dalvik",
"java",
"javax",
"junit",
"libcore",
"org.apache.harmony",
"org.hamcrest",
"org.json",
"org.junit",
"org.kxml2",
"org.mockito",
"org.w3c",
"org.xml.sax",
"org.xmlpull",
"sun.misc",
});

private Import(ITypeBinding type, NameTable nameTable) { private Import(ITypeBinding type, NameTable nameTable) {
this.type = type; this.type = type;
this.typeName = nameTable.getFullName(type); this.typeName = nameTable.getFullName(type);
Expand All @@ -94,7 +53,7 @@ private Import(ITypeBinding type, NameTable nameTable) {
} }
this.mainType = mainType; this.mainType = mainType;
this.mainTypeName = nameTable.getFullName(mainType); this.mainTypeName = nameTable.getFullName(mainType);
this.importFileName = getImportFileName(mainType) + ".h"; this.importFileName = Options.getHeaderMap().get(mainType);
} }


public ITypeBinding getType() { public ITypeBinding getType() {
Expand All @@ -113,47 +72,6 @@ public String getMainTypeName() {
return mainTypeName; return mainTypeName;
} }


private static String getImportFileName(ITypeBinding type) {
String javaName = type.getErasure().getQualifiedName();
if (type instanceof IOSTypeBinding) {
// Some IOS types are declared in a different header.
String header = ((IOSTypeBinding) type).getHeader();
if (header != null) {
javaName = header;
}
}

String mappedHeader = Options.getHeaderMappings().get(javaName);
if (mappedHeader == null) {
// Use package directories for platform classes if they do not have an entry in the header
// mapping.
if (Options.usePackageDirectories() || isPlatformClass(javaName)) {
return javaName.replace('.', '/');
} else {
return javaName.substring(javaName.lastIndexOf('.') + 1);
}
} else {
if (mappedHeader.substring(mappedHeader.length() - 2).equals(".h")) {
mappedHeader = mappedHeader.substring(0, mappedHeader.length() - 2);
} else {
ErrorUtil.error("filename \"" + mappedHeader + "\" is not a valid header file name");
}
return mappedHeader;
}
}

private static boolean isPlatformClass(String className) {
String[] parts = className.split("\\.");
String pkg = null;
for (int i = 0; i < parts.length; i++) {
pkg = i == 0 ? parts[0] : String.format("%s.%s", pkg, parts[i]);
if (PLATFORM_PACKAGES.contains(pkg)) {
return true;
}
}
return false;
}

public String getImportFileName() { public String getImportFileName() {
return importFileName; return importFileName;
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void initializeCommonJavaTypes() {
private void initializePrimitiveArray(String javaTypeName, String iosTypeName) { private void initializePrimitiveArray(String javaTypeName, String iosTypeName) {
ITypeBinding javaType = ast.resolveWellKnownType(javaTypeName); ITypeBinding javaType = ast.resolveWellKnownType(javaTypeName);
IOSTypeBinding iosType = mapIOSType(IOSTypeBinding.newUnmappedClass(iosTypeName)); IOSTypeBinding iosType = mapIOSType(IOSTypeBinding.newUnmappedClass(iosTypeName));
iosType.setHeader("IOSPrimitiveArray"); iosType.setHeader("IOSPrimitiveArray.h");
arrayBindingMap.put(javaType, iosType); arrayBindingMap.put(javaType, iosType);
} }


Expand Down
Loading

0 comments on commit 806de7c

Please sign in to comment.