Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually make Reflector.getOwnProperties return only own properties #23

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 4 additions & 19 deletions src/main/java/org/fulib/yaml/Reflector.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public Set<String> getOwnProperties()
}

final Set<String> ownProperties = new TreeSet<>();
addOwnProperties(this.getClazz(), ownProperties);
addProperties(this.getClazz(), false, ownProperties);
return this.ownProperties = Collections.unmodifiableSet(ownProperties);
}

Expand All @@ -147,11 +147,11 @@ public Set<String> getAllProperties()
}

final Set<String> ownProperties = new TreeSet<>();
addAllProperties(this.getClazz(), ownProperties);
addProperties(this.getClazz(), true, ownProperties);
return this.allProperties = Collections.unmodifiableSet(ownProperties);
}

private static void addOwnProperties(Class<?> clazz, Set<String> fieldNames)
private static void addProperties(Class<?> clazz, boolean inherited, Set<String> fieldNames)
{
final Method[] methods = clazz.getMethods();
for (Method method : methods)
Expand All @@ -160,6 +160,7 @@ private static void addOwnProperties(Class<?> clazz, Set<String> fieldNames)

// getter, but not getClass(), get() or parameterized
if (method.getParameterCount() == 0 && methodName.length() > 3 // fast checks
&& (inherited || method.getDeclaringClass() == clazz)
&& methodName.startsWith("get") && !"getClass".equals(methodName))
{
final String attributeName = StrUtil.downFirstChar(methodName.substring(3));
Expand All @@ -168,22 +169,6 @@ private static void addOwnProperties(Class<?> clazz, Set<String> fieldNames)
}
}

private static void addAllProperties(Class<?> clazz, Set<String> fieldNames)
{
addOwnProperties(clazz, fieldNames);

final Class<?> superClass = clazz.getSuperclass();
if (superClass != null)
{
addAllProperties(superClass, fieldNames);
}

for (final Class<?> superInterface : clazz.getInterfaces())
{
addAllProperties(superInterface, fieldNames);
}
}

// =============== Methods ===============

/**
Expand Down