Skip to content

Filter out static fields while inspecting #6

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

Merged
merged 1 commit into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
* @since 27.04.18
*/
public interface InspectingField {
interface InspectingField {
boolean shouldInspect();
boolean showNull();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.RequiredArgsConstructor;
import pl.wavesoftware.utils.stringify.configuration.BeanFactory;
import pl.wavesoftware.utils.stringify.configuration.DisplayNull;
import pl.wavesoftware.utils.stringify.configuration.InspectionPoint;
import pl.wavesoftware.utils.stringify.configuration.Mode;

Expand All @@ -27,30 +26,4 @@ private InspectFieldPredicate createPredicate(BeanFactory beanFactory) {
}
}

@RequiredArgsConstructor
private class InspectingFieldImpl implements InspectingField {
private final InspectionPoint inspectionPoint;
private final InspectFieldPredicate predicate;

@Override
public boolean shouldInspect() {
return technically() && predicate.shouldInspect(inspectionPoint);
}

private boolean technically() {
return !inspectionPoint.getField().isEnumConstant()
&& !inspectionPoint.getField().isSynthetic();
}

@Override
public boolean showNull() {
DisplayNull displayNull = inspectionPoint.getField()
.getAnnotation(DisplayNull.class);
if (displayNull != null) {
return displayNull.value();
} else {
return DisplayNull.BY_DEFAULT;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pl.wavesoftware.utils.stringify.impl;

import lombok.RequiredArgsConstructor;
import pl.wavesoftware.utils.stringify.configuration.DisplayNull;
import pl.wavesoftware.utils.stringify.configuration.InspectionPoint;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
* @author <a href="krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszyński</a>
* @since 2018-04-30
*/
@RequiredArgsConstructor
final class InspectingFieldImpl implements InspectingField {
private final InspectionPoint inspectionPoint;
private final InspectFieldPredicate predicate;

@Override
public boolean shouldInspect() {
return technically() && predicate.shouldInspect(inspectionPoint);
}

private boolean technically() {
Field field = inspectionPoint.getField();
int mods = field.getModifiers();
return !Modifier.isStatic(mods)
&& !field.isEnumConstant()
&& !field.isSynthetic();
}

@Override
public boolean showNull() {
DisplayNull displayNull = inspectionPoint.getField()
.getAnnotation(DisplayNull.class);
if (displayNull != null) {
return displayNull.value();
} else {
return DisplayNull.BY_DEFAULT;
}
}
}
3 changes: 3 additions & 0 deletions src/test/java/pl/wavesoftware/utils/stringify/Earth.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
@Data
@EqualsAndHashCode(callSuper = true)
final class Earth extends Planet {

private static final long serialVersionUID = 20180430201544L;

@Inspect
private Moon moon;
@Inspect
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/pl/wavesoftware/utils/stringify/Moon.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
@ToString(exclude = "nullinside")
@EqualsAndHashCode(callSuper = true)
final class Moon extends Planet {

private static final long serialVersionUID = 20180430201602L;

@Inspect
private Phase phase;
@Inspect
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/pl/wavesoftware/utils/stringify/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
*/
@Setter
class Person {
private int id;
private volatile int id;
@DisplayNull
private Person parent;
private transient Person parent;
private List<Person> childs;
private Account account;
@Inspect(conditionally = IsInDevelopment.class)
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/pl/wavesoftware/utils/stringify/Planet.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import pl.wavesoftware.utils.stringify.configuration.DoNotInspect;
import pl.wavesoftware.utils.stringify.configuration.Inspect;

import java.io.Serializable;

/**
* @author <a href="krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszyński</a>
* @since 2018-04-18
*/
@Data
abstract class Planet {
abstract class Planet implements Serializable {

private static final long serialVersionUID = 20180430201529L;

@Inspect
private String name;
@Inspect
Expand Down