1 // Copyright 2018 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.example.jni_generator; 6 7 import org.chromium.base.annotations.NativeMethods; 8 9 /** 10 * Sample class that uses the JNI annotation processor for static methods. 11 * See generated files at bottom. 12 */ 13 class SampleForAnnotationProcessor { 14 class TestStruct { 15 int mA; 16 int mB; 17 } 18 /** 19 * Static methods declared here, each of these refer to a native method 20 * which will have its declaration generated by our annotation processor. 21 * There will also be a class generated to wrap these native methods 22 * with the name SampleForAnnotationProcessorJni which will implement 23 * Natives. 24 */ 25 @NativeMethods 26 interface Natives { foo()27 void foo(); bar(SampleForAnnotationProcessor sample)28 SampleForAnnotationProcessor bar(SampleForAnnotationProcessor sample); revString(String stringToReverse)29 String revString(String stringToReverse); sendToNative(String[] strs)30 String[] sendToNative(String[] strs); sendSamplesToNative(SampleForAnnotationProcessor[] strs)31 SampleForAnnotationProcessor[] sendSamplesToNative(SampleForAnnotationProcessor[] strs); hasPhalange()32 boolean hasPhalange(); 33 testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools)34 int[] testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, 35 short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, 36 double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools); 37 testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, TestStruct tStruct, TestStruct[] structs, Object obj, Object[] objects)38 void testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, 39 Throwable[] throwables, String string, String[] strings, TestStruct tStruct, 40 TestStruct[] structs, Object obj, Object[] objects); 41 returnThrowable()42 Throwable returnThrowable(); returnThrowables()43 Throwable[] returnThrowables(); returnClass()44 Class returnClass(); returnClasses()45 Class[] returnClasses(); returnString()46 String returnString(); returnStrings()47 String[] returnStrings(); returnStruct()48 TestStruct returnStruct(); returnStructs()49 TestStruct[] returnStructs(); returnObject()50 Object returnObject(); returnObjects()51 Object[] returnObjects(); 52 } 53 test()54 void test() { 55 int[] x = new int[] {1, 2, 3, 4, 5}; 56 String[] strs = new String[] {"the", "quick", "brown", "fox"}; 57 strs = SampleForAnnotationProcessorJni.get().sendToNative(strs); 58 59 SampleForAnnotationProcessor[] samples = 60 new SampleForAnnotationProcessor[] {this, this, this}; 61 samples = SampleForAnnotationProcessorJni.get().sendSamplesToNative(samples); 62 63 // Instance of Natives accessed through (classname + "Jni").get(). 64 SampleForAnnotationProcessorJni.get().foo(); 65 SampleForAnnotationProcessor sample = SampleForAnnotationProcessorJni.get().bar(this); 66 boolean hasPhalange = SampleForAnnotationProcessorJni.get().hasPhalange(); 67 String s = SampleForAnnotationProcessorJni.get().revString("abcd"); 68 } 69 } 70