Skip to content

Commit

Permalink
Annotation support: enable injecting annotations to local classes.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 274041792
  • Loading branch information
antonio-cortes-perez authored and copybara-github committed Oct 10, 2019
1 parent b841934 commit 8fa592d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

package java.lang;

import com.google.j2objc.annotations.ReflectionSupport;
import java.lang.reflect.AnnotatedElement;
import java.lang.annotation.Annotation;
import java.net.URL;
Expand Down Expand Up @@ -317,7 +316,7 @@ private Class<?> getPackageInfo() {
packageInfo = Class.forName(pkgName + ".package-info", false, loader);
} catch (ClassNotFoundException ex) {
// store a proxy for the package info that has no annotations
@ReflectionSupport(ReflectionSupport.Level.FULL) class PackageInfoProxy {}
class PackageInfoProxy {}
packageInfo = PackageInfoProxy.class;
}
}
Expand Down
1 change: 1 addition & 0 deletions jre_emul/j2objc.jaif
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ package java.io:
class FileDescriptor: @ReflectionSupport(FULL)

package java.lang:
class Package$1PackageInfoProxy: @ReflectionSupport(FULL)
class Thread: @ReflectionSupport(FULL)

package java.math:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public boolean visit(EnumDeclaration node) {
@Override
public boolean visit(MethodDeclaration node) {
if (!annotatedElementStack.peekLast().isPresent()) {
return false;
return true;
}
AClass annotatedParent = (AClass) annotatedElementStack.peekLast().get();
ExecutableElement executable = node.getExecutableElement();
Expand All @@ -116,7 +116,7 @@ public boolean visit(MethodDeclaration node) {
recordAnnotations(node.getExecutableElement(), annotations);
injectAnnotationsToNode(node, annotations);
}
return false;
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,20 @@ public void testInjectToPackageInfo() throws IOException {
assertNotInTranslation(translation, "__metadata");
}

private void setupObjectiveCNameAnnotations() throws IOException {
private void setupObjectiveCNameAnnotations(String additionalAnnotations) throws IOException {
options.addExternalAnnotationFileContents(
"package com.google.j2objc.annotations: "
+ "annotation @ObjectiveCName: @java.lang.annotation.Retention(value=CLASS) "
+ " @java.lang.annotation.Target(value={TYPE,METHOD,CONSTRUCTOR,PACKAGE}) "
+ " String value "
+ "package p: "
+ "class Test: "
+ " method testMethod()V: @ObjectiveCName(value=\"ignoreMethod\")");
+ " method testMethod()V: @ObjectiveCName(value=\"ignoreMethod\") "
+ additionalAnnotations);
}

public void testInjectObjectiveCName_method() throws IOException {
setupObjectiveCNameAnnotations();
setupObjectiveCNameAnnotations("");
String source = "package p; public class Test { public void testMethod() {} }";
String translation = translateSourceFile(source, "p.Test", "p/Test.m");
// The selector is renamed.
Expand All @@ -299,7 +300,7 @@ public void testInjectObjectiveCName_method() throws IOException {
}

public void testInjectObjectiveCName_junit3Method() throws IOException {
setupObjectiveCNameAnnotations();
setupObjectiveCNameAnnotations("");
String source = "package p; "
+ "public class Test extends junit.framework.TestCase { public void testMethod() {} }";
String translation = translateSourceFile(source, "p.Test", "p/Test.m");
Expand All @@ -310,6 +311,15 @@ public void testInjectObjectiveCName_junit3Method() throws IOException {
assertNotInTranslation(translation, "\"testMethod\"");
}

public void testInjectObjectiveCName_localClass() throws IOException {
setupObjectiveCNameAnnotations("class Test$1Local: @ObjectiveCName(\"RenamedClass\")");
String source = "package p; "
+ "public class Test { public void testMethod() { class Local {} } }";
String translation = translateSourceFile(source, "p.Test", "p/Test.m");
assertTranslation(translation, "@implementation RenamedClass");
assertNotInTranslation(translation, "@implementation PTest_1Local");
}

public void testWeakOuter() throws IOException {
String source = "package p; "
+ "import com.google.j2objc.annotations.WeakOuter; "
Expand Down

0 comments on commit 8fa592d

Please sign in to comment.