Skip to content

Commit

Permalink
ignore fully qualified class names in comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ivysharev committed Jan 4, 2015
1 parent 22d469c commit 66cb90b
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 18 deletions.
75 changes: 67 additions & 8 deletions generator/src/org/immutables/generator/PostprocessingMachine.java
Expand Up @@ -27,9 +27,10 @@ static CharSequence rewrite(CharSequence content) {
State state = State.UNDEFINED;
int packageFrom = -1;
int importFrom = -1;
int nextPartFrom = -1;
int nextPartFrom = 0;
FiniteStateMachine machine = new FiniteStateMachine();
FullyQualifiedNameMachine fullyQualifiedNameMachine = new FullyQualifiedNameMachine();
CommentMachine commentMachine = new CommentMachine();

for (int i = 0; i < content.length(); i++) {
char c = content.charAt(i);
Expand Down Expand Up @@ -63,20 +64,26 @@ static CharSequence rewrite(CharSequence content) {
}
break;
case CLASS:
fullyQualifiedNameMachine.nextChar(c, i);
if (fullyQualifiedNameMachine.isFinished()) {
importsBuilder.addImport(
content.subSequence(fullyQualifiedNameMachine.importFrom, fullyQualifiedNameMachine.importTo).toString());
parts.add(content.subSequence(nextPartFrom, fullyQualifiedNameMachine.importFrom).toString());
nextPartFrom = fullyQualifiedNameMachine.packageTo;
commentMachine.nextChar(c);
if (!commentMachine.isInComment()) {
fullyQualifiedNameMachine.nextChar(c, i);
if (fullyQualifiedNameMachine.isFinished()) {
importsBuilder.addImport(
content.subSequence(fullyQualifiedNameMachine.importFrom, fullyQualifiedNameMachine.importTo)
.toString());
parts.add(content.subSequence(nextPartFrom, fullyQualifiedNameMachine.importFrom).toString());
nextPartFrom = fullyQualifiedNameMachine.packageTo;
}
}
break;
}
}
// last part
parts.add(content.subSequence(nextPartFrom, content.length()).toString());

parts.set(0, "package " + currentPackage + ";\n");
if (!currentPackage.isEmpty()) {
parts.set(0, "package " + currentPackage + ";\n");
}
parts.set(1, importsBuilder.build());
return JOINER.join(parts);
}
Expand Down Expand Up @@ -285,6 +292,58 @@ enum FullyQualifiedNameState {
FINISH
}

static final class CommentMachine {
CommentState state = CommentState.NOT_IN_COMMENT;

void nextChar(char c) {
switch (state) {
case NOT_IN_COMMENT:
if (c == '/') {
state = CommentState.COMMENT_CANDIDATE;
}
break;
case COMMENT_CANDIDATE:
if (c == '/') {
state = CommentState.LINE_COMMENT;
} else if (c == '*') {
state = CommentState.BLOCK_COMMENT;
}
break;
case LINE_COMMENT:
if (c == '\n') {
state = CommentState.NOT_IN_COMMENT;
}
break;
case BLOCK_COMMENT:
if (c == '*') {
state = CommentState.BLOCK_COMMENT_OUT_CANDIDATE;
}
break;
case BLOCK_COMMENT_OUT_CANDIDATE:
if (c == '/') {
state = CommentState.NOT_IN_COMMENT;
} else {
state = CommentState.BLOCK_COMMENT;
}
break;
}
}

boolean isInComment() {
return CommentState.LINE_COMMENT.equals(state)
|| CommentState.BLOCK_COMMENT.equals(state)
|| CommentState.BLOCK_COMMENT_OUT_CANDIDATE.equals(state);
}
}

enum CommentState {
NOT_IN_COMMENT,
COMMENT_CANDIDATE,
LINE_COMMENT,
BLOCK_COMMENT,
BLOCK_COMMENT_OUT_CANDIDATE
}

private static boolean isSpaceChar(char c) {
return Character.isSpaceChar(c) || c == '\n' || c == '\t' || c == '\r';
}
Expand Down
Expand Up @@ -2,7 +2,6 @@
package org.immutables.generator;

import com.google.common.base.Joiner;
import org.junit.Ignore;
import org.junit.Test;
import static org.immutables.check.Checkers.*;

Expand All @@ -24,21 +23,47 @@ public void imports() {
}

@Test
@Ignore
public void generatedImportsPlaceholder() {
public void lineComment() {
CharSequence rewrited = PostprocessingMachine.rewrite(
LINES.join("package start;",
"import java.util.List;",
"// Generated imports",
"",
"class My extends java.util.Set {}"));
"class My extends java.util.Set {",
"// comment",
"// comment with fully qualified class name java.until.Map",
"}"));

check(rewrited).hasToString(
LINES.join("package start;",
"import java.util.List;",
"import java.util.Set;",
"",
"class My extends Set {}"));
"class My extends Set {",
"// comment",
"// comment with fully qualified class name java.until.Map",
"}"));
}

@Test
public void blockComment() {
CharSequence rewrited = PostprocessingMachine.rewrite(
LINES.join("package start;",
"import java.util.List;",
"class My extends java.util.Set {",
"/* class name in block comment java.until.Map.get()*/",
"/**",
"class name in block comment java.until.Map.get()",
"**/",
"}"));

check(rewrited).hasToString(
LINES.join("package start;",
"import java.util.List;",
"import java.util.Set;",
"class My extends Set {",
"/* class name in block comment java.until.Map.get()*/",
"/**",
"class name in block comment java.until.Map.get()",
"**/",
"}"));
}

@Test
Expand All @@ -53,7 +78,6 @@ public void javaLangImports() {
}

@Test
@Ignore
public void importsNoPlaceholders() {
CharSequence rewrited = PostprocessingMachine.rewrite(
"class My extends java.util.Set {}");
Expand All @@ -72,5 +96,4 @@ public void importsNoPlaceholders() {
"import java.util.Set;",
"class My extends Set {}"));
}

}

0 comments on commit 66cb90b

Please sign in to comment.