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
4e2d0641
Commit
4e2d0641
authored
Jun 26, 2025
by
黄谋临
Browse files
添加DTO对象
parent
a4a03037
Changes
26
Hide whitespace changes
Inline
Side-by-side
src/main/java/com/cusc/adas/v2x/utils/Parse.java
View file @
4e2d0641
...
...
@@ -12,313 +12,432 @@ import java.util.Comparator;
import
java.util.List
;
import
java.util.SortedMap
;
import
java.util.TreeMap
;
import
java.util.UUID
;
import
com.cusc.adas.v2x.vo.DynamicsClassMap
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.Unpooled
;
public
class
Parse
{
public
static
<
T
>
T
parse
(
ByteBuf
data
,
T
t
)
throws
InstantiationException
,
IllegalAccessException
,
NoSuchFieldException
,
SecurityException
,
ClassNotFoundException
{
Field
[]
fields
=
t
.
getClass
().
getDeclaredFields
();
List
<
Field
>
fList
=
new
ArrayList
<>();
SortedMap
<
Integer
,
Field
>
rstMap
=
new
TreeMap
<>();
for
(
Field
field
:
fields
)
{
if
(
field
.
isAnnotationPresent
(
Order
.
class
))
{
fList
.
add
(
field
);
}
}
Collections
.
sort
(
fList
,
new
Comparator
<
Field
>()
{
public
static
<
T
>
T
parse
(
ByteBuf
data
,
T
t
)
throws
InstantiationException
,
IllegalAccessException
,
NoSuchFieldException
,
SecurityException
,
ClassNotFoundException
{
Field
[]
fields
=
t
.
getClass
().
getDeclaredFields
();
List
<
Field
>
fList
=
new
ArrayList
<>();
SortedMap
<
Integer
,
Field
>
rstMap
=
new
TreeMap
<>();
for
(
Field
field
:
fields
)
{
if
(
field
.
isAnnotationPresent
(
Order
.
class
))
{
fList
.
add
(
field
);
}
}
Collections
.
sort
(
fList
,
new
Comparator
<
Field
>()
{
@Override
public
int
compare
(
Field
f1
,
Field
f2
)
{
return
f1
.
getAnnotation
(
Order
.
class
).
value
()
-
f2
.
getAnnotation
(
Order
.
class
).
value
();
}
});
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
);
int
num
=
order
.
value
();
//基础数据类型 且 不是数组
if
(
field
.
getType
().
isPrimitive
()
&&
!
field
.
getType
().
isArray
())
{
// data.re
// field.set(fList, rstMap);
readBuf
(
t
,
field
,
data
);
}
else
if
(
field
.
getClass
().
isPrimitive
()
&&
field
.
getType
().
isArray
())
{
// 基础数据类型 且 是数组
}
else
if
(
List
.
class
.
isAssignableFrom
(
field
.
getType
()))
{
// 集合类型
//读取属性的注解是否引用其他字段的长度
if
(
field
.
isAnnotationPresent
(
RefNumFlag
.
class
))
{
RefNumFlag
refNumFlag
=
field
.
getAnnotation
(
RefNumFlag
.
class
);
String
refField
=
refNumFlag
.
value
();
Class
<?>
clazz1
=
t
.
getClass
();
Field
nameField
=
clazz1
.
getDeclaredField
(
refField
);
Type
genericType
=
nameField
.
getGenericType
();
nameField
.
setAccessible
(
true
);
if
(
genericType
instanceof
ParameterizedType
)
{
ParameterizedType
pt
=
(
ParameterizedType
)
genericType
;
Type
[]
actualTypes
=
pt
.
getActualTypeArguments
();
if
(
actualTypes
.
length
>
0
)
{
actualTypes
[
0
].
getClass
();
int
len
=
(
int
)
nameField
.
get
(
t
);
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
actualTypes
[
0
].
getClass
();
}
}
}
}
}
else
if
(
field
.
getType
()
==
String
.
class
)
{
//类型为字符串
readBuf
(
t
,
field
,
data
);
}
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
;
}
/**
* 读取字节流
*
* @param t
* @param field
* @param data
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public
static
void
readBuf
(
Object
t
,
Field
field
,
ByteBuf
data
)
throws
IllegalArgumentException
,
IllegalAccessException
{
if
(
data
.
readerIndex
()
==
data
.
maxCapacity
())
return
;
FieldDef
fieldDef
=
null
;
if
(
field
.
isAnnotationPresent
(
FieldDef
.
class
))
{
fieldDef
=
field
.
getAnnotation
(
FieldDef
.
class
);
}
Class
<?>
type
=
field
.
getType
();
if
(
type
==
String
.
class
&&
fieldDef
!=
null
&&
fieldDef
.
type
().
equals
(
"BYTE"
))
{
byte
[]
by
=
new
byte
[
fieldDef
.
length
()];
data
.
readBytes
(
by
);
String
str
=
new
String
(
by
,
StandardCharsets
.
UTF_8
);
field
.
set
(
t
,
str
);
}
else
if
(
type
==
short
.
class
)
{
field
.
set
(
t
,
data
.
readUnsignedByte
());
}
else
if
(
type
==
int
.
class
)
{
field
.
set
(
t
,
data
.
readUnsignedShort
());
}
else
if
(
type
==
long
.
class
&&
fieldDef
==
null
)
{
field
.
set
(
t
,
data
.
readUnsignedInt
());
}
else
if
(
type
==
long
.
class
&&
fieldDef
!=
null
&&
fieldDef
.
type
().
equals
(
"TIMESTAMP"
)
&&
fieldDef
.
length
()
==
8
)
{
field
.
set
(
t
,
data
.
readLong
());
}
else
if
(
type
==
long
.
class
&&
fieldDef
!=
null
&&
fieldDef
.
type
().
equals
(
"BYTE"
))
{
field
.
set
(
t
,
data
.
readLong
());
}
}
public
static
byte
[]
longToBytes
(
long
value
)
{
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
8
);
buffer
.
order
(
ByteOrder
.
BIG_ENDIAN
);
// 指定小端模式
buffer
.
putLong
(
value
);
// 将long值存入buffer
return
buffer
.
array
();
// 获取byte数组
}
public
static
byte
[]
longToBytes1
(
long
value
)
{
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
4
);
buffer
.
order
(
ByteOrder
.
BIG_ENDIAN
);
// 指定小端模式
buffer
.
putLong
(
value
);
// 将long值存入buffer
return
buffer
.
array
();
// 获取byte数组
}
public
static
byte
[]
intToBytes
(
int
value
)
{
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
4
);
buffer
.
order
(
ByteOrder
.
BIG_ENDIAN
);
// 指定小端模式
buffer
.
putLong
(
value
);
// 将long值存入buffer
return
buffer
.
array
();
// 获取byte数组
}
public
static
String
byteToHexString
(
byte
[]
bytes
)
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
byte
b
:
bytes
)
{
sb
.
append
(
String
.
format
(
"%02x"
,
b
));
}
return
sb
.
toString
();
//return null;
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
);
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
;
}
/**
* 读取字节流
* @param t
* @param field
* @param data
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SecurityException
* @throws NoSuchFieldException
*/
public
static
void
readBuf
(
Object
t
,
Field
field
,
ByteBuf
data
)
throws
IllegalArgumentException
,
IllegalAccessException
,
NoSuchFieldException
,
SecurityException
{
if
(
data
.
readerIndex
()==
data
.
maxCapacity
()
)
return
;
FieldDef
fieldDef
=
null
;
if
(
field
.
isAnnotationPresent
(
FieldDef
.
class
))
{
fieldDef
=
field
.
getAnnotation
(
FieldDef
.
class
);
}
RefNumFlag
refNumFlag
=
null
;
if
(
field
.
isAnnotationPresent
(
RefNumFlag
.
class
))
{
refNumFlag
=
field
.
getAnnotation
(
RefNumFlag
.
class
);
}
Class
<?>
type
=
field
.
getType
();
if
(
type
==
String
.
class
&&
fieldDef
!=
null
&&
fieldDef
.
type
().
equals
(
"BYTE"
))
{
byte
[]
by
=
new
byte
[
fieldDef
.
length
()];
data
.
readBytes
(
by
);
String
str
=
new
String
(
by
,
StandardCharsets
.
UTF_8
);
field
.
set
(
t
,
str
);
}
else
if
(
type
==
short
.
class
)
{
field
.
set
(
t
,
data
.
readUnsignedByte
());
}
else
if
(
type
==
int
.
class
)
{
field
.
set
(
t
,
data
.
readUnsignedShort
());
}
else
if
(
type
==
long
.
class
&&
fieldDef
==
null
)
{
field
.
set
(
t
,
data
.
readUnsignedInt
());
}
else
if
(
type
==
long
.
class
&&
fieldDef
!=
null
&&
fieldDef
.
type
().
equals
(
"TIMESTAMP"
)
&&
fieldDef
.
length
()==
8
)
{
field
.
set
(
t
,
data
.
readLong
());
}
else
if
(
type
==
long
.
class
&&
fieldDef
!=
null
&&
fieldDef
.
type
().
equals
(
"BYTE"
)
)
{
field
.
set
(
t
,
data
.
readLong
());
}
else
if
(
field
.
getType
().
isArray
()&&
refNumFlag
!=
null
)
{
Field
tField
=
t
.
getClass
().
getDeclaredField
(
refNumFlag
.
value
());
tField
.
setAccessible
(
true
);
Class
<?>
type1
=
tField
.
getType
();
byte
[]
by
=
null
;
if
(
type1
==
byte
.
class
)
{
Byte
obj
=(
Byte
)
tField
.
get
(
t
);
by
=
new
byte
[
obj
.
byteValue
()];
}
else
if
(
type1
==
short
.
class
)
{
Short
obj
=(
Short
)
tField
.
get
(
t
);
by
=
new
byte
[
obj
.
shortValue
()];
}
else
if
(
type1
==
int
.
class
)
{
Integer
obj
=(
Integer
)
tField
.
get
(
t
);
by
=
new
byte
[
obj
.
intValue
()];
}
data
.
readBytes
(
by
);
field
.
set
(
t
,
by
);
}
}
public
static
byte
[]
longToBytes
(
long
value
)
{
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
8
);
buffer
.
order
(
ByteOrder
.
BIG_ENDIAN
);
// 指定小端模式
buffer
.
putLong
(
value
);
// 将long值存入buffer
return
buffer
.
array
();
// 获取byte数组
}
public
static
byte
[]
longToBytes1
(
long
value
)
{
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
4
);
buffer
.
order
(
ByteOrder
.
BIG_ENDIAN
);
// 指定小端模式
buffer
.
putLong
(
value
);
// 将long值存入buffer
return
buffer
.
array
();
// 获取byte数组
}
public
static
byte
[]
intToBytes
(
int
value
)
{
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
4
);
buffer
.
order
(
ByteOrder
.
BIG_ENDIAN
);
// 指定小端模式
buffer
.
putLong
(
value
);
// 将long值存入buffer
return
buffer
.
array
();
// 获取byte数组
}
public
static
String
byteToHexString
(
byte
[]
bytes
)
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
byte
b
:
bytes
)
{
sb
.
append
(
String
.
format
(
"%02x"
,
b
));
}
return
sb
.
toString
();
//return null;
// ByteBuffer buffer = ByteBuffer.allocate(8);
// buffer.order(ByteOrder.LITTLE_ENDIAN); // 指定小端模式
// buffer.putLong(value); // 将long值存入buffer
// return buffer.array(); // 获取byte数组
}
public
static
String
long2Hex
(
long
num
,
int
len
)
{
String
hex
=
Long
.
toHexString
(
num
);
int
padLength
=
len
*
2
;
if
(
hex
.
length
()
>=
padLength
)
{
return
hex
.
substring
(
hex
.
length
()
-
padLength
);
// 超长时截断
}
return
String
.
format
(
"%0"
+
padLength
+
"d"
,
0
).
replace
(
"0"
,
"0"
)
+
hex
;
// 或直接格式化
}
public
static
void
main
(
String
[]
args
)
throws
InstantiationException
,
IllegalAccessException
,
NoSuchFieldException
,
SecurityException
,
ClassNotFoundException
{
String
hexString
=
"F20000000015018cb864640000000000"
;
byte
[]
bytes
=
HexStringToByteArray
.
hexStringToByteArray
(
hexString
);
ByteBuf
byteBuf
=
Unpooled
.
buffer
(
16
);
long
l
=
500000
;
//3600000000
// 1164916946
// byte [] bs = Long.toHexString(l);
String
s
=
long2Hex
(
l
,
4
);
//longToBytes1(l);
int
i
=
33
;
String
hex
=
Integer
.
toHexString
(
i
);
String
vid
=
"T1023459"
;
byte
[]
by1
=
vid
.
getBytes
();
s
=
byteToHexString
(
by1
);
System
.
out
.
println
(
s
);
}
public
static
String
long2Hex
(
long
num
,
int
len
)
{
String
hex
=
Long
.
toHexString
(
num
);
int
padLength
=
len
*
2
;
if
(
hex
.
length
()
>=
padLength
)
{
return
hex
.
substring
(
hex
.
length
()
-
padLength
);
// 超长时截断
}
return
String
.
format
(
"%0"
+
padLength
+
"d"
,
0
).
replace
(
"0"
,
"0"
)
+
hex
;
// 或直接格式化
}
public
static
void
main
(
String
[]
args
)
throws
InstantiationException
,
IllegalAccessException
,
NoSuchFieldException
,
SecurityException
,
ClassNotFoundException
{
String
hexString
=
"F20000000015018cb864640000000000"
;
byte
[]
bytes
=
HexStringToByteArray
.
hexStringToByteArray
(
hexString
);
ByteBuf
byteBuf
=
Unpooled
.
buffer
(
16
);
long
l
=
500000
;
//3600000000
// 1164916946
// byte [] bs = Long.toHexString(l);
String
s
=
long2Hex
(
l
,
4
);
//longToBytes1(l);
long
i
=
250000
;
String
hex
=
Long
.
toHexString
(
i
);
int
i1
=
1500
;
hex
=
Integer
.
toHexString
(
i1
);
String
uuid
=
UUID
.
randomUUID
().
toString
();
byte
[]
by2
=
uuid
.
getBytes
();
s
=
byteToHexString
(
by2
);
String
vid
=
"JL110001"
;
byte
[]
by1
=
vid
.
getBytes
();
s
=
byteToHexString
(
by1
);
System
.
out
.
println
(
s
);
//
//
// List<Byte> byteList = new ArrayList<Byte>();
//
// Byte[] bytes = byteList.toArray(new Byte[byteList.size()]);
//byte[] bs = convoter(bytes);
//byteBuf.writeBytes(bytes);
//VehicleMessage vehicleMessage = parse(byteBuf,new VehicleMessage());
//System.out.print(vehicleMessage);
}
public
static
void
convoterBean
(
Object
source
,
Object
target
)
{
Field
field
;
Field
tField
;
try
{
Field
[]
fields
=
source
.
getClass
().
getDeclaredFields
();
//List<Field> fList = new ArrayList<>();
//SortedMap<Integer,Field> rstMap = new TreeMap<>();
for
(
Field
cfield
:
fields
)
{
field
=
cfield
;
field
.
setAccessible
(
true
);
Object
o
=
field
.
get
(
source
);
if
(
o
==
null
)
{
continue
;
}
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
,
(
short
)
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
,
(
float
)
f
);
}
else
{
double
f
=
(
Long
)
o
*
offset
;
tField
.
set
(
target
,
(
float
)
f
);
}
}
else
{
double
d
=
(
Long
)
o
*
offset
;
tField
.
set
(
target
,
(
float
)
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
))
{
//byte[] bs = convoter(bytes);
//byteBuf.writeBytes(bytes);
//VehicleMessage vehicleMessage = parse(byteBuf,new VehicleMessage());
//System.out.print(vehicleMessage);
}
public
static
void
convoterBean
(
Object
source
,
Object
target
)
throws
IllegalArgumentException
,
IllegalAccessException
,
ClassNotFoundException
,
InstantiationException
,
NoSuchFieldException
,
SecurityException
{
Field
[]
fields
=
source
.
getClass
().
getDeclaredFields
();
//List<Field> fList = new ArrayList<>();
//SortedMap<Integer,Field> rstMap = new TreeMap<>();
for
(
Field
field
:
fields
)
{
field
.
setAccessible
(
true
);
Object
o
=
field
.
get
(
source
);
if
(
o
==
null
)
{
continue
;
}
OffsetDef
offsetDef
=
null
;
if
(
field
.
isAnnotationPresent
(
OffsetDef
.
class
))
{
offsetDef
=
field
.
getAnnotation
(
OffsetDef
.
class
);
}
//获取目标对象指定的属性
Field
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
,
(
float
)
f
);
}
else
{
double
f
=(
Long
)
o
*
offset
;
tField
.
set
(
target
,
(
float
)
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
))
{
// RefNumFlag refNumFlag = field.getAnnotation(RefNumFlag.class);
// String refField = refNumFlag.value();
// Class<?> clazz1 = t.getClass();
// Field nameField = clazz1.getDeclaredField(refField);
// Type genericType = nameField.getGenericType();
// nameField.setAccessible(true);
//
//
// if (genericType instanceof ParameterizedType) {
// ParameterizedType pt = (ParameterizedType) genericType;
// Type[] actualTypes = pt.getActualTypeArguments();
...
...
@@ -330,51 +449,47 @@ public class Parse {
// }
// }
// }
}
}
else
if
(
field
.
getType
()
==
String
.
class
)
{
//类型为字符串
}
}
else
if
(
field
.
getType
()==
String
.
class
)
{
//类型为字符串
// try {
// String s = (String)o;
// String s1 = new String(s.getBytes());
tField
.
set
(
target
,
o
);
tField
.
set
(
target
,
o
);
// }catch(Exception e) {
// e.printStackTrace();
// }
}
else
{
//其他对象类型
Class
<?>
cls
=
Class
.
forName
(
tField
.
getType
().
getName
());
Object
obj
=
cls
.
newInstance
();
convoterBean
(
o
,
obj
);
tField
.
set
(
target
,
obj
);
}
}
}
}
catch
(
IllegalArgumentException
e
)
{
System
.
out
.
println
(
"e = "
+
e
.
getMessage
());
}
catch
(
IllegalAccessException
e
)
{
System
.
out
.
println
(
"e = "
+
e
.
getMessage
());
}
catch
(
ClassNotFoundException
e
)
{
System
.
out
.
println
(
"e = "
+
e
.
getMessage
());
}
catch
(
InstantiationException
e
)
{
System
.
out
.
println
(
"e = "
+
e
.
getMessage
());
}
catch
(
NoSuchFieldException
e
)
{
System
.
out
.
println
(
"e = "
+
e
.
getMessage
());
}
catch
(
SecurityException
e
)
{
System
.
out
.
println
(
"e = "
+
e
.
getMessage
());
}
}
else
{
//其他对象类型
Class
<?>
cls
=
Class
.
forName
(
tField
.
getType
().
getName
());
Object
obj
=
cls
.
newInstance
();
convoterBean
(
o
,
obj
);
tField
.
set
(
target
,
obj
);
}
}
}
}
public
static
byte
[]
convoter
(
Byte
[]
bytes
)
{
byte
[]
bs
=
new
byte
[
bytes
.
length
];
int
i
=
0
;
for
(
Byte
b
:
bytes
)
{
bs
[
i
++]
=
b
;
}
return
bs
;
}
/**
* 16进制转2进制
* @param hex
* @return
*/
public
static
String
convertHexToBinary
(
String
hex
)
{
// 将16进制数转换为10进制Integer
int
decimal
=
Integer
.
parseInt
(
hex
,
16
);
// 将10进制数转换为2进制字符串
return
Integer
.
toBinaryString
(
decimal
);
}
public
static
byte
[]
convoter
(
Byte
[]
bytes
)
{
byte
[]
bs
=
new
byte
[
bytes
.
length
];
int
i
=
0
;
for
(
Byte
b
:
bytes
)
{
bs
[
i
++]
=
b
;
}
return
bs
;
}
}
src/main/java/com/cusc/adas/v2x/utils/SM4Utils.java
View file @
4e2d0641
...
...
@@ -20,9 +20,9 @@ public class SM4Utils {
}
/**
*
????
SM4
???
* @return
?????
Base64
?????????
* @throws Exception
??
*
生成
SM4
密钥
* @return
密钥的
Base64
编码字符串
* @throws Exception
异常
*/
public
static
String
generateKey
()
throws
Exception
{
KeyGenerator
kg
=
KeyGenerator
.
getInstance
(
ALGORITHM_NAME
,
"BC"
);
...
...
@@ -32,11 +32,11 @@ public class SM4Utils {
}
/**
* SM4
????
* @param plainText
????
* @param key
?????
Base64
?????????
* @return
?????
Base64
?????????
* @throws Exception
??
* SM4
加密
* @param plainText
明文
* @param key
密钥的
Base64
编码字符串
* @return
密文的
Base64
编码字符串
* @throws Exception
异常
*/
public
static
String
encrypt
(
String
plainText
,
String
key
)
throws
Exception
{
byte
[]
keyBytes
=
Base64
.
getDecoder
().
decode
(
key
);
...
...
@@ -48,11 +48,11 @@ public class SM4Utils {
}
/**
* SM4
????
* @param cipherText
?????
Base64
?????????
* @param key
?????
Base64
?????????
* @return
????
* @throws Exception
??
* SM4
解密
* @param cipherText
密文的
Base64
编码字符串
* @param key
密钥的
Base64
编码字符串
* @return
明文
* @throws Exception
异常
*/
public
static
String
decrypt
(
String
cipherText
,
String
key
)
throws
Exception
{
byte
[]
keyBytes
=
Base64
.
getDecoder
().
decode
(
key
);
...
...
src/main/java/com/cusc/adas/v2x/vehiclesubscribe/vo/VehicleSubscribeMessageBody.java
View file @
4e2d0641
package
com.cusc.adas.v2x.vehiclesubscribe.vo
;
import
com.cusc.adas.v2x.utils.FieldDef
;
import
com.cusc.adas.v2x.utils.NumFlag
;
import
com.cusc.adas.v2x.utils.Order
;
import
com.cusc.adas.v2x.utils.RefNumFlag
;
public
class
VehicleSubscribeMessageBody
{
@Order
(
1
)
@FieldDef
(
type
=
"BYTE"
,
isArray
=
true
,
length
=
8
)
private
String
vehicleId
;
private
byte
subsLen
;
private
Byte
[]
funcSubscribe
;
@Order
(
2
)
@NumFlag
private
short
subsLen
;
@Order
(
3
)
@RefNumFlag
(
value
=
"subsLen"
)
@FieldDef
(
type
=
"BYTE[N]"
,
isArray
=
true
)
private
byte
[]
funcSubscribe
;
...
...
src/main/java/com/cusc/adas/v2x/vo/DynamicsClassMap.java
0 → 100644
View file @
4e2d0641
package
com.cusc.adas.v2x.vo
;
import
java.util.HashMap
;
import
java.util.Map
;
import
com.cusc.adas.v2x.assist.vo.TrafficLight
;
import
com.cusc.adas.v2x.clouddecision.vo.AbnormalVehicle
;
import
com.cusc.adas.v2x.clouddecision.vo.AutoSpeedLimit
;
import
com.cusc.adas.v2x.clouddecision.vo.EmergencyVehicle
;
import
com.cusc.adas.v2x.clouddecision.vo.FawWarnning
;
import
com.cusc.adas.v2x.clouddecision.vo.RSIWarnning
;
import
com.cusc.adas.v2x.clouddecision.vo.TrafficLightInfo
;
public
class
DynamicsClassMap
{
private
static
Map
<
String
,
Map
<
Short
,
Class
>>
map
=
new
HashMap
<>();
static
{
Map
<
Short
,
Class
>
cloundDecisionsubMap
=
new
HashMap
<>();
// 参考 adviceType 列表定义
cloundDecisionsubMap
.
put
((
short
)
1
,
TrafficLightInfo
.
class
);
//信号灯路口车速引导功能指令
cloundDecisionsubMap
.
put
((
short
)
5
,
AutoSpeedLimit
.
class
);
//动态车道级限速指令
cloundDecisionsubMap
.
put
((
short
)
9
,
FawWarnning
.
class
);
//前向碰撞预警
cloundDecisionsubMap
.
put
((
short
)
11
,
AbnormalVehicle
.
class
);
//异常车辆预警
cloundDecisionsubMap
.
put
((
short
)
13
,
EmergencyVehicle
.
class
);
//紧急车辆预警
cloundDecisionsubMap
.
put
((
short
)
17
,
RSIWarnning
.
class
);
//通用 RSI 预警
map
.
put
(
"CloudDecision"
,
cloundDecisionsubMap
);
Map
<
Short
,
Class
>
assistSubMap
=
new
HashMap
<>();
assistSubMap
.
put
((
short
)
7
,
TrafficLight
.
class
);
map
.
put
(
"assist"
,
assistSubMap
);
}
public
static
Class
get
(
String
classtype
,
Short
classkey
)
{
Map
<
Short
,
Class
>
subMap
=
map
.
get
(
classtype
);
return
subMap
.
get
(
classkey
);
}
}
src/main/java/com/cusc/adas/v2x/vo/VehicleMessageBody.java
View file @
4e2d0641
...
...
@@ -8,9 +8,9 @@ public class VehicleMessageBody {
@Order
(
1
)
private
VehicleInfo
vehicleInfo
;
/****车辆辅助驾驶信息及状态信息*/
//
@Order(2)
//
private VehicleStatusInfo vehicleStatusInfo;
@Order
(
2
)
private
VehicleStatusInfo
vehicleStatusInfo
;
// //车辆自动驾驶信息
//
@Order(3)
//
private AutomaticDriveInfo automaticDriveInfo;
@Order
(
3
)
private
AutomaticDriveInfo
automaticDriveInfo
;
}
src/main/java/com/cusc/adas/v2x/vo/VehicleStatusInfo.java
View file @
4e2d0641
...
...
@@ -110,11 +110,11 @@ public class VehicleStatusInfo {
//数据偏移量 200,表示-200.00 m/s~200.00 m/s,
@Order
(
27
)
@RefNumFlag
(
value
=
"wheelNum"
)
private
int
[]
wheelVelocity
;
private
List
<
Integer
>
wheelVelocity
;
//胎压
@Order
(
28
)
@RefNumFlag
(
value
=
"wheelNum"
)
private
int
[]
tirePressure
;
private
List
<
Integer
>
tirePressure
;
//车灯状态 BIT2 = 左转灯 BIT3 = 右转灯
@Order
(
30
)
private
int
lights
;
...
...
Prev
1
2
Next
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