Skip to content
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
@@ -1,14 +1,14 @@
package pl.wavesoftware.utils.stringify.configuration;

import java.lang.reflect.Field;
import pl.wavesoftware.utils.stringify.lang.Predicate;

/**
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
* @since 27.04.18
*/
public final class AlwaysTruePredicate implements Predicate<Field> {
public final class AlwaysTruePredicate implements Predicate<InspectionPoint> {
@Override
public boolean test(Field field) {
public boolean test(InspectionPoint inspectionPoint) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package pl.wavesoftware.utils.stringify.configuration;

import pl.wavesoftware.utils.stringify.lang.Predicate;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

/**
* When running in {@link Mode#PROMISCUOUS} this annotation can be used to exclude a
Expand All @@ -22,5 +23,5 @@
*
* @return a class of predicate to be used to determine if field should not be inspected
*/
Class<? extends Predicate<Field>> conditionally() default AlwaysTruePredicate.class;
Class<? extends Predicate<InspectionPoint>> conditionally() default AlwaysTruePredicate.class;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package pl.wavesoftware.utils.stringify.configuration;

import pl.wavesoftware.utils.stringify.lang.Predicate;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

/**
* If {@link Mode} is set to {@link Mode#QUIET} (by default), this annotation
Expand All @@ -24,5 +25,5 @@
*
* @return a class of predicate to be used to determine if field should be inspected
*/
Class<? extends Predicate<Field>> conditionally() default AlwaysTruePredicate.class;
Class<? extends Predicate<InspectionPoint>> conditionally() default AlwaysTruePredicate.class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pl.wavesoftware.utils.stringify.configuration;

import pl.wavesoftware.utils.stringify.lang.Supplier;

import java.lang.reflect.Field;

/**
* This interface represents a inspection point in some object.
*
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
* @since 30.04.18
*/
public interface InspectionPoint {
/**
* Get field representation of inspection point
* @return a field
*/
Field getField();

/**
* Get object that contains this inspection point
* @return an object
*/
Object getContainingObject();

/**
* Get a field value supplier
*
* @return a supplier of a field value
*/
Supplier<Object> getValueSupplier();
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package pl.wavesoftware.utils.stringify.impl;

import java.lang.reflect.Field;
import pl.wavesoftware.utils.stringify.configuration.InspectionPoint;

/**
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
* @since 27.04.18
*/
interface InspectFieldPredicate {
boolean shouldInspect(Field field);
boolean shouldInspect(InspectionPoint inspectionPoint);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package pl.wavesoftware.utils.stringify.impl;

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;
import pl.wavesoftware.utils.stringify.configuration.BeanFactory;

import java.lang.reflect.Field;

/**
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
Expand All @@ -15,9 +14,9 @@
final class InspectingFieldFactory {
private final Mode mode;

InspectingField create(Field field,
InspectingField create(InspectionPoint inspectionPoint,
BeanFactory beanFactory) {
return new InspectingFieldImpl(field, createPredicate(beanFactory));
return new InspectingFieldImpl(inspectionPoint, createPredicate(beanFactory));
}

private InspectFieldPredicate createPredicate(BeanFactory beanFactory) {
Expand All @@ -30,21 +29,23 @@ private InspectFieldPredicate createPredicate(BeanFactory beanFactory) {

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

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

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

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

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import pl.wavesoftware.eid.utils.EidPreconditions;
import pl.wavesoftware.utils.stringify.configuration.InspectionPoint;
import pl.wavesoftware.utils.stringify.lang.Supplier;

import javax.annotation.Nonnull;
import java.lang.reflect.Field;

import static pl.wavesoftware.eid.utils.EidPreconditions.tryToExecute;

/**
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
* @since 30.04.18
*/
@Getter
@RequiredArgsConstructor
final class InspectionPointImpl implements InspectionPoint {
private final Field field;
private final Object containingObject;

@Override
public Supplier<Object> getValueSupplier() {
return new Supplier<Object>() {
@Override
public Object get() {
try (final FieldAccessiblier accessiblier = new FieldAccessiblier(getField())) {
return tryToExecute(new EidPreconditions.UnsafeSupplier<Object>() {
@Override
@Nonnull
public Object get() throws IllegalAccessException {
return accessiblier
.getField()
.get(getContainingObject());
}
}, "20180430:113514");
}
}
};
}

private static final class FieldAccessiblier implements AutoCloseable {
@Getter
private final Field field;
private final boolean accessible;

private FieldAccessiblier(Field field) {
this.field = field;
this.accessible = ensureAccessible(field);
}

@Override
public void close() {
if (!accessible) {
field.setAccessible(false);
}
}

private static boolean ensureAccessible(Field field) {
boolean ret = field.isAccessible();
if (!ret) {
field.setAccessible(true);
}
return ret;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import pl.wavesoftware.utils.stringify.configuration.AlwaysTruePredicate;
import pl.wavesoftware.utils.stringify.configuration.BeanFactory;
import pl.wavesoftware.utils.stringify.configuration.DoNotInspect;
import pl.wavesoftware.utils.stringify.configuration.Inspect;
import pl.wavesoftware.utils.stringify.configuration.Predicate;
import pl.wavesoftware.utils.stringify.configuration.BeanFactory;

import java.lang.reflect.Field;
import pl.wavesoftware.utils.stringify.configuration.InspectionPoint;
import pl.wavesoftware.utils.stringify.lang.Predicate;

/**
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
Expand All @@ -19,28 +18,30 @@ final class PromiscuousInspectFieldPredicate implements InspectFieldPredicate {
private final BeanFactory beanFactory;

@Override
public boolean shouldInspect(Field field) {
DoNotInspect doNotInspect = field.getAnnotation(DoNotInspect.class);
public boolean shouldInspect(InspectionPoint inspectionPoint) {
DoNotInspect doNotInspect = inspectionPoint.getField()
.getAnnotation(DoNotInspect.class);
if (doNotInspect != null) {
return shouldInspect(field, doNotInspect);
return shouldInspect(inspectionPoint, doNotInspect);
} else {
Inspect inspect = field.getAnnotation(Inspect.class);
Inspect inspect = inspectionPoint.getField()
.getAnnotation(Inspect.class);
if (inspect != null && inspect.conditionally() != AlwaysTruePredicate.class) {
Predicate<Field> predicate = beanFactory.create(inspect.conditionally());
return predicate.test(field);
Predicate<InspectionPoint> predicate = beanFactory.create(inspect.conditionally());
return predicate.test(inspectionPoint);
} else {
return true;
}
}
}

private boolean shouldInspect(Field field, DoNotInspect doNotInspect) {
Class<? extends Predicate<Field>> predicateClass = doNotInspect.conditionally();
private boolean shouldInspect(InspectionPoint inspectionPoint, DoNotInspect doNotInspect) {
Class<? extends Predicate<InspectionPoint>> predicateClass = doNotInspect.conditionally();
if (predicateClass == AlwaysTruePredicate.class) {
return false;
} else {
Predicate<Field> predicate = beanFactory.create(predicateClass);
return !predicate.test(field);
Predicate<InspectionPoint> predicate = beanFactory.create(predicateClass);
return !predicate.test(inspectionPoint);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import pl.wavesoftware.utils.stringify.configuration.AlwaysTruePredicate;
import pl.wavesoftware.utils.stringify.configuration.Inspect;
import pl.wavesoftware.utils.stringify.configuration.Predicate;
import pl.wavesoftware.utils.stringify.configuration.BeanFactory;

import java.lang.reflect.Field;
import pl.wavesoftware.utils.stringify.configuration.Inspect;
import pl.wavesoftware.utils.stringify.configuration.InspectionPoint;
import pl.wavesoftware.utils.stringify.lang.Predicate;

/**
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
Expand All @@ -18,22 +17,23 @@ final class QuietInspectFieldPredicate implements InspectFieldPredicate {
private final BeanFactory beanFactory;

@Override
public boolean shouldInspect(Field field) {
Inspect inspect = field.getAnnotation(Inspect.class);
public boolean shouldInspect(InspectionPoint inspectionPoint) {
Inspect inspect = inspectionPoint.getField()
.getAnnotation(Inspect.class);
if (inspect != null) {
return shouldInspect(field, inspect);
return shouldInspect(inspectionPoint, inspect);
} else {
return false;
}
}

private boolean shouldInspect(Field field, Inspect inspect) {
Class<? extends Predicate<Field>> predicateClass = inspect.conditionally();
private boolean shouldInspect(InspectionPoint inspectionPoint, Inspect inspect) {
Class<? extends Predicate<InspectionPoint>> predicateClass = inspect.conditionally();
if (predicateClass == AlwaysTruePredicate.class) {
return true;
} else {
Predicate<Field> predicate = beanFactory.create(predicateClass);
return predicate.test(field);
Predicate<InspectionPoint> predicate = beanFactory.create(predicateClass);
return predicate.test(inspectionPoint);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pl.wavesoftware.utils.stringify.impl;

import pl.wavesoftware.eid.utils.EidPreconditions;
import pl.wavesoftware.utils.stringify.configuration.InspectionPoint;
import pl.wavesoftware.utils.stringify.configuration.Mode;
import pl.wavesoftware.utils.stringify.configuration.BeanFactory;

Expand Down Expand Up @@ -120,13 +121,18 @@ private Map<String, CharSequence> inspectTargetAsClass(Class<?> type) {
private void inspectFields(Field[] fields,
Map<String, CharSequence> properties) {
for (Field field : fields) {
InspectingField inspectingField = inspectingFieldFactory.create(field, beanFactory);
InspectionPoint inspectionPoint = createInspectionPoint(field);
InspectingField inspectingField = inspectingFieldFactory.create(inspectionPoint, beanFactory);
if (inspectingField.shouldInspect()) {
inspectAnnotatedField(properties, field, inspectingField);
}
}
}

private InspectionPoint createInspectionPoint(Field field) {
return new InspectionPointImpl(field, target);
}

private void inspectAnnotatedField(final Map<String, CharSequence> properties,
final Field field,
final InspectingField inspectingField) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pl.wavesoftware.utils.stringify.configuration;
package pl.wavesoftware.utils.stringify.lang;

/**
* Represents a predicate (boolean-valued function) of one argument.
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/pl/wavesoftware/utils/stringify/lang/Supplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.wavesoftware.utils.stringify.lang;

/**
* Represents a supplier of results.
*
* <p>There is no requirement that a new or distinct result be returned each
* time the supplier is invoked.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #get()}.
*
* @param <T> the type of results supplied by this supplier
*
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
* @since 30.04.18
*/
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
* @since 30.04.18
*/
@ParametersAreNonnullByDefault
package pl.wavesoftware.utils.stringify.lang;

import javax.annotation.ParametersAreNonnullByDefault;
Loading