/*
* 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.shardingsphere.infra.util.reflection;
import com.google.common.base.CaseFormat;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Optional;
/**
* Reflection utility class.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ReflectionUtils {
private static final String GETTER_PREFIX = "get";
/**
* Get field value.
*
* @param target target
* @param fieldName field name
* @param <T> type of field value
* @return field value
*/
public static <T> Optional<T> getFieldValue(final Object target, final String fieldName) {
return findField(fieldName, target.getClass()).map(optional -> getFieldValue(target, optional));
}
@SuppressWarnings("unchecked")
@SneakyThrows(IllegalAccessException.class)
private static <T> T getFieldValue(final Object target, final Field field) {
boolean accessible = field.isAccessible();
if (!accessible) {
field.setAccessible(true);
}
T result = (T) field.get(target);
if (!accessible) {
field.setAccessible(false);
}
return result;
}
private static Optional<Field> findField(final String fieldName, final Class<?> targetClass) {
Class<?> currentTargetClass = targetClass;
while (Object.class != currentTargetClass) {
try {
return Optional.of(currentTargetClass.getDeclaredField(fieldName));
} catch (final NoSuchFieldException ignored) {
currentTargetClass = currentTargetClass.getSuperclass();
}
}
return Optional.empty();
}
/**
* Get static field value.
*
* @param target target
* @param fieldName field name
* @param <T> type of field value
* @return field value
*/
@SuppressWarnings("unchecked")
@SneakyThrows(ReflectiveOperationException.class)
public static <T> T getStaticFieldValue(final Class<?> target, final String fieldName) {
Field field = target.getDeclaredField(fieldName);
boolean accessible = field.isAccessible();
if (!accessible) {
field.setAccessible(true);
}
T result = (T) field.get(target);
if (!accessible) {
field.setAccessible(false);
}
return result;
}
/**
* Set static field value.
*
* @param target target
* @param fieldName field name
* @param value value
*/
@SneakyThrows(ReflectiveOperationException.class)
public static void setStaticFieldValue(final Class<?> target, final String fieldName, final Object value) {
Field field = target.getDeclaredField(fieldName);
boolean accessible = field.isAccessible();
if (!accessible) {
field.setAccessible(true);
}
field.set(target, value);
if (!accessible) {
field.setAccessible(false);
}
}
/**
* Invoke method.
*
* @param method method
* @param target target
* @param args arguments
* @param <T> type of invoke result
* @return invoke result
*/
@SuppressWarnings("unchecked")
@SneakyThrows(ReflectiveOperationException.class)
public static <T> T invokeMethod(final Method method, final Object target, final Object... args) {
boolean accessible = method.isAccessible();
if (!accessible) {
method.setAccessible(true);
}
T result = (T) method.invoke(target, args);
if (!accessible) {
method.setAccessible(false);
}
return result;
}
/**
* Get field value by get method.
*
* @param target target
* @param fieldName field name
* @param <T> type of field value
* @return field value
*/
public static <T> Optional<T> getFieldValueByGetMethod(final Object target, final String fieldName) {
String getterName = GETTER_PREFIX + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, fieldName);
final Optional<Method> method = findMethod(target.getClass(), getterName);
if (method.isPresent()) {
T value = invokeMethod(method.get(), target);
return Optional.ofNullable(value);
} else {
return Optional.empty();
}
}
private static Optional<Method> findMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterTypes) {
try {
return Optional.of(clazz.getMethod(methodName, parameterTypes));
} catch (final NoSuchMethodException ex) {
Class<?> superclass = clazz.getSuperclass();
if (null != superclass && Object.class != superclass) {
return findMethod(superclass, methodName, parameterTypes);
}
}
return Optional.empty();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springboot3.2.3集成shardingsphere5.4.1及动态数据源demo项目
共42个文件
java:20个
xml:8个
yaml:2个
需积分: 0 23 下载量 55 浏览量
2024-04-12
18:35:06
上传
评论
收藏 103KB RAR 举报
温馨提示
springboot3.2.3+shardingsphere5.4.1+dynamic-datasource 1.解决了兼容问题 2.可以在多个动态数据源之前切换,包括CK及两个Mysql数据库
资源推荐
资源详情
资源评论
收起资源包目录
DynamicDatasourceDemo.rar (42个子文件)
DynamicDatasourceDemo
HELP.md 2KB
mvnw.cmd 7KB
pom.xml 4KB
src
test
java
com
zzz
test
DynamicDatasourceDemoApplicationTests.java 228B
main
resources
sharding.yaml 871B
application.yaml 619B
java
org
apache
shardingsphere
infra
util
close
QuietlyCloser.java 1KB
retry
RetryExecutor.java 2KB
eventbus
EventBusContext.java 2KB
reflection
ReflectionUtils.java 6KB
yaml
shortcuts
ShardingSphereYamlShortcuts.java 1KB
swapper
NewYamlConfigurationSwapper.java 2KB
YamlConfigurationSwapper.java 1KB
datanode
YamlDataNode.java 1KB
representer
processor
DefaultYamlTupleProcessor.java 2KB
ShardingSphereYamlTupleProcessor.java 1KB
ShardingSphereYamlRepresenter.java 4KB
YamlEngine.java 4KB
constructor
ShardingSphereYamlConstructor.java 3KB
ShardingSphereYamlConstruct.java 1KB
YamlConfiguration.java 949B
json
JsonUtils.java 3KB
com
zzz
test
DynamicDatasourceDemoApplication.java 347B
controller
TestController.java 614B
config
XmShardingConfig.java 2KB
.mvn
wrapper
maven-wrapper.properties 233B
maven-wrapper.jar 61KB
.idea
jarRepositories.xml 879B
easyCodeTableSetting.xml 12KB
qaplug_profiles.xml 84KB
workspace.xml 5KB
misc.xml 2KB
compiler.xml 739B
.gitignore 184B
sonarlint
issuestore
0
9
index.pb 57B
9
d
1
a
1a273f73b7a477b0745593641bd708a6182c7ad5 0B
4
4
7
b
securityhotspotstore
0
9
index.pb 57B
9
d
1
a
1a273f73b7a477b0745593641bd708a6182c7ad5 0B
4
4
7
b
encodings.xml 191B
$PROJECT_FILE$ 364B
mvnw 11KB
.gitignore 395B
共 42 条
- 1
资源评论
robotABC
- 粉丝: 21
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功