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
1dc8c0da
Commit
1dc8c0da
authored
Oct 28, 2024
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
身份证识别
parent
608d2b96
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
143 additions
and
10 deletions
+143
-10
ObjectHelper.java
...src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
+2
-2
TestJsonHelper.java
yzg-util-base/src/test/java/helper/TestJsonHelper.java
+14
-3
MapRowCache.java
yzg-util-base/src/test/java/helper/vo/MapRowCache.java
+107
-0
MapSub.java
yzg-util-base/src/test/java/helper/vo/MapSub.java
+20
-5
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
View file @
1dc8c0da
...
@@ -281,7 +281,7 @@ public class ObjectHelper {
...
@@ -281,7 +281,7 @@ public class ObjectHelper {
* @param fromName 来源名称
* @param fromName 来源名称
* @return 名称结果
* @return 名称结果
*/
*/
p
rivate
static
String
get
SimpleName
(
String
fromName
)
{
p
ublic
static
String
getMethod
SimpleName
(
String
fromName
)
{
String
toName
=
getSimpleFieldName
(
fromName
);
String
toName
=
getSimpleFieldName
(
fromName
);
if
(
toName
.
startsWith
(
METHOD_IS
))
{
if
(
toName
.
startsWith
(
METHOD_IS
))
{
toName
=
toName
.
substring
(
METHOD_IS
.
length
());
toName
=
toName
.
substring
(
METHOD_IS
.
length
());
...
@@ -361,7 +361,7 @@ public class ObjectHelper {
...
@@ -361,7 +361,7 @@ public class ObjectHelper {
}
}
String
methodNameSource
=
method
.
getName
();
String
methodNameSource
=
method
.
getName
();
String
methodNameSimple
=
methodNameSource
.
toLowerCase
();
String
methodNameSimple
=
methodNameSource
.
toLowerCase
();
String
toName
=
getSimpleName
(
methodNameSource
);
String
toName
=
get
Method
SimpleName
(
methodNameSource
);
if
(
"getClass"
.
equals
(
methodNameSource
))
{
if
(
"getClass"
.
equals
(
methodNameSource
))
{
continue
;
continue
;
}
}
...
...
yzg-util-base/src/test/java/helper/TestJsonHelper.java
View file @
1dc8c0da
...
@@ -2,7 +2,6 @@ package helper;
...
@@ -2,7 +2,6 @@ package helper;
import
com.alibaba.fastjson.TypeReference
;
import
com.alibaba.fastjson.TypeReference
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.vo.MapRow
;
import
helper.vo.MapSub
;
import
helper.vo.MapSub
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.Test
;
...
@@ -48,14 +47,26 @@ public class TestJsonHelper {
...
@@ -48,14 +47,26 @@ public class TestJsonHelper {
@Test
@Test
public
void
testMapRow
()
{
public
void
testMapRow
()
{
String
key
=
"age"
;
String
key
=
"age"
;
String
name
=
"颜佐光"
;
MapSub
from
=
new
MapSub
();
MapSub
from
=
new
MapSub
();
from
.
setName
(
"颜佐光"
);
MapSub
from1
=
new
MapSub
();
from
.
put
(
"age"
,
15
);
from
.
setName
(
name
);
from
.
setName
(
name
);
from1
.
setName
(
name
);
from1
.
setName
(
name
);
from
.
setYear
(
15
);
from1
.
setYear
(
15
);
from
.
put
(
"age"
,
15
);
String
json
=
JsonHelper
.
serialize
(
from
);
String
json
=
JsonHelper
.
serialize
(
from
);
MapSub
to
=
JsonHelper
.
deserialize
(
json
,
new
TypeReference
<
MapSub
>()
{
MapSub
to
=
JsonHelper
.
deserialize
(
json
,
new
TypeReference
<
MapSub
>()
{
});
});
Assert
.
assertEquals
(
from
.
getName
(),
to
.
getName
());
Assert
.
assertEquals
(
from
.
getName
(),
to
.
getName
());
Assert
.
assertEquals
(
from
.
getYear
(),
to
.
getYear
());
Assert
.
assertEquals
(
from
.
get
(
key
),
to
.
get
(
key
));
Assert
.
assertEquals
(
from
.
get
(
key
),
to
.
get
(
key
));
Assert
.
assertEquals
(
from
.
getName
(),
name
);
Assert
.
assertEquals
(
from
,
to
);
Assert
.
assertEquals
(
from
,
to
);
}
}
}
}
...
...
yzg-util-base/src/test/java/helper/vo/MapRowCache.java
0 → 100644
View file @
1dc8c0da
package
helper
.
vo
;
import
com.alibaba.fastjson.TypeReference
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.vo.MapRow
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
public
class
MapRowCache
extends
MapRow
{
private
final
static
Map
<
String
,
TypeReference
<?>>
mapMethodReference
=
new
ConcurrentHashMap
<>();
private
final
static
Map
<
Class
<?>,
DataInfo
>
mapClassDataInfo
=
new
ConcurrentHashMap
<>();
public
MapRowCache
()
{
}
@Override
public
Object
get
(
Object
key
)
{
return
super
.
get
(
key
);
}
@Override
public
Object
put
(
String
key
,
Object
value
)
{
return
super
.
put
(
key
,
value
);
}
protected
<
T
>
T
getField
(
Get
<
T
>
getter
)
{
DataInfo
dataInfo
=
getDataInfo
(
getter
.
getClass
(),
3
);
System
.
out
.
println
(
dataInfo
.
getName
()
+
"()"
+
" called by "
+
dataInfo
.
getCls
().
getName
());
return
null
;
}
protected
<
T
>
void
putField
(
Set
<
T
>
setter
,
T
value
)
{
DataInfo
dataInfo
=
getDataInfo
(
setter
.
getClass
(),
3
);
System
.
out
.
println
(
dataInfo
.
getName
()
+
"()"
+
" called by "
+
dataInfo
.
getCls
().
getName
());
}
private
DataInfo
getDataInfo
(
Class
<?>
cls
,
int
pos
)
{
if
(!
mapClassDataInfo
.
containsKey
(
cls
))
{
StackTraceElement
[]
stackTraceElements
=
Thread
.
currentThread
().
getStackTrace
();
String
methodName
=
stackTraceElements
[
pos
].
getMethodName
();
String
fieldName
=
getMethodFieldName
(
methodName
);
String
fieldClsName
=
getMethodFieldClsName
(
this
.
getClass
(),
fieldName
);
if
(!
mapMethodReference
.
containsKey
(
fieldClsName
))
{
throw
new
IllegalArgumentException
(
"未注册类型:"
+
cls
.
getName
()
+
" 方法关联:"
+
methodName
);
}
DataInfo
dataInfo
=
new
DataInfo
(
methodName
,
cls
,
mapMethodReference
.
get
(
cls
));
mapClassDataInfo
.
put
(
cls
,
dataInfo
);
return
dataInfo
;
}
return
mapClassDataInfo
.
get
(
cls
);
}
private
static
String
getMethodFieldClsName
(
Class
<?>
cls
,
String
fieldName
)
{
return
cls
.
getName
()
+
"."
+
fieldName
;
}
protected
static
void
register
(
Class
<?>
cls
,
String
fieldName
,
TypeReference
<?>
typeReference
)
{
String
methodFieldClsName
=
getMethodFieldClsName
(
cls
,
fieldName
);
mapMethodReference
.
put
(
methodFieldClsName
,
typeReference
);
}
protected
interface
Get
<
T
>
{
T
get
();
}
protected
interface
Set
<
T
>
{
void
set
(
T
t
);
}
/**
* 字段关联的数据信息
*/
protected
static
class
DataInfo
{
private
final
String
name
;
private
final
Class
<?>
cls
;
private
final
TypeReference
<?>
typeReference
;
public
DataInfo
(
String
name
,
Class
<?>
cls
,
TypeReference
<?>
typeReference
)
{
this
.
name
=
name
;
this
.
cls
=
cls
;
this
.
typeReference
=
typeReference
;
}
public
String
getName
()
{
return
name
;
}
public
Class
<?>
getCls
()
{
return
cls
;
}
public
TypeReference
<?>
getTypeReference
()
{
return
typeReference
;
}
}
/**
* 获取方法字段名
*
* @param fullName 方法全名
* @return 方法字段名
*/
protected
String
getMethodFieldName
(
String
fullName
)
{
return
ObjectHelper
.
getMethodSimpleName
(
fullName
);
}
}
yzg-util-base/src/test/java/helper/vo/MapSub.java
View file @
1dc8c0da
package
helper
.
vo
;
package
helper
.
vo
;
import
com.
yanzuoguang.util.vo.MapRow
;
import
com.
alibaba.fastjson.TypeReference
;
public
class
MapSub
extends
MapRow
{
public
class
MapSub
extends
MapRow
Cache
{
private
String
name
;
static
{
TypeReference
<
String
>
typeStringReference
=
new
TypeReference
<
String
>()
{
};
TypeReference
<
Integer
>
typeIntegerReference
=
new
TypeReference
<
Integer
>()
{
};
register
(
MapSub
.
class
,
"name"
,
typeStringReference
);
register
(
MapSub
.
class
,
"year"
,
typeIntegerReference
);
}
public
String
getName
()
{
public
String
getName
()
{
return
name
;
return
this
.
getField
(
this
::
getName
)
;
}
}
public
void
setName
(
String
name
)
{
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
this
.
putField
(
this
::
setName
,
name
);
}
public
int
getYear
()
{
return
this
.
getField
(
this
::
getYear
);
}
public
void
setYear
(
int
age
)
{
this
.
putField
(
this
::
setYear
,
age
);
}
}
}
}
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