aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/squareup/javawriter/builders/CodeWriter.java
blob: 005b666f31acbc7fba4146c971b956e3cbb2c438 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2015 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.squareup.javawriter.builders;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.squareup.javawriter.ClassName;
import com.squareup.javawriter.StringLiteral;
import com.squareup.javawriter.TypeName;
import com.squareup.javawriter.TypeNames;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

/**
 * Converts a {@link JavaFile} to a string suitable to both human- and javac-consumption. This
 * honors imports, indentation, and deferred variable names.
 */
class CodeWriter {
  private final String indent = "  ";
  private final StringBuilder out;
  private final ImmutableMap<ClassName, String> importedTypes;
  private final LinkedHashSet<TypeName> emittedTypes = new LinkedHashSet<>();
  private final List<TypeName> visibleTypes = new ArrayList<>();
  private int indentLevel;

  public CodeWriter(StringBuilder out, ImmutableMap<ClassName, String> importedTypes) {
    this.out = checkNotNull(out);
    this.importedTypes = checkNotNull(importedTypes);
  }

  public CodeWriter indent() {
    indentLevel++;
    return this;
  }

  public CodeWriter unindent() {
    checkState(indentLevel > 0);
    indentLevel--;
    return this;
  }

  public CodeWriter pushVisibleType(TypeName typeName) {
    visibleTypes.add(typeName);
    return this;
  }

  public CodeWriter popVisibleType(TypeName typeName) {
    checkState(visibleTypes.remove(typeName));
    return this;
  }

  public CodeWriter emit(String format, Object... args) {
    return emit(new Snippet(format, args));
  }

  public CodeWriter emit(Snippet snippet) {
    int a = 0;
    for (String part : snippet.formatParts) {
      switch (part) {
        case "$L":
          emitAndIndent(String.valueOf(snippet.args.get(a++)));
          break;

        case "$S":
          String arg = String.valueOf(snippet.args.get(a++));
          emitAndIndent(StringLiteral.forValue(arg).literal());
          break;

        case "$T":
          emitType(snippet.args.get(a++));
          break;

        case "$$":
          emitAndIndent("$");
          break;

        default:
          emitAndIndent(part);
          break;
      }
    }
    return this;
  }

  private void emitType(Object arg) {
    TypeName typeName = toTypeName(arg);
    emittedTypes.add(typeName);

    String shortName = !visibleTypes.contains(typeName)
        ? importedTypes.get(typeName)
        : null;

    emitAndIndent(shortName != null ? shortName : typeName.toString());
  }

  /** Emits {@code s} with indentation as required. */
  private void emitAndIndent(String s) {
    boolean first = true;
    for (String line : s.split("\n", -1)) {
      if (!first) out.append('\n');
      first = false;
      if (line.isEmpty()) continue; // Don't indent empty lines.
      emitIndentationIfNecessary();
      out.append(line);
    }
  }

  private void emitIndentationIfNecessary() {
    // Only emit indentation immediately after a '\n' character.
    if (out.length() <= 0 || out.charAt(out.length() - 1) != '\n') return;

    for (int j = 0; j < indentLevel; j++) {
      out.append(indent);
    }
  }

  private TypeName toTypeName(Object arg) {
    if (arg instanceof TypeName) return (TypeName) arg;
    if (arg instanceof Class<?>) return TypeNames.forClass((Class<?>) arg);
    throw new IllegalArgumentException("Expected type but was " + arg);
  }

  /**
   * Returns the types that should have been imported for this code. If there were any simple name
   * collisions, that type's first use is imported.
   */
  ImmutableMap<ClassName, String> suggestedImports() {
    // Find the simple names that can be imported, and the classes that they target.
    Map<String, ClassName> simpleNameToType = new LinkedHashMap<>();
    for (TypeName typeName : emittedTypes) {
      if (!(typeName instanceof ClassName)) continue;
      ClassName className = (ClassName) typeName;
      if (simpleNameToType.containsKey(className.simpleName())) continue;
      simpleNameToType.put(className.simpleName(), className);
    }

    // Invert the map.
    ImmutableSortedMap.Builder<ClassName, String> typeToSimpleName
        = ImmutableSortedMap.naturalOrder();
    for (Map.Entry<String, ClassName> entry : simpleNameToType.entrySet()) {
      typeToSimpleName.put(entry.getValue(), entry.getKey());
    }

    // TODO(jwilson): omit imports from java.lang, unless their simple names is also present in the
    //     current class's package. (Yuck.)

    return typeToSimpleName.build();
  }
}