@Override public String toString() { StringBuilder out = new StringBuilder(); try { new CodeWriter(out).emit(this); return out.toString(); } catch (IOException e) { throw new AssertionError(); } }
switch (part) { case "$L": emitLiteral(codeBlock.args.get(a++)); break; emitAndIndent((String) codeBlock.args.get(a++)); break; emitAndIndent(string != null ? stringLiteralWithDoubleQuotes(string, indent) : "null"); emitAndIndent("$"); break; indent(); break; unindent(); break; checkState(statementLine != -1, "statement exit $] has no matching statement enter $["); if (statementLine > 0) { unindent(2); // End a multi-line statement. Decrease the indentation level. if (emitStaticImportMember(deferredTypeName.canonicalName, part)) { emitAndIndent(part); break;
codeWriter.emitJavadoc(javadoc); codeWriter.emitAnnotations(annotations, false); codeWriter.emit("$L", enumName); if (!anonymousTypeArguments.formatParts.isEmpty()) { codeWriter.emit("("); codeWriter.emit(anonymousTypeArguments); codeWriter.emit(")"); codeWriter.emit(" {\n"); } else if (anonymousTypeArguments != null) { TypeName supertype = !superinterfaces.isEmpty() ? superinterfaces.get(0) : superclass; codeWriter.emit("new $T(", supertype); codeWriter.emit(anonymousTypeArguments); codeWriter.emit(") {\n"); } else { codeWriter.pushType(new TypeSpec(this)); codeWriter.emitJavadoc(javadoc); codeWriter.emitAnnotations(annotations, false); codeWriter.emitModifiers(modifiers, Util.union(implicitModifiers, kind.asMemberModifiers)); if (kind == Kind.ANNOTATION) { codeWriter.emit("$L $L", "@interface", name); } else { codeWriter.emit("$L $L", kind.name().toLowerCase(Locale.US), name); codeWriter.emitTypeVariables(typeVariables); codeWriter.emit(" extends");
void emit(CodeWriter codeWriter, Set<Modifier> implicitModifiers) throws IOException { codeWriter.emitJavadoc(javadoc); codeWriter.emitAnnotations(annotations, false); codeWriter.emitModifiers(modifiers, implicitModifiers); codeWriter.emit("$T $L", type, name); if (!initializer.isEmpty()) { codeWriter.emit(" = "); codeWriter.emit(initializer); } codeWriter.emit(";\n"); }
private void emitAnnotationValues(CodeWriter codeWriter, String whitespace, String memberSeparator, List<CodeBlock> values) throws IOException { if (values.size() == 1) { codeWriter.indent(2); codeWriter.emit(values.get(0)); codeWriter.unindent(2); return; } codeWriter.emit("{" + whitespace); codeWriter.indent(2); boolean first = true; for (CodeBlock codeBlock : values) { if (!first) codeWriter.emit(memberSeparator); codeWriter.emit(codeBlock); first = false; } codeWriter.unindent(2); codeWriter.emit(whitespace + "}"); }
void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitModifiers) throws IOException { codeWriter.emitJavadoc(javadocWithParameters()); codeWriter.emitAnnotations(annotations, false); codeWriter.emitModifiers(modifiers, implicitModifiers); codeWriter.emitTypeVariables(typeVariables); codeWriter.emit(" "); codeWriter.emit("$L($Z", enclosingName); } else { codeWriter.emit("$T $L($Z", returnType, name); for (Iterator<ParameterSpec> i = parameters.iterator(); i.hasNext(); ) { ParameterSpec parameter = i.next(); if (!firstParameter) codeWriter.emit(",").emitWrappingSpace(); parameter.emit(codeWriter, !i.hasNext() && varargs); firstParameter = false; codeWriter.emit(")"); codeWriter.emit(" default "); codeWriter.emit(defaultValue); codeWriter.emitWrappingSpace().emit("throws"); boolean firstException = true; for (TypeName exception : exceptions) { if (!firstException) codeWriter.emit(","); codeWriter.emitWrappingSpace().emit("$T", exception); firstException = false;
private void emit(CodeWriter codeWriter) throws IOException { codeWriter.pushPackage(packageName); if (!fileComment.isEmpty()) { codeWriter.emitComment(fileComment); } if (!packageName.isEmpty()) { codeWriter.emit("package $L;\n", packageName); codeWriter.emit("\n"); } if (!staticImports.isEmpty()) { for (String signature : staticImports) { codeWriter.emit("import static $L;\n", signature); } codeWriter.emit("\n"); } int importedTypesCount = 0; for (ClassName className : new TreeSet<>(codeWriter.importedTypes().values())) { if (skipJavaLangImports && className.packageName().equals("java.lang")) continue; codeWriter.emit("import $L;\n", className.withoutAnnotations()); importedTypesCount++; } if (importedTypesCount > 0) { codeWriter.emit("\n"); } typeSpec.emit(codeWriter, null, Collections.emptySet()); codeWriter.popPackage(); }
CodeWriter emit(CodeWriter out) throws IOException { if (keyword == null) throw new AssertionError(); if (isAnnotated()) { out.emit(""); emitAnnotations(out); } return out.emitAndIndent(keyword); }
void emit(CodeWriter codeWriter, boolean varargs) throws IOException { codeWriter.emitAnnotations(annotations, true); codeWriter.emitModifiers(modifiers); if (varargs) { TypeName.asArray(type).emit(codeWriter, true); } else { type.emit(codeWriter); } codeWriter.emit(" $L", name); }
if (charsEmitted) { out.emit("."); simpleName = className.simpleName; String qualifiedName = out.lookupName(className); int dot = qualifiedName.lastIndexOf('.'); if (dot != -1) { out.emitAndIndent(qualifiedName.substring(0, dot + 1)); simpleName = qualifiedName.substring(dot + 1); charsEmitted = true; if (charsEmitted) out.emit(" "); className.emitAnnotations(out); out.emit(simpleName); charsEmitted = true;
@Override public String toString() { StringBuilder out = new StringBuilder(); try { CodeWriter codeWriter = new CodeWriter(out); emit(codeWriter, null, Collections.emptySet()); return out.toString(); } catch (IOException e) { throw new AssertionError(); } }
public void writeTo(Appendable out) throws IOException { // First pass: emit the entire class, just to collect the types we'll need to import. CodeWriter importsCollector = new CodeWriter(NULL_APPENDABLE, indent, staticImports); emit(importsCollector); Map<String, ClassName> suggestedImports = importsCollector.suggestedImports(); // Second pass: write the code, taking advantage of the imports. CodeWriter codeWriter = new CodeWriter(out, indent, suggestedImports, staticImports); emit(codeWriter); }
private boolean emitStaticImportMember(String canonical, String part) throws IOException { String partWithoutLeadingDot = part.substring(1); if (partWithoutLeadingDot.isEmpty()) return false; char first = partWithoutLeadingDot.charAt(0); if (!Character.isJavaIdentifierStart(first)) return false; String explicit = canonical + "." + extractMemberName(partWithoutLeadingDot); String wildcard = canonical + ".*"; if (staticImports.contains(explicit) || staticImports.contains(wildcard)) { emitAndIndent(partWithoutLeadingDot); return true; } return false; }
/** * Emit type variables with their bounds. This should only be used when declaring type variables; * everywhere else bounds are omitted. */ public void emitTypeVariables(List<TypeVariableName> typeVariables) throws IOException { if (typeVariables.isEmpty()) return; typeVariables.forEach(typeVariable -> currentTypeVariables.add(typeVariable.name)); emit("<"); boolean firstTypeVariable = true; for (TypeVariableName typeVariable : typeVariables) { if (!firstTypeVariable) emit(", "); emitAnnotations(typeVariable.annotations, true); emit("$L", typeVariable.name); boolean firstBound = true; for (TypeName bound : typeVariable.bounds) { emit(firstBound ? " extends $T" : " & $T", bound); firstBound = false; } firstTypeVariable = false; } emit(">"); }
emitIndentation(); out.append(javadoc ? " *" : "//"); if (statementLine != -1) { if (statementLine == 0) { indent(2); // Begin multiple-line statement. Increase the indentation level. emitIndentation(); if (javadoc) { out.append(" * ");
public CodeWriter emit(String s) throws IOException { return emitAndIndent(s); }
@Override CodeWriter emit(CodeWriter out) throws IOException { return out.emitAndIndent(out.lookupName(this)); } }
public void emitModifiers(Set<Modifier> modifiers) throws IOException { emitModifiers(modifiers, Collections.emptySet()); }
void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitModifiers) throws IOException { codeWriter.emitJavadoc(javadoc); codeWriter.emitAnnotations(annotations, false); codeWriter.emitModifiers(modifiers, implicitModifiers); codeWriter.emitTypeVariables(typeVariables); codeWriter.emit(" "); codeWriter.emit("$L($Z", enclosingName); } else { codeWriter.emit("$T $L($Z", returnType, name); for (Iterator<ParameterSpec> i = parameters.iterator(); i.hasNext(); ) { ParameterSpec parameter = i.next(); if (!firstParameter) codeWriter.emit(",").emitWrappingSpace(); parameter.emit(codeWriter, !i.hasNext() && varargs); firstParameter = false; codeWriter.emit(")"); codeWriter.emit(" default "); codeWriter.emit(defaultValue); codeWriter.emitWrappingSpace().emit("throws"); boolean firstException = true; for (TypeName exception : exceptions) { if (!firstException) codeWriter.emit(","); codeWriter.emitWrappingSpace().emit("$T", exception); firstException = false;