JAVA-20500: Changes made for Merge annotations module with google-auto-project(removing annotations module)
This commit is contained in:
parent
d0ed4ad157
commit
77270f8418
@ -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)
|
@ -1,28 +0,0 @@
|
||||
<?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>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>annotations</artifactId>
|
||||
</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>
|
@ -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<? 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("}");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package com.baeldung.annotation.processor;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface BuilderProperty {
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
<?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>
|
@ -1,23 +0,0 @@
|
||||
<?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>annotations</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>annotation-processing</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
<?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>
|
@ -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());
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
<?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>annotations</artifactId>
|
||||
<name>annotations</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>annotation-processing</module>
|
||||
<module>annotation-user</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
Loading…
x
Reference in New Issue
Block a user