<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Paul Pladziewicz]]></title><description><![CDATA[Paul Pladziewicz]]></description><link>https://blog.paulpladziewicz.com</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 08:47:34 GMT</lastBuildDate><atom:link href="https://blog.paulpladziewicz.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Java Annotations Demystified]]></title><description><![CDATA[A lot of the "magic" of Java is rooted within annotations, especially when working with libraries and frameworks. Once you create your own and see how it works, you’ll have an easier time developing Java applications.
Simple Example Code on Github
Wh...]]></description><link>https://blog.paulpladziewicz.com/java-annotations-demystified</link><guid isPermaLink="true">https://blog.paulpladziewicz.com/java-annotations-demystified</guid><category><![CDATA[Java]]></category><category><![CDATA[annotations]]></category><dc:creator><![CDATA[Paul Pladziewicz]]></dc:creator><pubDate>Tue, 21 Jan 2025 20:00:10 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1737491633018/4dc11886-53cb-4d2a-8568-317d16df0b79.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A lot of the "magic" of Java is rooted within annotations, especially when working with libraries and frameworks. Once you create your own and see how it works, you’ll have an easier time developing Java applications.</p>
<p><a target="_blank" href="https://github.com/paulpladziewicz/java-annotations">Simple Example Code on Github</a></p>
<h2 id="heading-what-they-are">What They Are</h2>
<p>Java annotations are just metadata, since they do not change the way your code runs. The body of an annotated method will run the same as it would without it.</p>
<p>But many tools within the Java ecosystem look for annotations and apply additional behavior if specific ones are present. This is why they seem like magic.</p>
<h2 id="heading-lets-create-our-own">Let’s Create Our Own</h2>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.lang.annotation.Retention;
<span class="hljs-keyword">import</span> java.lang.annotation.RetentionPolicy;

<span class="hljs-meta">@Retention(RetentionPolicy.RUNTIME)</span>
<span class="hljs-keyword">public</span> <span class="hljs-meta">@interface</span> SimpleAnnotation {
  <span class="hljs-function">String <span class="hljs-title">value</span><span class="hljs-params">()</span> <span class="hljs-keyword">default</span> ""</span>; <span class="hljs-comment">// set if only one value is passed without an attribute name</span>
  <span class="hljs-function">String <span class="hljs-title">name</span><span class="hljs-params">()</span> <span class="hljs-keyword">default</span> ""</span>; <span class="hljs-comment">// providing defaults help avoid null checks</span>
  <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">age</span><span class="hljs-params">()</span> <span class="hljs-keyword">default</span> -1</span>;
}
</code></pre>
<p>Notice the open and closing parentheses after the field name.</p>
<h3 id="heading-retention-policy">Retention Policy</h3>
<p>Annotations don’t need to stick around after compile time (RetentionPolicy.SOURCE). These annotations are primarily for IDEs, compilers, and code checkers. You can also have annotations included within class files, but not at runtime, which is the default policy (RetentionPolicy.CLASS). This is primarily for bytecode tools. But most of the time, you’ll want to have annotations included at runtime so your libraries or framework will be able to find them and add behavior (RetentionPolicy.RUNTIME).</p>
<h2 id="heading-how-are-annotations-used">How Are Annotations Used</h2>
<p>In order to do something with an annotation, we use <strong>reflection</strong>. Reflection refers to the ability to inspect and interact with a class's methods and properties. For our use case, it will help us know if an annotation has been applied to a member of our class.</p>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.lang.reflect.Method;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Main</span> </span>{
  <span class="hljs-meta">@SimpleAnnotation(name = "Paul", age = 3)</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
    Class&lt;?&gt; clazz = Main.class; // <span class="hljs-function">could also use <span class="hljs-title">getClass</span><span class="hljs-params">()</span> with an instantiated class object

    <span class="hljs-title">for</span> <span class="hljs-params">(Method method : clazz.getMethods()</span>) </span>{
      SimpleAnnotation simpleAnnotation = method.getAnnotation(SimpleAnnotation.class);

      <span class="hljs-keyword">if</span> (simpleAnnotation != <span class="hljs-keyword">null</span>) {
        <span class="hljs-keyword">if</span> (!simpleAnnotation.name().isEmpty()) {
          System.out.println(simpleAnnotation.name());
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!simpleAnnotation.value().isEmpty()) {
          System.out.println(simpleAnnotation.value());
        }

        <span class="hljs-keyword">if</span> (simpleAnnotation.age() &gt; <span class="hljs-number">0</span>) {
          System.out.println(simpleAnnotation.age());
        }
      }
    }
  }
}
</code></pre>
<p>Here, I am using reflection to review each method of the Main class to see if my SimpleAnnotation has been applied. If the annotation has been applied, I’ll print the values passed to standard output.</p>
<p>One way or another, the library or framework you're using is looking for annotations in a similar manner and adding behavior.</p>
<h2 id="heading-special-considerations">Special Considerations</h2>
<p>It's not obvious what values you must pass to an annotation and what’s optional. Java doesn't give us a way to express that.</p>
<p>For each annotation, you first have to know the annotation exists. Then, you have to read the documentation to really understand what values to pass and what the annotation is doing.</p>
]]></content:encoded></item></channel></rss>