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,42 @@
<?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>AnnotationProcessorDemo</artifactId>
<groupId>com.reflectoring.annotation.processor</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.reflectoring.annotation.processor</groupId>
<artifactId>annotation-processor</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.squareup/javapoet -->
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,11 @@
package com.reflectoring.annotation.processor;
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.SOURCE)
public @interface Builder {
}

View File

@@ -0,0 +1,249 @@
package com.reflectoring.annotation.processor;
import com.google.auto.service.AutoService;
import com.squareup.javapoet.*;
import org.apache.commons.text.CaseUtils;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@SupportedAnnotationTypes("com.reflectoring.annotation.processor.Builder")
@SupportedSourceVersion(SourceVersion.RELEASE_11)
@AutoService(Processor.class)
public class BuilderProcessor extends AbstractProcessor {
private Filer filer;
private Messager messager;
private Elements elementUtils;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
filer = processingEnv.getFiler();
messager = processingEnv.getMessager();
elementUtils = processingEnv.getElementUtils();
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element typeElement : roundEnv.getElementsAnnotatedWith(Builder.class)) {
List<Element> fieldElements = typeElement.getEnclosedElements().stream().filter(e -> ElementKind.FIELD.equals(e.getKind())).collect(
Collectors.toList());
String packageName = elementUtils.getPackageOf(typeElement).getQualifiedName().toString();
String className = typeElement.getSimpleName().toString();
String builderName = String.format("%sBuilder", typeElement.getSimpleName().toString());
String classVariableName = CaseUtils.toCamelCase(typeElement.getSimpleName().toString(), false, '_');
try {
//writeBuilderClass(packageName, className, classVariableName, builderName, fieldElements);
writeJavaPoetBuilderClass(packageName, className, classVariableName, builderName, fieldElements, typeElement);
} catch (IOException e) {
messager.printMessage(Diagnostic.Kind.ERROR, "Failed to write file for element", typeElement);
}
}
return true;
}
private String getBaseName(String name) {
int lastPeriodIndex = name.lastIndexOf('.');
if (lastPeriodIndex > 0) {
name = name.substring(lastPeriodIndex + 1);
}
return name;
}
private void writeBuilderClass(String packageName, String className, String classVariableName, String builderName,
List<Element> fieldElements) throws IOException {
JavaFileObject builder = processingEnv.getFiler().createSourceFile(builderName);
try (PrintWriter out = new PrintWriter(builder.openWriter())) {
// Write the Package name
out.print("package ");
out.print(packageName);
out.println(";");
out.println();
// Write the Class name
out.print("public final class ");
out.print(builderName);
out.println(" {");
out.println();
// Write the Field names
for (Element fieldElement : fieldElements) {
TypeMirror typeMirror = fieldElement.asType();
String fieldTypeName = getBaseName(typeMirror.toString());
String fieldName = getBaseName(fieldElement.getSimpleName().toString());
out.print("private ");
out.print(fieldTypeName);
out.print(" ");
out.print(fieldName);
out.print(";");
out.println();
}
out.println();
// Write the Setters
for (Element fieldElement : fieldElements) {
TypeMirror typeMirror = fieldElement.asType();
String fieldTypeName = getBaseName(typeMirror.toString());
String fieldName = getBaseName(fieldElement.getSimpleName().toString());
out.print("public ");
out.print(" ");
out.print(builderName);
out.print(" ");
out.print(fieldName);
out.print("(");
out.print(fieldTypeName);
out.print(" ");
out.print(fieldName);
out.print(") {");
out.println();
out.print(" this.");
out.print(fieldName);
out.print(" = ");
out.print(fieldName);
out.print(";");
out.println();
out.print(" return this;");
out.println();
out.print("}");
out.println();
out.println();
}
// Write the build function
out.print("public ");
out.print(" ");
out.print(className);
out.print(" build() {");
out.println();
out.print(" ");
out.print(className);
out.print(" ");
out.print(classVariableName);
out.print(" = new ");
out.print(className);
out.print("();");
out.println();
for (Element fieldElement : fieldElements) {
TypeMirror typeMirror = fieldElement.asType();
String fieldTypeName = getBaseName(typeMirror.toString());
String fieldName = getBaseName(fieldElement.getSimpleName().toString());
out.print(" ");
out.print(classVariableName);
out.print(".set");
out.print(CaseUtils.toCamelCase(fieldName, true, '_'));
out.print("(this.");
out.print(fieldName);
out.println(");");
}
out.println();
out.print(" return ");
out.print(classVariableName);
out.print(";");
out.println();
out.println(" }");
out.println("}");
}
}
private void writeJavaPoetBuilderClass(String packageName, String className, String classVariableName, String builderName,
List<Element> fieldElements, Element typeElement) throws IOException {
ClassName builderType = ClassName.get(packageName, builderName);
List<FieldSpec> fields = new ArrayList<>(fieldElements.size());
List<MethodSpec> fieldSetters = new ArrayList<>(fieldElements.size());
// Generate the fields and field setters
generateFieldsAndSetters(fields, fieldSetters, fieldElements, builderType);
TypeName targetType = TypeName.get(typeElement.asType());
// Generate the build method
MethodSpec buildMethod = generateBuildMethod(targetType, classVariableName, fields);
TypeSpec builder = TypeSpec.classBuilder(builderType)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addFields(fields)
.addMethods(fieldSetters)
.addMethod(buildMethod).build();
JavaFile file = JavaFile.builder(builderType.packageName(), builder.toBuilder().build()).build();
file.writeTo(filer);
}
private void generateFieldsAndSetters(List<FieldSpec> fields, List<MethodSpec> fieldSetters, List<Element> fieldElements, ClassName builderType){
for (Element fieldElement : fieldElements) {
TypeName typeName = TypeName.get(fieldElement.asType());
String fieldName = getBaseName(fieldElement.getSimpleName().toString());
fields.add(FieldSpec.builder(typeName, fieldName, Modifier.PRIVATE).build());
fieldSetters.add(
MethodSpec.methodBuilder(fieldName)
.addModifiers(Modifier.PUBLIC)
.returns(builderType)
.addParameter(typeName, fieldName)
.addStatement("this.$N = $N", fieldName, fieldName)
.addStatement("return this").build());
}
}
private MethodSpec generateBuildMethod(TypeName targetType, String variableName, List<FieldSpec> fields) {
MethodSpec.Builder buildMethodBuilder = MethodSpec.methodBuilder("build")
.addModifiers(Modifier.PUBLIC)
.returns(targetType)
.addStatement("$1T $2N = new $1T()", targetType, variableName);
for (FieldSpec field : fields) {
buildMethodBuilder.addStatement("$1N.set$2N(this.$3N)", variableName, CaseUtils.toCamelCase(field.name, true, '_'), field.name);
}
buildMethodBuilder.addStatement("return $N", variableName);
return buildMethodBuilder.build();
}
}

