Skip to content

Commit

Permalink
Clean up handling of package-info types:
Browse files Browse the repository at this point in the history
- Use the Java name instead of the underscored name as the main class name.
- Adds BindingUtil.isPackageInfo() and stops using the synthetic modifier to identify package-info types.

	Change on 2016/06/09 by kstanger <kstanger@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=124451338
  • Loading branch information
kstanger authored and Keith Stanger committed Jun 24, 2016
1 parent 3242d25 commit 12d6d69
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 27 deletions.
Expand Up @@ -51,12 +51,7 @@ public CompilationUnit(
typeEnv = new Types(jdtNode.getAST());
nameTable = nameTableFactory == null ? null : nameTableFactory.newNameTable(typeEnv);
this.sourceFilePath = Preconditions.checkNotNull(sourceFilePath);
Preconditions.checkNotNull(mainTypeName);
if (mainTypeName.endsWith(NameTable.PACKAGE_INFO_FILE_NAME)) {
mainTypeName =
mainTypeName.replace(NameTable.PACKAGE_INFO_FILE_NAME, NameTable.PACKAGE_INFO_MAIN_TYPE);
}
this.mainTypeName = mainTypeName;
this.mainTypeName = Preconditions.checkNotNull(mainTypeName);
this.source = Preconditions.checkNotNull(source);
newlines = findNewlines(source);
if (jdtNode.getPackage() == null) {
Expand Down
Expand Up @@ -24,7 +24,6 @@
import com.google.devtools.j2objc.ast.PackageDeclaration;
import com.google.devtools.j2objc.ast.TreeUtil;
import com.google.devtools.j2objc.file.InputFile;
import com.google.devtools.j2objc.util.NameTable;

import java.io.File;
import java.util.Collection;
Expand Down Expand Up @@ -213,9 +212,6 @@ public boolean isFullyParsed() {
*/
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)
Expand Down
Expand Up @@ -111,8 +111,7 @@ protected void generateInitialDeclaration() {
printDisallowedConstructors();
println("\n@end");

// Package-info type, skip all outer declarations.
if (BindingUtil.isSynthetic(typeBinding)) {
if (BindingUtil.isPackageInfo(typeBinding)) {
return;
}
printCompanionClassDeclaration();
Expand Down
Expand Up @@ -92,8 +92,7 @@ protected void generate() {
println("\n@end");
}

// Package-info type, skip all outer declarations.
if (BindingUtil.isSynthetic(typeBinding)) {
if (BindingUtil.isPackageInfo(typeBinding)) {
return;
}
printOuterDeclarations();
Expand Down
Expand Up @@ -74,7 +74,7 @@ public void endVisit(AnnotationTypeDeclaration node) {

private void visitType(AbstractTypeDeclaration node) {
ITypeBinding type = node.getTypeBinding();
if (BindingUtil.isSynthetic(type) || !TranslationUtil.needsReflection(type)) {
if (BindingUtil.isPackageInfo(type) || !TranslationUtil.needsReflection(type)) {
return;
}

Expand Down
Expand Up @@ -44,7 +44,7 @@ public class PackageInfoRewriter {
private final Types typeEnv;

public static void run(CompilationUnit unit) {
if (unit.getMainTypeName().endsWith(NameTable.PACKAGE_INFO_MAIN_TYPE)) {
if (unit.getMainTypeName().endsWith(NameTable.PACKAGE_INFO_CLASS_NAME)) {
new PackageInfoRewriter(unit).run();
}
}
Expand All @@ -63,14 +63,10 @@ private void run() {
return;
}

// The package prefix does not get renamed for the package_info type, so we
// generate the camel-cased name here so that NameTable doesn't rename it
// later.
String typeName = NameTable.camelCaseQualifiedName(pkg.getPackageBinding().getName())
+ NameTable.PACKAGE_INFO_MAIN_TYPE;
GeneratedTypeBinding typeBinding = GeneratedTypeBinding.newTypeBinding(
typeName, typeEnv.getNSObject(), false);
typeBinding.setModifiers(BindingUtil.ACC_SYNTHETIC | Modifier.PRIVATE);
GeneratedTypeBinding typeBinding = new GeneratedTypeBinding(
NameTable.PACKAGE_INFO_CLASS_NAME, pkg.getPackageBinding(), typeEnv.getNSObject(), false,
null);
typeBinding.setModifiers(Modifier.PRIVATE);
TypeDeclaration typeDecl = new TypeDeclaration(typeBinding);
TreeUtil.moveList(pkg.getAnnotations(), typeDecl.getAnnotations());

Expand Down
Expand Up @@ -168,6 +168,10 @@ public static boolean isLambda(ITypeBinding type) {
return type instanceof LambdaTypeBinding;
}

public static boolean isPackageInfo(ITypeBinding type) {
return type.getName().equals(NameTable.PACKAGE_INFO_CLASS_NAME);
}

/**
* Tests if this type is private to it's source file. A public type declared
* within a private type is considered private.
Expand Down
Expand Up @@ -68,8 +68,8 @@ public class NameTable {

// The JDT compiler requires package-info files be named as "package-info",
// but that's an illegal type to generate.
public static final String PACKAGE_INFO_FILE_NAME = "package-info";
public static final String PACKAGE_INFO_MAIN_TYPE = "package_info";
public static final String PACKAGE_INFO_CLASS_NAME = "package-info";
private static final String PACKAGE_INFO_OBJC_NAME = "package_info";

// The self name in Java is reserved in Objective-C, but functionized methods
// actually want the first parameter to be self. This is an internal name,
Expand Down Expand Up @@ -846,7 +846,13 @@ public static String getNativeEnumName(String typeName) {
public String getFullName(ITypeBinding binding) {
binding = typeEnv.mapType(binding.getErasure()); // Make sure type variables aren't included.

// Use ObjectiveCType annotation, if it exists.
// Avoid package prefix renaming for package-info types, and use a valid ObjC name that doesn't
// have a dash character.
if (BindingUtil.isPackageInfo(binding)) {
return camelCaseQualifiedName(binding.getPackage().getName()) + PACKAGE_INFO_OBJC_NAME;
}

// Use ObjectiveCName annotation, if it exists.
IAnnotationBinding annotation = BindingUtil.getAnnotation(binding, ObjectiveCName.class);
if (annotation != null) {
return (String) BindingUtil.getAnnotationValue(annotation, "value");
Expand Down

0 comments on commit 12d6d69

Please sign in to comment.