Skip to content

Commit

Permalink
Simplify handling of the empty package in NameTable and PackagePrefixes.
Browse files Browse the repository at this point in the history
	Change on 2015/10/02 by kstanger <kstanger@google.com>
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=104518667
  • Loading branch information
kstanger committed Oct 6, 2015
1 parent 07f2d0f commit 9281f6c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
Expand Up @@ -24,7 +24,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.ast.PackageDeclaration;
import com.google.devtools.j2objc.types.GeneratedVariableBinding; import com.google.devtools.j2objc.types.GeneratedVariableBinding;
import com.google.devtools.j2objc.types.IOSMethodBinding; import com.google.devtools.j2objc.types.IOSMethodBinding;
import com.google.devtools.j2objc.types.PointerTypeBinding; import com.google.devtools.j2objc.types.PointerTypeBinding;
Expand Down Expand Up @@ -924,9 +923,7 @@ private String getFullNameInner(ITypeBinding binding) {
} }


// Use camel-cased package+class name. // Use camel-cased package+class name.
IPackageBinding pkg = binding.getPackage(); return getPrefix(binding.getPackage()) + binding.getName();
String pkgName = pkg != null ? getPrefix(pkg) : "";
return pkgName + binding.getName();
} }


private static String getTypeSubName(ITypeBinding binding) { private static String getTypeSubName(ITypeBinding binding) {
Expand All @@ -949,12 +946,8 @@ private static boolean isReservedName(String name) {
} }


public static String getMainTypeFullName(CompilationUnit unit) { public static String getMainTypeFullName(CompilationUnit unit) {
PackageDeclaration pkg = unit.getPackage(); return unit.getNameTable().getPrefix(unit.getPackage().getPackageBinding())
if (pkg.isDefaultPackage()) { + unit.getMainTypeName();
return unit.getMainTypeName();
} else {
return unit.getNameTable().getPrefix(pkg.getPackageBinding()) + unit.getMainTypeName();
}
} }


public String getPrefix(IPackageBinding packageBinding) { public String getPrefix(IPackageBinding packageBinding) {
Expand Down
Expand Up @@ -111,6 +111,9 @@ public boolean hasPrefix(String packageName) {
* prefix is created from the package name. * prefix is created from the package name.
*/ */
public String getPrefix(IPackageBinding packageBinding) { public String getPrefix(IPackageBinding packageBinding) {
if (packageBinding == null) {
return "";
}
String packageName = packageBinding.getName(); String packageName = packageBinding.getName();
if (hasPrefix(packageName)) { if (hasPrefix(packageName)) {
return getPrefix(packageName); return getPrefix(packageName);
Expand All @@ -124,7 +127,7 @@ public String getPrefix(IPackageBinding packageBinding) {
} }
} }


String prefix = getPrefixFromPackageInfoSource(packageBinding); String prefix = getPrefixFromPackageInfoSource(packageName);
if (prefix == null) { if (prefix == null) {
prefix = getPrefixFromPackageInfoClass(packageName); prefix = getPrefixFromPackageInfoClass(packageName);
} }
Expand All @@ -138,15 +141,9 @@ public String getPrefix(IPackageBinding packageBinding) {
/** /**
* Check if there is a package-info.java source file with a prefix annotation. * Check if there is a package-info.java source file with a prefix annotation.
*/ */
private String getPrefixFromPackageInfoSource(IPackageBinding packageBinding) { private String getPrefixFromPackageInfoSource(String packageName) {
try { try {
String qualifiedName = "package-info"; InputFile file = FileUtil.findOnSourcePath(packageName + ".package-info");
String packageName = packageBinding.getName();
// Path will be null if this is the empty package.
if (packageName != null) {
qualifiedName = packageName + '.' + qualifiedName;
}
InputFile file = FileUtil.findOnSourcePath(qualifiedName);
if (file != null) { if (file != null) {
String pkgInfo = FileUtil.readFile(file); String pkgInfo = FileUtil.readFile(file);
int i = pkgInfo.indexOf("@ObjectiveCName"); int i = pkgInfo.indexOf("@ObjectiveCName");
Expand Down Expand Up @@ -176,11 +173,7 @@ private String getPrefixFromPackageInfoSource(IPackageBinding packageBinding) {
private String getPrefixFromPackageInfoClass(String packageName) { private String getPrefixFromPackageInfoClass(String packageName) {
final String[] result = new String[1]; final String[] result = new String[1];
try { try {
String qualifiedName = "package-info"; InputFile file = FileUtil.findOnClassPath(packageName + ".package-info");
if (packageName != null) {
qualifiedName = packageName + '.' + qualifiedName;
}
InputFile file = FileUtil.findOnClassPath(qualifiedName);
if (file != null) { if (file != null) {
ClassReader classReader = new ClassReader(file.getInputStream()); ClassReader classReader = new ClassReader(file.getInputStream());
classReader.accept(new ClassVisitor(Opcodes.ASM5) { classReader.accept(new ClassVisitor(Opcodes.ASM5) {
Expand Down

0 comments on commit 9281f6c

Please sign in to comment.