Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
red5-server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Otmane Mabrouk
red5-server
Commits
bf803c72
Commit
bf803c72
authored
1 year ago
by
Paul Gregoire
Browse files
Options
Downloads
Patches
Plain Diff
Fix issue with conversion of ImmutableCollections type
parent
5f089e6f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
io/src/main/java/org/red5/io/utils/ConversionUtils.java
+37
-28
37 additions, 28 deletions
io/src/main/java/org/red5/io/utils/ConversionUtils.java
server/src/test/java/org/red5/io/utils/ConversionUtilsTest.java
+1
-4
1 addition, 4 deletions
.../src/test/java/org/red5/io/utils/ConversionUtilsTest.java
with
38 additions
and
32 deletions
io/src/main/java/org/red5/io/utils/ConversionUtils.java
+
37
−
28
View file @
bf803c72
...
@@ -18,6 +18,7 @@ import java.util.LinkedHashMap;
...
@@ -18,6 +18,7 @@ import java.util.LinkedHashMap;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.Set
;
import
java.util.stream.Collectors
;
import
org.apache.commons.beanutils.BeanMap
;
import
org.apache.commons.beanutils.BeanMap
;
import
org.apache.commons.beanutils.BeanUtils
;
import
org.apache.commons.beanutils.BeanUtils
;
...
@@ -107,13 +108,14 @@ public class ConversionUtils {
...
@@ -107,13 +108,14 @@ public class ConversionUtils {
// Don't convert NaN values
// Don't convert NaN values
return
source
;
return
source
;
}
}
if
(
target
.
isInstance
(
source
))
{
final
Class
<?>
sourceClass
=
source
.
getClass
();
return
source
;
log
.
info
(
"Source: {} target: {}"
,
sourceClass
,
target
)
;
}
if
(
target
.
isInstance
(
source
)
||
target
.
isAssignableFrom
(
sourceClass
))
{
if
(
target
.
isAssignableFrom
(
source
.
getClass
()))
{
log
.
info
(
"Source: {} is already an instance of: {}"
,
source
,
target
);
return
source
;
return
source
;
}
}
if
(
target
.
isArray
())
{
if
(
target
.
isArray
())
{
log
.
info
(
"Source: {} to target array: {}"
,
source
,
target
);
return
convertToArray
(
source
,
target
);
return
convertToArray
(
source
,
target
);
}
}
if
(
target
.
equals
(
String
.
class
))
{
if
(
target
.
equals
(
String
.
class
))
{
...
@@ -128,20 +130,37 @@ public class ConversionUtils {
...
@@ -128,20 +130,37 @@ public class ConversionUtils {
if
(
target
.
equals
(
Map
.
class
))
{
if
(
target
.
equals
(
Map
.
class
))
{
return
convertBeanToMap
(
source
);
return
convertBeanToMap
(
source
);
}
}
if
(
target
.
equals
(
List
.
class
)
||
target
.
equals
(
Collection
.
class
))
{
if
(
sourceClass
.
equals
(
LinkedHashMap
.
class
))
{
if
(
source
.
getClass
().
equals
(
LinkedHashMap
.
class
))
{
return
convertMapToList
((
LinkedHashMap
<?,
?>)
source
);
return
convertMapToList
((
LinkedHashMap
<?,
?>)
source
);
}
else
if
(
sourceClass
.
isArray
())
{
}
else
if
(
source
.
getClass
().
isArray
())
{
if
(
List
.
class
.
isAssignableFrom
(
target
))
{
return
convertArrayToList
((
Object
[])
source
);
log
.
info
(
"Source: {} to target list: {}"
,
source
,
target
);
return
Arrays
.
stream
((
Object
[])
source
).
collect
(
Collectors
.
toCollection
(
ArrayList:
:
new
));
}
else
if
(
Set
.
class
.
isAssignableFrom
(
target
))
{
log
.
info
(
"Source: {} to target set: {}"
,
source
,
target
);
// special handling for sets when the source is a list
if
(
source
instanceof
List
)
{
return
((
List
<?>)
source
).
stream
().
collect
(
Collectors
.
toCollection
(
HashSet:
:
new
));
}
return
Arrays
.
stream
((
Object
[])
source
).
collect
(
Collectors
.
toCollection
(
HashSet:
:
new
));
}
}
}
}
if
(
target
.
equals
(
Set
.
class
)
&&
source
.
getClass
().
isArray
())
{
if
(
Map
.
class
.
isAssignableFrom
(
sourceClass
))
{
return
convertArrayToSet
((
Object
[])
source
);
return
convertMapToBean
((
Map
)
source
,
target
);
}
if
(
target
.
equals
(
Set
.
class
)
&&
source
instanceof
List
)
{
return
new
HashSet
((
List
)
source
);
}
}
if
(
source
instanceof
Map
)
{
// handle immutable collections
final
String
sourceClassName
=
sourceClass
.
getName
();
if
(
sourceClassName
.
equals
(
"java.util.ImmutableCollections$ListN"
))
{
if
(
Set
.
class
.
isAssignableFrom
(
target
))
{
return
((
List
<?>)
source
).
stream
().
collect
(
Collectors
.
toCollection
(
HashSet:
:
new
));
}
return
((
List
<?>)
source
).
stream
().
collect
(
Collectors
.
toCollection
(
ArrayList:
:
new
));
}
else
if
(
sourceClassName
.
equals
(
"java.util.ImmutableCollections$SetN"
))
{
if
(
Set
.
class
.
isAssignableFrom
(
target
))
{
return
((
Set
<?>)
source
).
stream
().
collect
(
Collectors
.
toCollection
(
HashSet:
:
new
));
}
return
((
Set
<?>)
source
).
stream
().
collect
(
Collectors
.
toCollection
(
ArrayList:
:
new
));
}
else
if
(
sourceClassName
.
equals
(
"java.util.ImmutableCollections$MapN"
))
{
return
convertMapToBean
((
Map
)
source
,
target
);
return
convertMapToBean
((
Map
)
source
,
target
);
}
}
throw
new
ConversionException
(
String
.
format
(
"Unable to preform conversion from %s to %s"
,
source
,
target
));
throw
new
ConversionException
(
String
.
format
(
"Unable to preform conversion from %s to %s"
,
source
,
target
));
...
@@ -185,9 +204,7 @@ public class ConversionUtils {
...
@@ -185,9 +204,7 @@ public class ConversionUtils {
}
}
public
static
List
<
Object
>
convertMapToList
(
Map
<?,
?>
map
)
{
public
static
List
<
Object
>
convertMapToList
(
Map
<?,
?>
map
)
{
List
<
Object
>
list
=
new
ArrayList
<
Object
>(
map
.
size
());
return
List
.
of
(
map
.
values
());
list
.
addAll
(
map
.
values
());
return
list
;
}
}
/**
/**
...
@@ -333,11 +350,7 @@ public class ConversionUtils {
...
@@ -333,11 +350,7 @@ public class ConversionUtils {
* on failure
* on failure
*/
*/
public
static
List
<?>
convertArrayToList
(
Object
[]
source
)
throws
ConversionException
{
public
static
List
<?>
convertArrayToList
(
Object
[]
source
)
throws
ConversionException
{
List
<
Object
>
list
=
new
ArrayList
<
Object
>(
source
.
length
);
return
Arrays
.
stream
(
source
).
collect
(
Collectors
.
toCollection
(
ArrayList:
:
new
));
for
(
Object
element
:
source
)
{
list
.
add
(
element
);
}
return
list
;
}
}
/**
/**
...
@@ -387,11 +400,7 @@ public class ConversionUtils {
...
@@ -387,11 +400,7 @@ public class ConversionUtils {
* @return Set
* @return Set
*/
*/
public
static
Set
<?>
convertArrayToSet
(
Object
[]
source
)
{
public
static
Set
<?>
convertArrayToSet
(
Object
[]
source
)
{
Set
<
Object
>
set
=
new
HashSet
<
Object
>();
return
Arrays
.
stream
(
source
).
collect
(
Collectors
.
toCollection
(
HashSet:
:
new
));
for
(
Object
element
:
source
)
{
set
.
add
(
element
);
}
return
set
;
}
}
/**
/**
...
...
This diff is collapsed.
Click to expand it.
server/src/test/java/org/red5/io/utils/ConversionUtilsTest.java
+
1
−
4
View file @
bf803c72
...
@@ -122,10 +122,7 @@ public class ConversionUtilsTest {
...
@@ -122,10 +122,7 @@ public class ConversionUtilsTest {
@Test
@Test
public
void
testConvertArrayListToSet
()
{
public
void
testConvertArrayListToSet
()
{
List
<
String
>
source
=
new
ArrayList
<
String
>(
3
);
List
<
String
>
source
=
List
.
of
(
"a"
,
"b"
,
"c"
);
source
.
add
(
"a"
);
source
.
add
(
"b"
);
source
.
add
(
"c"
);
Object
result
=
ConversionUtils
.
convert
(
source
,
Set
.
class
);
Object
result
=
ConversionUtils
.
convert
(
source
,
Set
.
class
);
if
(!(
result
instanceof
Set
<?>))
{
if
(!(
result
instanceof
Set
<?>))
{
fail
(
"Should be a set"
);
fail
(
"Should be a set"
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment