Note that there are some explanatory texts on larger screens.

plurals
  1. POannotation invisible?
    primarykey
    data
    text
    <p>This puzzles me. I have a class with a custom annotation and I can't seem to verify that the annotation is present. What am I doing wrong here? If I run <code>MyOperationTest</code> (see below), I get this as a result:</p> <pre><code>implements Library.Operation: true has @Library.Marker: false Tada! </code></pre> <hr> <p>Library.java:</p> <pre><code>package com.example.gotchas; public class Library { private Library() {} public @interface Marker {} public interface Operation { public void execute(); } } </code></pre> <p>MyOperation.java:</p> <pre><code>package com.example.gotchas; @Library.Marker public class MyOperation implements Library.Operation { @Override public void execute() { System.out.println("Tada!"); } } </code></pre> <p>MyOperationTest.java:</p> <pre><code>package com.example.gotchas; public class MyOperationTest { static public void main(String[] args) { try { Class&lt;?&gt; cl = Class.forName("com.example.gotchas.MyOperation"); boolean implementsLibraryOperation = Library.Operation.class.isAssignableFrom(cl); boolean hasLibraryMarker = cl.isAnnotationPresent(Library.Marker.class); System.out.println("implements Library.Operation: " +implementsLibraryOperation); System.out.println("has @Library.Marker: "+hasLibraryMarker); if (implementsLibraryOperation) { Class&lt;? extends Library.Operation&gt; opClass = cl.asSubclass(Library.Operation.class); Library.Operation op = opClass.newInstance(); op.execute(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload