-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
288 additions
and
489 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http:https://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>daydayup</artifactId> | ||
<groupId>com.github.landyking</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>learn-apt-demo</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.landyking</groupId> | ||
<artifactId>learn-apt</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.5.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
<encoding>UTF-8</encoding> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package usage; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println(EnableStatus.READ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package usage; | ||
|
||
import enumTool.CodeWithValue; | ||
import enumTool.EnumCodeValue; | ||
|
||
@EnumCodeValue({ | ||
@CodeWithValue(name = "READ", code = 1, value = "hello"), | ||
@CodeWithValue(name = "UN_READ", code = 1, value = "world"), | ||
}) | ||
public class _EnableStatus { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package enums; | ||
|
||
import enumTool.CodeWithValue; | ||
import enumTool.EnumCodeValue; | ||
|
||
@EnumCodeValue({ | ||
@CodeWithValue(name = "INIT", code = 1, value = "默认"), | ||
@CodeWithValue(name = "SUCCESS", code = 2, value = "成功"), | ||
@CodeWithValue(name = "FAILURE", code = 3, value = "失败"), | ||
@CodeWithValue(name = "REFUND", code = 4, value = "退款"), | ||
}) | ||
public class _PayStatus { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package enums; | ||
|
||
import enumTool.CodeWithValue; | ||
import enumTool.EnumCodeValue; | ||
|
||
@EnumCodeValue({ | ||
@CodeWithValue(name = "READ", code = 1, value = "hello"), | ||
@CodeWithValue(name = "UN_READ", code = 1, value = "hello"), | ||
}) | ||
public class _ReadStatus { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package test; | ||
|
||
import enums.ReadStatus; | ||
|
||
public class Test { | ||
@org.junit.Test | ||
public void name() { | ||
System.out.println(ReadStatus.READ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package test; | ||
|
||
import enums.PayStatus; | ||
|
||
public class Test2 { | ||
public static void main(String[] args) { | ||
System.out.println(PayStatus.FAILURE.getValue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http:https://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>daydayup</artifactId> | ||
<groupId>com.github.landyking</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>learn-apt</artifactId> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.5.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
<encoding>UTF-8</encoding> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<!--<dependency> | ||
<groupId>com.squareup</groupId> | ||
<artifactId>javapoet</artifactId> | ||
<version>1.11.1</version> | ||
</dependency>--> | ||
<dependency> | ||
<groupId>com.google.auto.service</groupId> | ||
<artifactId>auto-service</artifactId> | ||
<version>1.0-rc5</version> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package enumTool; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.CLASS) | ||
@Target({}) | ||
public @interface CodeWithValue { | ||
String name(); | ||
|
||
int code(); | ||
|
||
String value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package enumTool; | ||
|
||
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 EnumCodeValue { | ||
CodeWithValue[] value() default {}; | ||
} |
113 changes: 113 additions & 0 deletions
113
learn-apt/src/main/java/enumTool/EnumCodeValueProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package enumTool; | ||
|
||
|
||
import javax.annotation.processing.AbstractProcessor; | ||
import javax.annotation.processing.ProcessingEnvironment; | ||
import javax.annotation.processing.Processor; | ||
import javax.annotation.processing.RoundEnvironment; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.util.Elements; | ||
import javax.tools.JavaFileObject; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import com.google.auto.service.AutoService; | ||
|
||
@AutoService(Processor.class) | ||
public class EnumCodeValueProcessor extends AbstractProcessor { | ||
private Elements elementUtils; | ||
|
||
@Override | ||
public Set<String> getSupportedAnnotationTypes() { | ||
return Collections.singleton(EnumCodeValue.class.getCanonicalName()); | ||
} | ||
|
||
@Override | ||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | ||
if (!annotations.isEmpty()) { | ||
Set<? extends Element> elems = roundEnv.getElementsAnnotatedWith(EnumCodeValue.class); | ||
for (Element elem : elems) { | ||
EnumCodeValue at = elem.getAnnotation(EnumCodeValue.class); | ||
CodeWithValue[] value = at.value(); | ||
TypeElement te = (TypeElement) elem; | ||
String qualifiedName = te.getQualifiedName().toString(); | ||
String simpleName = te.getSimpleName().toString(); | ||
JavaFileObject builderFile = null; | ||
String clazz = simpleName.substring(1); | ||
String fullName; | ||
String pkg = ""; | ||
if (!qualifiedName.equals(simpleName)) { | ||
pkg = qualifiedName.substring(0, qualifiedName.lastIndexOf(simpleName) - 1); | ||
fullName=pkg+"."+clazz; | ||
}else{ | ||
fullName=clazz; | ||
} | ||
try { | ||
builderFile = processingEnv.getFiler() | ||
.createSourceFile(fullName); | ||
try (PrintWriter out = new PrintWriter(builderFile.openWriter())) { | ||
out.write(buildOutput(pkg, clazz, value)); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public String buildOutput(String pkg, String clazz, CodeWithValue[] vals) { | ||
StringBuilder sb = new StringBuilder(); | ||
if (pkg.trim().length() > 0) { | ||
sb.append("package ").append(pkg).append(";\n"); | ||
} | ||
sb.append("public enum ").append(clazz).append("{\n"); | ||
for (CodeWithValue c : vals) { | ||
sb.append("\t").append(c.name()).append("(").append(c.code()).append(",\"").append(c.value()).append("\"),\n"); | ||
} | ||
sb.append("\t;\n"); | ||
sb.append("\tprivate Integer code;\n"); | ||
sb.append("\tprivate String value;\n"); | ||
sb.append("\t").append(clazz).append("(Integer code,String value){\n"); | ||
sb.append("\t\tthis.code=code;\n"); | ||
sb.append("\t\tthis.value=value;\n"); | ||
sb.append("\t}"); | ||
sb.append("\tpublic String getValue() {\n" + | ||
"\t\treturn value;\n" + | ||
"\t}\n"); | ||
sb.append("\tpublic Integer getCode() {\n" + | ||
"\t\treturn code;\n" + | ||
"\t}\n"); | ||
sb.append("\tpublic static " + clazz + " getEnumFromCode(Integer code) {\n" + | ||
"\t\tfor (" + clazz + " e : " + clazz + ".values()) {\n" + | ||
"\t\t\tif (e.getCode().equals(code)) {\n" + | ||
"\t\t\t\treturn e;\n" + | ||
"\t\t\t}\n" + | ||
"\t\t}\n" + | ||
"\t\treturn null;\n" + | ||
"\t}"); | ||
sb.append("}"); | ||
return sb.toString(); | ||
} | ||
|
||
private String getPackageName(TypeElement type) { | ||
return elementUtils.getPackageOf(type).getQualifiedName().toString(); | ||
} | ||
|
||
@Override | ||
public synchronized void init(ProcessingEnvironment processingEnv) { | ||
super.init(processingEnv); | ||
elementUtils = processingEnv.getElementUtils(); | ||
} | ||
|
||
@Override | ||
public SourceVersion getSupportedSourceVersion() { | ||
return SourceVersion.RELEASE_8; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.