Skip to content

Commit

Permalink
Issue #318 - @generated on multiline
Browse files Browse the repository at this point in the history
  • Loading branch information
boretti committed May 22, 2020
1 parent f801be5 commit c8cc97f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
*/
package ch.powerunit.extensions.matchers.common;

import static java.util.Optional.ofNullable;

import java.io.PrintStream;
import java.time.Instant;
import java.util.stream.Collectors;

import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
Expand All @@ -39,16 +42,10 @@ private CommonUtils() {
}

public static String toJavaSyntax(String unformatted) {
StringBuilder sb = new StringBuilder();
sb.append('"');
for (char c : unformatted.toCharArray()) {
sb.append(toJavaSyntax(c));
}
sb.append('"');
return sb.toString();
return unformatted.chars().mapToObj(CommonUtils::toJavaSyntax).collect(Collectors.joining("", "\"", "\""));
}

private static String toJavaSyntax(char ch) {
private static String toJavaSyntax(int ch) {
switch (ch) {
case '"':
return "\\\"";
Expand All @@ -59,7 +56,7 @@ private static String toJavaSyntax(char ch) {
case '\t':
return "\\t";
default:
return "" + ch;
return String.valueOf(Character.toChars(ch));
}
}

Expand All @@ -69,15 +66,15 @@ public static String addPrefix(String prefix, String input) {
}

public static String generateGeneratedAnnotation(Class<?> generatedBy, String comments) {
return "@javax.annotation.Generated(value=\"" + generatedBy.getName() + "\",date=\"" + Instant.now().toString()
+ "\"" + (comments == null ? "" : (",comments=" + toJavaSyntax(comments))) + ")";
return "@javax.annotation.Generated(\n value=\"" + generatedBy.getName() + "\",\n date=\"" + Instant.now().toString()
+ "\"" + ofNullable(comments).map(c->",\n comments=" + toJavaSyntax(c)).orElse("") + ")";
}

public static void traceErrorAndDump(Messager messager, Filer filer, Exception e, Element te) {
FileObjectHelper.processFileWithIOException(
() -> filer.createResource(StandardLocation.SOURCE_OUTPUT, "",
"dump" + System.currentTimeMillis() + "txt", te),
s -> new PrintStream(s.openOutputStream()), s -> e.printStackTrace(s),
s -> new PrintStream(s.openOutputStream()), e::printStackTrace,
e2 -> messager.printMessage(Kind.ERROR,
"Unable to create the file containing the dump of the error because of " + e2
+ " during handling of " + e,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public void testAddPrefix() {
@Test(fastFail = false)
public void testGenerateGeneratedAnnotation() {
assertThatBiFunction(CommonUtils::generateGeneratedAnnotation, Object.class, null)
.is(both(containsString("@javax.annotation.Generated(value=\"java.lang.Object\",date"))
.is(both(containsString("@javax.annotation.Generated(\n value=\"java.lang.Object\",\n date"))
.and(not(containsString("comments"))));
assertThatBiFunction(CommonUtils::generateGeneratedAnnotation, Object.class, "x")
.is(both(containsString("@javax.annotation.Generated(value=\"java.lang.Object\",date"))
.is(both(containsString("@javax.annotation.Generated(\n value=\"java.lang.Object\",\n date"))
.and(containsString("comments=\"x\"")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void testGenerateFactoryClass() {
String clean = output.toString().replaceAll("\r", "");
assertThat(clean).containsString("package pckName;");
assertThat(clean)
.containsString("@javax.annotation.Generated(value=\"javax.annotation.processing.AbstractProcessor");
.containsString("@javax.annotation.Generated(\n value=\"javax.annotation.processing.AbstractProcessor");
assertThat(clean).containsString("public static final clazzName DSL = new clazzName() {};");
assertThat(clean).containsString("yxx");
}
Expand Down

0 comments on commit c8cc97f

Please sign in to comment.