Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mmm-rain
AndroidUtilCode
提交
933ebc81
A
AndroidUtilCode
项目概览
mmm-rain
/
AndroidUtilCode
与 Fork 源项目一致
从无法访问的项目Fork
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
AndroidUtilCode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
933ebc81
编写于
1月 08, 2019
作者:
B
Blankj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
see 01/08 log
上级
368926cc
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
50 addition
and
32 deletion
+50
-32
utilcode/lib/src/main/java/com/blankj/utilcode/util/JsonUtils.java
...lib/src/main/java/com/blankj/utilcode/util/JsonUtils.java
+17
-0
utilcode/lib/src/main/java/com/blankj/utilcode/util/LogUtils.java
.../lib/src/main/java/com/blankj/utilcode/util/LogUtils.java
+27
-4
utilcode/lib/src/test/java/com/blankj/utilcode/util/BaseTest.java
.../lib/src/test/java/com/blankj/utilcode/util/BaseTest.java
+3
-0
utilcode/lib/src/test/java/com/blankj/utilcode/util/LogUtilsTest.java
.../src/test/java/com/blankj/utilcode/util/LogUtilsTest.java
+3
-28
未找到文件。
utilcode/lib/src/main/java/com/blankj/utilcode/util/JsonUtils.java
浏览文件 @
933ebc81
...
...
@@ -204,4 +204,21 @@ public final class JsonUtils {
return
defaultValue
;
}
}
public
static
String
formatJson
(
final
String
json
)
{
return
formatJson
(
json
,
4
);
}
public
static
String
formatJson
(
final
String
json
,
final
int
indentSpaces
)
{
try
{
if
(
json
.
startsWith
(
"{"
))
{
return
new
JSONObject
(
json
).
toString
(
indentSpaces
);
}
else
if
(
json
.
startsWith
(
"["
))
{
return
new
JSONArray
(
json
).
toString
(
indentSpaces
);
}
}
catch
(
JSONException
e
)
{
e
.
printStackTrace
();
}
return
json
;
}
}
utilcode/lib/src/main/java/com/blankj/utilcode/util/LogUtils.java
浏览文件 @
933ebc81
...
...
@@ -38,7 +38,9 @@ import java.lang.reflect.Type;
import
java.net.UnknownHostException
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.Formatter
;
import
java.util.Iterator
;
...
...
@@ -784,9 +786,7 @@ public final class LogUtils {
private
static
class
LogFormatter
{
static
String
object2String
(
Object
object
)
{
if
(
object
.
getClass
().
isArray
())
return
object2Json
(
object
);
if
(
object
instanceof
Collection
)
return
object2Json
(
object
);
if
(
object
instanceof
Map
)
return
object2Json
(
object
);
if
(
object
.
getClass
().
isArray
())
return
array2String
(
object
);
if
(
object
instanceof
Throwable
)
return
throwable2String
((
Throwable
)
object
);
if
(
object
instanceof
Bundle
)
return
bundle2String
((
Bundle
)
object
);
if
(
object
instanceof
Intent
)
return
intent2String
((
Intent
)
object
);
...
...
@@ -798,7 +798,7 @@ public final class LogUtils {
return
formatJson
(
object
.
toString
());
}
try
{
return
GSON
.
toJson
(
object
);
return
formatJson
(
GSON
.
toJson
(
object
)
);
}
catch
(
Throwable
t
)
{
return
object
.
toString
();
}
...
...
@@ -1021,6 +1021,29 @@ public final class LogUtils {
sb
.
append
(
"NULL"
);
sb
.
append
(
"}"
);
}
private
static
String
array2String
(
Object
object
)
{
if
(
object
instanceof
Object
[])
{
return
Arrays
.
deepToString
((
Object
[])
object
);
}
else
if
(
object
instanceof
boolean
[])
{
return
Arrays
.
toString
((
boolean
[])
object
);
}
else
if
(
object
instanceof
byte
[])
{
return
Arrays
.
toString
((
byte
[])
object
);
}
else
if
(
object
instanceof
char
[])
{
return
Arrays
.
toString
((
char
[])
object
);
}
else
if
(
object
instanceof
double
[])
{
return
Arrays
.
toString
((
double
[])
object
);
}
else
if
(
object
instanceof
float
[])
{
return
Arrays
.
toString
((
float
[])
object
);
}
else
if
(
object
instanceof
int
[])
{
return
Arrays
.
toString
((
int
[])
object
);
}
else
if
(
object
instanceof
long
[])
{
return
Arrays
.
toString
((
long
[])
object
);
}
else
if
(
object
instanceof
short
[])
{
return
Arrays
.
toString
((
short
[])
object
);
}
throw
new
IllegalArgumentException
(
"Array has incompatible type: "
+
object
.
getClass
());
}
}
private
static
<
T
>
Class
getTypeClassFromParadigm
(
final
IFormatter
<
T
>
formatter
)
{
...
...
utilcode/lib/src/test/java/com/blankj/utilcode/util/BaseTest.java
浏览文件 @
933ebc81
...
...
@@ -7,6 +7,7 @@ import org.robolectric.RobolectricTestRunner;
import
org.robolectric.RuntimeEnvironment
;
import
org.robolectric.annotation.Config
;
import
org.robolectric.shadows.ShadowLog
;
import
org.robolectric.shadows.ShadowLooper
;
/**
* <pre>
...
...
@@ -22,6 +23,8 @@ public class BaseTest {
public
BaseTest
()
{
ShadowLog
.
stream
=
System
.
out
;
ReflectUtils
.
reflect
(
"com.blankj.utilcode.util.ThreadUtils$Deliver"
)
.
field
(
"MAIN_HANDLER"
,
null
);
Utils
.
init
(
RuntimeEnvironment
.
application
);
}
...
...
utilcode/lib/src/test/java/com/blankj/utilcode/util/LogUtilsTest.java
浏览文件 @
933ebc81
...
...
@@ -31,7 +31,7 @@ public class LogUtilsTest extends BaseTest {
private
static
final
int
[]
ONE_D_ARRAY
=
new
int
[]{
1
,
2
,
3
};
private
static
final
int
[][]
TWO_D_ARRAY
=
new
int
[][]{{
1
,
2
,
3
},
{
4
,
5
,
6
},
{
7
,
8
,
9
}};
private
static
final
ArrayList
<
Object
>
LIST
=
new
ArrayList
<>();
private
static
final
Map
<
String
,
String
>
MAP
=
new
HashMap
<>();
private
static
final
Map
<
String
,
Object
>
MAP
=
new
HashMap
<>();
@Test
public
void
testV
()
{
...
...
@@ -172,13 +172,11 @@ public class LogUtilsTest extends BaseTest {
LIST
.
add
(
new
Application
());
MAP
.
put
(
"name"
,
"AndroidUtilCode"
);
MAP
.
put
(
"class"
,
"LogUtils"
);
MAP
.
put
(
"class"
,
new
Application
()
);
LogUtils
.
d
((
Object
)
ONE_D_ARRAY
);
LogUtils
.
d
((
Object
)
TWO_D_ARRAY
);
LogUtils
.
d
(
formatJson
(
collection2String
(
LIST
))
);
LogUtils
.
d
(
LIST
);
LogUtils
.
d
(
MAP
);
LogUtils
.
d
(
array2String
(
TWO_D_ARRAY
));
}
static
String
collection2String
(
Collection
collection
)
{
...
...
@@ -206,29 +204,6 @@ public class LogUtilsTest extends BaseTest {
return
json
;
}
static
String
array2String
(
Object
object
)
{
if
(
object
instanceof
Object
[])
{
return
Arrays
.
deepToString
((
Object
[])
object
);
}
else
if
(
object
instanceof
boolean
[])
{
return
Arrays
.
toString
((
boolean
[])
object
);
}
else
if
(
object
instanceof
byte
[])
{
return
Arrays
.
toString
((
byte
[])
object
);
}
else
if
(
object
instanceof
char
[])
{
return
Arrays
.
toString
((
char
[])
object
);
}
else
if
(
object
instanceof
double
[])
{
return
Arrays
.
toString
((
double
[])
object
);
}
else
if
(
object
instanceof
float
[])
{
return
Arrays
.
toString
((
float
[])
object
);
}
else
if
(
object
instanceof
int
[])
{
return
Arrays
.
toString
((
int
[])
object
);
}
else
if
(
object
instanceof
long
[])
{
return
Arrays
.
toString
((
long
[])
object
);
}
else
if
(
object
instanceof
short
[])
{
return
Arrays
.
toString
((
short
[])
object
);
}
throw
new
IllegalArgumentException
(
"Array has incompatible type: "
+
object
.
getClass
());
}
static
class
Person
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录