private MethodSpec(Builder builder) { CodeBlock code = builder.code.build(); checkArgument(code.isEmpty() || !builder.modifiers.contains(Modifier.ABSTRACT), "abstract method %s cannot have code", builder.name); checkArgument(!builder.varargs || lastParameterIsArray(builder.parameters), "last parameter of varargs method %s must be an array", builder.name); this.name = checkNotNull(builder.name, "name == null"); this.javadoc = builder.javadoc.build(); this.annotations = Util.immutableList(builder.annotations); this.modifiers = Util.immutableSet(builder.modifiers); this.typeVariables = Util.immutableList(builder.typeVariables); this.returnType = builder.returnType; this.parameters = Util.immutableList(builder.parameters); this.varargs = builder.varargs; this.exceptions = Util.immutableList(builder.exceptions); this.defaultValue = builder.defaultValue; this.code = code; }
public Builder addModifiers(Modifier... modifiers) { checkState(anonymousTypeArguments == null, "forbidden on anonymous types."); for (Modifier modifier : modifiers) { checkArgument(modifier != null, "modifiers contain null"); this.modifiers.add(modifier); } return this; }
private TypeSpec(Builder builder) { this.kind = builder.kind; this.name = builder.name; this.anonymousTypeArguments = builder.anonymousTypeArguments; this.javadoc = builder.javadoc.build(); this.annotations = Util.immutableList(builder.annotations); this.modifiers = Util.immutableSet(builder.modifiers); this.typeVariables = Util.immutableList(builder.typeVariables); this.superclass = builder.superclass; this.superinterfaces = Util.immutableList(builder.superinterfaces); this.enumConstants = Util.immutableMap(builder.enumConstants); this.fieldSpecs = Util.immutableList(builder.fieldSpecs); this.staticBlock = builder.staticBlock.build(); this.initializerBlock = builder.initializerBlock.build(); this.methodSpecs = Util.immutableList(builder.methodSpecs); this.typeSpecs = Util.immutableList(builder.typeSpecs); List<Element> originatingElementsMutable = new ArrayList<>(); originatingElementsMutable.addAll(builder.originatingElements); for (TypeSpec typeSpec : builder.typeSpecs) { originatingElementsMutable.addAll(typeSpec.originatingElements); } this.originatingElements = Util.immutableList(originatingElementsMutable); }
public CodeWriter pushPackage(String packageName) { checkState(this.packageName == NO_PACKAGE, "package already set: %s", this.packageName); this.packageName = checkNotNull(packageName, "packageName == null"); return this; }
private ParameterSpec(Builder builder) { this.name = checkNotNull(builder.name, "name == null"); this.annotations = Util.immutableList(builder.annotations); this.modifiers = Util.immutableSet(builder.modifiers); this.type = checkNotNull(builder.type, "type == null"); this.javadoc = builder.javadoc.build(); }
private ParameterizedTypeName(ParameterizedTypeName enclosingType, ClassName rawType, List<TypeName> typeArguments, List<AnnotationSpec> annotations) { super(annotations); this.rawType = checkNotNull(rawType, "rawType == null").annotated(annotations); this.enclosingType = enclosingType; this.typeArguments = Util.immutableList(typeArguments); checkArgument(!this.typeArguments.isEmpty() || enclosingType != null, "no type arguments: %s", rawType); for (TypeName typeArgument : this.typeArguments) { checkArgument(!typeArgument.isPrimitive() && typeArgument != VOID, "invalid type parameter: %s", typeArgument); } }
private TypeVariableName(String name, List<TypeName> bounds, List<AnnotationSpec> annotations) { super(annotations); this.name = checkNotNull(name, "name == null"); this.bounds = bounds; for (TypeName bound : this.bounds) { checkArgument(!bound.isPrimitive() && bound != VOID, "invalid bound: %s", bound); } }
public Builder addField(FieldSpec fieldSpec) { if (kind == Kind.INTERFACE || kind == Kind.ANNOTATION) { requireExactlyOneOf(fieldSpec.modifiers, Modifier.PUBLIC, Modifier.PRIVATE); Set<Modifier> check = EnumSet.of(Modifier.STATIC, Modifier.FINAL); checkState(fieldSpec.modifiers.containsAll(check), "%s %s.%s requires modifiers %s", kind, name, fieldSpec.name, check); } fieldSpecs.add(fieldSpec); return this; }
private ClassName(List<String> names, List<AnnotationSpec> annotations) { super(annotations); for (int i = 1; i < names.size(); i++) { checkArgument(SourceVersion.isName(names.get(i)), "part '%s' is keyword", names.get(i)); } this.names = Util.immutableList(names); this.canonicalName = (names.get(0).isEmpty() ? Util.join(".", names.subList(1, names.size())) : Util.join(".", names)); }
public Builder addMethod(MethodSpec methodSpec) { if (kind == Kind.INTERFACE) { requireExactlyOneOf(methodSpec.modifiers, Modifier.ABSTRACT, Modifier.STATIC, Util.DEFAULT); requireExactlyOneOf(methodSpec.modifiers, Modifier.PUBLIC, Modifier.PRIVATE); } else if (kind == Kind.ANNOTATION) { checkState(methodSpec.modifiers.equals(kind.implicitMethodModifiers), "%s %s.%s requires modifiers %s", kind, name, methodSpec.name, kind.implicitMethodModifiers); } if (kind != Kind.ANNOTATION) { checkState(methodSpec.defaultValue == null, "%s %s.%s cannot have a default value", kind, name, methodSpec.name); } if (kind != Kind.INTERFACE) { checkState(!hasDefaultModifier(methodSpec.modifiers), "%s %s.%s cannot be default", kind, name, methodSpec.name); } methodSpecs.add(methodSpec); return this; }
public CodeWriter unindent(int levels) { checkArgument(indentLevel - levels >= 0, "cannot unindent %s from %s", levels, indentLevel); indentLevel -= levels; return this; }
/** * Delegates to {@link #addMember(String, String, Object...)}, with parameter {@code format} * depending on the given {@code value} object. Falls back to {@code "$L"} literal format if * the class of the given {@code value} object is not supported. */ Builder addMemberForValue(String memberName, Object value) { checkNotNull(memberName, "memberName == null"); checkNotNull(value, "value == null, constant non-null value expected for %s", memberName); checkArgument(SourceVersion.isName(memberName), "not a valid name: %s", memberName); if (value instanceof Class<?>) { return addMember(memberName, "$T.class", value); } if (value instanceof Enum) { return addMember(memberName, "$T.$L", value.getClass(), ((Enum<?>) value).name()); } if (value instanceof String) { return addMember(memberName, "$S", value); } if (value instanceof Float) { return addMember(memberName, "$Lf", value); } if (value instanceof Character) { return addMember(memberName, "'$L'", characterLiteralWithoutSingleQuotes((char) value)); } return addMember(memberName, "$L", value); }
LineWrapper(Appendable out, String indent, int columnLimit) { checkNotNull(out, "out == null"); this.out = out; this.indent = indent; this.columnLimit = columnLimit; }
? stringLiteralWithDoubleQuotes(string, indent) : "null"); break; ClassName candidate = (ClassName) typeName; if (staticImportClassNames.contains(candidate.canonicalName)) { checkState(deferredTypeName == null, "pending type for static import?!"); deferredTypeName = candidate; break; checkState(statementLine == -1, "statement enter $[ followed by statement enter $["); statementLine = 0; 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.
private TypeName(String keyword, List<AnnotationSpec> annotations) { this.keyword = keyword; this.annotations = Util.immutableList(annotations); }
/** * Delegates to {@link #addMember(String, String, Object...)}, with parameter {@code format} * depending on the given {@code value} object. Falls back to {@code "$L"} literal format if * the class of the given {@code value} object is not supported. */ Builder addMemberForValue(String memberName, Object value) { checkNotNull(memberName, "memberName == null"); checkNotNull(value, "value == null, constant non-null value expected for %s", memberName); if (value instanceof Class<?>) { return addMember(memberName, "$T.class", value); } if (value instanceof Enum) { return addMember(memberName, "$T.$L", value.getClass(), ((Enum<?>) value).name()); } if (value instanceof String) { return addMember(memberName, "$S", value); } if (value instanceof Float) { return addMember(memberName, "$Lf", value); } if (value instanceof Character) { return addMember(memberName, "'$L'", characterLiteralWithoutSingleQuotes((char) value)); } return addMember(memberName, "$L", value); }
private WildcardTypeName(List<TypeName> upperBounds, List<TypeName> lowerBounds, List<AnnotationSpec> annotations) { super(annotations); this.upperBounds = Util.immutableList(upperBounds); this.lowerBounds = Util.immutableList(lowerBounds); checkArgument(this.upperBounds.size() == 1, "unexpected extends bounds: %s", upperBounds); for (TypeName upperBound : this.upperBounds) { checkArgument(!upperBound.isPrimitive() && upperBound != VOID, "invalid upper bound: %s", upperBound); } for (TypeName lowerBound : this.lowerBounds) { checkArgument(!lowerBound.isPrimitive() && lowerBound != VOID, "invalid lower bound: %s", lowerBound); } }
public CodeWriter popPackage() { checkState(this.packageName != NO_PACKAGE, "package not set"); this.packageName = NO_PACKAGE; return this; }
private JavaFile(Builder builder) { this.fileComment = builder.fileComment.build(); this.packageName = builder.packageName; this.typeSpec = builder.typeSpec; this.skipJavaLangImports = builder.skipJavaLangImports; this.staticImports = Util.immutableSet(builder.staticImports); this.indent = builder.indent; }
private AnnotationSpec(Builder builder) { this.type = builder.type; this.members = Util.immutableMultimap(builder.members); }