Skip to content

Commit

Permalink
[fix]
Browse files Browse the repository at this point in the history
  • Loading branch information
TimmyYu committed Oct 9, 2024
1 parent 861577b commit 30ed6d9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 35 deletions.
6 changes: 6 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,11 @@
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>5.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
5 changes: 1 addition & 4 deletions core/src/main/java/com/qq/tars/protocol/util/TarsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import com.qq.tars.protocol.tars.support.TarsMethodParameterInfo;
import com.qq.tars.protocol.tars.support.TarsStructInfo;
import com.qq.tars.protocol.tars.support.TarsStrutPropertyInfo;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
Expand All @@ -44,7 +42,6 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -488,7 +485,7 @@ public static TarsMethodInfo getMethodInfo(Class<?> api, Method method, String o
Type returnOriginType = method.getReturnType();

if (returnOriginType == CompletableFuture.class) {
ParameterizedTypeImpl parameterizedType = (ParameterizedTypeImpl) method.getGenericReturnType();
ParameterizedType parameterizedType = (ParameterizedType) method.getGenericReturnType();
TarsMethodParameterInfo returnInfo = new TarsMethodParameterInfo();
returnInfo.setStamp(TarsHelper.getParameterStamp(parameterizedType.getActualTypeArguments()[0]));//CompletableFuture use gengeric inner type class
returnInfo.setName("result");
Expand Down
38 changes: 25 additions & 13 deletions core/src/test/java/com/tencent/tars/TestForGenericType.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.tencent.tars;

import junit.framework.Assert;
import org.junit.Test;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import junit.framework.Assert;
import org.junit.Test;

public class TestForGenericType {
public static class TestsMethod {
Expand All @@ -22,15 +21,28 @@ public void testGenerictype() throws InvocationTargetException, IllegalAccessExc
CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
Method[] methods = TestsMethod.class.getMethods();
Method method = methods[0];
Type type = method.getGenericReturnType();
Type type2 = method.getReturnType();
Assert.assertEquals(type2, CompletableFuture.class);
ParameterizedTypeImpl parameterizedType = (ParameterizedTypeImpl) method.getGenericReturnType();
Assert.assertEquals(parameterizedType.getActualTypeArguments()[0].getTypeName(), "java.util.List<java.lang.Integer>");
System.out.println(parameterizedType.getActualTypeArguments()[0].getTypeName());
System.out.println("get inner type is " + parameterizedType.getActualTypeArguments()[0]);
System.out.println("return type is " + method.getGenericReturnType());
System.out.println(methods[0].invoke(TestsMethod.class.newInstance()));
Type returnType = method.getGenericReturnType();
Type rawReturnType = method.getReturnType();

// 确保返回类型是 CompletableFuture
Assert.assertEquals(rawReturnType, CompletableFuture.class);

// 处理泛型返回类型
if (returnType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) returnType;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
Assert.assertEquals(actualTypeArguments[0].getTypeName(), "java.util.List<java.lang.Integer>");

// 打印信息
System.out.println(actualTypeArguments[0].getTypeName());
System.out.println("get inner type is " + actualTypeArguments[0]);
System.out.println("return type is " + method.getGenericReturnType());
// 调用方法
System.out.println(method.invoke(TestsMethod.class.newInstance()));
} else {
System.out.println("Return type is not a ParameterizedType");
}

System.out.println(completableFuture.getClass().getName());
}
}
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>com.tencent.tars</groupId>
<artifactId>tars-parent</artifactId>
<version>${revision}</version>
<version>${revision}</version>
<packaging>pom</packaging>
<name>Tars</name>
<description>Super POM For Projects</description>
Expand Down Expand Up @@ -79,7 +79,6 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven_javadoc_plugin_version}</version>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.qq.tars.register.eureka;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
Expand Down Expand Up @@ -53,7 +51,6 @@ public TarsEurekaInstance(InetUtils inetUtils) {
super(inetUtils);
}

@PostConstruct
public void init() {
getMetadataMap().put("isTcp", String.valueOf(isTcp));
getMetadataMap().put("timeOut", String.valueOf(timeOut));
Expand Down
4 changes: 2 additions & 2 deletions tars-plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
<dependency>
<groupId>com.tencent.tars</groupId>
<artifactId>tars-client</artifactId>
<version>${revision}</version>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>com.tencent.tars</groupId>
<artifactId>tars-server</artifactId>
<version>${revision}</version>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
Expand Down
19 changes: 8 additions & 11 deletions tools/tars-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>${maven_version}</version>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${maven_version}</version>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
Expand All @@ -56,27 +56,27 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-parameter-documenter</artifactId>
<version>${maven_version}</version>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-repository-metadata</artifactId>
<version>${maven_version}</version>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-error-diagnostics</artifactId>
<version>${maven_version}</version>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-descriptor</artifactId>
<version>${maven_version}</version>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-monitor</artifactId>
<version>${maven_version}</version>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
Expand Down Expand Up @@ -160,7 +160,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>${maven_plugin_version}</version>
<version>3.15.0</version>
<configuration>
<goalPrefix>tars</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
Expand Down Expand Up @@ -198,9 +198,6 @@
<artifactId>
maven-plugin-plugin
</artifactId>
<versionRange>
[3.4,)
</versionRange>
<goals>
<goal>descriptor</goal>
</goals>
Expand Down

0 comments on commit 30ed6d9

Please sign in to comment.