Skip to content

Commit

Permalink
Lazy FieldKey#getDepth
Browse files Browse the repository at this point in the history
Speeds up `FieldKey` constructor, hence `FieldDictionary.fieldOrNull`
  • Loading branch information
VladRassokhin authored and joehni committed Oct 22, 2024
1 parent 1c63ab0 commit 27345db
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, 2014 XStream Committers.
* Copyright (C) 2007, 2014, 2014 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand All @@ -19,8 +19,8 @@
public class FieldKey {
final private String fieldName;
final private Class<?> declaringClass;
final private int depth;
final private int order;
private int depth = -1;

public FieldKey(final String fieldName, final Class<?> declaringClass, final int order) {
if (fieldName == null || declaringClass == null) {
Expand All @@ -29,13 +29,6 @@ public FieldKey(final String fieldName, final Class<?> declaringClass, final int
this.fieldName = fieldName;
this.declaringClass = declaringClass;
this.order = order;
Class<?> c = declaringClass;
int i = 0;
while (c.getSuperclass() != null) {
i++;
c = c.getSuperclass();
}
depth = i;
}

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

public int getDepth() {
return depth;
if (this.depth == -1) {
Class<?> c = declaringClass;
int i = 0;
while (c.getSuperclass() != null) {
i++;
c = c.getSuperclass();
}
depth = i;
}
return this.depth;
}

public int getOrder() {
Expand Down Expand Up @@ -89,7 +91,7 @@ public String toString() {
+ "order="
+ order
+ ", writer="
+ depth
+ getDepth()
+ ", declaringClass="
+ declaringClass
+ ", fieldName='"
Expand Down

0 comments on commit 27345db

Please sign in to comment.