Skip to content

Commit

Permalink
Removes private types from the header file.
Browse files Browse the repository at this point in the history
	Change on 2015/04/02 by kstanger <kstanger@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=90154286
  • Loading branch information
kstanger committed Apr 9, 2015
1 parent db0d9e1 commit 123c387
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 48 deletions.
Expand Up @@ -30,6 +30,7 @@
import com.google.devtools.j2objc.ast.NativeDeclaration; import com.google.devtools.j2objc.ast.NativeDeclaration;
import com.google.devtools.j2objc.ast.TreeVisitor; import com.google.devtools.j2objc.ast.TreeVisitor;
import com.google.devtools.j2objc.ast.Type; import com.google.devtools.j2objc.ast.Type;
import com.google.devtools.j2objc.util.BindingUtil;


import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Modifier; import org.eclipse.jdt.core.dom.Modifier;
Expand Down Expand Up @@ -118,13 +119,11 @@ private void addPublicType(Type typeNode) {
} }


private boolean isPrivateType(ITypeBinding type) { private boolean isPrivateType(ITypeBinding type) {
// TODO(kstanger): Uncomment the code below to hide private types. if (type == null || !Options.hidePrivateMembers()) {
return false;
/*if (type == null || !Options.hidePrivateMembers()) {
return false; return false;
} }
return isPrivateType(type.getDeclaringClass()) || BindingUtil.isPrivate(type) || type.isLocal() return isPrivateType(type.getDeclaringClass()) || BindingUtil.isPrivate(type) || type.isLocal()
|| type.isAnonymous();*/ || type.isAnonymous();
} }


