判断 java.lang.reflect.Field 的数据类型是否为数值类型

本文介绍了一种在Java中判断给定类型是否为数值类型的方法。通过检查输入类是否为Number类的子类或者基本数据类型来实现。该方法排除了一些非数值的基本类型如boolean和char。

what

见题思意

before

1. 定义 Set<Class<?>> 静态不可变常量
2. 把已知的数值类型 add
3. 获取 field.getType **数据**类型 tempClazz
4. 判断该 set 集合中是否存在 tempClazz
5. 。。。

now

/** 
 * @param targetClazz java.lang.reflect.Field 的数据类型 clazz。 getType 方法获取
 * @return
 */
boolean isNumberType(Class<?> targetClazz) {
    // 判断包装类
    if (Number.class.isAssignableFrom(targetClazz)) {
        return true;
    }
    // 判断原始类,过滤掉特殊的基本类型
    if (targetClazz == boolean.class || targetClazz == char.class || targetClazz == void.class) {
        return false;
    }
    return targetClazz.isPrimitive();
}
org.dbunit.dataset.DataSetException: org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block at org.dbunit.database.DatabaseDataSet.getTable(DatabaseDataSet.java:342) at com.github.springtestdbunit.assertion.NonStrictDatabaseAssertion.assertEquals(NonStrictDatabaseAssertion.java:47) at com.github.springtestdbunit.DbUnitRunner.verifyExpected(DbUnitRunner.java:154) at com.github.springtestdbunit.DbUnitRunner.verifyExpected(DbUnitRunner.java:120) at com.github.springtestdbunit.DbUnitRunner.afterTestMethod(DbUnitRunner.java:86) at com.github.springtestdbunit.DbUnitTestExecutionListener.afterTestMethod(DbUnitTestExecutionListener.java:190) at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:487) at org.springframework.test.context.junit.jupiter.SpringExtension.afterEach(SpringExtension.java:278) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) Caused by: org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2733) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2420) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:372) at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:517) at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:434) at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:356) at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:341) at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:317) at org.postgresql.jdbc.PgStatement.executeQuery(PgStatement.java:266) at java.base/java.lang.reflect.Method.invoke(Method.java:569) at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:123) at jdk.proxy2/jdk.proxy2.$Proxy217.executeQuery(Unknown Source) at org.dbunit.database.AbstractResultSetTable.<init>(AbstractResultSetTable.java:110) at org.dbunit.database.ForwardOnlyResultSetTable.<init>(ForwardOnlyResultSetTable.java:59) at org.dbunit.database.CachedResultSetTableFactory.createTable(CachedResultSetTableFactory.java:60) at org.dbunit.database.DatabaseDataSet.getTable(DatabaseDataSet.java:338) ... 9 more Caused by: org.postgresql.util.PSQLException: ERROR: numeric field overflow 詳細: A field with precision 10, scale 0 must round to an absolute value less than 10^10. at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2733) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2420) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:372) at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:517) at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:434) at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:194) at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:180) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:569) at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:123) at jdk.proxy2/jdk.proxy2.$Proxy218.execute(Unknown Source) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:569) at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:58) at jdk.proxy3/jdk.proxy3.$Proxy220.execute(Unknown Source) at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:48) at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:75) at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50) at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117) at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:569) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:425) at jdk.proxy2/jdk.proxy2.$Proxy189.update(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:288) at com.nec.jp.rpf.zwb.framework.datastoreaccess.FWSingleDao.update(FWSingleDao.java:52) at jp.co.sej.ssc.pu.spua3546.logic.Spua3546Logic.execute(Spua3546Logic.java:166) at jp.co.sej.ssc.pu.spua3546.Spua3546LogicTest.test_1_1_12(Spua3546LogicTest.java:94) at java.base/java.lang.reflect.Method.invoke(Method.java:569) ... 2 more
最新发布
10-15
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值