Skip to content

Commit

Permalink
feat: support non http annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
tuya-qiufeng committed Aug 21, 2023
1 parent ddc6c2b commit 50debcd
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion connector-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>connector</artifactId>
<groupId>com.tuya</groupId>
<version>1.3.1</version>
<version>1.3.2</version>
</parent>

<artifactId>connector-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ private Class<?> deriveFrom(Class<?> connectorInterface) {

service.addMethod(serviceMethod);
}

return service.toClass(configuration.getClass().getClassLoader(), null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.tuya.connector.api.utils.Utils;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.AttributeInfo;
import javassist.bytecode.ConstPool;
import lombok.SneakyThrows;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -45,7 +48,9 @@ public class ApiAnnotationHandlerDispatcher {
public static Object dispatchAndHandle(Annotation annotation, CtClass ctClass, CtMethod ctMethod) {
try {
BaseAnnotationHandler handler = KNOWN_HANDLERS.get(annotation.annotationType());
Objects.requireNonNull(handler, String.format("Handler is null for annotation: %s", annotation.getClass().getName()));
if (Objects.isNull(handler)) {
return otherAnnotations(annotation, ctClass, ctMethod);
}
return handler.handle(annotation, ctClass, ctMethod);
} catch (Exception e) {
throw new ConnectorAnnotationException(
Expand All @@ -55,4 +60,26 @@ public static Object dispatchAndHandle(Annotation annotation, CtClass ctClass, C
);
}
}

/**
* 非 HTTP 相关直接返回原注解
* @param annotation
* @param ctClass
* @param ctMethod
* @return
*/
private static Object otherAnnotations(Annotation annotation, CtClass ctClass, CtMethod ctMethod) {
ConstPool constPool = ctClass.getClassFile().getConstPool();
AnnotationsAttribute methodAttribute;
AttributeInfo attribute = ctMethod.getMethodInfo().getAttribute(AnnotationsAttribute.visibleTag);
if (Objects.nonNull(attribute)) {
methodAttribute = (AnnotationsAttribute) attribute;
} else {
methodAttribute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
}
javassist.bytecode.annotation.Annotation ctAnnotation = new javassist.bytecode.annotation.Annotation(annotation.annotationType().getTypeName(), constPool);
methodAttribute.addAnnotation(ctAnnotation);
ctMethod.getMethodInfo().addAttribute(methodAttribute);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tuya.connector.api.annotations;

import lombok.NonNull;

import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -70,5 +72,6 @@ public interface AnnotationConnector extends AnnotationAbility{

@Override
@GET("/test/annotations/url-object")
@NonNull
List<ResultObject> urlPost();
}
2 changes: 1 addition & 1 deletion connector-messaging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>connector</artifactId>
<groupId>com.tuya</groupId>
<version>1.3.1</version>
<version>1.3.2</version>
</parent>

<artifactId>connector-messaging</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>connector-spring-boot</artifactId>
<groupId>com.tuya</groupId>
<version>1.3.1</version>
<version>1.3.2</version>
</parent>

<artifactId>connector-spring-boot-autoconfigure</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>connector-spring-boot</artifactId>
<groupId>com.tuya</groupId>
<version>1.3.1</version>
<version>1.3.2</version>
</parent>

<artifactId>connector-spring-boot-starter</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion connector-spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>connector</artifactId>
<groupId>com.tuya</groupId>
<version>1.3.1</version>
<version>1.3.2</version>
</parent>

<artifactId>connector-spring-boot</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion connector-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>connector</artifactId>
<groupId>com.tuya</groupId>
<version>1.3.1</version>
<version>1.3.2</version>
</parent>

<artifactId>connector-spring</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.tuya</groupId>
<artifactId>connector</artifactId>
<packaging>pom</packaging>
<version>1.3.1</version>
<version>1.3.2</version>

<modules>
<module>connector-api</module>
Expand Down

0 comments on commit 50debcd

Please sign in to comment.