Skip to content

Commit

Permalink
feature: update annotation processor
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqi committed Nov 16, 2024
1 parent 969aa2d commit f829e06
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package cn.sliew.carp.support.annotation.processor;

import cn.sliew.carp.support.annotation.processor.plugins.jackson.javapoet.JacksonJavapoetPlugin;
import cn.sliew.carp.support.annotation.processor.plugins.web.WebPlugin;
import com.google.auto.common.BasicAnnotationProcessor;
import com.google.auto.service.AutoService;
Expand Down Expand Up @@ -47,6 +48,7 @@ public class CarpAnnotationProcessor extends AbstractProcessor {
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
plugins.add(new WebPlugin());
plugins.add(new JacksonJavapoetPlugin());
plugins.forEach(plugin -> plugin.init(processingEnv));
}

Expand Down
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 {

}
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 {

}
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* limitations under the License.
*/

package cn.sliew.carp.support.annotation.processor.plugins.web;
package cn.sliew.carp.support.annotation.processor.plugins;

import java.util.function.Function;

record IndexedValue<T>(int index, T value) {
public record IndexedValue<T>(int index, T value) {
static <T> Function<T, IndexedValue<T>> indexed() {
return new Function<>() {
int index = 1;
Expand Down
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();
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@

import com.palantir.javapoet.*;

import javax.annotation.processing.Generated;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.*;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import java.util.List;
import java.util.Objects;

Expand Down
Loading

0 comments on commit f829e06

Please sign in to comment.