How to create Java annotation for a fake abstract method?

417 Views Asked by At

I say fake because the method isn't declared as abstract, but it doesn't have an implementation.

I'm trying to fix a bug introduced by using BridJ. BridJ uses a custom annotation to annotate a method inside of an Android Activity. The method has the following signature:

@Library("example")
public static void helloLog(Pointer<Byte> logThis);

But the compiler complains of Missing method body, or declare abstract on these two lines of code. So I decided to look at the source code for the annotation, which is:

@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR})
public @interface Library {

    /**
     * Name of this library.
     */
    String value();

    /**
     * Names of libraries that need to be loaded before this library is loaded
     */
    String[] dependencies() default {};

    String versionPattern() default "";
}

After reading up on Java Custom Annotations, I don't see anything wrong with the definition of the annotation. For the heck of it, I added an implementation to the helloLog() method.

public static void helloLog(Pointer<Byte> logThis) {}

After which the error goes away. I didn't expect it to fix the problem, but FWIW, it allows me to build and run, but it crashes on startup because of a NullPointerException that I plan on looking into.

Update 1

@thomas-kläger and others suggested adding the native keyword to helloLog. I tried that, but then I get this runtime error:

java.lang.UnsatisfiedLinkError: No implementation found for void connectedsolutions.cummins.com.bridjtest.MainActivity.helloLog(org.bridj.Pointer) (tried Java_connectedsolutions_cummins_com_bridjtest_MainActivity_helloLog and Java_connectedsolutions_cummins_com_bridjtest_MainActivity_helloLog__Lorg_bridj_Pointer_2)

I will read up on the documentation and examples some more. Thanks.

0

There are 0 best solutions below