Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
superrain51
apollo
提交
ab54e424
apollo
项目概览
superrain51
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ab54e424
编写于
4月 18, 2016
作者:
Y
Yiming Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Enhacne entity toString
上级
ef280938
变更
11
显示空白变更内容
内联
并排
Showing
11 changed file
with
52 addition
and
225 deletion
+52
-225
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/App.java
...lo-biz/src/main/java/com/ctrip/apollo/biz/entity/App.java
+5
-0
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/AppNamespace.java
...c/main/java/com/ctrip/apollo/biz/entity/AppNamespace.java
+4
-0
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Audit.java
...-biz/src/main/java/com/ctrip/apollo/biz/entity/Audit.java
+6
-2
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/BaseEntity.java
...src/main/java/com/ctrip/apollo/biz/entity/BaseEntity.java
+12
-3
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Cluster.java
...iz/src/main/java/com/ctrip/apollo/biz/entity/Cluster.java
+3
-0
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Item.java
...o-biz/src/main/java/com/ctrip/apollo/biz/entity/Item.java
+5
-0
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Namespace.java
.../src/main/java/com/ctrip/apollo/biz/entity/Namespace.java
+4
-0
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Privilege.java
.../src/main/java/com/ctrip/apollo/biz/entity/Privilege.java
+5
-0
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Release.java
...iz/src/main/java/com/ctrip/apollo/biz/entity/Release.java
+5
-0
apollo-biz/src/test/java/com/ctrip/apollo/biz/service/AdminServiceTest.java
...t/java/com/ctrip/apollo/biz/service/AdminServiceTest.java
+3
-0
apollo-core/src/main/java/com/ctrip/apollo/core/utils/ToStringHelper.java
...main/java/com/ctrip/apollo/core/utils/ToStringHelper.java
+0
-220
未找到文件。
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/App.java
浏览文件 @
ab54e424
...
...
@@ -56,4 +56,9 @@ public class App extends BaseEntity {
public
void
setOwnerName
(
String
ownerName
)
{
this
.
ownerName
=
ownerName
;
}
public
String
toString
()
{
return
toStringHelper
().
add
(
"name"
,
name
).
add
(
"appId"
,
appId
).
add
(
"ownerName"
,
ownerName
)
.
add
(
"ownerEmail"
,
ownerEmail
).
toString
();
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/AppNamespace.java
浏览文件 @
ab54e424
...
...
@@ -46,4 +46,8 @@ public class AppNamespace extends BaseEntity {
this
.
name
=
name
;
}
public
String
toString
()
{
return
toStringHelper
().
add
(
"name"
,
name
).
add
(
"appId"
,
appId
).
add
(
"comment"
,
comment
)
.
toString
();
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Audit.java
浏览文件 @
ab54e424
...
...
@@ -20,7 +20,7 @@ public class Audit extends BaseEntity {
@Column
(
name
=
"EntityName"
,
nullable
=
false
)
private
String
entityName
;
@Column
(
name
=
"EntityId"
)
@Column
(
name
=
"EntityId"
)
private
Long
entityId
;
@Column
(
name
=
"OpName"
,
nullable
=
false
)
...
...
@@ -61,4 +61,8 @@ public class Audit extends BaseEntity {
this
.
opName
=
opName
;
}
public
String
toString
()
{
return
toStringHelper
().
add
(
"entityName"
,
entityName
).
add
(
"entityId"
,
entityId
)
.
add
(
"opName"
,
opName
).
add
(
"comment"
,
comment
).
toString
();
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/BaseEntity.java
浏览文件 @
ab54e424
...
...
@@ -12,7 +12,8 @@ import javax.persistence.PrePersist;
import
javax.persistence.PreRemove
;
import
javax.persistence.PreUpdate
;
import
com.ctrip.apollo.core.utils.ToStringHelper
;
import
com.google.common.base.MoreObjects
;
import
com.google.common.base.MoreObjects.ToStringHelper
;
@MappedSuperclass
@Inheritance
(
strategy
=
InheritanceType
.
TABLE_PER_CLASS
)
...
...
@@ -101,7 +102,15 @@ public abstract class BaseEntity {
this
.
dataChangeLastModifiedTime
=
new
Date
();
}
public
String
toString
()
{
return
ToStringHelper
.
toString
(
this
);
protected
ToStringHelper
toStringHelper
()
{
return
MoreObjects
.
toStringHelper
(
this
).
omitNullValues
().
add
(
"id"
,
id
)
.
add
(
"dataChangeCreatedBy"
,
dataChangeCreatedBy
)
.
add
(
"dataChangeCreatedTime"
,
dataChangeCreatedTime
)
.
add
(
"dataChangeLastModifiedBy"
,
dataChangeLastModifiedBy
)
.
add
(
"dataChangeLastModifiedTime"
,
dataChangeLastModifiedTime
);
}
public
String
toString
(){
return
toStringHelper
().
toString
();
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Cluster.java
浏览文件 @
ab54e424
...
...
@@ -38,4 +38,7 @@ public class Cluster extends BaseEntity {
this
.
name
=
name
;
}
public
String
toString
()
{
return
toStringHelper
().
add
(
"name"
,
name
).
add
(
"appId"
,
appId
).
toString
();
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Item.java
浏览文件 @
ab54e424
...
...
@@ -67,4 +67,9 @@ public class Item extends BaseEntity {
public
void
setLineNum
(
Integer
lineNum
)
{
this
.
lineNum
=
lineNum
;
}
public
String
toString
()
{
return
toStringHelper
().
add
(
"namespaceId"
,
namespaceId
).
add
(
"key"
,
key
).
add
(
"value"
,
value
)
.
add
(
"lineNum"
,
lineNum
).
add
(
"comment"
,
comment
).
toString
();
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Namespace.java
浏览文件 @
ab54e424
...
...
@@ -46,4 +46,8 @@ public class Namespace extends BaseEntity {
this
.
namespaceName
=
namespaceName
;
}
public
String
toString
()
{
return
toStringHelper
().
add
(
"appId"
,
appId
).
add
(
"clusterName"
,
clusterName
)
.
add
(
"namespaceName"
,
namespaceName
).
toString
();
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Privilege.java
浏览文件 @
ab54e424
...
...
@@ -45,4 +45,9 @@ public class Privilege extends BaseEntity {
public
void
setPrivilType
(
String
privilType
)
{
this
.
privilType
=
privilType
;
}
public
String
toString
()
{
return
toStringHelper
().
add
(
"namespaceId"
,
namespaceId
).
add
(
"privilType"
,
privilType
)
.
add
(
"name"
,
name
).
toString
();
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/Release.java
浏览文件 @
ab54e424
...
...
@@ -84,4 +84,9 @@ public class Release extends BaseEntity {
this
.
name
=
name
;
}
public
String
toString
()
{
return
toStringHelper
().
add
(
"name"
,
name
).
add
(
"appId"
,
appId
).
add
(
"clusterName"
,
clusterName
)
.
add
(
"namespaceName"
,
namespaceName
).
add
(
"configurations"
,
configurations
)
.
add
(
"comment"
,
comment
).
toString
();
}
}
apollo-biz/src/test/java/com/ctrip/apollo/biz/service/AdminServiceTest.java
浏览文件 @
ab54e424
...
...
@@ -59,6 +59,9 @@ public class AdminServiceTest {
List
<
Audit
>
audits
=
auditService
.
findByOwner
(
owner
);
Assert
.
assertEquals
(
4
,
audits
.
size
());
for
(
Audit
audit
:
audits
){
System
.
out
.
println
(
audit
);
}
}
}
apollo-core/src/main/java/com/ctrip/apollo/core/utils/ToStringHelper.java
已删除
100644 → 0
浏览文件 @
ef280938
package
com.ctrip.apollo.core.utils
;
import
java.beans.IntrospectionException
;
import
java.beans.Introspector
;
import
java.beans.PropertyDescriptor
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
import
java.util.Calendar
;
public
final
class
ToStringHelper
{
private
ToStringHelper
()
{
}
/**
* For a given class object, return a string representation which contains the implementation of
* POJO's get methods only. This should be used for POJO's (Plain Old Java Objects) only.
*
* @param objectInstance java.lang.Object of the POJO for which toString implementation should be
* returned.
*
* @return POJO getters are invoked and appended to a string which is returned from this method.
*
* @since Project v1.1
* @see #getStringUsingBean(Object)
*/
public
static
String
toString
(
Object
objectInstance
)
{
return
getStringUsingBean
(
objectInstance
);
}
/**
* Uses java.beans.PropertyDescriptor to get the getters. This way, we avoid using filters like in
* {@link #getString(Object)}
*
* @param objectInstance Instance of an object for which tostring is required.
*
* @return toString implementation of this.
*
* @see #toString(Object)
*/
private
static
String
getStringUsingBean
(
Object
objectInstance
)
{
StringBuilder
buildString
=
null
;
try
{
PropertyDescriptor
[]
propertyDescriptors
=
Introspector
.
getBeanInfo
(
objectInstance
.
getClass
()).
getPropertyDescriptors
();
buildString
=
new
StringBuilder
(
propertyDescriptors
.
length
*
4
);
for
(
PropertyDescriptor
propertyDescriptor
:
propertyDescriptors
)
{
Method
method
=
propertyDescriptor
.
getReadMethod
();
if
(
method
!=
null
&&
!
"class"
.
equals
(
propertyDescriptor
.
getName
()))
{
String
methodName
=
method
.
getName
().
substring
(
3
);
buildString
.
append
(
methodName
);
buildString
.
append
(
" = "
);
// Check if there exists any parent. This check will avoid stack over flow if any.
if
(
isParent
(
methodName
,
method
,
buildString
))
{
continue
;
}
else
{
Object
objectReturned
=
method
.
invoke
(
objectInstance
);
if
(
objectReturned
instanceof
Calendar
)
{
// No need to print the entire Calendar object. just print the date and time.
buildString
.
append
(
getCalendarString
((
Calendar
)
objectReturned
));
}
else
{
// Print the entire object.
buildString
.
append
(
objectReturned
);
}
}
buildString
.
append
(
", "
);
}
}
}
catch
(
IntrospectionException
|
IllegalAccessException
|
IllegalArgumentException
|
InvocationTargetException
ex1
)
{
// getLogger().error("IntrospectionException while executing toString...", ex1);
}
return
buildString
.
toString
();
}
/**
* Check if there exists any parent in the methodName if so, get the declaraingClass just to
* indicate that this is a parent. Append to the buildString.
*
* @param methodName Name of the method (substring to 3 - to avoid get).
* @param method {@link Method}
* @param buildString {@link StringBuilder} to append
*
* @return True if an only if there exists a recursion.
*
* @see #toString(Object)
*/
private
static
boolean
isParent
(
String
methodName
,
Method
method
,
StringBuilder
buildString
)
{
// If methodName is one of the following, its going to go for infinite loop as its going to
// refer to
// the parent.
switch
(
methodName
)
{
case
"ParentItem"
:
case
"ParentRoot"
:
// Avoiding stackOverFlow.
buildString
.
append
(
method
.
getDeclaringClass
());
return
true
;
default
:
return
false
;
}
}
/**
* @return calendarReturned
*
* @see #toString(Object)
*/
private
static
String
getCalendarString
(
Calendar
calendarReturned
)
{
StringBuilder
buildString
=
new
StringBuilder
(
13
);
buildString
.
append
(
calendarReturned
.
get
(
Calendar
.
YEAR
));
buildString
.
append
(
"-"
);
buildString
.
append
(
calendarReturned
.
get
(
Calendar
.
MONTH
)
+
1
);
buildString
.
append
(
"-"
);
buildString
.
append
(
calendarReturned
.
get
(
Calendar
.
DAY_OF_MONTH
));
buildString
.
append
(
" "
);
buildString
.
append
(
calendarReturned
.
get
(
Calendar
.
HOUR_OF_DAY
));
buildString
.
append
(
":"
);
buildString
.
append
(
calendarReturned
.
get
(
Calendar
.
MINUTE
));
buildString
.
append
(
":"
);
buildString
.
append
(
calendarReturned
.
get
(
Calendar
.
SECOND
));
buildString
.
append
(
"."
);
buildString
.
append
(
calendarReturned
.
get
(
Calendar
.
MILLISECOND
));
return
buildString
.
toString
();
}
/**
* Uses a typical reflection to get the methods of a given instance. Once we get the methods, we
* filter out the methods by set, get and invoke only get methods to append to the string which
* will later result into tostring-implementation.
*
* @param objectInstance Instance of an object for which tostring is required.
*
* @return toString implementation of this.
*
* @see #getString(Object)
*/
private
static
String
getString
(
Object
objectInstance
)
{
Class
classObject
=
objectInstance
.
getClass
();
// Get all the methods
Method
[]
methods
=
classObject
.
getDeclaredMethods
();
int
noOfMethods
=
methods
.
length
;
StringBuilder
buildString
=
new
StringBuilder
(
noOfMethods
+
2
);
buildString
.
append
(
classObject
);
buildString
.
append
(
" -->> "
);
for
(
Method
method
:
methods
)
{
String
methodName
=
method
.
getName
();
switch
(
methodName
)
{
case
"toString"
:
case
"main"
:
case
"getLogger"
:
// Do Nothing
break
;
default
:
try
{
buildString
.
append
(
extractMethodNames
(
classObject
,
objectInstance
,
methodName
,
method
));
}
catch
(
IllegalAccessException
|
IllegalArgumentException
|
InvocationTargetException
|
InstantiationException
ex
)
{
// Do nothing as this is just printing the POJO implementations...
// getLogger().error("Exception while executing toString...", ex);
}
break
;
}
}
return
buildString
.
toString
();
}
/**
* Executes a get method and returns the output as a string representing methodName = methodValue.
*
* @param methodName methodName for which method needs to be executed.
* @param method java.lang.reflect.Method
*
* @return A String value with methodName = methodValue.
*
* @throws IllegalAccessException if this Method object is enforcing Java language access control
* and the underlying method is inaccessible.
* @throws IllegalArgumentException if the method is an instance method and the specified object
* argument is not an instance of the class or interface declaring the underlying method
* (or of a subclass or implementor thereof); if the number of actual and formal
* parameters differ; if an unwrapping conversion for primitive arguments fails; or if,
* after possible unwrapping, a parameter value cannot be converted to the corresponding
* formal parameter type by a method invocation conversion.
* @throws InvocationTargetException if the underlying method throws an exception.
* @throws InstantiationException if this Class represents an abstract class, an interface, an
* array class, a primitive type, or void; or if the class has no nullary constructor; or
* if the instantiation fails for some other reason.
*
* @since Project v1.0
* @see #getString(Object)
*/
private
static
String
extractMethodNames
(
Class
classObject
,
Object
objectInstance
,
String
methodName
,
Method
method
)
throws
IllegalAccessException
,
IllegalArgumentException
,
InvocationTargetException
,
InstantiationException
{
if
(
methodName
.
startsWith
(
"set"
))
{
// Do nothing. We are interested only on get methods in toString method.
}
else
{
return
methodName
.
substring
(
3
)
+
" = "
+
method
.
invoke
(
objectInstance
,
(
Object
[])
null
)
+
", "
;
}
return
""
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录