Skip to content

Commit

Permalink
fix building rewrited content
Browse files Browse the repository at this point in the history
  • Loading branch information
ivysharev committed Feb 4, 2015
1 parent 8044c39 commit 7c31f60
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
22 changes: 16 additions & 6 deletions generator/src/org/immutables/generator/PostprocessingMachine.java
Expand Up @@ -5,9 +5,11 @@
import com.google.common.base.Optional;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.TreeSet;

final class PostprocessingMachine {
Expand Down Expand Up @@ -72,6 +74,7 @@ static CharSequence rewrite(CharSequence content) {
if (classNameOccurrencesInImportBlock == 1) {
importsBuilder.addToStopList(content.subSequence(classNameFrom, classNameTo).toString());
nameMachine.reset();
classNameOccurrencesInImportBlock = 0;
}
}
break;
Expand Down Expand Up @@ -105,7 +108,7 @@ static CharSequence rewrite(CharSequence content) {

StringBuilder stringBuilder = new StringBuilder(content.length() << 1);

for (ImportCandidate importCandidate : importsBuilder.importCandidates.values()) {
for (ImportCandidate importCandidate : importsBuilder.candidates()) {
if (importCandidate.importTo != -1) {
importsBuilder.addImport(importCandidate.preparedImport);
}
Expand All @@ -123,7 +126,7 @@ static CharSequence rewrite(CharSequence content) {

// package
if (!currentPackage.isEmpty()) {
stringBuilder.insert(0, ";\n").insert(0, currentPackage).insert(0, "package ");
stringBuilder.insert(0, ";\n\n").insert(0, currentPackage).insert(0, "package ");
}

return stringBuilder.toString();
Expand Down Expand Up @@ -200,7 +203,7 @@ static final class ImportsBuilder {

private TreeSet<String> imports = Sets.newTreeSet();
private Optional<String> currentPackage = Optional.absent();
private LinkedHashMap<String, ImportCandidate> importCandidates = Maps.newLinkedHashMap();
private HashMap<String, ImportCandidate> importCandidates = Maps.newHashMap();
private HashSet<String> exceptions = Sets.newHashSet();
private HashSet<String> stopList = Sets.newHashSet();

Expand Down Expand Up @@ -245,6 +248,10 @@ void preBuild() {
String build() {
return JOINER.join(Iterables.transform(imports, ToImportStatement.FUNCTION));
}

List<ImportCandidate> candidates() {
return Ordering.natural().sortedCopy(importCandidates.values());
}
}

private enum ToImportStatement implements Function<String, String> {
Expand Down Expand Up @@ -447,8 +454,7 @@ enum ClassNameState {
CLASS_NAME
}

private static final class ImportCandidate {

private static final class ImportCandidate implements Comparable<ImportCandidate> {
final int importFrom;
final int importTo;
final int packageTo;
Expand All @@ -461,6 +467,10 @@ private ImportCandidate(int importFrom, int importTo, int packageTo, String prep
this.preparedImport = preparedImport;
}

@Override
public int compareTo(ImportCandidate other) {
return this.importFrom - other.importFrom;
}
}

private static boolean isDigit(char c) {
Expand Down
Expand Up @@ -2,6 +2,7 @@
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 @@ -20,6 +21,7 @@ public void imports() {

check(rewrited).hasToString(
LINES.join("package start;",
"",
"import com.google.common.collect.Maps;",
"import java.util.List;",
"import java.util.Map;",
Expand All @@ -42,6 +44,7 @@ public void lineComment() {

check(rewrited).hasToString(
LINES.join("package start;",
"",
"import java.util.List;",
"import java.util.Set;",
"class My extends Set {",
Expand All @@ -64,6 +67,7 @@ public void blockComment() {

check(rewrited).hasToString(
LINES.join("package start;",
"",
"import java.util.List;",
"import java.util.Set;",
"class My extends Set {",
Expand All @@ -85,6 +89,7 @@ public void stringLiteral() {

check(rewrited).hasToString(
LINES.join("package start;",
"",
"import java.util.List;",
"import java.util.Set;",
"class My extends Set {",
Expand All @@ -100,6 +105,7 @@ public void javaLangImports() {

check(rewrited).hasToString(
LINES.join("package start;",
"",
"class My extends Throwable {}"));

rewrited = PostprocessingMachine.rewrite(
Expand All @@ -108,6 +114,7 @@ public void javaLangImports() {

check(rewrited).hasToString(
LINES.join("package start;",
"",
"class Throwable extends java.lang.Throwable {}"));
}

Expand All @@ -119,6 +126,7 @@ public void currentPackageImport() {

check(rewrited).hasToString(
LINES.join("package start;",
"",
"class My extends Utils {}"));

rewrited = PostprocessingMachine.rewrite(
Expand All @@ -129,6 +137,7 @@ public void currentPackageImport() {

check(rewrited).hasToString(
LINES.join("package start;",
"",
"class Throwable extends start.Utils {",
" private class Utils {}",
"}"));
Expand Down Expand Up @@ -194,6 +203,7 @@ public void keepClassModifiers() {

check(rewrited).hasToString(LINES.join(
"package mypack;",
"",
"private final class My{}"));

rewrited = PostprocessingMachine.rewrite(LINES.join(
Expand All @@ -211,6 +221,7 @@ public void keepClassModifiers() {

check(rewrited).hasToString(LINES.join(
"package mypack;",
"",
"import java.util.List;",
"abstract class My{}"));

Expand Down

0 comments on commit 7c31f60

Please sign in to comment.