forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-5140 Retrieve field's annotations (eugenp#11254)
- Loading branch information
Showing
6 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...ore-java-annotations/src/main/java/com/baeldung/readannotations/ClassWithAnnotations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
8 changes: 8 additions & 0 deletions
8
...les/core-java-annotations/src/main/java/com/baeldung/readannotations/FirstAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
8 changes: 8 additions & 0 deletions
8
...es/core-java-annotations/src/main/java/com/baeldung/readannotations/SecondAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
8 changes: 8 additions & 0 deletions
8
...les/core-java-annotations/src/main/java/com/baeldung/readannotations/ThirdAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
34 changes: 34 additions & 0 deletions
34
...-annotations/src/test/java/com/baeldung/readannotations/ClassWithAnnotationsUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |