diff --git a/carp-support/carp-annotation-processor/pom.xml b/carp-support/carp-annotation-processor/pom.xml new file mode 100644 index 0000000..30152ae --- /dev/null +++ b/carp-support/carp-annotation-processor/pom.xml @@ -0,0 +1,42 @@ + + + + + 4.0.0 + + cn.sliew + carp-support + 0.0.13-SNAPSHOT + ../pom.xml + + carp-annotation-processor + + + + com.google.auto.service + auto-service + + + com.palantir.javapoet + javapoet + + + + \ No newline at end of file diff --git a/carp-support/carp-annotation-processor/src/main/java/cn/sliew/carp/support/annotation/processor/CarpAnnotationProcessor.java b/carp-support/carp-annotation-processor/src/main/java/cn/sliew/carp/support/annotation/processor/CarpAnnotationProcessor.java new file mode 100644 index 0000000..02ff4e1 --- /dev/null +++ b/carp-support/carp-annotation-processor/src/main/java/cn/sliew/carp/support/annotation/processor/CarpAnnotationProcessor.java @@ -0,0 +1,76 @@ +/* + * 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; + +import com.google.auto.common.BasicAnnotationProcessor; +import com.google.auto.service.AutoService; +import com.google.common.collect.Lists; +import com.palantir.javapoet.JavaFile; + +import javax.annotation.processing.*; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.TypeElement; +import javax.tools.Diagnostic; +import java.io.IOException; +import java.util.Collection; +import java.util.List; +import java.util.Set; + +/** + * @see BasicAnnotationProcessor + */ +@SupportedAnnotationTypes({"cn.sliew.carp.support.annotation.processor.CarpProcessor"}) +@SupportedSourceVersion(SourceVersion.RELEASE_17) +@AutoService(Processor.class) +public class CarpAnnotationProcessor extends AbstractProcessor { + + private List plugins = Lists.newArrayList(); + + @Override + public synchronized void init(ProcessingEnvironment processingEnv) { + super.init(processingEnv); + plugins.forEach(plugin -> plugin.init(processingEnv)); + } + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + try { + return processInternal(roundEnv); + } catch (Exception e) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Exception occurred %s".formatted(e)); + } + return false; + } + + protected boolean processInternal(RoundEnvironment roundEnv) { + plugins.stream().map(plugin -> plugin.process(roundEnv.getElementsAnnotatedWith(plugin.supported()))) + .flatMap(Collection::stream) + .forEach(this::writeFile); + return true; + } + + private void writeFile(JavaFile javaFile) { + try { + javaFile.writeTo(processingEnv.getFiler()); + } catch (IOException e) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write definition %s".formatted(javaFile)); + } + } + +} diff --git a/carp-support/carp-annotation-processor/src/main/java/cn/sliew/carp/support/annotation/processor/CarpProcessor.java b/carp-support/carp-annotation-processor/src/main/java/cn/sliew/carp/support/annotation/processor/CarpProcessor.java new file mode 100644 index 0000000..d7721d1 --- /dev/null +++ b/carp-support/carp-annotation-processor/src/main/java/cn/sliew/carp/support/annotation/processor/CarpProcessor.java @@ -0,0 +1,31 @@ +/* + * 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; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) +@Retention(RetentionPolicy.SOURCE) +public @interface CarpProcessor { + + Class[] value(); +} diff --git a/carp-support/carp-annotation-processor/src/main/java/cn/sliew/carp/support/annotation/processor/CarpProcessorPlugin.java b/carp-support/carp-annotation-processor/src/main/java/cn/sliew/carp/support/annotation/processor/CarpProcessorPlugin.java new file mode 100644 index 0000000..ff342d5 --- /dev/null +++ b/carp-support/carp-annotation-processor/src/main/java/cn/sliew/carp/support/annotation/processor/CarpProcessorPlugin.java @@ -0,0 +1,41 @@ +/* + * 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; + +import com.google.auto.common.BasicAnnotationProcessor; +import com.palantir.javapoet.JavaFile; + +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.Element; +import java.lang.annotation.Annotation; +import java.util.Collection; +import java.util.Set; + +/** + * @see BasicAnnotationProcessor.Step + */ +public interface CarpProcessorPlugin { + + void init(ProcessingEnvironment processingEnv); + + Class supported(); + + Collection process(Set annotated); + +} diff --git a/carp-support/pom.xml b/carp-support/pom.xml index 8f8faf7..0bb1559 100644 --- a/carp-support/pom.xml +++ b/carp-support/pom.xml @@ -30,6 +30,7 @@ pom + carp-annotation-processor carp-generator diff --git a/pom.xml b/pom.xml index 4a81d9a..ec263c0 100644 --- a/pom.xml +++ b/pom.xml @@ -133,6 +133,7 @@ 3.3.2 0.0.9 9.7 + 0.5.0 @@ -653,6 +654,11 @@ asm ${asm.version} + + com.palantir.javapoet + javapoet + ${javapoet.version} +