Initial Commit (#142)

* Initial Commit

Added Maven Annotation Processor Demo project. Initial commit.

* Test - checking

* Delete test.rtf

* Initial commits

Initial commits for sample code

* Update SuppressWarningsDemo.java

* Code changes till SafeVarargs

Code changes till SafeVarargs

* Completed source code

Completed source code

* Final draft

Final draft version. Grammarly check done.

* Deleted java docs

Deleted java docs
This commit is contained in:
syedaf
2022-02-04 19:34:58 -05:00
committed by GitHub
parent 9d6f34dd98
commit 2b97cc155f
91 changed files with 996 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>annotations-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

View File

@@ -0,0 +1,15 @@
package com.reflectoring;
public class AnnotatedMethods {
@Test
public void Test_1() {
System.out.println("This is the first test");
}
public void Test_2() {
System.out.println("This is the second test");
}
}

View File

@@ -0,0 +1,8 @@
package com.reflectoring;
public class BasicAnnotationTest {
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,11 @@
package com.reflectoring;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface ClassRetention {
}

View File

@@ -0,0 +1,12 @@
package com.reflectoring;
import java.lang.annotation.*;
@Inherited
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Company{
String name() default "ABC";
String city() default "XYZ";
}

View File

@@ -0,0 +1,19 @@
package com.reflectoring;
@Company
public class CustomAnnotatedEmployee {
private int id;
private String name;
public CustomAnnotatedEmployee(int id, String name) {
this.id = id;
this.name = name;
}
public void getEmployeeDetails(){
System.out.println("Employee Id: " + id);
System.out.println("Employee Name: " + name);
}
}

View File

@@ -0,0 +1,8 @@
package com.reflectoring;
public class CustomAnnotatedManager extends CustomAnnotatedEmployee{
public CustomAnnotatedManager(int id, String name) {
super(id, name);
}
}

View File

@@ -0,0 +1,10 @@
package com.reflectoring;
public class DeprecatedDemo {
@Deprecated(since = "4.5", forRemoval = true)
public void testLegacyFunction() {
System.out.println("This is a legacy function");
}
}

View File

@@ -0,0 +1,10 @@
package com.reflectoring;
public class DeprecatedDemoTest {
public static void main(String[] args) {
DeprecatedDemo demo = new DeprecatedDemo();
demo.testLegacyFunction();
}
}

View File

@@ -0,0 +1,10 @@
package com.reflectoring;
@Company
public class Employee {
public void getEmployeeStatus(){
System.out.println("This is the Base Employee class");
}
}

View File

@@ -0,0 +1,7 @@
package com.reflectoring;
@SourceRetention
@RuntimeRetention
@ClassRetention
public class EmployeeRetentionAnnotation {
}

View File

@@ -0,0 +1,15 @@
package com.reflectoring;
@FunctionalInterface
interface Print {
void printString(String testString);
}
public class FunctionalInterfaceTest {
public static void main(String args[]) {
Print testPrint = (String testString) -> System.out.println(testString);
testPrint.printString("This is a String");
}
}

View File

@@ -0,0 +1,10 @@
package com.reflectoring;
public class Manager extends Employee {
@Override
public void getEmployeeStatus(){
System.out.println("This is the Manager class");
}
}

View File

@@ -0,0 +1,6 @@
package com.reflectoring;
@Company(name = "AAA", city = "ZZZ")
public class MultiValueAnnotatedEmployee {
}

View File

@@ -0,0 +1,10 @@
package com.reflectoring;
public class OverrideTest {
public static void main(String[] args) {
Manager manager = new Manager();
manager.getEmployeeStatus();
}
}

View File

@@ -0,0 +1,9 @@
package com.reflectoring;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatableCompanies {
RepeatableCompany[] value() default{};
}

View File

@@ -0,0 +1,11 @@
package com.reflectoring;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Repeatable(RepeatableCompanies.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatableCompany {
String name() default "Name_1";
String city() default "City_1";
}

View File

@@ -0,0 +1,6 @@
package com.reflectoring;
@RepeatableCompany
@RepeatableCompany(name = "Name_2", city = "City_2")
public class RepeatedAnnotatedEmployee {
}

View File

@@ -0,0 +1,16 @@
package com.reflectoring;
public class RetentionTest {
public static void main(String[] args) {
SourceRetention[] sourceRetention = new EmployeeRetentionAnnotation().getClass().getAnnotationsByType(SourceRetention.class);
System.out.println("Source Retentions at run-time: " + sourceRetention.length);
RuntimeRetention[] runtimeRetention = new EmployeeRetentionAnnotation().getClass().getAnnotationsByType(RuntimeRetention.class);
System.out.println("Run-time Retentions at run-time: " + runtimeRetention.length);
ClassRetention[] classRetention = new EmployeeRetentionAnnotation().getClass().getAnnotationsByType(ClassRetention.class);
System.out.println("Class Retentions at run-time: " + classRetention.length);
}
}

View File

@@ -0,0 +1,11 @@
package com.reflectoring;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RuntimeRetention {
}

View File

@@ -0,0 +1,57 @@
package com.reflectoring;
import java.util.Arrays;
import java.util.List;
public class SafeVarargsTest {
private void printString(String test1) {
System.out.println(test1);
}
private void printString(String test1, String test2) {
System.out.println(test1);
System.out.println(test2);
}
private void printStringVarargs(String... tests) {
for (String test : tests) {
System.out.println(test);
}
}
@SafeVarargs
private void printStringSafeVarargs(List<String>... testStringLists) {
for (List<String> testStringList : testStringLists) {
for (String testString : testStringList) {
System.out.println(testString);
}
}
}
public static void main(String[] args) {
SafeVarargsTest test = new SafeVarargsTest();
test.printString("String1");
test.printString("*******");
test.printString("String1", "String2");
test.printString("*******");
test.printStringVarargs("String1", "String2");
test.printString("*******");
List<String> testStringList1 = Arrays.asList("One", "Two");
List<String> testStringList2 = Arrays.asList("Three", "Four");
test.printStringSafeVarargs(testStringList1, testStringList2);
}
}

View File

@@ -0,0 +1,19 @@
package com.reflectoring;
@SingleValueAnnotationCompany("XYZ")
public class SingleValueAnnotatedEmployee {
private int id;
private String name;
public SingleValueAnnotatedEmployee(int id, String name) {
this.id = id;
this.name = name;
}
public void getEmployeeDetails(){
System.out.println("Employee Id: " + id);
System.out.println("Employee Name: " + name);
}
}

View File

@@ -0,0 +1,9 @@
package com.reflectoring;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SingleValueAnnotationCompany {
String value() default "ABC";
}

View File

@@ -0,0 +1,8 @@
package com.reflectoring;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface SourceRetention {
}

View File

@@ -0,0 +1,22 @@
package com.reflectoring;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings({"rawtypes", "unchecked"})
public class SuppressWarningsDemo {
public static void main(String[] args) {
SuppressWarningsDemo swDemo = new SuppressWarningsDemo();
swDemo.testSuppressWarning();
}
public void testSuppressWarning() {
Map testMap = new HashMap();
testMap.put(1, "Item_1");
testMap.put(2, "Item_2");
testMap.put(3, "Item_3");
}
}

View File

@@ -0,0 +1,11 @@
package com.reflectoring;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test {
}

View File

@@ -0,0 +1,31 @@
package com.reflectoring;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class TestAnnotatedMethods {
public static void main(String[] args) throws Exception {
Class<AnnotatedMethods> annotatedMethodsClass = AnnotatedMethods.class;
for (Method method : annotatedMethodsClass.getDeclaredMethods()) {
Annotation annotation = method.getAnnotation(Test.class);
Test test = (Test) annotation;
// If the annotation is not null
if (test != null) {
try {
method.invoke(annotatedMethodsClass.getDeclaredConstructor().newInstance());
} catch (Throwable ex) {
System.out.println(ex.getCause());
}
}
}
}
}

View File

@@ -0,0 +1,18 @@
package com.reflectoring;
import java.lang.annotation.Annotation;
public class TestCustomAnnotatedEmployee {
public static void main(String[] args) {
CustomAnnotatedEmployee employee = new CustomAnnotatedEmployee(1, "John Doe");
employee.getEmployeeDetails();
Annotation companyAnnotation = employee.getClass().getAnnotation(Company.class);
Company company = (Company)companyAnnotation;
System.out.println("Company Name: " + company.name());
System.out.println("Company City: " + company.city());
}
}

View File

@@ -0,0 +1,18 @@
package com.reflectoring;
import java.lang.annotation.Annotation;
public class TestCustomAnnotatedManager {
public static void main(String[] args) {
CustomAnnotatedManager manager = new CustomAnnotatedManager(1, "John Doe");
manager.getEmployeeDetails();
Annotation companyAnnotation = manager.getClass().getAnnotation(Company.class);
Company company = (Company)companyAnnotation;
System.out.println("Company Name: " + company.name());
System.out.println("Company City: " + company.city());
}
}

View File

@@ -0,0 +1,17 @@
package com.reflectoring;
import java.lang.annotation.Annotation;
public class TestMultiValueAnnotatedEmployee {
public static void main(String[] args) {
MultiValueAnnotatedEmployee employee = new MultiValueAnnotatedEmployee();
Annotation companyAnnotation = employee.getClass().getAnnotation(Company.class);
Company company = (Company)companyAnnotation;
System.out.println("Company Name: " + company.name());
System.out.println("Company City: " + company.city());
}
}

View File

@@ -0,0 +1,14 @@
package com.reflectoring;
public class TestRepeatedAnnotation {
public static void main(String[] args) {
RepeatableCompany[] repeatableCompanies = RepeatedAnnotatedEmployee.class.getAnnotationsByType(RepeatableCompany.class);
for (RepeatableCompany repeatableCompany : repeatableCompanies) {
System.out.println("Name: " + repeatableCompany.name());
System.out.println("City: " + repeatableCompany.city());
}
}
}

View File

@@ -0,0 +1,17 @@
package com.reflectoring;
import java.lang.annotation.Annotation;
public class TestSingleValueAnnotatedEmployee {
public static void main(String[] args) {
SingleValueAnnotatedEmployee employee = new SingleValueAnnotatedEmployee(1, "John Doe");
employee.getEmployeeDetails();
Annotation companyAnnotation = employee.getClass().getAnnotation(SingleValueAnnotationCompany.class);
SingleValueAnnotationCompany company = (SingleValueAnnotationCompany)companyAnnotation;
System.out.println("Company Name: " + company.value());
}
}