Skip to content

Commit

Permalink
JNI: Rename @JniStaticNatives -> @NativeMethods
Browse files Browse the repository at this point in the history
This name makes more sense given that this interface will also
contain non-static natives.

Bug: 898261
Change-Id: Icc3e2bb9c863cb722047accd3e699e9b4511a5ff
Reviewed-on: https://chromium-review.googlesource.com/c/1366224
Commit-Queue: Aiden Benner <abenner@google.com>
Reviewed-by: agrieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#614831}
  • Loading branch information
Aiden Benner authored and Commit Bot committed Dec 7, 2018
1 parent a87d3c4 commit 0c63af8
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2894,7 +2894,7 @@ if (is_android) {
java_library("jni_java") {
supports_android = true
java_files = [
"android/java/src/org/chromium/base/annotations/JniStaticNatives.java",
"android/java/src/org/chromium/base/annotations/NativeMethods.java",
"android/java/src/org/chromium/base/JniStaticTestMocker.java",
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import android.animation.TimeAnimator.TimeListener;
import android.util.Log;

import org.chromium.base.annotations.JniStaticNatives;
import org.chromium.base.annotations.NativeMethods;

/**
* Record Android animation frame rate and save it to UMA histogram. This is mainly for monitoring
Expand Down Expand Up @@ -143,7 +143,7 @@ public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime)
}
}

@JniStaticNatives
@NativeMethods
interface Natives {
void saveHistogram(String histogramName, long[] frameTimesMs, int count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Implemented by the TEST_HOOKS field in JNI wrapper classes that are generated
* by the JNI annotation processor. Used in tests for setting the mock
* implementation of a {@link org.chromium.base.annotations.JniStaticNatives} interface.
* @param <T> The interface annotated with {@link org.chromium.base.annotations.JniStaticNatives}
* implementation of a {@link org.chromium.base.annotations.NativeMethods} interface.
* @param <T> The interface annotated with {@link org.chromium.base.annotations.NativeMethods}
*/
public interface JniStaticTestMocker<T> { void setInstanceForTesting(T instance); }
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* static native void nativeFoo(@JCaller A a, long nativeCppClass);
*
* @JniStaticNatives
* @NativeMethods
* interface Natives {
* void foo(@JCaller A a, long nativeCppClass);
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface JniStaticNatives {}
public @interface NativeMethods {}
10 changes: 5 additions & 5 deletions base/android/jni_generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ There are two ways to call native methods:
bindings will automatically generate the appropriate cast and call into C++
code (JNI itself is only C).

**Method 2 - Using an interface annotated with @JniStaticNatives**
**Method 2 - Using an interface annotated with @NativeMethods**

- Only works for static methods.
- Supports mocking in tests.
- Inner-interfaces annotated with `@JniStaticNatives` defines the native
- Inner-interfaces annotated with `@NativeMethods` defines the native
interface.
- A companion JNI class is generated which implements this interface with the
proper bindings to forward calls to C++ functions (that you must write).
Expand All @@ -94,7 +94,7 @@ Usage:
1. Enable the JNI processor by adding
`annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]`
to the Java target.
2. Create an inner-interface annotated with `@JniStaticNatives` that contains
2. Create an inner-interface annotated with `@NativeMethods` that contains
the declaration of the corresponding static methods you wish to have
implemented.
3. Call native functions using `${OriginalClassName}Jni.get().${method}` e.g.
Expand Down Expand Up @@ -125,15 +125,15 @@ class A {
// Using AJni.get() everywhere has no overhead when running in release with R8.
}

@JniStaticNatives
@NativeMethods
interface Natives {
void foo();
double bar(int a, int b);
}
}
```

- If a class contains an interface annotated with `@JniStaticNatives`, the JNI
- If a class contains an interface annotated with `@NativeMethods`, the JNI
annotation processor generates a class named `${OriginalClassName}Jni` which
has a function `get()` which returns an implementation of the annotated
interface (and will call the corresponding static method as if it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package org.chromium.example.jni_generator;

import org.chromium.base.annotations.JniStaticNatives;
import org.chromium.base.annotations.NativeMethods;

/**
* Sample class that uses the JNI annotation processor for static methods.
Expand All @@ -22,7 +22,7 @@ class TestStruct {
* with the name SampleForAnnotationProcessorJni which will implement
* Natives.
*/
@JniStaticNatives
@NativeMethods
interface Natives {
void foo();
SampleForAnnotationProcessor bar(SampleForAnnotationProcessor sample);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.squareup.javapoet.TypeSpec;

import org.chromium.base.JniStaticTestMocker;
import org.chromium.base.annotations.JniStaticNatives;
import org.chromium.base.annotations.NativeMethods;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -49,17 +49,17 @@

/**
* Annotation processor that finds inner interfaces annotated with
* {@link JniStaticNatives} and generates a class with native bindings
* {@link NativeMethods} and generates a class with native bindings
* (GEN_JNI) and a class specific wrapper class with name (classnameJni)
*
* NativeClass - refers to the class that contains all native declarations.
* NativeWrapperClass - refers to the class that is generated for each class
* containing an interface annotated with JniStaticNatives.
* containing an interface annotated with NativeMethods.
*
*/
@AutoService(Processor.class)
public class JniProcessor extends AbstractProcessor {
private static final Class<JniStaticNatives> JNI_STATIC_NATIVES_CLASS = JniStaticNatives.class;
private static final Class<NativeMethods> JNI_STATIC_NATIVES_CLASS = NativeMethods.class;

private static final String NATIVE_WRAPPER_CLASS_POSTFIX = "Jni";

Expand Down Expand Up @@ -164,11 +164,11 @@ public boolean process(

List<JavaFile> writeQueue = Lists.newArrayList();
for (Element e : roundEnvironment.getElementsAnnotatedWith(JNI_STATIC_NATIVES_CLASS)) {
// @JniStaticNatives can only annotate types so this is safe.
// @NativeMethods can only annotate types so this is safe.
TypeElement type = (TypeElement) e;

if (!e.getKind().isInterface()) {
printError("@JniStaticNatives must annotate an interface", e);
printError("@NativeMethods must annotate an interface", e);
}

// Interface must be nested within a class.
Expand Down Expand Up @@ -208,7 +208,7 @@ public boolean process(

// Queue this file for writing.
// Can't write right now because the wrapper class depends on NativeClass
// to be written and we can't write NativeClass until all @JniStaticNatives
// to be written and we can't write NativeClass until all @NativeMethods
// interfaces are processed because each will add new native methods.
JavaFile file = JavaFile.builder(packageName, nativeWrapperClassSpec).build();
writeQueue.add(file);
Expand Down Expand Up @@ -274,8 +274,8 @@ String getNativeMethodName(String packageName, String className, String oldMetho

/**
* Creates method specs for the native methods of NativeClass given
* the method declarations from a {@link JniStaticNatives} annotated interface
* @param interfaceMethods method declarations from a {@link JniStaticNatives} annotated
* the method declarations from a {@link NativeMethods} annotated interface
* @param interfaceMethods method declarations from a {@link NativeMethods} annotated
* interface
* @param outerType ClassName of class that contains the annotated interface
* @return map from old method name to new native method specification
Expand Down Expand Up @@ -318,11 +318,11 @@ void printError(String s) {

void printError(String s, Element e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
String.format("Error processing @JniStaticNatives interface: %s", s), e);
String.format("Error processing @NativeMethods interface: %s", s), e);
}

/**
* Creates a class spec for an implementation of an {@link JniStaticNatives} annotated interface
* Creates a class spec for an implementation of an {@link NativeMethods} annotated interface
* that will wrap calls to the NativesClass which contains the actual native method
* declarations.
*
Expand All @@ -336,7 +336,7 @@ void printError(String s, Element e) {
*
* @param name name of the wrapper class.
* @param isPublic if true, a public modifier will be added to this native wrapper.
* @param nativeInterface the {@link JniStaticNatives} annotated type that this native wrapper
* @param nativeInterface the {@link NativeMethods} annotated type that this native wrapper
* will implement.
* @param methodMap a map from the old method name to the new method spec in NativeClass.
* */
Expand All @@ -355,7 +355,7 @@ TypeSpec createNativeWrapperClassSpec(String name, boolean isPublic,
// Start by adding all the native method wrappers.
for (Element enclosed : nativeInterface.getEnclosedElements()) {
if (enclosed.getKind() != ElementKind.METHOD) {
printError("Cannot have a non-method in a @JniStaticNatives annotated interface",
printError("Cannot have a non-method in a @NativeMethods annotated interface",
enclosed);
}

Expand Down Expand Up @@ -452,7 +452,7 @@ TypeSpec createNativeWrapperClassSpec(String name, boolean isPublic,

/**
* Creates a wrapper method that overrides interfaceMethod and calls staticNativeMethod.
* @param interfaceMethod method that will be overridden in a {@link JniStaticNatives} annotated
* @param interfaceMethod method that will be overridden in a {@link NativeMethods} annotated
* interface.
* @param staticNativeMethod method that will be called in NativeClass.
*/
Expand Down
4 changes: 2 additions & 2 deletions base/android/jni_generator/jni_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
flags=re.DOTALL)

_NATIVE_PROXY_EXTRACTION_REGEX = re.compile(
r'@JniStaticNatives\s*(public|private)*\s*interface\s*'
r'@NativeMethods\s*(public|private)*\s*interface\s*'
r'(?P<interface_name>\w*)\s*(?P<interface_body>{(\s*.*)+?\s*})')

# Use 100 columns rather than 80 because it makes many lines more readable.
Expand Down Expand Up @@ -837,7 +837,7 @@ def CreateFromClass(class_file, options):
return jni_from_javap


# 'Proxy' native methods are declared in an @JniStaticNatives interface without
# 'Proxy' native methods are declared in an @NativeMethods interface without
# a native qualifier and indicate that the JNI annotation processor should
# generate code to link between the equivalent native method as if it were
# declared statically.
Expand Down
12 changes: 6 additions & 6 deletions base/android/jni_generator/jni_generator_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ def testProxyNativesWithNatives(self):
class Foo {
@JniStaticNatives
@NativeMethods
interface Natives {
void foo();
String bar(String s, int y, char x, short z);
Expand All @@ -1267,7 +1267,7 @@ class Foo {
def testEscapingProxyNatives(self):
test_data = """
class SampleProxyJni {
@JniStaticNatives
@NativeMethods
interface Natives {
void foo_bar();
void foo__bar();
Expand Down Expand Up @@ -1306,7 +1306,7 @@ def testProxyNativesMainDex(self):
test_data = """
@MainDex
class Foo() {
@JniStaticNatives
@NativeMethods
interface Natives {
void thisismaindex();
}
Expand All @@ -1318,7 +1318,7 @@ class Foo() {

non_main_dex_test_data = """
class Bar() {
@JniStaticNatives
@NativeMethods
interface Natives {
void foo();
void bar();
Expand Down Expand Up @@ -1382,7 +1382,7 @@ def testProxyNatives(self):
test_data = """
class SampleProxyJni {
private void do_not_match();
@JniStaticNatives
@NativeMethods
interface Natives {
void foo();
int bar(int x, int y);
Expand All @@ -1396,7 +1396,7 @@ class SampleProxyJni {

bad_spaced_test_data = """
class SampleProxyJni{
@JniStaticNatives interface
@NativeMethods interface
Natives
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.junit.rules.ExternalResource;

import org.chromium.base.JniStaticTestMocker;
import org.chromium.base.annotations.JniStaticNatives;
import org.chromium.base.annotations.NativeMethods;

import java.util.ArrayList;

Expand All @@ -23,7 +23,7 @@ public class JniMocker extends ExternalResource {
* All TEST_HOOKS set with this function will have their test instance
* set to null after each test is run.
*
* @param T Interface type that implements {@link JniStaticNatives}
* @param T Interface type that implements {@link NativeMethods}
* @param hook Instance of the corresponding JniStaticTestMocker in the
* wrapper class generated by the JNI annotation processor
* @param testInst Mock instance of type T which will be set as the
Expand Down

0 comments on commit 0c63af8

Please sign in to comment.