Skip to content

Commit

Permalink
Change DebugExpressionVisitor to output debug information to a generi…
Browse files Browse the repository at this point in the history
…c Appendable instance
  • Loading branch information
asereda-gs committed Oct 18, 2019
1 parent 50987bf commit 9d5079c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,34 @@

package org.immutables.criteria.expression;

import com.google.common.io.CharStreams;

import java.io.PrintWriter;
import java.util.Collections;
import java.util.Objects;

/**
* Used to output expression tree as string. Useful for debugging expressions.
*/
public class DebugExpressionVisitor<Void> extends AbstractExpressionVisitor<Void> {
public class DebugExpressionVisitor<A extends Appendable> extends AbstractExpressionVisitor<A> {

private final PrintWriter writer;
private final A target;

private int depth;

public DebugExpressionVisitor(PrintWriter writer) {
public DebugExpressionVisitor(A target) {
super(e -> { throw new UnsupportedOperationException(); });
this.writer = Objects.requireNonNull(writer, "writer");
this.target = Objects.requireNonNull(target, "target");
this.writer = new PrintWriter(CharStreams.asWriter(target));
}

private A target() {
return target;
}

@Override
public Void visit(Call call) {
public A visit(Call call) {
writer.println();
writer.print(String.join("", Collections.nCopies(depth * 2, " ")));
writer.print("call op=" + call.operator().name());
Expand All @@ -44,21 +52,20 @@ public Void visit(Call call) {
expr.accept(this);
depth--;
}
return null;
return target();
}

@Override
public Void visit(Constant constant) {
public A visit(Constant constant) {
writer.print(" constant=");
writer.print(constant.value());
return null;
return target();
}

@Override
public Void visit(Path path) {
public A visit(Path path) {
writer.print(" path=");
writer.print(path.toStringPath());
return null;
return target();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public String toString() {
final StringWriter string = new StringWriter();
final PrintWriter writer = new PrintWriter(string);

final DebugExpressionVisitor<Object> visitor = new DebugExpressionVisitor<>(writer);
final DebugExpressionVisitor<PrintWriter> visitor = new DebugExpressionVisitor<>(writer);
writer.append("entity: ").append(entityClass().getName()).println();

if (!projections().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public String toString() {
printer.append(SimpleCall.class.getSimpleName());
printer.append("{");
printer.print(operator().name());
DebugExpressionVisitor<Void> debug = new DebugExpressionVisitor<>(printer);
DebugExpressionVisitor<PrintWriter> debug = new DebugExpressionVisitor<>(printer);
arguments().forEach(a -> {
a.accept(debug);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ public void aggregation() {

private static void assertProjection(Aggregation<?> aggregation, String ... expectedLines) {
Expression expression = Matchers.toExpression(aggregation);
StringWriter out = new StringWriter();
expression.accept(new DebugExpressionVisitor<>(new PrintWriter(out)));
String out = expression.accept(new DebugExpressionVisitor<>(new StringBuilder())).toString();
final String expected = Arrays.stream(expectedLines).collect(Collectors.joining(System.lineSeparator()));
Assertions.assertEquals(expected, out.toString().trim());
Assertions.assertEquals(expected, out.trim());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,9 @@ void inner() {


private static void assertExpressional(Criterion<?> crit, String ... expectedLines) {
final StringWriter out = new StringWriter();
final StringBuilder out = new StringBuilder();
Query query = Criterias.toQuery(crit);
query.filter().ifPresent(f -> f.accept(new DebugExpressionVisitor<>(new PrintWriter(out))));
query.filter().ifPresent(f -> f.accept(new DebugExpressionVisitor<>(out)));
final String expected = Arrays.stream(expectedLines).collect(Collectors.joining(System.lineSeparator()));
Assertions.assertEquals(expected, out.toString().trim());
}
Expand Down

0 comments on commit 9d5079c

Please sign in to comment.