Skip to content

Commit

Permalink
BAEL-5140 Retrieve field's annotations (eugenp#11254)
Browse files Browse the repository at this point in the history
  • Loading branch information
polomos authored Oct 11, 2021
1 parent e046577 commit 92ad02a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
17 changes: 15 additions & 2 deletions core-java-modules/core-java-annotations/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-annotations</artifactId>
<version>0.1.0-SNAPSHOT</version>
Expand All @@ -14,6 +14,19 @@
<version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<assertj-core.version>3.10.0</assertj-core.version>
</properties>

<build>
<finalName>core-java-annotations</finalName>
<resources>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.readannotations;

public class ClassWithAnnotations {

@FirstAnnotation
@SecondAnnotation
@ThirdAnnotation
private String classMember;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.readannotations;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface FirstAnnotation {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.readannotations;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface SecondAnnotation {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.readannotations;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.SOURCE)
public @interface ThirdAnnotation {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baeldung.readannotations;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import static org.assertj.core.api.Assertions.assertThat;

public class ClassWithAnnotationsUnitTest {

@Test
public void whenCallingGetDeclaredAnnotations_thenOnlyRuntimeAnnotationsAreAvailable() throws NoSuchFieldException {
Field classMemberField = ClassWithAnnotations.class.getDeclaredField("classMember");
Annotation[] annotations = classMemberField.getDeclaredAnnotations();
assertThat(annotations).hasSize(2);
}

@Test
public void whenCallingIsAnnotationPresent_thenOnlyRuntimeAnnotationsAreAvailable() throws NoSuchFieldException {
Field classMemberField = ClassWithAnnotations.class.getDeclaredField("classMember");
assertThat(classMemberField.isAnnotationPresent(FirstAnnotation.class)).isTrue();
assertThat(classMemberField.isAnnotationPresent(SecondAnnotation.class)).isTrue();
assertThat(classMemberField.isAnnotationPresent(ThirdAnnotation.class)).isFalse();
}

@Test
public void whenCallingGetDeclaredAnnotationsOrGetAnnotations_thenSameAnnotationsAreReturned() throws NoSuchFieldException {
Field classMemberField = ClassWithAnnotations.class.getDeclaredField("classMember");
Annotation[] declaredAnnotations = classMemberField.getDeclaredAnnotations();
Annotation[] annotations = classMemberField.getAnnotations();
assertThat(declaredAnnotations).containsExactly(annotations);
}
}

0 comments on commit 92ad02a

Please sign in to comment.