16
16
17
17
package com .google .devtools .j2objc .util ;
18
18
19
- import com .google .common .annotations .VisibleForTesting ;
20
19
import com .google .common .base .Function ;
21
20
import com .google .common .base .Joiner ;
22
21
import com .google .common .collect .ImmutableMap ;
26
25
import com .google .devtools .j2objc .Options ;
27
26
import com .google .devtools .j2objc .ast .CompilationUnit ;
28
27
import com .google .devtools .j2objc .ast .PackageDeclaration ;
29
- import com .google .devtools .j2objc .file .InputFile ;
30
28
import com .google .devtools .j2objc .types .GeneratedVariableBinding ;
31
29
import com .google .devtools .j2objc .types .IOSMethodBinding ;
32
30
import com .google .devtools .j2objc .types .PointerTypeBinding ;
41
39
import org .eclipse .jdt .core .dom .VariableDeclarationFragment ;
42
40
43
41
import java .io .File ;
44
- import java .io .IOException ;
45
42
import java .util .Collection ;
46
43
import java .util .Collections ;
47
44
import java .util .Iterator ;
@@ -274,7 +271,7 @@ public class NameTable {
274
271
* can share a prefix; for example, the com.google.common packages in
275
272
* Guava could share a "GG" (Google Guava) or simply "Guava" prefix.
276
273
*/
277
- private final Map < String , String > prefixMap ;
274
+ private final PackagePrefixes prefixMap ;
278
275
279
276
private final Map <String , String > methodMappings ;
280
277
@@ -286,7 +283,7 @@ public static class Factory {
286
283
// Currently a shared map.
287
284
// TODO(kstanger): For thread safety this will either need to be a
288
285
// concurrent map, or make a copy for each NameTable.
289
- private Map < String , String > prefixMap = Options .getPackagePrefixes ();
286
+ private PackagePrefixes prefixMap = Options .getPackagePrefixes ();
290
287
291
288
private final Map <String , String > methodMappings = ImmutableMap .copyOf (
292
289
Maps .transformValues (Options .getMethodMappings (), EXTRACT_SELECTOR_FUNC ));
@@ -308,7 +305,7 @@ public String apply(String value) {
308
305
};
309
306
310
307
private NameTable (
311
- Types typeEnv , Map < String , String > prefixMap , Map <String , String > methodMappings ) {
308
+ Types typeEnv , PackagePrefixes prefixMap , Map <String , String > methodMappings ) {
312
309
this .typeEnv = typeEnv ;
313
310
this .prefixMap = prefixMap ;
314
311
this .methodMappings = methodMappings ;
@@ -943,104 +940,11 @@ public static String getMainTypeFullName(CompilationUnit unit) {
943
940
}
944
941
}
945
942
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
- */
956
943
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 );
1041
945
}
1042
946
1043
947
public boolean hasPrefix (String packageName ) {
1044
- return prefixMap .containsKey (packageName );
948
+ return prefixMap .hasPrefix (packageName );
1045
949
}
1046
950
}
0 commit comments