Skip to content

Commit

Permalink
Day087
Browse files Browse the repository at this point in the history
  • Loading branch information
hbelmiro committed Aug 14, 2021
1 parent 8279da2 commit eff6402
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3237,4 +3237,42 @@ public class Day086 {
}
}
}
----

== Day 87 - Verifying if a class is annotated by a particular annotation.
[source,java]
----
package com.thegreatapi.ahundreddaysofjava.day087;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class Day087 {
public static void main(String[] args) {
var myAnnotatedObject = new AnnotatedClass();
var myNonAnnotatedObject = new NonAnnotatedClass();
System.out.println(isAnnotated(myAnnotatedObject)); // Prints: true
System.out.println(isAnnotated(myNonAnnotatedObject)); // Prints: false
}
private static boolean isAnnotated(Object object) {
return object.getClass().isAnnotationPresent(MyAnnotation.class);
}
@MyAnnotation
static class AnnotatedClass {
}
static class NonAnnotatedClass {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation {
}
}
----
19 changes: 19 additions & 0 deletions days/day087/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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">
<parent>
<artifactId>days</artifactId>
<groupId>com.thegreatapi.100daysofjava</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>day087</artifactId>

<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.thegreatapi.ahundreddaysofjava.day087;

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

public class Day087 {

public static void main(String[] args) {
var myAnnotatedObject = new AnnotatedClass();
var myNonAnnotatedObject = new NonAnnotatedClass();

System.out.println(isAnnotated(myAnnotatedObject)); // Prints: true
System.out.println(isAnnotated(myNonAnnotatedObject)); // Prints: false
}

private static boolean isAnnotated(Object object) {
return object.getClass().isAnnotationPresent(MyAnnotation.class);
}

@MyAnnotation
static class AnnotatedClass {
}

static class NonAnnotatedClass {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation {
}
}
1 change: 1 addition & 0 deletions days/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<module>day084</module>
<module>day085</module>
<module>day086</module>
<module>day087</module>
</modules>

</project>

0 comments on commit eff6402

Please sign in to comment.