|
46 | 46 | import io.spring.initializr.generator.language.SourceCodeWriter;
|
47 | 47 | import io.spring.initializr.generator.language.SourceStructure;
|
48 | 48 |
|
| 49 | +import org.springframework.util.CollectionUtils; |
| 50 | + |
49 | 51 | /**
|
50 | 52 | * A {@link SourceCodeWriter} that writes {@link SourceCode} in Kotlin.
|
51 | 53 | *
|
@@ -95,9 +97,14 @@ private void writeTo(SourceStructure structure, KotlinCompilationUnit compilatio
|
95 | 97 | writeAnnotations(writer, type);
|
96 | 98 | writeModifiers(writer, type.getModifiers());
|
97 | 99 | writer.print("class " + type.getName());
|
98 |
| - if (type.getExtends() != null) { |
| 100 | + boolean hasExtends = type.getExtends() != null; |
| 101 | + if (hasExtends) { |
99 | 102 | writer.print(" : " + getUnqualifiedName(type.getExtends()) + "()");
|
100 | 103 | }
|
| 104 | + if (!CollectionUtils.isEmpty(type.getImplements())) { |
| 105 | + writer.print(hasExtends ? ", " : " : "); |
| 106 | + writeImplements(type, writer); |
| 107 | + } |
101 | 108 | List<KotlinPropertyDeclaration> propertyDeclarations = type.getPropertyDeclarations();
|
102 | 109 | List<KotlinFunctionDeclaration> functionDeclarations = type.getFunctionDeclarations();
|
103 | 110 | boolean hasDeclarations = !propertyDeclarations.isEmpty() || !functionDeclarations.isEmpty();
|
@@ -136,6 +143,17 @@ private void writeTo(SourceStructure structure, KotlinCompilationUnit compilatio
|
136 | 143 | }
|
137 | 144 | }
|
138 | 145 |
|
| 146 | + private void writeImplements(KotlinTypeDeclaration type, IndentingWriter writer) { |
| 147 | + Iterator<String> iterator = type.getImplements().iterator(); |
| 148 | + while (iterator.hasNext()) { |
| 149 | + String name = iterator.next(); |
| 150 | + writer.print(getUnqualifiedName(name)); |
| 151 | + if (iterator.hasNext()) { |
| 152 | + writer.print(", "); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
139 | 157 | private String escapeKotlinKeywords(String packageName) {
|
140 | 158 | return Arrays.stream(packageName.split("\\."))
|
141 | 159 | .map((segment) -> this.language.isKeyword(segment) ? "`" + segment + "`" : segment)
|
@@ -240,6 +258,7 @@ private Set<String> determineImports(KotlinCompilationUnit compilationUnit) {
|
240 | 258 | List<String> imports = new ArrayList<>();
|
241 | 259 | for (KotlinTypeDeclaration typeDeclaration : compilationUnit.getTypeDeclarations()) {
|
242 | 260 | imports.add(typeDeclaration.getExtends());
|
| 261 | + imports.addAll(typeDeclaration.getImplements()); |
243 | 262 | imports.addAll(appendImports(typeDeclaration.annotations().values(), Annotation::getImports));
|
244 | 263 | typeDeclaration.getPropertyDeclarations()
|
245 | 264 | .forEach(((propertyDeclaration) -> imports.addAll(determinePropertyImports(propertyDeclaration))));
|
|
0 commit comments