Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
p x
earlywarning
Commits
3169f154
Commit
3169f154
authored
Jul 31, 2025
by
p x
Browse files
优化解析内存使用
parent
4a1d6d44
Changes
2
Show whitespace changes
Inline
Side-by-side
src/main/java/com/cusc/adas/v2x/utils/Parse.java
View file @
3169f154
package
com.cusc.adas.v2x.utils
;
package
com.cusc.adas.v2x.utils
;
import
com.cusc.adas.v2x.vo.DynamicsClassMap
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.ParameterizedType
;
import
java.lang.reflect.ParameterizedType
;
import
java.lang.reflect.Type
;
import
java.lang.reflect.Type
;
...
@@ -14,13 +16,13 @@ import java.util.SortedMap;
...
@@ -14,13 +16,13 @@ import java.util.SortedMap;
import
java.util.TreeMap
;
import
java.util.TreeMap
;
import
java.util.UUID
;
import
java.util.UUID
;
import
com.cusc.adas.v2x.vo.DynamicsClassMap
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.Unpooled
;
import
io.netty.buffer.Unpooled
;
public
class
Parse
{
public
class
Parse
{
// private List<Field> fList = new ArrayList<>();
public
static
<
T
>
T
parse
(
ByteBuf
data
,
T
t
)
throws
InstantiationException
,
IllegalAccessException
,
NoSuchFieldException
,
SecurityException
,
ClassNotFoundException
{
public
static
<
T
>
T
parse
(
ByteBuf
data
,
T
t
)
throws
InstantiationException
,
IllegalAccessException
,
NoSuchFieldException
,
SecurityException
,
ClassNotFoundException
{
Field
[]
fields
=
t
.
getClass
().
getDeclaredFields
();
Field
[]
fields
=
t
.
getClass
().
getDeclaredFields
();
...
@@ -47,7 +49,7 @@ public class Parse {
...
@@ -47,7 +49,7 @@ public class Parse {
String
fieldName
=
field
.
getName
();
String
fieldName
=
field
.
getName
();
Order
order
=
field
.
getAnnotation
(
Order
.
class
);
Order
order
=
field
.
getAnnotation
(
Order
.
class
);
//
System.out.println(fieldName);
//
System.out.println(
"---------fieldName " +
fieldName);
int
num
=
order
.
value
();
int
num
=
order
.
value
();
//基础数据类型 且 不是数组
//基础数据类型 且 不是数组
...
@@ -188,6 +190,164 @@ public class Parse {
...
@@ -188,6 +190,164 @@ public class Parse {
return
t
;
return
t
;
}
}
/**
* @param t = 要解析的目标对象
* @param fList 按Order 从小到大排序好的属性
*/
public
static
<
T
>
T
parse
(
ByteBuf
data
,
T
t
,
List
<
Field
>
fList
)
{
try
{
for
(
Field
field
:
fList
)
{
field
.
setAccessible
(
true
);
if
(
field
.
isAnnotationPresent
(
Order
.
class
))
{
String
fieldName
=
field
.
getName
();
Order
order
=
field
.
getAnnotation
(
Order
.
class
);
// System.out.println("---------fieldName " + fieldName);
int
num
=
order
.
value
();
//基础数据类型 且 不是数组
if
(
field
.
getType
().
isPrimitive
()
&&
!
field
.
getType
().
isArray
())
{
// data.re
// field.set(fList, rstMap);
readBuf
(
t
,
field
,
data
);
}
else
if
(
field
.
getType
().
isArray
())
{
// 基础数据类型 且 是数组
readBuf
(
t
,
field
,
data
);
}
else
if
(
List
.
class
.
isAssignableFrom
(
field
.
getType
()))
{
// 集合类型
//读取属性的注解是否引用其他字段的长度
if
(
field
.
isAnnotationPresent
(
RefNumFlag
.
class
)
&&
!
field
.
isAnnotationPresent
(
DynamicsClassDef
.
class
))
{
RefNumFlag
refNumFlag
=
field
.
getAnnotation
(
RefNumFlag
.
class
);
String
refField
=
refNumFlag
.
value
();
Class
<?>
clazz1
=
t
.
getClass
();
Field
nameField
=
clazz1
.
getDeclaredField
(
refField
);
Type
genericType
=
field
.
getGenericType
();
nameField
.
setAccessible
(
true
);
if
(
genericType
instanceof
ParameterizedType
)
{
ParameterizedType
pt
=
(
ParameterizedType
)
genericType
;
Type
[]
actualTypes
=
pt
.
getActualTypeArguments
();
if
(
actualTypes
!=
null
&&
actualTypes
.
length
>
0
)
{
actualTypes
[
0
].
getClass
();
long
len
=
0
l
;
if
(
nameField
.
getType
()
==
short
.
class
)
{
short
val
=
(
short
)
nameField
.
get
(
t
);
len
=
(
long
)
val
;
}
else
if
(
nameField
.
getType
()
==
int
.
class
)
{
int
val
=
(
int
)
nameField
.
get
(
t
);
len
=
(
long
)
val
;
}
else
if
(
nameField
.
getType
()
==
long
.
class
)
{
len
=
(
long
)
nameField
.
get
(
t
);
}
//short len= (short) nameField.get(t);
if
(
len
>
0
)
{
List
list
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
String
classname
=
actualTypes
[
0
].
getTypeName
();
Object
obj
=
null
;
if
(
classname
.
contains
(
"Short"
))
{
obj
=
data
.
readUnsignedByte
();
}
else
if
(
classname
.
contains
(
"Integer"
))
{
obj
=
data
.
readUnsignedShort
();
}
else
if
(
classname
.
contains
(
"Long"
))
{
obj
=
data
.
readUnsignedInt
();
}
else
{
Class
<?>
cls
=
Class
.
forName
(
actualTypes
[
0
].
getTypeName
());
;
obj
=
cls
.
newInstance
();
parse
(
data
,
obj
);
}
list
.
add
(
obj
);
}
System
.
out
.
println
(
"actualTypes::"
+
actualTypes
[
0
].
getTypeName
());
field
.
set
(
t
,
list
);
}
}
}
}
else
if
(
field
.
isAnnotationPresent
(
RefNumFlag
.
class
)
&&
field
.
isAnnotationPresent
(
DynamicsClassDef
.
class
))
{
DynamicsClassDef
dynamicsClassDef
=
field
.
getAnnotation
(
DynamicsClassDef
.
class
);
String
classtype
=
dynamicsClassDef
.
classtype
();
DependencyDef
dependencyDef
=
field
.
getAnnotation
(
DependencyDef
.
class
);
String
fieldname
=
dependencyDef
.
value
();
Field
tField2
=
t
.
getClass
().
getDeclaredField
(
fieldname
);
tField2
.
setAccessible
(
true
);
Short
value
=
(
Short
)
tField2
.
get
(
t
);
RefNumFlag
refNumFlag
=
field
.
getAnnotation
(
RefNumFlag
.
class
);
String
refFieldName
=
refNumFlag
.
value
();
Field
tField
=
t
.
getClass
().
getDeclaredField
(
fieldname
);
tField
.
setAccessible
(
true
);
Field
tField1
=
t
.
getClass
().
getDeclaredField
(
refFieldName
);
tField1
.
setAccessible
(
true
);
Integer
len
=
(
Integer
)
tField1
.
get
(
t
);
if
(
len
>
0
)
{
Class
clazz1
=
DynamicsClassMap
.
get
(
classtype
,
value
);
Class
<?>
cls
=
Class
.
forName
(
clazz1
.
getName
());
List
<
Object
>
list
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
Object
obj
=
cls
.
newInstance
();
System
.
out
.
println
(
"clazz1 name::"
+
clazz1
.
getName
());
parse
(
data
,
obj
);
list
.
add
(
obj
);
}
field
.
set
(
t
,
list
);
}
}
}
else
if
(
field
.
getType
()
==
String
.
class
)
{
//类型为字符串
readBuf
(
t
,
field
,
data
);
}
else
{
//其他对象类型
if
(
data
.
readerIndex
()
==
data
.
maxCapacity
())
{
return
t
;
}
if
(
field
.
isAnnotationPresent
(
DynamicsClassDef
.
class
))
{
DynamicsClassDef
dynamicsClassDef
=
field
.
getAnnotation
(
DynamicsClassDef
.
class
);
String
classtype
=
dynamicsClassDef
.
classtype
();
DependencyDef
dependencyDef
=
field
.
getAnnotation
(
DependencyDef
.
class
);
String
fieldname
=
dependencyDef
.
value
();
Field
tField2
=
t
.
getClass
().
getDeclaredField
(
fieldname
);
tField2
.
setAccessible
(
true
);
Short
key
=
(
Short
)
tField2
.
get
(
t
);
Class
clazz1
=
DynamicsClassMap
.
get
(
classtype
,
key
);
Class
<?>
cls
=
Class
.
forName
(
clazz1
.
getName
());
Object
obj
=
cls
.
newInstance
();
parse
(
data
,
obj
);
field
.
set
(
t
,
obj
);
}
else
{
Class
<?>
cls
=
Class
.
forName
(
field
.
getType
().
getName
());
Object
obj
=
cls
.
newInstance
();
parse
(
data
,
obj
);
field
.
set
(
t
,
obj
);
}
}
// rstMap.put(num, field);
}
}
return
t
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
/**
/**
* 读取字节流
* 读取字节流
*
*
...
@@ -342,6 +502,7 @@ public class Parse {
...
@@ -342,6 +502,7 @@ public class Parse {
public
static
void
convoterBean
(
Object
source
,
Object
target
)
{
public
static
void
convoterBean
(
Object
source
,
Object
target
)
{
Field
[]
fields
=
source
.
getClass
().
getDeclaredFields
();
Field
[]
fields
=
source
.
getClass
().
getDeclaredFields
();
List
<
Field
>
fList
=
new
ArrayList
<>();
List
<
Field
>
fList
=
new
ArrayList
<>();
// List<Field> fList = Arrays.asList(fields);
for
(
Field
field
:
fields
)
{
for
(
Field
field
:
fields
)
{
if
(
field
.
isAnnotationPresent
(
Order
.
class
))
{
if
(
field
.
isAnnotationPresent
(
Order
.
class
))
{
fList
.
add
(
field
);
fList
.
add
(
field
);
...
@@ -353,8 +514,6 @@ public class Parse {
...
@@ -353,8 +514,6 @@ public class Parse {
return
f1
.
getAnnotation
(
Order
.
class
).
value
()
-
f2
.
getAnnotation
(
Order
.
class
).
value
();
return
f1
.
getAnnotation
(
Order
.
class
).
value
()
-
f2
.
getAnnotation
(
Order
.
class
).
value
();
}
}
});
});
Field
tField
=
null
;
Field
tField
=
null
;
try
{
try
{
// Field[] fields = source.getClass().getDeclaredFields();
// Field[] fields = source.getClass().getDeclaredFields();
...
@@ -367,6 +526,7 @@ public class Parse {
...
@@ -367,6 +526,7 @@ public class Parse {
if
(
o
==
null
)
{
if
(
o
==
null
)
{
continue
;
continue
;
}
}
System
.
out
.
println
(
"---------fieldName "
+
field
.
getName
());
OffsetDef
offsetDef
=
null
;
OffsetDef
offsetDef
=
null
;
if
(
field
.
isAnnotationPresent
(
OffsetDef
.
class
))
{
if
(
field
.
isAnnotationPresent
(
OffsetDef
.
class
))
{
...
@@ -524,6 +684,171 @@ public class Parse {
...
@@ -524,6 +684,171 @@ public class Parse {
}
}
}
}
/**
* 将source对象转成 target(Android对象)
*
* @param fList 对source 属性排序的属性列表
****/
public
static
void
convoterBean
(
Object
source
,
List
<
Field
>
fList
,
Object
target
)
{
Field
tField
=
null
;
try
{
for
(
Field
field
:
fList
)
{
field
.
setAccessible
(
true
);
Object
o
=
field
.
get
(
source
);
if
(
o
==
null
)
{
continue
;
}
// System.out.println("---------convoterBean fieldName " + field.getName());
OffsetDef
offsetDef
=
null
;
if
(
field
.
isAnnotationPresent
(
OffsetDef
.
class
))
{
offsetDef
=
field
.
getAnnotation
(
OffsetDef
.
class
);
}
//获取目标对象指定的属性
tField
=
target
.
getClass
().
getDeclaredField
(
field
.
getName
());
tField
.
setAccessible
(
true
);
if
(
field
.
isAnnotationPresent
(
Order
.
class
))
{
if
(
field
.
getType
().
isPrimitive
()
&&
!
field
.
getType
().
isArray
())
{
//如果属性添加了自定义注解 offset
Class
<?>
type
=
field
.
getType
();
if
(
offsetDef
!=
null
)
{
double
offset
=
offsetDef
.
value
();
int
type1
=
offsetDef
.
type
();
int
minValidLength
=
offsetDef
.
minValidLength
();
//startindex
if
(
type
==
byte
.
class
)
{
tField
.
set
(
target
,
o
);
}
else
if
(
type
==
short
.
class
)
{
if
(
minValidLength
>
0
)
{
int
len
=
String
.
valueOf
((
Short
)
o
).
length
();
if
(
len
<
minValidLength
)
{
int
subtract
=
minValidLength
-
len
;
double
f
=
(
Short
)
o
*
Math
.
pow
(
10
,
subtract
)
*
offset
;
tField
.
set
(
target
,
(
float
)
f
);
}
else
{
double
f
=
(
Short
)
o
*
offset
;
tField
.
set
(
target
,
(
float
)
f
);
}
}
else
{
double
f
=
(
Short
)
o
*
offset
;
tField
.
set
(
target
,
(
float
)
f
);
}
}
else
if
(
type
==
int
.
class
)
{
if
(
minValidLength
>
0
)
{
int
len
=
String
.
valueOf
((
Integer
)
o
).
length
();
if
(
len
<
minValidLength
)
{
int
subtract
=
minValidLength
-
len
;
double
f
=
(
Integer
)
o
*
Math
.
pow
(
10
,
subtract
)
*
offset
;
tField
.
set
(
target
,
(
float
)
f
);
}
else
{
double
f
=
(
Integer
)
o
*
offset
;
tField
.
set
(
target
,
(
float
)
f
);
}
}
else
{
double
f
=
(
Integer
)
o
*
offset
;
tField
.
set
(
target
,
(
float
)
f
);
}
}
else
if
(
type
==
long
.
class
)
{
if
(
minValidLength
>
0
)
{
int
len
=
String
.
valueOf
((
Long
)
o
).
length
();
if
(
len
<
minValidLength
)
{
int
subtract
=
minValidLength
-
len
;
double
f
=
(
Long
)
o
*
Math
.
pow
(
10
,
subtract
)
*
offset
;
tField
.
set
(
target
,
(
double
)
f
);
}
else
{
double
f
=
(
Long
)
o
*
offset
;
tField
.
set
(
target
,
(
double
)
f
);
}
}
else
{
double
d
=
(
Long
)
o
*
offset
;
tField
.
set
(
target
,
d
);
}
}
}
else
{
tField
.
set
(
target
,
o
);
}
}
else
if
(
field
.
getClass
().
isPrimitive
()
&&
field
.
getType
().
isArray
())
{
// 基础数据类型 且 是数组
}
else
if
(
List
.
class
.
isAssignableFrom
(
field
.
getType
()))
{
// 集合类型
//读取属性的注解是否引用其他字段的长度
if
(
field
.
isAnnotationPresent
(
RefNumFlag
.
class
)
&&
!
field
.
isAnnotationPresent
(
DynamicsClassDef
.
class
))
{
List
<?>
temp
=
(
List
<?>)
o
;
RefNumFlag
refNumFlag
=
field
.
getAnnotation
(
RefNumFlag
.
class
);
String
refField
=
refNumFlag
.
value
();
Class
<?>
clazz1
=
target
.
getClass
();
Field
nameField
=
clazz1
.
getDeclaredField
(
refField
);
Type
genericType
=
field
.
getGenericType
();
Type
targetType
=
tField
.
getGenericType
();
nameField
.
setAccessible
(
true
);
if
(
genericType
instanceof
ParameterizedType
)
{
List
<
Object
>
dsts
=
new
ArrayList
<>();
ParameterizedType
pt
=
(
ParameterizedType
)
genericType
;
Type
[]
actualTypes
=
pt
.
getActualTypeArguments
();
ParameterizedType
dspt
=
(
ParameterizedType
)
targetType
;
Type
[]
dstTypes
=
dspt
.
getActualTypeArguments
();
if
(
actualTypes
.
length
>
0
&&
actualTypes
!=
null
)
{
for
(
int
i
=
0
;
i
<
temp
.
size
();
i
++)
{
Object
src
=
temp
.
get
(
i
);
Object
dst
=
null
;
if
(
src
instanceof
Short
)
{
dst
=
(
Short
)
src
;
}
else
if
(
src
instanceof
Integer
)
{
dst
=
(
int
)
src
;
}
else
{
Class
<?>
cls
=
Class
.
forName
(
dstTypes
[
0
].
getTypeName
());
dst
=
cls
.
newInstance
();
convoterBean
(
src
,
dst
);
}
dsts
.
add
(
dst
);
}
tField
.
set
(
target
,
dsts
);
}
}
}
}
else
if
(
field
.
getType
()
==
String
.
class
)
{
//类型为字符串
// try {
// String s = (String)o;
// String s1 = new String(s.getBytes());
tField
.
set
(
target
,
o
);
// }catch(Exception e) {
// e.printStackTrace();
// }
}
else
{
//其他对象类型
if
(
field
.
isAnnotationPresent
(
DynamicsClassDef
.
class
))
{
DynamicsClassDef
dynamicsClassDef
=
field
.
getAnnotation
(
DynamicsClassDef
.
class
);
String
classtype
=
dynamicsClassDef
.
classtype
();
DependencyDef
dependencyDef
=
field
.
getAnnotation
(
DependencyDef
.
class
);
String
fieldname
=
dependencyDef
.
value
();
Field
tField2
=
target
.
getClass
().
getDeclaredField
(
fieldname
);
tField2
.
setAccessible
(
true
);
Short
key
=
(
Short
)
tField2
.
get
(
target
);
Class
<?>
cls
=
DynamicsClassMap
.
getDto
(
classtype
,
key
);
Object
obj
=
cls
.
newInstance
();
convoterBean
(
o
,
obj
);
tField
.
set
(
target
,
obj
);
}
else
{
Class
<?>
cls
=
Class
.
forName
(
tField
.
getType
().
getName
());
Object
obj
=
cls
.
newInstance
();
convoterBean
(
o
,
obj
);
tField
.
set
(
target
,
obj
);
}
}
}
}
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"----convoterBean Exception "
+
e
+
" tField "
+
tField
);
}
}
public
static
byte
[]
convoter
(
Byte
[]
bytes
)
{
public
static
byte
[]
convoter
(
Byte
[]
bytes
)
{
byte
[]
bs
=
new
byte
[
bytes
.
length
];
byte
[]
bs
=
new
byte
[
bytes
.
length
];
int
i
=
0
;
int
i
=
0
;
...
...
src/main/java/com/cusc/adas/v2x/vo/VehicleMessageBody.java
View file @
3169f154
...
@@ -11,6 +11,6 @@ public class VehicleMessageBody {
...
@@ -11,6 +11,6 @@ public class VehicleMessageBody {
@Order
(
2
)
@Order
(
2
)
private
VehicleStatusInfo
vehicleStatusInfo
;
private
VehicleStatusInfo
vehicleStatusInfo
;
// //车辆自动驾驶信息
// //车辆自动驾驶信息
@Order
(
3
)
//
@Order(3)
private
AutomaticDriveInfo
automaticDriveInfo
;
//
private AutomaticDriveInfo automaticDriveInfo;
}
}
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