View File

@@ -0,0 +1,5 @@
#Generated by Maven
#Wed Jan 26 14:56:24 EST 2022
groupId=com.reflectoring.annotation.processor
artifactId=annotation-processor
version=1.0

View File

@@ -0,0 +1,3 @@
META-INF\services\javax.annotation.processing.Processor
com\reflectoring\annotation\processor\Builder.class
com\reflectoring\annotation\processor\BuilderProcessor.class

View File

@@ -0,0 +1,2 @@
D:\Documents\IntelliJ Projects\AnnotationProcessorDemo\annotation-processor\src\main\java\com\reflectoring\annotation\processor\Builder.java
D:\Documents\IntelliJ Projects\AnnotationProcessorDemo\annotation-processor\src\main\java\com\reflectoring\annotation\processor\BuilderProcessor.java

View File

@@ -0,0 +1,28 @@
<?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>AnnotationProcessorDemo</artifactId>
<groupId>com.reflectoring.annotation.processor</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.reflectoring.annotation.processor</groupId>
<version>1.0</version>
<artifactId>core</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.reflectoring.annotation.processor</groupId>
<artifactId>annotation-processor</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,23 @@
package com.reflectoring.annotation.processor;
public class Department {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@@ -0,0 +1,23 @@
package com.reflectoring.annotation.processor;
public class Employee {
private int id;
private String department;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}

View File

@@ -0,0 +1,15 @@
package com.test;
import com.reflectoring.annotation.processor.Employee;
public class BuilderTest {
public static void main(String[] args) {
Employee employee = new EmployeeBuilder()
.department("Sales")
.build();
System.out.println("Employee dept: " + employee.getDepartment());
}
}

View File

@@ -0,0 +1,27 @@
package com.test;
import com.reflectoring.annotation.processor.Department;
public final class DepartmentBuilder {
private int id;
private String name;
public DepartmentBuilder id(int id) {
this.id = id;
return this;
}
public DepartmentBuilder name(String name) {
this.name = name;
return this;
}
public Department build() {
Department department = new Department();
department.setId(this.id);
department.setName(this.name);
return department;
}
}

View File

@@ -0,0 +1,27 @@
package com.test;
import com.reflectoring.annotation.processor.Employee;
public final class EmployeeBuilder {
private int id;
private String department;
public EmployeeBuilder id(int id) {
this.id = id;
return this;
}
public EmployeeBuilder department(String department) {
this.department = department;
return this;
}
public Employee build() {
Employee employee = new Employee();
employee.setId(this.id);
employee.setDepartment(this.department);
return employee;
}
}

View File

@@ -0,0 +1,26 @@
package com.reflectoring.annotation.processor;
import java.lang.String;
public final class DepartmentBuilder {
private int id;
private String name;
public DepartmentBuilder id(int id) {
this.id = id;
return this;
}
public DepartmentBuilder name(String name) {
this.name = name;
return this;
}
public Department build() {
Department department = new Department();
department.setId(this.id);
department.setName(this.name);
return department;
}
}

View File

@@ -0,0 +1,26 @@
package com.reflectoring.annotation.processor;
import java.lang.String;
public final class EmployeeBuilder {
private int id;
private String department;
public EmployeeBuilder id(int id) {
this.id = id;
return this;
}
public EmployeeBuilder department(String department) {
this.department = department;
return this;
}
public Employee build() {
Employee employee = new Employee();
employee.setId(this.id);
employee.setDepartment(this.department);
return employee;
}
}

View File

@@ -0,0 +1,4 @@
com\reflectoring\annotation\processor\Employee.class
com\reflectoring\annotation\processor\Department.class
com\reflectoring\annotation\processor\EmployeeBuilder.class
com\reflectoring\annotation\processor\DepartmentBuilder.class

View File

@@ -0,0 +1,2 @@
D:\Documents\IntelliJ Projects\AnnotationProcessorDemo\core\src\main\java\com\reflectoring\annotation\processor\Employee.java
D:\Documents\IntelliJ Projects\AnnotationProcessorDemo\core\src\main\java\com\reflectoring\annotation\processor\Department.java

View File

@@ -0,0 +1,21 @@
<?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>com.reflectoring.annotation.processor</groupId>
<artifactId>AnnotationProcessorDemo</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>core</module>
<module>annotation-processor</module>
</modules>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

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());
}
}