From 77270f8418105cd7e3971b72b7a86b31c85af13b Mon Sep 17 00:00:00 2001 From: Bipinkumar27 Date: Tue, 12 Sep 2023 19:22:23 +0530 Subject: [PATCH] JAVA-20500: Changes made for Merge annotations module with google-auto-project(removing annotations module) --- annotations/README.md | 7 - annotations/annotation-processing/pom.xml | 28 ---- .../processor/BuilderProcessor.java | 123 ------------------ .../annotation/processor/BuilderProperty.java | 8 -- .../src/main/resources/logback.xml | 13 -- annotations/annotation-user/pom.xml | 23 ---- .../java/com/baeldung/annotation/Person.java | 29 ----- .../src/main/resources/logback.xml | 13 -- .../annotation/PersonBuilderUnitTest.java | 19 --- annotations/pom.xml | 21 --- 10 files changed, 284 deletions(-) delete mode 100644 annotations/README.md delete mode 100644 annotations/annotation-processing/pom.xml delete mode 100644 annotations/annotation-processing/src/main/java/com/baeldung/annotation/processor/BuilderProcessor.java delete mode 100644 annotations/annotation-processing/src/main/java/com/baeldung/annotation/processor/BuilderProperty.java delete mode 100644 annotations/annotation-processing/src/main/resources/logback.xml delete mode 100644 annotations/annotation-user/pom.xml delete mode 100644 annotations/annotation-user/src/main/java/com/baeldung/annotation/Person.java delete mode 100644 annotations/annotation-user/src/main/resources/logback.xml delete mode 100644 annotations/annotation-user/src/test/java/com/baeldung/annotation/PersonBuilderUnitTest.java delete mode 100644 annotations/pom.xml diff --git a/annotations/README.md b/annotations/README.md deleted file mode 100644 index ec4005fc5e..0000000000 --- a/annotations/README.md +++ /dev/null @@ -1,7 +0,0 @@ -## Annotations - -This module contains articles about Java annotations - -### Relevant Articles: - -- [Java Annotation Processing and Creating a Builder](https://www.baeldung.com/java-annotation-processing-builder) diff --git a/annotations/annotation-processing/pom.xml b/annotations/annotation-processing/pom.xml deleted file mode 100644 index 14bbc409e5..0000000000 --- a/annotations/annotation-processing/pom.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - 4.0.0 - annotation-processing - annotation-processing - - - com.baeldung - 1.0.0-SNAPSHOT - annotations - - - - - com.google.auto.service - auto-service - ${auto-service.version} - provided - - - - - 1.0-rc2 - - - \ No newline at end of file diff --git a/annotations/annotation-processing/src/main/java/com/baeldung/annotation/processor/BuilderProcessor.java b/annotations/annotation-processing/src/main/java/com/baeldung/annotation/processor/BuilderProcessor.java deleted file mode 100644 index 18d8f9a8a9..0000000000 --- a/annotations/annotation-processing/src/main/java/com/baeldung/annotation/processor/BuilderProcessor.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.baeldung.annotation.processor; - -import java.io.IOException; -import java.io.PrintWriter; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; -import javax.annotation.processing.*; -import javax.lang.model.SourceVersion; -import javax.lang.model.element.Element; -import javax.lang.model.element.TypeElement; -import javax.lang.model.type.ExecutableType; -import javax.tools.Diagnostic; -import javax.tools.JavaFileObject; - -import com.google.auto.service.AutoService; - -@SupportedAnnotationTypes("com.baeldung.annotation.processor.BuilderProperty") -@SupportedSourceVersion(SourceVersion.RELEASE_8) -@AutoService(Processor.class) -public class BuilderProcessor extends AbstractProcessor { - - @Override - public boolean process(Set annotations, RoundEnvironment roundEnv) { - for (TypeElement annotation : annotations) { - - Set annotatedElements = roundEnv.getElementsAnnotatedWith(annotation); - - Map> annotatedMethods = annotatedElements.stream().collect(Collectors.partitioningBy(element -> ((ExecutableType) element.asType()).getParameterTypes().size() == 1 && element.getSimpleName().toString().startsWith("set"))); - - List setters = annotatedMethods.get(true); - List otherMethods = annotatedMethods.get(false); - - otherMethods.forEach(element -> processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@BuilderProperty must be applied to a setXxx method with a single argument", element)); - - if (setters.isEmpty()) { - continue; - } - - String className = ((TypeElement) setters.get(0).getEnclosingElement()).getQualifiedName().toString(); - - Map setterMap = setters.stream().collect(Collectors.toMap(setter -> setter.getSimpleName().toString(), setter -> ((ExecutableType) setter.asType()).getParameterTypes().get(0).toString())); - - try { - writeBuilderFile(className, setterMap); - } catch (IOException e) { - e.printStackTrace(); - } - - } - - return true; - } - - private void writeBuilderFile(String className, Map setterMap) throws IOException { - - String packageName = null; - int lastDot = className.lastIndexOf('.'); - if (lastDot > 0) { - packageName = className.substring(0, lastDot); - } - - String simpleClassName = className.substring(lastDot + 1); - String builderClassName = className + "Builder"; - String builderSimpleClassName = builderClassName.substring(lastDot + 1); - - JavaFileObject builderFile = processingEnv.getFiler().createSourceFile(builderClassName); - try (PrintWriter out = new PrintWriter(builderFile.openWriter())) { - - if (packageName != null) { - out.print("package "); - out.print(packageName); - out.println(";"); - out.println(); - } - - out.print("public class "); - out.print(builderSimpleClassName); - out.println(" {"); - out.println(); - - out.print(" private "); - out.print(simpleClassName); - out.print(" object = new "); - out.print(simpleClassName); - out.println("();"); - out.println(); - - out.print(" public "); - out.print(simpleClassName); - out.println(" build() {"); - out.println(" return object;"); - out.println(" }"); - out.println(); - - setterMap.entrySet().forEach(setter -> { - String methodName = setter.getKey(); - String argumentType = setter.getValue(); - - out.print(" public "); - out.print(builderSimpleClassName); - out.print(" "); - out.print(methodName); - - out.print("("); - - out.print(argumentType); - out.println(" value) {"); - out.print(" object."); - out.print(methodName); - out.println("(value);"); - out.println(" return this;"); - out.println(" }"); - out.println(); - }); - - out.println("}"); - - } - } - -} diff --git a/annotations/annotation-processing/src/main/java/com/baeldung/annotation/processor/BuilderProperty.java b/annotations/annotation-processing/src/main/java/com/baeldung/annotation/processor/BuilderProperty.java deleted file mode 100644 index 84fcc73850..0000000000 --- a/annotations/annotation-processing/src/main/java/com/baeldung/annotation/processor/BuilderProperty.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.baeldung.annotation.processor; - -import java.lang.annotation.*; - -@Target(ElementType.METHOD) -@Retention(RetentionPolicy.SOURCE) -public @interface BuilderProperty { -} diff --git a/annotations/annotation-processing/src/main/resources/logback.xml b/annotations/annotation-processing/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/annotations/annotation-processing/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/annotations/annotation-user/pom.xml b/annotations/annotation-user/pom.xml deleted file mode 100644 index 37a2e36f61..0000000000 --- a/annotations/annotation-user/pom.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - 4.0.0 - annotation-user - annotation-user - - - com.baeldung - annotations - 1.0.0-SNAPSHOT - - - - - com.baeldung - annotation-processing - ${project.parent.version} - - - - \ No newline at end of file diff --git a/annotations/annotation-user/src/main/java/com/baeldung/annotation/Person.java b/annotations/annotation-user/src/main/java/com/baeldung/annotation/Person.java deleted file mode 100644 index 23787ba4f4..0000000000 --- a/annotations/annotation-user/src/main/java/com/baeldung/annotation/Person.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.annotation; - -import com.baeldung.annotation.processor.BuilderProperty; - -public class Person { - - private int age; - - private String name; - - public int getAge() { - return age; - } - - @BuilderProperty - public void setAge(int age) { - this.age = age; - } - - public String getName() { - return name; - } - - @BuilderProperty - public void setName(String name) { - this.name = name; - } - -} diff --git a/annotations/annotation-user/src/main/resources/logback.xml b/annotations/annotation-user/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/annotations/annotation-user/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/annotations/annotation-user/src/test/java/com/baeldung/annotation/PersonBuilderUnitTest.java b/annotations/annotation-user/src/test/java/com/baeldung/annotation/PersonBuilderUnitTest.java deleted file mode 100644 index d5f758089a..0000000000 --- a/annotations/annotation-user/src/test/java/com/baeldung/annotation/PersonBuilderUnitTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.annotation; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class PersonBuilderUnitTest { - - @Test - public void whenBuildPersonWithBuilder_thenObjectHasPropertyValues() { - - Person person = new PersonBuilder().setAge(25).setName("John").build(); - - assertEquals(25, person.getAge()); - assertEquals("John", person.getName()); - - } - -} diff --git a/annotations/pom.xml b/annotations/pom.xml deleted file mode 100644 index b3fabb8637..0000000000 --- a/annotations/pom.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - 4.0.0 - annotations - annotations - pom - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - annotation-processing - annotation-user - - - \ No newline at end of file