Skip to content

Commit b54ca0f

Browse files
committed
Added support for wildcards in package prefix flags.
Change on 2015/08/20 by tball <[email protected]> ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=101138655
1 parent 5ea76f1 commit b54ca0f

File tree

7 files changed

+341
-152
lines changed

7 files changed

+341
-152
lines changed

translator/src/main/java/com/google/devtools/j2objc/Options.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.devtools.j2objc.util.ErrorUtil;
2727
import com.google.devtools.j2objc.util.FileUtil;
2828
import com.google.devtools.j2objc.util.HeaderMap;
29+
import com.google.devtools.j2objc.util.PackagePrefixes;
2930

3031
import java.io.File;
3132
import java.io.FileInputStream;
@@ -83,7 +84,8 @@ public class Options {
8384
private boolean staticAccessorMethods = false;
8485
private int batchTranslateMaximum = 0;
8586
private List<String> headerMappingFiles = null;
86-
private Map<String, String> packagePrefixes = Maps.newHashMap();
87+
88+
private PackagePrefixes packagePrefixes = new PackagePrefixes();
8789

8890
private static final Set<String> VALID_JAVA_VERSIONS = ImmutableSet.of("1.8", "1.7", "1.6",
8991
"1.5");
@@ -441,14 +443,7 @@ private static void addPrefixesFile(String filename) throws IOException {
441443
FileInputStream fis = new FileInputStream(filename);
442444
props.load(fis);
443445
fis.close();
444-
addPrefixProperties(props);
445-
}
446-
447-
@VisibleForTesting
448-
static void addPrefixProperties(Properties props) {
449-
for (String pkg : props.stringPropertyNames()) {
450-
addPackagePrefix(pkg, props.getProperty(pkg).trim());
451-
}
446+
instance.packagePrefixes.addPrefixProperties(props);
452447
}
453448

454449
private void addMappingsFiles(String[] filenames) throws IOException {
@@ -682,12 +677,12 @@ public static List<String> getBootClasspath() {
682677
return getPathArgument(bootclasspath);
683678
}
684679

685-
public static Map<String, String> getPackagePrefixes() {
680+
public static PackagePrefixes getPackagePrefixes() {
686681
return instance.packagePrefixes;
687682
}
688683

689684
public static void addPackagePrefix(String pkg, String prefix) {
690-
addMapping(instance.packagePrefixes, pkg, prefix, "package prefix");
685+
instance.packagePrefixes.addPrefix(pkg, prefix);
691686
}
692687

693688
public static String fileEncoding() {

translator/src/main/java/com/google/devtools/j2objc/util/NameTable.java

Lines changed: 5 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.devtools.j2objc.util;
1818

19-
import com.google.common.annotations.VisibleForTesting;
2019
import com.google.common.base.Function;
2120
import com.google.common.base.Joiner;
2221
import com.google.common.collect.ImmutableMap;
@@ -26,7 +25,6 @@
2625
import com.google.devtools.j2objc.Options;
2726
import com.google.devtools.j2objc.ast.CompilationUnit;
2827
import com.google.devtools.j2objc.ast.PackageDeclaration;
29-
import com.google.devtools.j2objc.file.InputFile;
3028
import com.google.devtools.j2objc.types.GeneratedVariableBinding;
3129
import com.google.devtools.j2objc.types.IOSMethodBinding;
3230
import com.google.devtools.j2objc.types.PointerTypeBinding;
@@ -41,7 +39,6 @@
4139
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
4240

4341
import java.io.File;
44-
import java.io.IOException;
4542
import java.util.Collection;
4643
import java.util.Collections;
4744
import java.util.Iterator;
@@ -274,7 +271,7 @@ public class NameTable {
274271
* can share a prefix; for example, the com.google.common packages in
275272
* Guava could share a "GG" (Google Guava) or simply "Guava" prefix.
276273
*/
277-
private final Map<String, String> prefixMap;
274+
private final PackagePrefixes prefixMap;
278275

279276
private final Map<String, String> methodMappings;
280277

@@ -286,7 +283,7 @@ public static class Factory {
286283
// Currently a shared map.
287284
// TODO(kstanger): For thread safety this will either need to be a
288285
// concurrent map, or make a copy for each NameTable.
289-
private Map<String, String> prefixMap = Options.getPackagePrefixes();
286+
private PackagePrefixes prefixMap = Options.getPackagePrefixes();
290287

291288
private final Map<String, String> methodMappings = ImmutableMap.copyOf(
292289
Maps.transformValues(Options.getMethodMappings(), EXTRACT_SELECTOR_FUNC));
@@ -308,7 +305,7 @@ public String apply(String value) {
308305
};
309306

310307
private NameTable(
311-
Types typeEnv, Map<String, String> prefixMap, Map<String, String> methodMappings) {
308+
Types typeEnv, PackagePrefixes prefixMap, Map<String, String> methodMappings) {
312309
this.typeEnv = typeEnv;
313310
this.prefixMap = prefixMap;
314311
this.methodMappings = methodMappings;
@@ -943,104 +940,11 @@ public static String getMainTypeFullName(CompilationUnit unit) {
943940
}
944941
}
945942

946-
@VisibleForTesting
947-
public void mapPackageToPrefix(String packageName, String prefix) {
948-
prefixMap.put(packageName, prefix);
949-
}
950-
951-
/**
952-
* Return the prefix for a specified package. If a prefix was specified
953-
* for the package, then that prefix is returned. Otherwise, a camel-cased
954-
* prefix is created from the package name.
955-
*/
956943
public String getPrefix(IPackageBinding packageBinding) {
957-
String packageName = packageBinding.getName();
958-
if (hasPrefix(packageName)) {
959-
return prefixMap.get(packageName);
960-
}
961-
962-
for (IAnnotationBinding annotation : packageBinding.getAnnotations()) {
963-
if (annotation.getName().endsWith("ObjectiveCName")) {
964-
String prefix = (String) BindingUtil.getAnnotationValue(annotation, "value");
965-
prefixMap.put(packageName, prefix);
966-
// Don't return, as there may be a prefix annotation that overrides this value.
967-
}
968-
}
969-
970-
String prefix = getPrefixFromPackageInfoSource(packageBinding);
971-
if (prefix == null) {
972-
prefix = getPrefixFromPackageInfoClass(packageName);
973-
}
974-
if (prefix == null) {
975-
prefix = camelCaseQualifiedName(packageName);
976-
}
977-
prefixMap.put(packageName, prefix);
978-
return prefix;
979-
}
980-
981-
/**
982-
* Check if there is a package-info.java source file with a prefix annotation.
983-
*/
984-
private static String getPrefixFromPackageInfoSource(IPackageBinding packageBinding) {
985-
try {
986-
String qualifiedName = "package-info";
987-
String packageName = packageBinding.getName();
988-
// Path will be null if this is the empty package.
989-
if (packageName != null) {
990-
qualifiedName = packageName + '.' + qualifiedName;
991-
}
992-
InputFile file = FileUtil.findOnSourcePath(qualifiedName);
993-
if (file != null) {
994-
String pkgInfo = FileUtil.readFile(file);
995-
int i = pkgInfo.indexOf("@ObjectiveCName");
996-
if (i == -1) {
997-
i = pkgInfo.indexOf("@com.google.j2objc.annotations.ObjectiveCName");
998-
}
999-
if (i > -1) {
1000-
// Extract annotation's value string.
1001-
i = pkgInfo.indexOf('"', i + 1);
1002-
if (i > -1) {
1003-
int j = pkgInfo.indexOf('"', i + 1);
1004-
if (j > -1) {
1005-
return pkgInfo.substring(i + 1, j);
1006-
}
1007-
}
1008-
}
1009-
}
1010-
} catch (IOException e) {
1011-
// Continue, as there's no package-info to check.
1012-
}
1013-
return null;
1014-
}
1015-
1016-
/**
1017-
* Check if there is a package-info class with a prefix annotation.
1018-
*/
1019-
private static String getPrefixFromPackageInfoClass(String packageName) {
1020-
List<String> paths = Options.getBootClasspath();
1021-
paths.addAll(Options.getClassPathEntries());
1022-
PathClassLoader classLoader = new PathClassLoader(paths);
1023-
try {
1024-
Class<?> clazz = classLoader.loadClass(packageName + ".package-info");
1025-
ObjectiveCName objectiveCName = clazz.getAnnotation(ObjectiveCName.class);
1026-
if (objectiveCName != null) {
1027-
return objectiveCName.value();
1028-
}
1029-
} catch (ClassNotFoundException e) {
1030-
// Class does not exist -- ignore exception.
1031-
} catch (SecurityException e) {
1032-
// Failed fetching a package-info class from a secure package -- ignore exception.
1033-
} finally {
1034-
try {
1035-
classLoader.close();
1036-
} catch (IOException e) {
1037-
// Ignore, any open files will be closed on exit.
1038-
}
1039-
}
1040-
return null;
944+
return prefixMap.getPrefix(packageBinding);
1041945
}
1042946

1043947
public boolean hasPrefix(String packageName) {
1044-
return prefixMap.containsKey(packageName);
948+
return prefixMap.hasPrefix(packageName);
1045949
}
1046950
}

0 commit comments

Comments
 (0)