Skip to content

Commit

Permalink
Add Dubbo Annotation Processor (Add deprecated method warn when calle…
Browse files Browse the repository at this point in the history
…d by user, #10850) (#11513)
  • Loading branch information
win120a authored Mar 24, 2023
1 parent 414785c commit 0b6f2e4
Show file tree
Hide file tree
Showing 36 changed files with 2,228 additions and 7 deletions.
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ header:
- 'dubbo-native/src/main/java/org/apache/dubbo/aot/generate/BasicJsonWriter.java'
- 'dubbo-native/src/main/java/org/apache/dubbo/aot/generate/ExecutableMode.java'
- 'dubbo-native/src/main/java/org/apache/dubbo/aot/generate/MemberCategory.java'
- 'dubbo-annotation-processor/src/main/java/org/apache/dubbo/annotation/permit/**'

comment: on-failure

Expand Down
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright 2018-2023 The Apache Software Foundation
This product includes software developed at
The Apache Software Foundation (http://www.apache.org/).

This product contains code form the Netty Project:
This product contains code from the Netty Project:

The Netty Project
=================
Expand Down
165 changes: 165 additions & 0 deletions dubbo-annotation-processor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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.
-->

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

<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-annotation-processor</artifactId>
<version>1.0.0</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- System dependencies may cause problems in checkstyle. -->
<checkstyle.skip>true</checkstyle.skip>
<checkstyle_unix.skip>true</checkstyle_unix.skip>
<rat.skip>true</rat.skip>
<jacoco.skip>true</jacoco.skip>

<maven.deploy.skip>true</maven.deploy.skip>

<junit_jupiter_version>5.9.0</junit_jupiter_version>
<javassist_version>3.29.2-GA</javassist_version>
</properties>

<packaging>jar</packaging>

<dependencies>
<!-- JUnit Jupiter Engine to depend on the JUnit5 engine and JUnit 5 API -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit_jupiter_version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit_jupiter_version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit_jupiter_version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist_version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<proc>none</proc>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>default-profile-in-jdk-8</id>
<activation>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
<jdk>(, 9)</jdk>
</activation>
<properties>
<toolsjar>${java.home}/../lib/tools.jar</toolsjar>
</properties>

<dependencies>
<dependency>
<groupId>javac</groupId>
<artifactId>javac</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${toolsjar}</systemPath>
</dependency>
</dependencies>
</profile>

<profile>
<id>legacy-apple-jdk-profile</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../Classes/classes.jar</exists>
</file>
<jdk>(, 9)</jdk>
</activation>

<properties>
<toolsjar>${java.home}/../Classes/classes.jar</toolsjar>
</properties>

<dependencies>
<dependency>
<groupId>javac</groupId>
<artifactId>javac</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${toolsjar}</systemPath>
</dependency>
</dependencies>
</profile>

<profile>
<id>add-export-greater-than-9</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>[9, )</jdk>
</activation>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<fork>true</fork>
<proc>none</proc>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 org.apache.dubbo.annotation;

import javax.lang.model.element.Element;
import java.lang.annotation.Annotation;
import java.util.Set;

/**
* Represents an annotation processing handler, which is invoked by DispatchingAnnotationProcessor.
*/
public interface AnnotationProcessingHandler {

/**
* Set the annotations that this handler will handle.
*
* @return annotations to handle
*/
Set<Class<? extends Annotation>> getAnnotationsToHandle();

/**
* Formal processing method.
*
* @param elements the elements that annotated by the annotations of getAnnotationsToHandle()
* @param annotationProcessorContext the annotation processor context object (Javac object encapsulation)
*/
void process(Set<Element> elements,
AnnotationProcessorContext annotationProcessorContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* 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 org.apache.dubbo.annotation;

import com.sun.source.util.Trees;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Names;

import javax.annotation.processing.ProcessingEnvironment;
import java.lang.reflect.Method;
import java.util.Objects;

/**
* The Context Object of Annotation Processor, which stores objects related to javac.
*/
public class AnnotationProcessorContext {
private JavacProcessingEnvironment javacProcessingEnvironment;
private JavacTrees javacTrees;
private TreeMaker treeMaker;
private Names names;
private Context javacContext;
private Trees trees;

private AnnotationProcessorContext() { }

private static <T> T jbUnwrap(Class<? extends T> iface, T wrapper) {
T unwrapped = null;
try {
final Class<?> apiWrappers = wrapper.getClass().getClassLoader().loadClass("org.jetbrains.jps.javac.APIWrappers");
final Method unwrapMethod = apiWrappers.getDeclaredMethod("unwrap", Class.class, Object.class);
unwrapped = iface.cast(unwrapMethod.invoke(null, iface, wrapper));
} catch (Throwable ignored) {
}

return unwrapped != null ? unwrapped : wrapper;
}

public static AnnotationProcessorContext fromProcessingEnvironment(ProcessingEnvironment processingEnv) {
AnnotationProcessorContext apContext = new AnnotationProcessorContext();

Object procEnvToUnwrap = processingEnv.getClass() == JavacProcessingEnvironment.class ?
processingEnv : jbUnwrap(JavacProcessingEnvironment.class, processingEnv);

JavacProcessingEnvironment jcProcessingEnvironment = (JavacProcessingEnvironment) procEnvToUnwrap;

Context context = jcProcessingEnvironment.getContext();

apContext.javacProcessingEnvironment = jcProcessingEnvironment;

apContext.javacContext = context;
apContext.javacTrees = JavacTrees.instance(jcProcessingEnvironment);
apContext.treeMaker = TreeMaker.instance(context);
apContext.names = Names.instance(context);

apContext.trees = Trees.instance(jcProcessingEnvironment);

return apContext;
}

// Auto-generated methods.

public JavacTrees getJavacTrees() {
return javacTrees;
}

public TreeMaker getTreeMaker() {
return treeMaker;
}

public Names getNames() {
return names;
}

public Context getJavacContext() {
return javacContext;
}

public Trees getTrees() {
return trees;
}

public JavacProcessingEnvironment getJavacProcessingEnvironment() {
return javacProcessingEnvironment;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

AnnotationProcessorContext context = (AnnotationProcessorContext) o;

if (!Objects.equals(javacTrees, context.javacTrees)) return false;
if (!Objects.equals(treeMaker, context.treeMaker)) return false;
if (!Objects.equals(names, context.names)) return false;
if (!Objects.equals(javacContext, context.javacContext))
return false;
return Objects.equals(trees, context.trees);
}

@Override
public int hashCode() {
int result = javacTrees != null ? javacTrees.hashCode() : 0;
result = 31 * result + (treeMaker != null ? treeMaker.hashCode() : 0);
result = 31 * result + (names != null ? names.hashCode() : 0);
result = 31 * result + (javacContext != null ? javacContext.hashCode() : 0);
result = 31 * result + (trees != null ? trees.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "AnnotationProcessorContext{" +
"javacTrees=" + javacTrees +
", treeMaker=" + treeMaker +
", names=" + names +
", javacContext=" + javacContext +
", trees=" + trees +
'}';
}
}
Loading

0 comments on commit 0b6f2e4

Please sign in to comment.