Skip to content

Commit

Permalink
Interceptor using aspect instead of reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
shedfreez committed Mar 12, 2024
1 parent d721d27 commit 580a8bd
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 325 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import java.util.concurrent.ScheduledExecutorService;

import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.common.util.OkHttpUtil;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.cloud.polaris.util.OkHttpUtil;
import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatProperties;
import com.tencent.polaris.api.config.global.StatReporterConfig;
import com.tencent.polaris.api.core.ProviderAPI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License.
*/

package com.tencent.cloud.polaris.util;
package com.tencent.cloud.common.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
package com.tencent.cloud.common.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.springframework.util.ClassUtils;

Expand Down Expand Up @@ -70,102 +68,4 @@ public static Object getFieldValue(Object instance, String fieldName) {
}
return null;
}

/**
* get property of class object by property name.
*
* @param target object
* @param fieldName property name of class object
* @return value
*/
public static Object getObjectByFieldName(Object target, String fieldName) {
try {
Field field = target.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(target);
}
catch (Exception e) {
throw new RuntimeException("getObjectByFieldName", e);
}
}

/**
* get property of parent class object by property name.
*
* @param target object
* @param fieldName property name of parent class object
* @return value
*/
public static Object getSuperObjectByFieldName(Object target, String fieldName) {
try {
Field field = target.getClass().getSuperclass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(target);
}
catch (Exception e) {
throw new RuntimeException("getSuperObjectByFieldName", e);
}
}


/**
* set property of class object by property name.
*
* @param target object
* @param fieldName property name of class object
* @param value new value
*/
public static void setValueByFieldName(Object target, String fieldName, Object value) {
try {
Field field = target.getClass().getDeclaredField(fieldName);
setValue(target, field, value);
}
catch (Exception e) {
throw new RuntimeException("setValueByFieldName", e);
}
}

/**
* set property of parent class object by property name.
*
* @param target object
* @param fieldName property name of parent class object
* @param value new value
*/
public static void setSuperValueByFieldName(Object target, String fieldName, Object value) {
try {
Field field = target.getClass().getSuperclass().getDeclaredField(fieldName);
setValue(target, field, value);
}
catch (Exception e) {
throw new RuntimeException("setSuperValueByFieldName", e);
}
}

private static void setValue(Object target, Field field, Object value) {
try {
Field modifiers = getModifiersField();
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.setAccessible(true);
field.set(target, value);
}
catch (Exception e) {
throw new RuntimeException("setValue", e);
}
}

private static Field getModifiersField() throws Exception {
Method getDeclaredFields0 = Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
getDeclaredFields0.setAccessible(true);
Field[] fields = (Field[]) getDeclaredFields0.invoke(Field.class, false);
Field modifierField = null;
for (Field f : fields) {
if ("modifiers".equals(f.getName())) {
modifierField = f;
break;
}
}
return modifierField;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License.
*/

package com.tencent.cloud.polaris.util;
package com.tencent.cloud.common.util;

import org.assertj.core.util.Maps;
import org.junit.jupiter.api.Test;
Expand All @@ -37,7 +37,13 @@
* @author Haotian Zhang
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = OkHttpUtilTest.TestApplication.class, properties = {"spring.application.name=test", "spring.cloud.polaris.discovery.register=false"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = OkHttpUtilTest.TestApplication.class,
properties = {
"spring.application.name=test",
"spring.cloud.polaris.discovery.register=false",
"spring.cloud.gateway.enabled=false"
})
public class OkHttpUtilTest {

@LocalServerPort
Expand All @@ -47,6 +53,7 @@ public class OkHttpUtilTest {
public void testGet() {
assertThat(OkHttpUtil.get("http://localhost:" + port + "/test", Maps.newHashMap("key", "value"))).isTrue();
assertThat(OkHttpUtil.checkUrl("localhost", port, "/test", Maps.newHashMap("key", "value"))).isTrue();
assertThat(OkHttpUtil.checkUrl("localhost", port, "test", Maps.newHashMap("key", "value"))).isTrue();
assertThat(OkHttpUtil.get("http://localhost:" + port + "/error", Maps.newHashMap("key", "value"))).isFalse();
assertThat(OkHttpUtil.get("http://localhost:55555/error", Maps.newHashMap("key", "value"))).isFalse();
}
Expand Down
6 changes: 6 additions & 0 deletions spring-cloud-tencent-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-lossless-plugin</artifactId>
<version>${revision}</version>
</dependency>

<!-- third part framework dependencies -->
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@
<artifactId>spring-cloud-tencent-commons</artifactId>
</dependency>

<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency>

<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>lossless-register</artifactId>
Expand Down Expand Up @@ -57,6 +52,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-test-mock-discovery</artifactId>
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 580a8bd

Please sign in to comment.