Skip to content

Commit

Permalink
codegen: Replace code blocks with only whitespaces with {}
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyknic committed May 17, 2017
1 parent e3a7be9 commit 521f905
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
Expand Up @@ -19,13 +19,16 @@
import com.speedment.common.codegen.Generator;
import com.speedment.common.codegen.Transform;
import com.speedment.common.codegen.internal.java.view.trait.*;
import com.speedment.common.codegen.model.ClassOrInterface;

import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static com.speedment.common.codegen.internal.util.NullUtil.requireNonNullElements;
import static com.speedment.common.codegen.internal.util.NullUtil.requireNonNulls;
import com.speedment.common.codegen.model.ClassOrInterface;
import static com.speedment.common.codegen.util.Formatting.*;
import java.util.Optional;
import static java.util.stream.Collectors.joining;
import java.util.stream.Stream;


/**
Expand All @@ -47,7 +50,10 @@ abstract class ClassOrInterfaceView<M extends ClassOrInterface<M>> implements Tr
HasClassesView<M>,
HasAnnotationUsageView<M>,
HasFieldsView<M> {


private static final Pattern EMPTY_BLOCK =
Pattern.compile("^\\s*\\{\\s*}\\s*$");

protected static final String
CLASS_STRING = "class ",
INTERFACE_STRING = "interface ",
Expand Down Expand Up @@ -109,6 +115,19 @@ public String fieldSuffix() {
@Override
public Optional<String> transform(Generator gen, M model) {
requireNonNulls(gen, model);

String code = block(nl() + separate(
onBeforeFields(gen, model), // Enums have constants here.´
renderFields(gen, model),
renderConstructors(gen, model),
renderInitalizers(gen, model),
renderMethods(gen, model),
renderClasses(gen, model)
));

if (EMPTY_BLOCK.matcher(code).find()) {
code = "{}";
}

return Optional.of(renderJavadoc(gen, model) +
renderAnnotations(gen, model) +
Expand All @@ -121,14 +140,7 @@ public Optional<String> transform(Generator gen, M model) {
renderInterfaces(gen, model) +

// Code
block(nl() + separate(
onBeforeFields(gen, model), // Enums have constants here.´
renderFields(gen, model),
renderConstructors(gen, model),
renderInitalizers(gen, model),
renderMethods(gen, model),
renderClasses(gen, model)
))
code
);
}

Expand Down
Expand Up @@ -42,8 +42,12 @@ public interface HasCodeView<M extends HasCode<M>> extends Transform<M, String>
* @return the generated code
*/
default String renderCode(Generator gen, M model) {
return block(model.getCode().stream()
.collect(joining(nl()))
);
if (model.getCode().isEmpty()) {
return "{}";
} else {
return block(model.getCode().stream()
.collect(joining(nl()))
);
}
}
}

0 comments on commit 521f905

Please sign in to comment.