Skip to content

Commit 27345db

Browse files
VladRassokhinjoehni
authored andcommitted
Lazy FieldKey#getDepth
Speeds up `FieldKey` constructor, hence `FieldDictionary.fieldOrNull`
1 parent 1c63ab0 commit 27345db

File tree

1 file changed

+13
-11
lines changed
  • xstream/src/java/com/thoughtworks/xstream/converters/reflection

1 file changed

+13
-11
lines changed

xstream/src/java/com/thoughtworks/xstream/converters/reflection/FieldKey.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2007, 2014 XStream Committers.
2+
* Copyright (C) 2007, 2014, 2014 XStream Committers.
33
* All rights reserved.
44
*
55
* The software in this package is published under the terms of the BSD
@@ -19,8 +19,8 @@
1919
public class FieldKey {
2020
final private String fieldName;
2121
final private Class<?> declaringClass;
22-
final private int depth;
2322
final private int order;
23+
private int depth = -1;
2424

2525
public FieldKey(final String fieldName, final Class<?> declaringClass, final int order) {
2626
if (fieldName == null || declaringClass == null) {
@@ -29,13 +29,6 @@ public FieldKey(final String fieldName, final Class<?> declaringClass, final int
2929
this.fieldName = fieldName;
3030
this.declaringClass = declaringClass;
3131
this.order = order;
32-
Class<?> c = declaringClass;
33-
int i = 0;
34-
while (c.getSuperclass() != null) {
35-
i++;
36-
c = c.getSuperclass();
37-
}
38-
depth = i;
3932
}
4033

4134
public String getFieldName() {
@@ -47,7 +40,16 @@ public Class<?> getDeclaringClass() {
4740
}
4841

4942
public int getDepth() {
50-
return depth;
43+
if (this.depth == -1) {
44+
Class<?> c = declaringClass;
45+
int i = 0;
46+
while (c.getSuperclass() != null) {
47+
i++;
48+
c = c.getSuperclass();
49+
}
50+
depth = i;
51+
}
52+
return this.depth;
5153
}
5254

5355
public int getOrder() {
@@ -89,7 +91,7 @@ public String toString() {
8991
+ "order="
9092
+ order
9193
+ ", writer="
92-
+ depth
94+
+ getDepth()
9395
+ ", declaringClass="
9496
+ declaringClass
9597
+ ", fieldName='"

0 commit comments

Comments
 (0)