Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
ab54e424
Commit
ab54e424
authored
Apr 18, 2016
by
Yiming Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enhacne entity toString
parent
ef280938
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
52 additions
and
225 deletions
+52
-225
App.java
...lo-biz/src/main/java/com/ctrip/apollo/biz/entity/App.java
+5
-0
AppNamespace.java
...c/main/java/com/ctrip/apollo/biz/entity/AppNamespace.java
+4
-0
Audit.java
...-biz/src/main/java/com/ctrip/apollo/biz/entity/Audit.java
+6
-2
BaseEntity.java
...src/main/java/com/ctrip/apollo/biz/entity/BaseEntity.java
+12
-3
Cluster.java
...iz/src/main/java/com/ctrip/apollo/biz/entity/Cluster.java
+3
-0
Item.java
...o-biz/src/main/java/com/ctrip/apollo/biz/entity/Item.java
+5
-0
Namespace.java
.../src/main/java/com/ctrip/apollo/biz/entity/Namespace.java
+4
-0
Privilege.java
.../src/main/java/com/ctrip/apollo/biz/entity/Privilege.java
+5
-0
Release.java
...iz/src/main/java/com/ctrip/apollo/biz/entity/Release.java
+5
-0
AdminServiceTest.java
...t/java/com/ctrip/apollo/biz/service/AdminServiceTest.java
+3
-0
ToStringHelper.java
...main/java/com/ctrip/apollo/core/utils/ToStringHelper.java
+0
-220
No files found.
apollo-biz/src/main/java/com/ctrip/apollo/biz/entity/App.java
View file @
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
View file @
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
View file @
ab54e424
...
...
@@ -19,8 +19,8 @@ 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
View file @
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
View file @
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
View file @
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
View file @
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
View file @
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
View file @
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
View file @
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
deleted
100644 → 0
View file @
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
""
;
}
}
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