-
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.
feature: update annotation processor
- Loading branch information
wangqi
committed
Nov 16, 2024
1 parent
969aa2d
commit f829e06
Showing
10 changed files
with
283 additions
and
62 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
26 changes: 26 additions & 0 deletions
26
.../java/cn/sliew/carp/support/annotation/processor/plugins/AbstractClassGeneratePlugin.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,26 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cn.sliew.carp.support.annotation.processor.plugins; | ||
|
||
/** | ||
* 生成一个新的类,实现特定接口 | ||
*/ | ||
public abstract class AbstractClassGeneratePlugin extends AbstractProcessorPlugin { | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...java/cn/sliew/carp/support/annotation/processor/plugins/AbstractMethodGeneratePlugin.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,26 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cn.sliew.carp.support.annotation.processor.plugins; | ||
|
||
/** | ||
* 在原类中生成一个新方法 | ||
*/ | ||
public abstract class AbstractMethodGeneratePlugin extends AbstractProcessorPlugin { | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
...main/java/cn/sliew/carp/support/annotation/processor/plugins/AbstractProcessorPlugin.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,116 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cn.sliew.carp.support.annotation.processor.plugins; | ||
|
||
import cn.sliew.carp.support.annotation.processor.CarpProcessorPlugin; | ||
import cn.sliew.carp.support.annotation.processor.util.ProcessingEnvUtils; | ||
import com.palantir.javapoet.JavaFile; | ||
import com.palantir.javapoet.TypeSpec; | ||
|
||
import javax.annotation.processing.ProcessingEnvironment; | ||
import javax.lang.model.element.AnnotationMirror; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.util.ElementFilter; | ||
import javax.tools.Diagnostic; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* 生成一个新的类,实现特定接口 | ||
*/ | ||
public abstract class AbstractProcessorPlugin implements CarpProcessorPlugin { | ||
|
||
protected ProcessingEnvironment processingEnv; | ||
|
||
@Override | ||
public void init(ProcessingEnvironment processingEnv) { | ||
this.processingEnv = processingEnv; | ||
} | ||
|
||
@Override | ||
public Collection<JavaFile> process(Set<? extends Element> annotated) { | ||
Map<ElementMethod, List<ExecutableElement>> nameDataListMap = ElementFilter | ||
.methodsIn(annotated) | ||
.stream() | ||
.collect(Collectors.groupingBy(ElementMethod::new)); | ||
|
||
return nameDataListMap | ||
.entrySet() | ||
.stream() | ||
.flatMap(entry -> handle(entry.getKey(), entry.getValue())) | ||
.toList(); | ||
} | ||
|
||
|
||
private Stream<JavaFile> handle(ElementMethod elementMethod, List<ExecutableElement> handlers) { | ||
return handlers.stream() | ||
.map(IndexedValue.indexed()) | ||
.map(element -> handle(elementMethod, element)); | ||
} | ||
|
||
private JavaFile handle(ElementMethod elementMethod, IndexedValue<ExecutableElement> indexedValue) { | ||
try { | ||
return getJavaFile(elementMethod, indexedValue); | ||
} catch (Exception e) { | ||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage(), indexedValue.value()); | ||
throw e; | ||
} | ||
} | ||
|
||
private JavaFile getJavaFile(ElementMethod elementMethod, IndexedValue<ExecutableElement> indexedValue) { | ||
var handlerMethod = indexedValue.value(); | ||
var typeSpec = createType(elementMethod, indexedValue, handlerMethod); | ||
String packageName = ProcessingEnvUtils.getPackageName(processingEnv, handlerMethod); | ||
return JavaFile.builder(packageName, typeSpec).build(); | ||
} | ||
|
||
protected abstract TypeSpec createType(ElementMethod elementMethod, IndexedValue<ExecutableElement> indexedValue, ExecutableElement handlerMethod); | ||
|
||
protected void log(String msg) { | ||
if (processingEnv.getOptions().containsKey("debug")) { | ||
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, msg); | ||
} | ||
} | ||
|
||
protected void error(String msg, Element element) { | ||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, element); | ||
} | ||
|
||
protected void error(String msg, Element element, AnnotationMirror annotation) { | ||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, element, annotation); | ||
} | ||
|
||
protected void fatalError(Exception e) { | ||
// We don't allow exceptions of any kind to propagate to the compiler | ||
StringWriter writer = new StringWriter(); | ||
e.printStackTrace(new PrintWriter(writer)); | ||
fatalError(writer.toString()); | ||
} | ||
|
||
protected void fatalError(String msg) { | ||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "FATAL ERROR: " + msg); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...essor/src/main/java/cn/sliew/carp/support/annotation/processor/plugins/ElementMethod.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,15 @@ | ||
package cn.sliew.carp.support.annotation.processor.plugins; | ||
|
||
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.element.Name; | ||
import javax.lang.model.element.TypeElement; | ||
|
||
public record ElementMethod(TypeElement typeElement, Name annotatedMethodName) { | ||
ElementMethod(ExecutableElement element) { | ||
this((TypeElement) element.getEnclosingElement(), element.getSimpleName()); | ||
} | ||
|
||
public String toMethodName(int index) { | ||
return "%s$%s$%d$Generated".formatted(typeElement.getSimpleName().toString(), annotatedMethodName.toString(), index); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
...c/main/java/cn/sliew/carp/support/annotation/processor/plugins/jackson/JacksonGetter.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,33 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cn.sliew.carp.support.annotation.processor.plugins.jackson; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.SOURCE) | ||
@Target(ElementType.METHOD) | ||
public @interface JacksonGetter { | ||
|
||
Class<?> mapping(); | ||
|
||
String attribute(); | ||
} |
56 changes: 56 additions & 0 deletions
56
...iew/carp/support/annotation/processor/plugins/jackson/javapoet/JacksonJavapoetPlugin.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,56 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cn.sliew.carp.support.annotation.processor.plugins.jackson.javapoet; | ||
|
||
import cn.sliew.carp.support.annotation.processor.plugins.AbstractMethodGeneratePlugin; | ||
import cn.sliew.carp.support.annotation.processor.plugins.IndexedValue; | ||
import cn.sliew.carp.support.annotation.processor.plugins.ElementMethod; | ||
import cn.sliew.carp.support.annotation.processor.plugins.jackson.JacksonGetter; | ||
import com.palantir.javapoet.JavaFile; | ||
import com.palantir.javapoet.TypeSpec; | ||
|
||
import javax.annotation.processing.ProcessingEnvironment; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.ExecutableElement; | ||
import java.lang.annotation.Annotation; | ||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
public class JacksonJavapoetPlugin extends AbstractMethodGeneratePlugin { | ||
|
||
@Override | ||
public void init(ProcessingEnvironment processingEnv) { | ||
super.init(processingEnv); | ||
} | ||
|
||
@Override | ||
public Class<? extends Annotation> supported() { | ||
return JacksonGetter.class; | ||
} | ||
|
||
@Override | ||
public Collection<JavaFile> process(Set<? extends Element> annotated) { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected TypeSpec createType(ElementMethod nameData, IndexedValue<ExecutableElement> indexedValue, ExecutableElement handlerMethod) { | ||
return null; | ||
} | ||
} |
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
Oops, something went wrong.