Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
a7a67dbc
Commit
a7a67dbc
authored
Apr 15, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复bug
parent
dfd49478
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
40 deletions
+32
-40
ObjectHelper.java
...src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
+32
-40
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
View file @
a7a67dbc
...
...
@@ -20,7 +20,7 @@ public class ObjectHelper {
/**
* 缓存的类型
*/
private
static
final
Map
<
Class
<?>,
HashMap
<
String
,
MethodField
>>
mapCache
=
new
HashMap
<>();
private
static
final
Map
<
Class
<?>,
HashMap
<
String
,
MethodField
>>
MAP_CACHE
=
new
HashMap
<>();
// --------------------------------------------- 类型判断 ---------------------------------------------------------
...
...
@@ -29,9 +29,9 @@ public class ObjectHelper {
*
* @param to 需要转换后的类型
* @param from 转换前的类型
* @return
* @return
是否可以转换
*/
public
static
boolean
isAutoConvert
(
Class
to
,
Class
from
)
{
public
static
boolean
isAutoConvert
(
Class
<?>
to
,
Class
<?>
from
)
{
return
from
!=
to
&&
(
Number
.
class
.
isAssignableFrom
(
to
)
||
to
.
isEnum
()
||
to
==
String
.
class
);
}
...
...
@@ -42,7 +42,7 @@ public class ObjectHelper {
* @param from 子类
* @return 是否满足条件
*/
public
static
boolean
isSub
(
Class
to
,
Class
from
)
{
public
static
boolean
isSub
(
Class
<?>
to
,
Class
<?>
from
)
{
return
from
==
to
||
from
.
isAssignableFrom
(
to
);
}
...
...
@@ -137,20 +137,17 @@ public class ObjectHelper {
* @return 获取之后的值
*/
public
static
Object
get
(
Object
target
,
String
field
)
{
Object
ret
=
null
;
if
(
target
==
null
)
{
return
ret
;
return
null
;
}
Class
vType
=
target
.
getClass
();
Class
<?>
vType
=
target
.
getClass
();
if
(
target
instanceof
Number
||
vType
.
isEnum
())
{
ret
=
null
;
ret
urn
null
;
}
else
if
(
target
instanceof
Map
)
{
ret
=
((
Map
)
target
).
get
(
field
);
ret
urn
((
Map
)
target
).
get
(
field
);
}
else
{
ret
=
getByType
(
target
.
getClass
(),
target
,
field
);
ret
urn
getByType
(
target
.
getClass
(),
target
,
field
);
}
return
ret
;
}
/**
...
...
@@ -190,7 +187,7 @@ public class ObjectHelper {
if
(
target
==
null
)
{
return
;
}
Class
vType
=
target
.
getClass
();
Class
<?>
vType
=
target
.
getClass
();
if
(
target
instanceof
Number
||
vType
.
isEnum
())
{
return
;
}
else
if
(
target
instanceof
Map
)
{
...
...
@@ -227,11 +224,11 @@ public class ObjectHelper {
public
static
void
setByType
(
Object
target
,
MethodField
item
,
Object
value
)
throws
IllegalAccessException
,
InvocationTargetException
{
if
(
item
.
getField
()
!=
null
&&
Modifier
.
isPublic
(
item
.
getField
().
getModifiers
()))
{
Class
toType
=
item
.
getField
().
getType
();
Class
<?>
toType
=
item
.
getField
().
getType
();
Object
toValue
=
StringHelper
.
to
(
toType
,
value
);
item
.
getField
().
set
(
target
,
toValue
);
}
else
if
(
item
.
getSetMethod
()
!=
null
&&
Modifier
.
isPublic
(
item
.
getSetMethod
().
getModifiers
()))
{
Class
toType
=
item
.
getSetMethod
().
getParameterTypes
()[
0
];
Class
<?>
toType
=
item
.
getSetMethod
().
getParameterTypes
()[
0
];
Object
toValue
=
StringHelper
.
to
(
toType
,
value
);
item
.
getSetMethod
().
invoke
(
target
,
toValue
);
}
...
...
@@ -246,11 +243,11 @@ public class ObjectHelper {
* @param <T> 复制的对象的类型
* @return 新的对象
*/
public
static
<
T
extends
Object
>
T
clone
(
T
from
)
{
public
static
<
T
>
T
clone
(
T
from
)
{
if
(
from
==
null
)
{
return
from
;
return
null
;
}
Class
cls
=
from
.
getClass
();
Class
<?>
cls
=
from
.
getClass
();
try
{
Object
to
=
cls
.
newInstance
();
writeWithFromClass
(
to
,
from
);
...
...
@@ -265,7 +262,7 @@ public class ObjectHelper {
* @param typeCache 类型
* @param fromName 来源名称
* @param toName 目标名称
* @return
* @return
返回找到的字段
*/
private
static
MethodField
getField
(
HashMap
<
String
,
MethodField
>
typeCache
,
String
fromName
,
String
toName
)
{
if
(!
typeCache
.
containsKey
(
toName
))
{
...
...
@@ -280,8 +277,8 @@ public class ObjectHelper {
/**
* 获取简单名称
*
* @param fromName
* @return
* @param fromName
来源名称
* @return
名称结果
*/
private
static
String
getSimpleName
(
String
fromName
)
{
String
toName
=
getSimpleFieldName
(
fromName
);
...
...
@@ -349,7 +346,8 @@ public class ObjectHelper {
String
toName
=
getSimpleName
(
methodNameSource
);
if
(
"getClass"
.
equals
(
methodNameSource
))
{
continue
;
}
else
if
(
methodNameSimple
.
startsWith
(
"set"
))
{
}
if
(
methodNameSimple
.
startsWith
(
"set"
))
{
if
(
method
.
getParameterTypes
().
length
!=
1
)
{
continue
;
}
...
...
@@ -358,17 +356,11 @@ public class ObjectHelper {
obj
.
setSetMethod
(
method
);
}
}
else
if
(
methodNameSimple
.
startsWith
(
"get"
))
{
if
(
method
.
getReturnType
()
==
null
)
{
continue
;
}
MethodField
obj
=
getField
(
typeCache
,
methodNameSource
,
toName
);
if
(
obj
.
getGetMethod
()
==
null
)
{
obj
.
setGetMethod
(
method
);
}
}
else
if
(
methodNameSimple
.
startsWith
(
"is"
))
{
if
(
method
.
getReturnType
()
==
null
)
{
continue
;
}
MethodField
obj
=
getField
(
typeCache
,
methodNameSource
,
toName
);
if
(
obj
.
getGetMethod
()
==
null
)
{
obj
.
setGetMethod
(
method
);
...
...
@@ -386,11 +378,11 @@ public class ObjectHelper {
*/
public
static
HashMap
<
String
,
MethodField
>
getTypeField
(
Class
<?>
type
)
{
HashMap
<
String
,
MethodField
>
typeCache
;
if
(
mapCache
.
containsKey
(
type
))
{
typeCache
=
mapCache
.
get
(
type
);
if
(
MAP_CACHE
.
containsKey
(
type
))
{
typeCache
=
MAP_CACHE
.
get
(
type
);
}
else
{
typeCache
=
getInitTypeField
(
type
);
mapCache
.
put
(
type
,
typeCache
);
MAP_CACHE
.
put
(
type
,
typeCache
);
}
return
typeCache
;
}
...
...
@@ -398,14 +390,14 @@ public class ObjectHelper {
/**
* 获取字段的名称
*
* @param type
* @param name
* @return
* @param type
类型
* @param name
名称
* @return
名称
*/
private
static
MethodField
getMethodField
(
Class
<?>
type
,
String
name
)
{
HashMap
<
String
,
MethodField
>
typeCache
=
getTypeField
(
type
);
name
=
getSimpleFieldName
(
name
);
return
typeCache
.
containsKey
(
name
)
?
typeCache
.
get
(
name
)
:
null
;
return
typeCache
.
getOrDefault
(
name
,
null
)
;
}
...
...
@@ -544,7 +536,7 @@ public class ObjectHelper {
public
static
Object
writeWithFromMap
(
boolean
emptyWrite
,
Object
to
,
Map
from
)
{
for
(
Object
key
:
from
.
keySet
())
{
Object
fromValue
=
from
.
get
(
key
);
if
(
StringHelper
.
isEmpty
(
fromValue
))
{
if
(
StringHelper
.
isEmpty
(
fromValue
)
&&
!
emptyWrite
)
{
continue
;
}
ObjectHelper
.
set
(
to
,
StringHelper
.
toString
(
key
),
fromValue
);
...
...
@@ -595,7 +587,7 @@ public class ObjectHelper {
for
(
Map
.
Entry
<
String
,
MethodField
>
field
:
mapField
.
entrySet
())
{
String
name
=
field
.
getValue
().
getName
();
Object
fromValue
=
ObjectHelper
.
get
(
from
,
name
);
if
(
StringHelper
.
isEmpty
(
fromValue
))
{
if
(
StringHelper
.
isEmpty
(
fromValue
)
&&
!
emptyWrite
)
{
continue
;
}
ObjectHelper
.
set
(
to
,
name
,
fromValue
);
...
...
@@ -612,7 +604,7 @@ public class ObjectHelper {
* @return 转换后的结果
*/
public
static
<
T
>
ArrayList
<
T
>
getList
(
Class
<
T
>
cls
,
Object
froms
)
{
ArrayList
<
T
>
tos
=
new
ArrayList
<
T
>();
ArrayList
<
T
>
tos
=
new
ArrayList
<>();
if
(
froms
instanceof
List
)
{
List
vCodeFrom
=
(
List
)
froms
;
for
(
Object
from
:
vCodeFrom
)
{
...
...
@@ -638,7 +630,7 @@ public class ObjectHelper {
* @param froms 需要转换的数组
* @return 数组类型
*/
public
static
<
T
extends
Object
>
T
[]
getArray
(
T
...
froms
)
{
public
static
<
T
>
T
[]
getArray
(
T
...
froms
)
{
return
froms
;
}
...
...
@@ -650,7 +642,7 @@ public class ObjectHelper {
* @param <T> 泛型
* @return 数组长度
*/
public
static
<
T
extends
Object
>
T
[]
createArray
(
Class
<?>
cls
,
int
length
)
{
public
static
<
T
>
T
[]
createArray
(
Class
<?>
cls
,
int
length
)
{
Object
array
=
Array
.
newInstance
(
cls
,
length
);
return
(
T
[])
array
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment