JAVA-20500: Changes made for Merge annotations module with google-auto-project
This commit is contained in:
parent
42188fdc2d
commit
d0ed4ad157
@ -8,3 +8,5 @@ This module contains articles about automatic code generation
|
|||||||
- [Introduction to AutoFactory](https://www.baeldung.com/autofactory)
|
- [Introduction to AutoFactory](https://www.baeldung.com/autofactory)
|
||||||
- [Google AutoService](https://www.baeldung.com/google-autoservice)
|
- [Google AutoService](https://www.baeldung.com/google-autoservice)
|
||||||
- [Defensive Copies for Collections Using AutoValue](https://www.baeldung.com/autovalue-defensive-copies)
|
- [Defensive Copies for Collections Using AutoValue](https://www.baeldung.com/autovalue-defensive-copies)
|
||||||
|
- [Java Annotation Processing and Creating a Builder](https://www.baeldung.com/java-annotation-processing-builder)
|
||||||
|
|
||||||
|
28
google-auto-project/annotation-processing/pom.xml
Normal file
28
google-auto-project/annotation-processing/pom.xml
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
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>
|
||||||
|
<artifactId>annotation-processing</artifactId>
|
||||||
|
<name>annotation-processing</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>google-auto-project</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.auto.service</groupId>
|
||||||
|
<artifactId>auto-service</artifactId>
|
||||||
|
<version>${auto-service.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<auto-service.version>1.0-rc2</auto-service.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,123 @@
|
|||||||
|
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<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||||
|
for (TypeElement annotation : annotations) {
|
||||||
|
|
||||||
|
Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation);
|
||||||
|
|
||||||
|
Map<Boolean, List<Element>> annotatedMethods = annotatedElements.stream().collect(Collectors.partitioningBy(element -> ((ExecutableType) element.asType()).getParameterTypes().size() == 1 && element.getSimpleName().toString().startsWith("set")));
|
||||||
|
|
||||||
|
List<Element> setters = annotatedMethods.get(true);
|
||||||
|
List<Element> 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<String, String> 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<String, String> 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("}");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.annotation.processor;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
|
public @interface BuilderProperty {
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
23
google-auto-project/annotation-user/pom.xml
Normal file
23
google-auto-project/annotation-user/pom.xml
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?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>
|
||||||
|
<artifactId>annotation-user</artifactId>
|
||||||
|
<name>annotation-user</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>google-auto-project</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>annotation-processing</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,29 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -0,0 +1,19 @@
|
|||||||
|
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());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -6,6 +6,8 @@
|
|||||||
<artifactId>google-auto-project</artifactId>
|
<artifactId>google-auto-project</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<name>google-auto-project</name>
|
<name>google-auto-project</name>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
@ -13,6 +15,11 @@
|
|||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>annotation-processing</module>
|
||||||
|
<module>annotation-user</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.auto.value</groupId>
|
<groupId>com.google.auto.value</groupId>
|
||||||
|
2
pom.xml
2
pom.xml
@ -812,7 +812,6 @@
|
|||||||
<!-- Modules from default-first -->
|
<!-- Modules from default-first -->
|
||||||
|
|
||||||
<module>akka-modules</module>
|
<module>akka-modules</module>
|
||||||
<module>annotations</module>
|
|
||||||
<module>httpclient-simple</module>
|
<module>httpclient-simple</module>
|
||||||
<module>antlr</module>
|
<module>antlr</module>
|
||||||
<module>apache-kafka</module>
|
<module>apache-kafka</module>
|
||||||
@ -1086,7 +1085,6 @@
|
|||||||
<!-- Modules from default-first -->
|
<!-- Modules from default-first -->
|
||||||
|
|
||||||
<module>akka-modules</module>
|
<module>akka-modules</module>
|
||||||
<module>annotations</module>
|
|
||||||
<module>antlr</module>
|
<module>antlr</module>
|
||||||
<module>apache-kafka</module>
|
<module>apache-kafka</module>
|
||||||
<module>apache-kafka-2</module>
|
<module>apache-kafka-2</module>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user