@Override @Override
Expand Down
Expand Up @@ -28,6 +28,7 @@
import com.google.devtools.j2objc.gen.PrimitiveArrayTest; import com.google.devtools.j2objc.gen.PrimitiveArrayTest;
import com.google.devtools.j2objc.gen.SignatureGeneratorTest; import com.google.devtools.j2objc.gen.SignatureGeneratorTest;
import com.google.devtools.j2objc.gen.StatementGeneratorTest; import com.google.devtools.j2objc.gen.StatementGeneratorTest;
import com.google.devtools.j2objc.gen.TypeDeclarationGeneratorTest;
import com.google.devtools.j2objc.gen.TypeImplementationGeneratorTest; import com.google.devtools.j2objc.gen.TypeImplementationGeneratorTest;
import com.google.devtools.j2objc.translate.AbstractMethodRewriterTest; import com.google.devtools.j2objc.translate.AbstractMethodRewriterTest;
import com.google.devtools.j2objc.translate.AnonymousClassConverterTest; import com.google.devtools.j2objc.translate.AnonymousClassConverterTest;
Expand All @@ -51,6 +52,7 @@
import com.google.devtools.j2objc.translate.OperatorRewriterTest; import com.google.devtools.j2objc.translate.OperatorRewriterTest;
import com.google.devtools.j2objc.translate.OuterReferenceFixerTest; import com.google.devtools.j2objc.translate.OuterReferenceFixerTest;
import com.google.devtools.j2objc.translate.OuterReferenceResolverTest; import com.google.devtools.j2objc.translate.OuterReferenceResolverTest;
import com.google.devtools.j2objc.translate.PrivateDeclarationResolverTest;
import com.google.devtools.j2objc.translate.RewriterTest; import com.google.devtools.j2objc.translate.RewriterTest;
import com.google.devtools.j2objc.translate.StaticVarRewriterTest; import com.google.devtools.j2objc.translate.StaticVarRewriterTest;
import com.google.devtools.j2objc.translate.SuperMethodInvocationRewriterTest; import com.google.devtools.j2objc.translate.SuperMethodInvocationRewriterTest;
Expand Down Expand Up @@ -118,6 +120,7 @@ public class SmallTests {
OuterReferenceFixerTest.class, OuterReferenceFixerTest.class,
OuterReferenceResolverTest.class, OuterReferenceResolverTest.class,
PrimitiveArrayTest.class, PrimitiveArrayTest.class,
PrivateDeclarationResolverTest.class,
ProGuardUsageParserTest.class, ProGuardUsageParserTest.class,
RenamedTypeBindingTest.class, RenamedTypeBindingTest.class,
RewriterTest.class, RewriterTest.class,
Expand All @@ -126,6 +129,7 @@ public class SmallTests {
StaticVarRewriterTest.class, StaticVarRewriterTest.class,
SuperMethodInvocationRewriterTest.class, SuperMethodInvocationRewriterTest.class,
TreeConvertTest.class, TreeConvertTest.class,
TypeDeclarationGeneratorTest.class,
TypeImplementationGeneratorTest.class, TypeImplementationGeneratorTest.class,
TranslationProcessorTest.class, TranslationProcessorTest.class,
TreeUtilTest.class, TreeUtilTest.class,
Expand Down
Expand Up @@ -445,17 +445,6 @@ public void testInnerClassDeclarationWithOuterReference() throws IOException {
assertTranslation(translation, "Example *this$0_;"); assertTranslation(translation, "Example *this$0_;");
} }


public void testAnonymousClassDeclaration() throws IOException {
String translation = translateSourceFile(
"public class Example { Runnable run = new Runnable() { public void run() {} }; }",
"Example", "Example.h");
assertTranslation(translation, "@interface Example_$1 : NSObject < JavaLangRunnable >");
assertTranslation(translation, "- (void)run;");
// Outer reference is not required.
assertNotInTranslation(translation, "Example *this");
assertNotInTranslation(translation, "- (id)initWithExample:");
}

public void testEnum() throws IOException { public void testEnum() throws IOException {
String translation = translateSourceFile( String translation = translateSourceFile(
"public enum Color { RED, WHITE, BLUE }", "public enum Color { RED, WHITE, BLUE }",
Expand Down Expand Up @@ -603,17 +592,6 @@ public void testNoImportForMappedTypes() throws IOException {
assertTranslation(translation, "NSCopying"); assertTranslation(translation, "NSCopying");
} }


public void testAnonymousConcreteSubclassOfGenericAbstractType() throws IOException {
String translation = translateSourceFile(
"public class Test {"
+ " interface FooInterface<T> { public void foo1(T t); public void foo2(); }"
+ " abstract static class Foo<T> implements FooInterface<T> { public void foo2() { } }"
+ " Foo<Integer> foo = new Foo<Integer>() {"
+ " public void foo1(Integer i) { } }; }",
"Test", "Test.h");
assertTranslation(translation, "foo1WithId:(JavaLangInteger *)i");
}

// Verify that an empty Java enum doesn't define an empty C enum, // Verify that an empty Java enum doesn't define an empty C enum,
// which is illegal. // which is illegal.
public void testEmptyEnum() throws IOException { public void testEmptyEnum() throws IOException {
Expand Down
Expand Up @@ -443,11 +443,10 @@ public void testGenericMethodWithAnonymousReturn() throws IOException {
"Test", "Test.m"); "Test", "Test.m");
assertTranslation(translation, assertTranslation(translation,
"return [new_Test_$1_initWithJavaUtilCollection_(collection) autorelease];"); "return [new_Test_$1_initWithJavaUtilCollection_(collection) autorelease];");
translation = getTranslatedFile("Test.h");
assertTranslation(translation, assertTranslation(translation,
"- (instancetype)initWithJavaUtilCollection:(id<JavaUtilCollection>)capture$0;"); "- (instancetype)initWithJavaUtilCollection:(id<JavaUtilCollection>)capture$0;");
assertTranslation(translation, assertTranslation(translation,
"FOUNDATION_EXPORT Test_$1 *new_Test_$1_initWithJavaUtilCollection_(" "__attribute__((unused)) static Test_$1 *new_Test_$1_initWithJavaUtilCollection_("
+ "id<JavaUtilCollection> capture$0) NS_RETURNS_RETAINED;"); + "id<JavaUtilCollection> capture$0) NS_RETURNS_RETAINED;");
} }


Expand Down
@@ -0,0 +1,49 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.devtools.j2objc.gen;

import com.google.devtools.j2objc.GenerationTest;

import java.io.IOException;

/**
* Tests for {@link TypeDeclarationGenerator}.
*
* @author Keith Stanger
*/
public class TypeDeclarationGeneratorTest extends GenerationTest {

public void testAnonymousClassDeclaration() throws IOException {
String translation = translateSourceFile(
"public class Example { Runnable run = new Runnable() { public void run() {} }; }",
"Example", "Example.m");
assertTranslation(translation, "@interface Example_$1 : NSObject < JavaLangRunnable >");
assertTranslation(translation, "- (void)run;");
// Outer reference is not required.
assertNotInTranslation(translation, "Example *this");
assertNotInTranslation(translation, "- (id)initWithExample:");
}

public void testAnonymousConcreteSubclassOfGenericAbstractType() throws IOException {
String translation = translateSourceFile(
"public class Test {"
+ " interface FooInterface<T> { public void foo1(T t); public void foo2(); }"
+ " abstract static class Foo<T> implements FooInterface<T> { public void foo2() { } }"
+ " Foo<Integer> foo = new Foo<Integer>() {"
+ " public void foo1(Integer i) { } }; }",
"Test", "Test.m");
assertTranslation(translation, "foo1WithId:(JavaLangInteger *)i");
}
}
Expand Up @@ -59,21 +59,20 @@ public void testAnonymousClassNaming() throws IOException {
+ " public Object next() { return null; }" + " public Object next() { return null; }"
+ " public void remove() {}};}};}" + " public void remove() {}};}};}"
+ "}"; + "}";
String header = translateSourceFile(source, "Test", "Test.h"); String impl = translateSourceFile(source, "Test", "Test.m");
assertTranslation(header, "@interface Test_$1_$1 : NSObject < JavaUtilIterator >"); assertTranslation(impl, "@interface Test_$1_$1 : NSObject < JavaUtilIterator >");
assertTranslation(header, "@interface Test_$1 : JavaUtilAbstractSet"); assertTranslation(impl, "@interface Test_$1 : JavaUtilAbstractSet");
assertTranslation(header, "@interface Test_$2_$1 : NSObject < JavaUtilIterator >"); assertTranslation(impl, "@interface Test_$2_$1 : NSObject < JavaUtilIterator >");
assertTranslation(header, "@interface Test_$2 : JavaUtilAbstractCollection"); assertTranslation(impl, "@interface Test_$2 : JavaUtilAbstractCollection");
} }


public void testFinalArrayInnerAccess() throws IOException { public void testFinalArrayInnerAccess() throws IOException {
String source = "public class Test { void foo() { " String source = "public class Test { void foo() { "
+ "final boolean[] bar = new boolean[1];" + "final boolean[] bar = new boolean[1];"
+ "Runnable r = new Runnable() { public void run() { bar[0] = true; }}; }}"; + "Runnable r = new Runnable() { public void run() { bar[0] = true; }}; }}";
String header = translateSourceFile(source, "Test", "Test.h"); String impl = translateSourceFile(source, "Test", "Test.m");
String impl = getTranslatedFile("Test.m");
assertTranslation(impl, "IOSBooleanArray *val$bar_;"); assertTranslation(impl, "IOSBooleanArray *val$bar_;");
assertTranslation(header, assertTranslation(impl,
"- (instancetype)initWithBooleanArray:(IOSBooleanArray *)capture$0;"); "- (instancetype)initWithBooleanArray:(IOSBooleanArray *)capture$0;");
assertTranslation(impl, "IOSBooleanArray *bar = [IOSBooleanArray arrayWithLength:1];"); assertTranslation(impl, "IOSBooleanArray *bar = [IOSBooleanArray arrayWithLength:1];");
assertTranslation(impl, "new_Test_$1_initWithBooleanArray_(bar)"); assertTranslation(impl, "new_Test_$1_initWithBooleanArray_(bar)");
Expand Down Expand Up @@ -341,12 +340,11 @@ public void testEnumConstantAnonymousClassNaming() throws IOException {
+ "UP { public boolean isUp() { return true; }}," + "UP { public boolean isUp() { return true; }},"
+ "DOWN { public boolean isUp() { return false; }};" + "DOWN { public boolean isUp() { return false; }};"
+ "public abstract boolean isUp(); }"; + "public abstract boolean isUp(); }";
String header = translateSourceFile(source, "Test", "Test.h"); String impl = translateSourceFile(source, "Test", "Test.m");
String impl = getTranslatedFile("Test.m");


assertTranslation(header, "@interface Test_$1Enum : TestEnum"); assertTranslation(impl, "@interface Test_$1Enum : TestEnum");
assertTranslation(header, "@interface Test_$2Enum : TestEnum"); assertTranslation(impl, "@interface Test_$2Enum : TestEnum");
assertTranslatedLines(header, assertTranslatedLines(impl,
"- (instancetype)initWithNSString:(NSString *)__name", "- (instancetype)initWithNSString:(NSString *)__name",
"withInt:(jint)__ordinal;"); "withInt:(jint)__ordinal;");


Expand Down
Expand Up @@ -111,10 +111,10 @@ public void testInnerMethodAnonymousClass() throws IOException {
+ "}"; + "}";
String translation = translateSourceFile(source, "A", "A.h"); String translation = translateSourceFile(source, "A", "A.h");
assertTranslation(translation, "- (instancetype)initWithA:(A *)outer$;"); assertTranslation(translation, "- (instancetype)initWithA:(A *)outer$;");
translation = getTranslatedFile("A.m");
assertTranslatedLines(translation, assertTranslatedLines(translation,
"- (instancetype)initWithA_B:(A_B *)outer$", "- (instancetype)initWithA_B:(A_B *)outer$",
"withInt:(jint)capture$0;"); "withInt:(jint)capture$0;");
translation = getTranslatedFile("A.m");
assertTranslation(translation, "A *this$0_;"); assertTranslation(translation, "A *this$0_;");
assertTranslation(translation, "A_B *this$1_;"); assertTranslation(translation, "A_B *this$1_;");
assertTranslation(translation, "jint val$j_;"); assertTranslation(translation, "jint val$j_;");
Expand Down Expand Up @@ -510,7 +510,7 @@ public void testStaticReferenceInInnerClass() throws IOException {
public void testMethodInnerClass() throws IOException { public void testMethodInnerClass() throws IOException {
String source = "public class A { void foo() { class MyRunnable implements Runnable {" String source = "public class A { void foo() { class MyRunnable implements Runnable {"
+ "public void run() {} }}}"; + "public void run() {} }}}";
String translation = translateSourceFile(source, "A", "A.h"); String translation = translateSourceFile(source, "A", "A.m");
assertTranslation(translation, "@interface A_1MyRunnable : NSObject < JavaLangRunnable >"); assertTranslation(translation, "@interface A_1MyRunnable : NSObject < JavaLangRunnable >");
assertNotInTranslation(translation, "A *this"); assertNotInTranslation(translation, "A *this");
} }
Expand All @@ -525,6 +525,7 @@ public void testMethodInnerClassWithSameName() throws IOException {
String source = "public class A { class MyClass {} void foo() { class MyClass {}}}"; String source = "public class A { class MyClass {} void foo() { class MyClass {}}}";
String translation = translateSourceFile(source, "A", "A.h"); String translation = translateSourceFile(source, "A", "A.h");
assertTranslation(translation, "@interface A_MyClass"); assertTranslation(translation, "@interface A_MyClass");
translation = getTranslatedFile("A.m");
assertTranslation(translation, "@interface A_1MyClass"); assertTranslation(translation, "@interface A_1MyClass");
} }


Expand Down Expand Up @@ -595,11 +596,10 @@ public void testNoOuterWhenInStaticMethod() throws IOException {
+ " Iterator it = c.iterator(); " + " Iterator it = c.iterator(); "
+ " public boolean hasMoreElements() { return it.hasNext(); }" + " public boolean hasMoreElements() { return it.hasNext(); }"
+ " public Object nextElement() { return it.next(); }}; }}"; + " public Object nextElement() { return it.next(); }}; }}";
String translation = translateSourceFile(source, "A", "A.h"); String translation = translateSourceFile(source, "A", "A.m");
assertFalse(translation.contains("this$0_")); assertFalse(translation.contains("this$0_"));
assertTranslation(translation, assertTranslation(translation,
"- (instancetype)initWithJavaUtilCollection:(id<JavaUtilCollection>)capture$0;"); "- (instancetype)initWithJavaUtilCollection:(id<JavaUtilCollection>)capture$0;");
translation = getTranslatedFile("A.m");
assertTranslation(translation, "id<JavaUtilCollection> val$c_;"); assertTranslation(translation, "id<JavaUtilCollection> val$c_;");
assertFalse(translation.contains("this$0_")); assertFalse(translation.contains("this$0_"));
assertTranslation(translation, assertTranslation(translation,
Expand All @@ -614,9 +614,8 @@ public void testInnerAccessingOuterArrayLength() throws IOException {
+ "public boolean hasNext() { return elements.length > 0; } " + "public boolean hasNext() { return elements.length > 0; } "
+ "public E next() { return null; }" + "public E next() { return null; }"
+ "public void remove() {} }}"; + "public void remove() {} }}";
String translation = translateSourceFile(source, "A", "A.h"); String translation = translateSourceFile(source, "A", "A.m");
assertTranslation(translation, "- (instancetype)initWithA:(A *)outer$;"); assertTranslation(translation, "- (instancetype)initWithA:(A *)outer$;");
translation = getTranslatedFile("A.m");
assertTranslation(translation, "A *this$0_;"); assertTranslation(translation, "A *this$0_;");
assertTranslation(translation, "((IOSObjectArray *) nil_chk(this$0_->elements_))->size_"); assertTranslation(translation, "((IOSObjectArray *) nil_chk(this$0_->elements_))->size_");
} }
Expand Down Expand Up @@ -731,7 +730,7 @@ public void testAnonymousClassWithinTypeDeclarationStatement() throws IOExceptio
String translation = translateSourceFile( String translation = translateSourceFile(
"class Test { Runnable foo() { class MyRunnable implements Runnable { " "class Test { Runnable foo() { class MyRunnable implements Runnable { "
+ "public void run() { Runnable r = new Runnable() { public void run() {} }; } } " + "public void run() { Runnable r = new Runnable() { public void run() {} }; } } "
+ "return new MyRunnable(); } }", "Test", "Test.h"); + "return new MyRunnable(); } }", "Test", "Test.m");
assertOccurrences(translation, "@interface Test_1MyRunnable_$1", 1); assertOccurrences(translation, "@interface Test_1MyRunnable_$1", 1);
} }


Expand Down
@@ -0,0 +1,50 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.devtools.j2objc.translate;

import com.google.devtools.j2objc.GenerationTest;

import java.io.IOException;

/**
* Unit tests for {@link PrivateDeclarationResolver}.
*
* @author Keith Stanger
*/
public class PrivateDeclarationResolverTest extends GenerationTest {

public void testPrivateSuperclassOfPublicClass() throws IOException {
String translation = translateSourceFile(
"class Test { private static class A {} public static class B extends A {} }",
"Test", "Test.h");
// Make sure the private superclass is still declared in the header.
assertTranslation(translation, "@interface Test_A");
}

public void testPrivateGenericSuperclassOfPublicClass() throws IOException {
String translation = translateSourceFile(
"class Test { private static class A<T> {} public static class B extends A<String> {} }",
"Test", "Test.h");
// Make sure the private superclass is still declared in the header.
assertTranslation(translation, "@interface Test_A");
}

public void testPublicClassInsidePrivateClass() throws IOException {
String translation = translateSourceFile(
"class Test { private static class A { public static class B {} } }", "Test", "Test.h");
// "B" should not be public because it is inside the private class "A".
assertNotInTranslation(translation, "Test_A_B");
}
}

0 comments on commit 123c387

Please sign in to comment.