refactor: Generic fetch method modification

This commit is contained in:
chuntaojun 2020-03-27 18:48:00 +08:00
parent 57fc219f72
commit 3dd42fed27
5 changed files with 12 additions and 41 deletions

View File

@ -23,7 +23,7 @@ import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.consistency.LogFuture; import com.alibaba.nacos.consistency.LogFuture;
import com.alibaba.nacos.consistency.SerializeFactory; import com.alibaba.nacos.consistency.SerializeFactory;
import com.alibaba.nacos.consistency.Serializer; import com.alibaba.nacos.consistency.Serializer;
import com.alibaba.nacos.common.utils.ClassUtils; import com.alibaba.nacos.core.utils.ClassUtils;
import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.exception.NJdbcException; import com.alibaba.nacos.config.server.exception.NJdbcException;
import com.alibaba.nacos.config.server.service.DynamicDataSource; import com.alibaba.nacos.config.server.service.DynamicDataSource;

View File

@ -1,7 +1,8 @@
package com.alibaba.nacos.config.server.service; package com.alibaba.nacos.config.server.service;
import com.alibaba.nacos.common.utils.ClassUtils; import com.alibaba.nacos.core.utils.ClassUtils;
import com.alibaba.nacos.config.server.model.User; import com.alibaba.nacos.config.server.model.User;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
@ -10,7 +11,7 @@ public class RowMapperManagerTest {
@Test @Test
public void test_user_mapper() { public void test_user_mapper() {
RowMapper<User> mapper = new RowMapperManager.UserRowMapper(); RowMapper<User> mapper = new RowMapperManager.UserRowMapper();
System.out.println(ClassUtils.resolveGenericTypeByInterface(mapper.getClass())); Assert.assertEquals(ClassUtils.resolveGenericTypeByInterface(mapper.getClass()).getSimpleName(), User.class.getSimpleName());
} }
} }

View File

@ -16,7 +16,7 @@
package com.alibaba.nacos.core.cluster; package com.alibaba.nacos.core.cluster;
import com.alibaba.nacos.common.utils.ClassUtils; import com.alibaba.nacos.core.utils.ClassUtils;
import com.alibaba.nacos.consistency.Config; import com.alibaba.nacos.consistency.Config;
import com.alibaba.nacos.consistency.ConsistencyProtocol; import com.alibaba.nacos.consistency.ConsistencyProtocol;
import com.alibaba.nacos.consistency.LogProcessor; import com.alibaba.nacos.consistency.LogProcessor;

View File

@ -1,26 +0,0 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.core.controller;
import org.springframework.web.bind.annotation.RestController;
/**
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
@RestController
public class BaseController {
}

View File

@ -14,26 +14,22 @@
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.common.utils; package com.alibaba.nacos.core.utils;
import java.lang.reflect.ParameterizedType; import org.springframework.core.ResolvableType;
import java.lang.reflect.Type;
/** /**
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a> * @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/ */
public class ClassUtils { @SuppressWarnings("all")
public final class ClassUtils {
public static <T> Class<T> resolveGenericType(Class<?> declaredClass) { public static <T> Class<T> resolveGenericType(Class<?> declaredClass) {
ParameterizedType parameterizedType = (ParameterizedType) declaredClass.getGenericSuperclass(); return (Class<T>) ResolvableType.forClass(declaredClass).getSuperType().resolveGeneric(0);
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
return (Class<T>) actualTypeArguments[0];
} }
public static <T> Class<T> resolveGenericTypeByInterface(Class<?> cls) { public static <T> Class<T> resolveGenericTypeByInterface(Class<?> declaredClass) {
Type[] types = cls.getGenericInterfaces(); return (Class<T>) ResolvableType.forClass(declaredClass).getInterfaces()[0].resolveGeneric(0);
Type[] actualTypeArguments = ((ParameterizedType) types[0]).getActualTypeArguments();
return (Class<T>) actualTypeArguments[0];
} }
public static Class findClassByName(String className) { public static Class findClassByName(String className) {