mirror of
https://gitee.com/dcren/initializr.git
synced 2025-05-15 20:49:38 +08:00
Merge pull request #881 from mattyb678
* pr/881: Polish "Adding field declaration for code generation" Adding field declaration for code generation Closes gh-881
This commit is contained in:
commit
74e0bcc3a5
@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 io.spring.initializr.generator.language.groovy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.language.Annotatable;
|
||||
import io.spring.initializr.generator.language.Annotation;
|
||||
|
||||
/**
|
||||
* Declaration of a field written in Groovy.
|
||||
*
|
||||
* @author Matt Berteaux
|
||||
*/
|
||||
public final class GroovyFieldDeclaration implements Annotatable {
|
||||
|
||||
private final List<Annotation> annotations = new ArrayList<>();
|
||||
|
||||
private final int modifiers;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String returnType;
|
||||
|
||||
private final Object value;
|
||||
|
||||
private final boolean initialized;
|
||||
|
||||
private GroovyFieldDeclaration(Builder builder) {
|
||||
this.modifiers = builder.modifiers;
|
||||
this.name = builder.name;
|
||||
this.returnType = builder.returnType;
|
||||
this.value = builder.value;
|
||||
this.initialized = builder.initialized;
|
||||
}
|
||||
|
||||
public static Builder field(String name) {
|
||||
return new Builder(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void annotate(Annotation annotation) {
|
||||
this.annotations.add(annotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Annotation> getAnnotations() {
|
||||
return Collections.unmodifiableList(this.annotations);
|
||||
}
|
||||
|
||||
public int getModifiers() {
|
||||
return this.modifiers;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getReturnType() {
|
||||
return this.returnType;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public boolean isInitialized() {
|
||||
return this.initialized;
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
|
||||
private final String name;
|
||||
|
||||
private String returnType;
|
||||
|
||||
private int modifiers;
|
||||
|
||||
private Object value;
|
||||
|
||||
private boolean initialized;
|
||||
|
||||
private Builder(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Builder modifiers(int modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder value(Object value) {
|
||||
this.value = value;
|
||||
this.initialized = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public GroovyFieldDeclaration returning(String returnType) {
|
||||
this.returnType = returnType;
|
||||
return new GroovyFieldDeclaration(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -46,11 +46,14 @@ import io.spring.initializr.generator.language.SourceCodeWriter;
|
||||
* A {@link SourceCodeWriter} that writes {@link SourceCode} in Groovy.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Matt Berteaux
|
||||
*/
|
||||
public class GroovySourceCodeWriter implements SourceCodeWriter<GroovySourceCode> {
|
||||
|
||||
private static final Map<Predicate<Integer>, String> TYPE_MODIFIERS;
|
||||
|
||||
private static final Map<Predicate<Integer>, String> FIELD_MODIFIERS;
|
||||
|
||||
private static final Map<Predicate<Integer>, String> METHOD_MODIFIERS;
|
||||
|
||||
static {
|
||||
@ -62,6 +65,15 @@ public class GroovySourceCodeWriter implements SourceCodeWriter<GroovySourceCode
|
||||
typeModifiers.put(Modifier::isFinal, "final");
|
||||
typeModifiers.put(Modifier::isStrict, "strictfp");
|
||||
TYPE_MODIFIERS = typeModifiers;
|
||||
Map<Predicate<Integer>, String> fieldModifiers = new LinkedHashMap<>();
|
||||
fieldModifiers.put(Modifier::isPublic, "public");
|
||||
fieldModifiers.put(Modifier::isProtected, "protected");
|
||||
fieldModifiers.put(Modifier::isPrivate, "private");
|
||||
fieldModifiers.put(Modifier::isStatic, "static");
|
||||
fieldModifiers.put(Modifier::isFinal, "final");
|
||||
fieldModifiers.put(Modifier::isTransient, "transient");
|
||||
fieldModifiers.put(Modifier::isVolatile, "volatile");
|
||||
FIELD_MODIFIERS = fieldModifiers;
|
||||
Map<Predicate<Integer>, String> methodModifiers = new LinkedHashMap<>(typeModifiers);
|
||||
methodModifiers.put(Modifier::isSynchronized, "synchronized");
|
||||
methodModifiers.put(Modifier::isNative, "native");
|
||||
@ -107,6 +119,14 @@ public class GroovySourceCodeWriter implements SourceCodeWriter<GroovySourceCode
|
||||
}
|
||||
writer.println(" {");
|
||||
writer.println();
|
||||
List<GroovyFieldDeclaration> fieldDeclarations = type.getFieldDeclarations();
|
||||
if (!fieldDeclarations.isEmpty()) {
|
||||
writer.indented(() -> {
|
||||
for (GroovyFieldDeclaration fieldDeclaration : fieldDeclarations) {
|
||||
writeFieldDeclaration(writer, fieldDeclaration);
|
||||
}
|
||||
});
|
||||
}
|
||||
List<GroovyMethodDeclaration> methodDeclarations = type.getMethodDeclarations();
|
||||
if (!methodDeclarations.isEmpty()) {
|
||||
writer.indented(() -> {
|
||||
@ -165,6 +185,20 @@ public class GroovySourceCodeWriter implements SourceCodeWriter<GroovySourceCode
|
||||
return (values.size() > 1) ? "{ " + result + " }" : result;
|
||||
}
|
||||
|
||||
private void writeFieldDeclaration(IndentingWriter writer, GroovyFieldDeclaration fieldDeclaration) {
|
||||
writeAnnotations(writer, fieldDeclaration);
|
||||
writeModifiers(writer, FIELD_MODIFIERS, fieldDeclaration.getModifiers());
|
||||
writer.print(getUnqualifiedName(fieldDeclaration.getReturnType()));
|
||||
writer.print(" ");
|
||||
writer.print(fieldDeclaration.getName());
|
||||
if (fieldDeclaration.isInitialized()) {
|
||||
writer.print(" = ");
|
||||
writer.print(String.valueOf(fieldDeclaration.getValue()));
|
||||
}
|
||||
writer.println();
|
||||
writer.println();
|
||||
}
|
||||
|
||||
private void writeMethodDeclaration(IndentingWriter writer, GroovyMethodDeclaration methodDeclaration) {
|
||||
writeAnnotations(writer, methodDeclaration);
|
||||
writeModifiers(writer, METHOD_MODIFIERS, methodDeclaration.getModifiers());
|
||||
@ -230,6 +264,12 @@ public class GroovySourceCodeWriter implements SourceCodeWriter<GroovySourceCode
|
||||
imports.add(typeDeclaration.getExtends());
|
||||
}
|
||||
imports.addAll(getRequiredImports(typeDeclaration.getAnnotations(), this::determineImports));
|
||||
for (GroovyFieldDeclaration fieldDeclaration : typeDeclaration.getFieldDeclarations()) {
|
||||
if (requiresImport(fieldDeclaration.getReturnType())) {
|
||||
imports.add(fieldDeclaration.getReturnType());
|
||||
}
|
||||
imports.addAll(getRequiredImports(fieldDeclaration.getAnnotations(), this::determineImports));
|
||||
}
|
||||
for (GroovyMethodDeclaration methodDeclaration : typeDeclaration.getMethodDeclarations()) {
|
||||
if (requiresImport(methodDeclaration.getReturnType())) {
|
||||
imports.add(methodDeclaration.getReturnType());
|
||||
|
@ -30,6 +30,8 @@ public class GroovyTypeDeclaration extends TypeDeclaration {
|
||||
|
||||
private int modifiers;
|
||||
|
||||
private final List<GroovyFieldDeclaration> fieldDeclarations = new ArrayList<>();
|
||||
|
||||
private final List<GroovyMethodDeclaration> methodDeclarations = new ArrayList<>();
|
||||
|
||||
GroovyTypeDeclaration(String name) {
|
||||
@ -44,6 +46,14 @@ public class GroovyTypeDeclaration extends TypeDeclaration {
|
||||
return this.modifiers;
|
||||
}
|
||||
|
||||
public void addFieldDeclaration(GroovyFieldDeclaration fieldDeclaration) {
|
||||
this.fieldDeclarations.add(fieldDeclaration);
|
||||
}
|
||||
|
||||
public List<GroovyFieldDeclaration> getFieldDeclarations() {
|
||||
return this.fieldDeclarations;
|
||||
}
|
||||
|
||||
public void addMethodDeclaration(GroovyMethodDeclaration methodDeclaration) {
|
||||
this.methodDeclarations.add(methodDeclaration);
|
||||
}
|
||||
|
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 io.spring.initializr.generator.language.java;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.language.Annotatable;
|
||||
import io.spring.initializr.generator.language.Annotation;
|
||||
|
||||
/**
|
||||
* Declaration of a field written in Java.
|
||||
*
|
||||
* @author Matt Berteaux
|
||||
*/
|
||||
public final class JavaFieldDeclaration implements Annotatable {
|
||||
|
||||
private final List<Annotation> annotations = new ArrayList<>();
|
||||
|
||||
private final int modifiers;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String returnType;
|
||||
|
||||
private final Object value;
|
||||
|
||||
private final boolean initialized;
|
||||
|
||||
private JavaFieldDeclaration(Builder builder) {
|
||||
this.modifiers = builder.modifiers;
|
||||
this.name = builder.name;
|
||||
this.returnType = builder.returnType;
|
||||
this.value = builder.value;
|
||||
this.initialized = builder.initialized;
|
||||
}
|
||||
|
||||
public static Builder field(String name) {
|
||||
return new Builder(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void annotate(Annotation annotation) {
|
||||
this.annotations.add(annotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Annotation> getAnnotations() {
|
||||
return Collections.unmodifiableList(this.annotations);
|
||||
}
|
||||
|
||||
public int getModifiers() {
|
||||
return this.modifiers;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getReturnType() {
|
||||
return this.returnType;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public boolean isInitialized() {
|
||||
return this.initialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for creating a {@link JavaFieldDeclaration}.
|
||||
*/
|
||||
public static final class Builder {
|
||||
|
||||
private final String name;
|
||||
|
||||
private String returnType;
|
||||
|
||||
private int modifiers;
|
||||
|
||||
private Object value;
|
||||
|
||||
private boolean initialized;
|
||||
|
||||
private Builder(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Builder modifiers(int modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder value(Object value) {
|
||||
this.value = value;
|
||||
this.initialized = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaFieldDeclaration returning(String returnType) {
|
||||
this.returnType = returnType;
|
||||
return new JavaFieldDeclaration(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -46,11 +46,14 @@ import io.spring.initializr.generator.language.SourceCodeWriter;
|
||||
* A {@link SourceCodeWriter} that writes {@link SourceCode} in Java.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Matt Berteaux
|
||||
*/
|
||||
public class JavaSourceCodeWriter implements SourceCodeWriter<JavaSourceCode> {
|
||||
|
||||
private static final Map<Predicate<Integer>, String> TYPE_MODIFIERS;
|
||||
|
||||
private static final Map<Predicate<Integer>, String> FIELD_MODIFIERS;
|
||||
|
||||
private static final Map<Predicate<Integer>, String> METHOD_MODIFIERS;
|
||||
|
||||
static {
|
||||
@ -63,6 +66,15 @@ public class JavaSourceCodeWriter implements SourceCodeWriter<JavaSourceCode> {
|
||||
typeModifiers.put(Modifier::isFinal, "final");
|
||||
typeModifiers.put(Modifier::isStrict, "strictfp");
|
||||
TYPE_MODIFIERS = typeModifiers;
|
||||
Map<Predicate<Integer>, String> fieldModifiers = new LinkedHashMap<>();
|
||||
fieldModifiers.put(Modifier::isPublic, "public");
|
||||
fieldModifiers.put(Modifier::isProtected, "protected");
|
||||
fieldModifiers.put(Modifier::isPrivate, "private");
|
||||
fieldModifiers.put(Modifier::isStatic, "static");
|
||||
fieldModifiers.put(Modifier::isFinal, "final");
|
||||
fieldModifiers.put(Modifier::isTransient, "transient");
|
||||
fieldModifiers.put(Modifier::isVolatile, "volatile");
|
||||
FIELD_MODIFIERS = fieldModifiers;
|
||||
Map<Predicate<Integer>, String> methodModifiers = new LinkedHashMap<>(typeModifiers);
|
||||
methodModifiers.put(Modifier::isSynchronized, "synchronized");
|
||||
methodModifiers.put(Modifier::isNative, "native");
|
||||
@ -108,6 +120,14 @@ public class JavaSourceCodeWriter implements SourceCodeWriter<JavaSourceCode> {
|
||||
}
|
||||
writer.println(" {");
|
||||
writer.println();
|
||||
List<JavaFieldDeclaration> fieldDeclarations = type.getFieldDeclarations();
|
||||
if (!fieldDeclarations.isEmpty()) {
|
||||
writer.indented(() -> {
|
||||
for (JavaFieldDeclaration fieldDeclaration : fieldDeclarations) {
|
||||
writeFieldDeclaration(writer, fieldDeclaration);
|
||||
}
|
||||
});
|
||||
}
|
||||
List<JavaMethodDeclaration> methodDeclarations = type.getMethodDeclarations();
|
||||
if (!methodDeclarations.isEmpty()) {
|
||||
writer.indented(() -> {
|
||||
@ -166,6 +186,20 @@ public class JavaSourceCodeWriter implements SourceCodeWriter<JavaSourceCode> {
|
||||
return (values.size() > 1) ? "{ " + result + " }" : result;
|
||||
}
|
||||
|
||||
private void writeFieldDeclaration(IndentingWriter writer, JavaFieldDeclaration fieldDeclaration) {
|
||||
writeAnnotations(writer, fieldDeclaration);
|
||||
writeModifiers(writer, FIELD_MODIFIERS, fieldDeclaration.getModifiers());
|
||||
writer.print(getUnqualifiedName(fieldDeclaration.getReturnType()));
|
||||
writer.print(" ");
|
||||
writer.print(fieldDeclaration.getName());
|
||||
if (fieldDeclaration.isInitialized()) {
|
||||
writer.print(" = ");
|
||||
writer.print(String.valueOf(fieldDeclaration.getValue()));
|
||||
}
|
||||
writer.println(";");
|
||||
writer.println();
|
||||
}
|
||||
|
||||
private void writeMethodDeclaration(IndentingWriter writer, JavaMethodDeclaration methodDeclaration) {
|
||||
writeAnnotations(writer, methodDeclaration);
|
||||
writeModifiers(writer, METHOD_MODIFIERS, methodDeclaration.getModifiers());
|
||||
@ -232,6 +266,12 @@ public class JavaSourceCodeWriter implements SourceCodeWriter<JavaSourceCode> {
|
||||
imports.add(typeDeclaration.getExtends());
|
||||
}
|
||||
imports.addAll(getRequiredImports(typeDeclaration.getAnnotations(), this::determineImports));
|
||||
for (JavaFieldDeclaration fieldDeclaration : typeDeclaration.getFieldDeclarations()) {
|
||||
if (requiresImport(fieldDeclaration.getReturnType())) {
|
||||
imports.add(fieldDeclaration.getReturnType());
|
||||
}
|
||||
imports.addAll(getRequiredImports(fieldDeclaration.getAnnotations(), this::determineImports));
|
||||
}
|
||||
for (JavaMethodDeclaration methodDeclaration : typeDeclaration.getMethodDeclarations()) {
|
||||
if (requiresImport(methodDeclaration.getReturnType())) {
|
||||
imports.add(methodDeclaration.getReturnType());
|
||||
|
@ -25,11 +25,14 @@ import io.spring.initializr.generator.language.TypeDeclaration;
|
||||
* A {@link TypeDeclaration declaration } of a type written in Java.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Matt Berteaux
|
||||
*/
|
||||
public class JavaTypeDeclaration extends TypeDeclaration {
|
||||
|
||||
private int modifiers;
|
||||
|
||||
private final List<JavaFieldDeclaration> fieldDeclarations = new ArrayList<>();
|
||||
|
||||
private final List<JavaMethodDeclaration> methodDeclarations = new ArrayList<>();
|
||||
|
||||
JavaTypeDeclaration(String name) {
|
||||
@ -44,6 +47,14 @@ public class JavaTypeDeclaration extends TypeDeclaration {
|
||||
return this.modifiers;
|
||||
}
|
||||
|
||||
public void addFieldDeclaration(JavaFieldDeclaration fieldDeclaration) {
|
||||
this.fieldDeclarations.add(fieldDeclaration);
|
||||
}
|
||||
|
||||
public List<JavaFieldDeclaration> getFieldDeclarations() {
|
||||
return this.fieldDeclarations;
|
||||
}
|
||||
|
||||
public void addMethodDeclaration(JavaMethodDeclaration methodDeclaration) {
|
||||
this.methodDeclarations.add(methodDeclaration);
|
||||
}
|
||||
|
@ -56,6 +56,11 @@ public enum KotlinModifier {
|
||||
/**
|
||||
* Override a member.
|
||||
*/
|
||||
OVERRIDE
|
||||
OVERRIDE,
|
||||
|
||||
/**
|
||||
* Declare a late-initialized property.
|
||||
*/
|
||||
LATEINIT
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,274 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 io.spring.initializr.generator.language.kotlin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import io.spring.initializr.generator.language.Annotatable;
|
||||
import io.spring.initializr.generator.language.Annotation;
|
||||
|
||||
/**
|
||||
* Declaration of a property written in Kotlin.
|
||||
*
|
||||
* @author Matt Berteaux
|
||||
*/
|
||||
public final class KotlinPropertyDeclaration implements Annotatable {
|
||||
|
||||
private final List<Annotation> annotations = new ArrayList<>();
|
||||
|
||||
private final boolean isVal;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String returnType;
|
||||
|
||||
private final List<KotlinModifier> modifiers;
|
||||
|
||||
private final KotlinExpressionStatement valueExpression;
|
||||
|
||||
private final Accessor getter;
|
||||
|
||||
private final Accessor setter;
|
||||
|
||||
private KotlinPropertyDeclaration(Builder<?> builder) {
|
||||
this.name = builder.name;
|
||||
this.returnType = builder.returnType;
|
||||
this.modifiers = new ArrayList<>(builder.modifiers);
|
||||
this.isVal = builder.isVal;
|
||||
this.valueExpression = builder.initializerStatement;
|
||||
this.getter = builder.getter;
|
||||
this.setter = builder.setter;
|
||||
}
|
||||
|
||||
public static ValBuilder val(String name) {
|
||||
return new ValBuilder(name);
|
||||
}
|
||||
|
||||
public static VarBuilder var(String name) {
|
||||
return new VarBuilder(name);
|
||||
}
|
||||
|
||||
boolean isVal() {
|
||||
return this.isVal;
|
||||
}
|
||||
|
||||
String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
String getReturnType() {
|
||||
return this.returnType;
|
||||
}
|
||||
|
||||
public List<KotlinModifier> getModifiers() {
|
||||
return this.modifiers;
|
||||
}
|
||||
|
||||
KotlinExpressionStatement getValueExpression() {
|
||||
return this.valueExpression;
|
||||
}
|
||||
|
||||
Accessor getGetter() {
|
||||
return this.getter;
|
||||
}
|
||||
|
||||
Accessor getSetter() {
|
||||
return this.setter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void annotate(Annotation annotation) {
|
||||
this.annotations.add(annotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Annotation> getAnnotations() {
|
||||
return Collections.unmodifiableList(this.annotations);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for creating a {@link KotlinPropertyDeclaration}.
|
||||
*
|
||||
* @param <T> a {@link Builder} subclass.
|
||||
*/
|
||||
public abstract static class Builder<T extends Builder<T>> {
|
||||
|
||||
private final boolean isVal;
|
||||
|
||||
private final String name;
|
||||
|
||||
private String returnType;
|
||||
|
||||
private List<KotlinModifier> modifiers = new ArrayList<>();
|
||||
|
||||
private KotlinExpressionStatement initializerStatement;
|
||||
|
||||
private Accessor getter;
|
||||
|
||||
private Accessor setter;
|
||||
|
||||
private Builder(String name, boolean isVal) {
|
||||
this.name = name;
|
||||
this.isVal = isVal;
|
||||
}
|
||||
|
||||
protected abstract T self();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public AccessorBuilder getter() {
|
||||
return new AccessorBuilder<>((T) this, (created) -> this.getter = created);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public AccessorBuilder setter() {
|
||||
return new AccessorBuilder<>((T) this, (created) -> this.setter = created);
|
||||
}
|
||||
|
||||
public T returning(String returnType) {
|
||||
this.returnType = returnType;
|
||||
return self();
|
||||
}
|
||||
|
||||
public T modifiers(KotlinModifier... modifiers) {
|
||||
this.modifiers = Arrays.asList(modifiers);
|
||||
return self();
|
||||
}
|
||||
|
||||
public KotlinPropertyDeclaration emptyValue() {
|
||||
return new KotlinPropertyDeclaration(this);
|
||||
}
|
||||
|
||||
public KotlinPropertyDeclaration value(Object value) {
|
||||
this.initializerStatement = new KotlinExpressionStatement(new SimpleValueExpression(value));
|
||||
return new KotlinPropertyDeclaration(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static final class ValBuilder extends Builder<ValBuilder> {
|
||||
|
||||
private ValBuilder(String name) {
|
||||
super(name, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ValBuilder self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static final class VarBuilder extends Builder<VarBuilder> {
|
||||
|
||||
private VarBuilder(String name) {
|
||||
super(name, false);
|
||||
}
|
||||
|
||||
public KotlinPropertyDeclaration empty() {
|
||||
return new KotlinPropertyDeclaration(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected VarBuilder self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static final class AccessorBuilder<T extends Builder<T>> {
|
||||
|
||||
private final List<Annotation> annotations = new ArrayList<>();
|
||||
|
||||
private KotlinExpressionStatement body;
|
||||
|
||||
private final T parent;
|
||||
|
||||
private final Consumer<Accessor> accessorFunction;
|
||||
|
||||
private AccessorBuilder(T parent, Consumer<Accessor> accessorFunction) {
|
||||
this.parent = parent;
|
||||
this.accessorFunction = accessorFunction;
|
||||
}
|
||||
|
||||
public AccessorBuilder<?> withAnnotation(Annotation annotation) {
|
||||
this.annotations.add(annotation);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AccessorBuilder<?> withBody(KotlinExpressionStatement expressionStatement) {
|
||||
this.body = expressionStatement;
|
||||
return this;
|
||||
}
|
||||
|
||||
public T buildAccessor() {
|
||||
this.accessorFunction.accept(new Accessor(this));
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static final class Accessor implements Annotatable {
|
||||
|
||||
private final List<Annotation> annotations = new ArrayList<>();
|
||||
|
||||
private final KotlinExpressionStatement body;
|
||||
|
||||
Accessor(AccessorBuilder<?> builder) {
|
||||
this.annotations.addAll(builder.annotations);
|
||||
this.body = builder.body;
|
||||
}
|
||||
|
||||
boolean isEmptyBody() {
|
||||
return this.body == null;
|
||||
}
|
||||
|
||||
KotlinExpressionStatement getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void annotate(Annotation annotation) {
|
||||
this.annotations.add(annotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Annotation> getAnnotations() {
|
||||
return Collections.unmodifiableList(this.annotations);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class SimpleValueExpression extends KotlinExpression {
|
||||
|
||||
private final Object value;
|
||||
|
||||
SimpleValueExpression(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.valueOf(this.value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -42,6 +42,7 @@ import io.spring.initializr.generator.language.SourceCodeWriter;
|
||||
* A {@link SourceCodeWriter} that writes {@link SourceCode} in Kotlin.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Matt Berteaux
|
||||
*/
|
||||
public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode> {
|
||||
|
||||
@ -82,20 +83,33 @@ public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode
|
||||
if (type.getExtends() != null) {
|
||||
writer.print(" : " + getUnqualifiedName(type.getExtends()) + "()");
|
||||
}
|
||||
List<KotlinPropertyDeclaration> propertyDeclarations = type.getPropertyDeclarations();
|
||||
List<KotlinFunctionDeclaration> functionDeclarations = type.getFunctionDeclarations();
|
||||
if (!functionDeclarations.isEmpty()) {
|
||||
boolean hasDeclarations = !propertyDeclarations.isEmpty() || !functionDeclarations.isEmpty();
|
||||
if (hasDeclarations) {
|
||||
writer.println(" {");
|
||||
}
|
||||
if (!propertyDeclarations.isEmpty()) {
|
||||
writer.indented(() -> {
|
||||
for (KotlinPropertyDeclaration propertyDeclaration : propertyDeclarations) {
|
||||
writeProperty(writer, propertyDeclaration);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!functionDeclarations.isEmpty()) {
|
||||
writer.indented(() -> {
|
||||
for (KotlinFunctionDeclaration functionDeclaration : functionDeclarations) {
|
||||
writeFunction(writer, functionDeclaration);
|
||||
}
|
||||
});
|
||||
writer.println();
|
||||
writer.println("}");
|
||||
}
|
||||
else {
|
||||
writer.println("");
|
||||
}
|
||||
if (hasDeclarations) {
|
||||
writer.println("}");
|
||||
}
|
||||
}
|
||||
List<KotlinFunctionDeclaration> topLevelFunctions = compilationUnit.getTopLevelFunctions();
|
||||
if (!topLevelFunctions.isEmpty()) {
|
||||
@ -107,6 +121,48 @@ public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode
|
||||
}
|
||||
}
|
||||
|
||||
private void writeProperty(IndentingWriter writer, KotlinPropertyDeclaration propertyDeclaration) {
|
||||
writer.println();
|
||||
writeModifiers(writer, propertyDeclaration.getModifiers());
|
||||
if (propertyDeclaration.isVal()) {
|
||||
writer.print("val ");
|
||||
}
|
||||
else {
|
||||
writer.print("var ");
|
||||
}
|
||||
writer.print(propertyDeclaration.getName());
|
||||
if (propertyDeclaration.getReturnType() != null) {
|
||||
writer.print(": " + getUnqualifiedName(propertyDeclaration.getReturnType()));
|
||||
}
|
||||
if (propertyDeclaration.getValueExpression() != null) {
|
||||
writer.print(" = ");
|
||||
writeExpression(writer, propertyDeclaration.getValueExpression().getExpression());
|
||||
}
|
||||
if (propertyDeclaration.getGetter() != null) {
|
||||
writer.println();
|
||||
writer.indented(() -> writeAccessor(writer, "get", propertyDeclaration.getGetter()));
|
||||
}
|
||||
if (propertyDeclaration.getSetter() != null) {
|
||||
writer.println();
|
||||
writer.indented(() -> writeAccessor(writer, "set", propertyDeclaration.getSetter()));
|
||||
}
|
||||
writer.println();
|
||||
}
|
||||
|
||||
private void writeAccessor(IndentingWriter writer, String accessorName,
|
||||
KotlinPropertyDeclaration.Accessor accessor) {
|
||||
if (!accessor.getAnnotations().isEmpty()) {
|
||||
for (Annotation annotation : accessor.getAnnotations()) {
|
||||
writeAnnotation(writer, annotation, false);
|
||||
}
|
||||
}
|
||||
writer.print(accessorName);
|
||||
if (!accessor.isEmptyBody()) {
|
||||
writer.print("() = ");
|
||||
writeExpression(writer, accessor.getBody().getExpression());
|
||||
}
|
||||
}
|
||||
|
||||
private void writeFunction(IndentingWriter writer, KotlinFunctionDeclaration functionDeclaration) {
|
||||
writer.println();
|
||||
writeAnnotations(writer, functionDeclaration);
|
||||
@ -142,11 +198,11 @@ public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode
|
||||
|
||||
private void writeAnnotations(IndentingWriter writer, Annotatable annotatable) {
|
||||
for (Annotation annotation : annotatable.getAnnotations()) {
|
||||
writeAnnotation(writer, annotation);
|
||||
writeAnnotation(writer, annotation, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeAnnotation(IndentingWriter writer, Annotation annotation) {
|
||||
private void writeAnnotation(IndentingWriter writer, Annotation annotation, boolean newLine) {
|
||||
writer.print("@" + getUnqualifiedName(annotation.getName()));
|
||||
List<Annotation.Attribute> attributes = annotation.getAttributes();
|
||||
if (!attributes.isEmpty()) {
|
||||
@ -161,7 +217,12 @@ public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode
|
||||
}
|
||||
writer.print(")");
|
||||
}
|
||||
writer.println();
|
||||
if (newLine) {
|
||||
writer.println();
|
||||
}
|
||||
else {
|
||||
writer.print(" ");
|
||||
}
|
||||
}
|
||||
|
||||
private String formatAnnotationAttribute(Annotation.Attribute attribute) {
|
||||
@ -203,6 +264,9 @@ public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode
|
||||
else if (expression instanceof KotlinReifiedFunctionInvocation) {
|
||||
writeReifiedFunctionInvocation(writer, (KotlinReifiedFunctionInvocation) expression);
|
||||
}
|
||||
else if (expression != null) {
|
||||
writer.print(expression.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void writeFunctionInvocation(IndentingWriter writer, KotlinFunctionInvocation functionInvocation) {
|
||||
@ -233,6 +297,8 @@ public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode
|
||||
imports.add(typeDeclaration.getExtends());
|
||||
}
|
||||
imports.addAll(getRequiredImports(typeDeclaration.getAnnotations(), this::determineImports));
|
||||
typeDeclaration.getPropertyDeclarations()
|
||||
.forEach(((propertyDeclaration) -> imports.addAll(determinePropertyImports(propertyDeclaration))));
|
||||
typeDeclaration.getFunctionDeclarations()
|
||||
.forEach((functionDeclaration) -> imports.addAll(determineFunctionImports(functionDeclaration)));
|
||||
}
|
||||
@ -242,6 +308,14 @@ public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode
|
||||
return new LinkedHashSet<>(imports);
|
||||
}
|
||||
|
||||
private Set<String> determinePropertyImports(KotlinPropertyDeclaration propertyDeclaration) {
|
||||
Set<String> imports = new LinkedHashSet<>();
|
||||
if (requiresImport(propertyDeclaration.getReturnType())) {
|
||||
imports.add(propertyDeclaration.getReturnType());
|
||||
}
|
||||
return imports;
|
||||
}
|
||||
|
||||
private Set<String> determineFunctionImports(KotlinFunctionDeclaration functionDeclaration) {
|
||||
Set<String> imports = new LinkedHashSet<>();
|
||||
if (requiresImport(functionDeclaration.getReturnType())) {
|
||||
|
@ -31,6 +31,8 @@ public class KotlinTypeDeclaration extends TypeDeclaration {
|
||||
|
||||
private List<KotlinModifier> modifiers = new ArrayList<>();
|
||||
|
||||
private final List<KotlinPropertyDeclaration> propertyDeclarations = new ArrayList<>();
|
||||
|
||||
private final List<KotlinFunctionDeclaration> functionDeclarations = new ArrayList<>();
|
||||
|
||||
KotlinTypeDeclaration(String name) {
|
||||
@ -45,6 +47,14 @@ public class KotlinTypeDeclaration extends TypeDeclaration {
|
||||
return this.modifiers;
|
||||
}
|
||||
|
||||
public void addPropertyDeclaration(KotlinPropertyDeclaration propertyDeclaration) {
|
||||
this.propertyDeclarations.add(propertyDeclaration);
|
||||
}
|
||||
|
||||
public List<KotlinPropertyDeclaration> getPropertyDeclarations() {
|
||||
return this.propertyDeclarations;
|
||||
}
|
||||
|
||||
public void addFunctionDeclaration(KotlinFunctionDeclaration methodDeclaration) {
|
||||
this.functionDeclarations.add(methodDeclaration);
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link GroovySourceCodeWriter}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Matt Berteaux
|
||||
*/
|
||||
class GroovySourceCodeWriterTests {
|
||||
|
||||
@ -114,6 +115,74 @@ class GroovySourceCodeWriterTests {
|
||||
" SpringApplication.run(Test, args)", " }", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void field() throws IOException {
|
||||
GroovySourceCode sourceCode = new GroovySourceCode();
|
||||
GroovyCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
GroovyTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addFieldDeclaration(GroovyFieldDeclaration.field("testString").returning("java.lang.String"));
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.groovy");
|
||||
assertThat(lines).containsExactly("package com.example", "", "class Test {", "", " String testString", "",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldsWithValues() throws IOException {
|
||||
GroovySourceCode sourceCode = new GroovySourceCode();
|
||||
GroovyCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
GroovyTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addFieldDeclaration(GroovyFieldDeclaration.field("testNoInit").returning("boolean"));
|
||||
test.addFieldDeclaration(
|
||||
GroovyFieldDeclaration.field("testInteger").value("42").returning("java.lang.Integer"));
|
||||
test.addFieldDeclaration(GroovyFieldDeclaration.field("testDouble").modifiers(Modifier.PRIVATE).value("1986.0")
|
||||
.returning("double"));
|
||||
test.addFieldDeclaration(GroovyFieldDeclaration.field("testLong").value("1986L").returning("long"));
|
||||
test.addFieldDeclaration(
|
||||
GroovyFieldDeclaration.field("testNullBoolean").value(null).returning("java.lang.Boolean"));
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.groovy");
|
||||
assertThat(lines).containsExactly("package com.example", "", "class Test {", "", " boolean testNoInit", "",
|
||||
" Integer testInteger = 42", "", " private double testDouble = 1986.0", "",
|
||||
" long testLong = 1986L", "", " Boolean testNullBoolean = null", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void privateField() throws IOException {
|
||||
GroovySourceCode sourceCode = new GroovySourceCode();
|
||||
GroovyCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
GroovyTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addFieldDeclaration(
|
||||
GroovyFieldDeclaration.field("testString").modifiers(Modifier.PRIVATE).returning("java.lang.String"));
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.groovy");
|
||||
assertThat(lines).containsExactly("package com.example", "", "class Test {", "",
|
||||
" private String testString", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldImport() throws IOException {
|
||||
GroovySourceCode sourceCode = new GroovySourceCode();
|
||||
GroovyCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
GroovyTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addFieldDeclaration(
|
||||
GroovyFieldDeclaration.field("testString").modifiers(Modifier.PUBLIC).returning("com.example.One"));
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.groovy");
|
||||
assertThat(lines).containsExactly("package com.example", "", "import com.example.One", "", "class Test {", "",
|
||||
" public One testString", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldAnnotation() throws IOException {
|
||||
GroovySourceCode sourceCode = new GroovySourceCode();
|
||||
GroovyCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
GroovyTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
GroovyFieldDeclaration field = GroovyFieldDeclaration.field("testString").returning("java.lang.String");
|
||||
field.annotate(Annotation.name("org.springframework.beans.factory.annotation.Autowired"));
|
||||
test.addFieldDeclaration(field);
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.groovy");
|
||||
assertThat(lines).containsExactly("package com.example", "",
|
||||
"import org.springframework.beans.factory.annotation.Autowired", "", "class Test {", "",
|
||||
" @Autowired", " String testString", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void annotationWithSimpleAttribute() throws IOException {
|
||||
List<String> lines = writeClassAnnotation(Annotation.name("org.springframework.test.TestApplication",
|
||||
|
@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link JavaSourceCodeWriter}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Matt Berteaux
|
||||
*/
|
||||
class JavaSourceCodeWriterTests {
|
||||
|
||||
@ -95,6 +96,74 @@ class JavaSourceCodeWriterTests {
|
||||
" public String trim(String value) {", " return value.trim();", " }", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void field() throws IOException {
|
||||
JavaSourceCode sourceCode = new JavaSourceCode();
|
||||
JavaCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
JavaTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.modifiers(Modifier.PUBLIC);
|
||||
test.addFieldDeclaration(
|
||||
JavaFieldDeclaration.field("testString").modifiers(Modifier.PRIVATE).returning("java.lang.String"));
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.java");
|
||||
assertThat(lines).containsExactly("package com.example;", "", "public class Test {", "",
|
||||
" private String testString;", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldImport() throws IOException {
|
||||
JavaSourceCode sourceCode = new JavaSourceCode();
|
||||
JavaCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
JavaTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addFieldDeclaration(
|
||||
JavaFieldDeclaration.field("testString").modifiers(Modifier.PUBLIC).returning("com.example.One"));
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.java");
|
||||
assertThat(lines).containsExactly("package com.example;", "", "import com.example.One;", "", "class Test {", "",
|
||||
" public One testString;", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldAnnotation() throws IOException {
|
||||
JavaSourceCode sourceCode = new JavaSourceCode();
|
||||
JavaCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
JavaTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.modifiers(Modifier.PUBLIC);
|
||||
JavaFieldDeclaration field = JavaFieldDeclaration.field("testString").modifiers(Modifier.PRIVATE)
|
||||
.returning("java.lang.String");
|
||||
field.annotate(Annotation.name("org.springframework.beans.factory.annotation.Autowired"));
|
||||
test.addFieldDeclaration(field);
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.java");
|
||||
assertThat(lines).containsExactly("package com.example;", "",
|
||||
"import org.springframework.beans.factory.annotation.Autowired;", "", "public class Test {", "",
|
||||
" @Autowired", " private String testString;", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fields() throws IOException {
|
||||
JavaSourceCode sourceCode = new JavaSourceCode();
|
||||
JavaCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
JavaTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.modifiers(Modifier.PUBLIC);
|
||||
test.addFieldDeclaration(JavaFieldDeclaration.field("testString").modifiers(Modifier.PRIVATE)
|
||||
.value("\"Test String\"").returning("java.lang.String"));
|
||||
test.addFieldDeclaration(JavaFieldDeclaration.field("testChar").modifiers(Modifier.PRIVATE | Modifier.TRANSIENT)
|
||||
.value("'\\u03a9'").returning("char"));
|
||||
test.addFieldDeclaration(JavaFieldDeclaration.field("testInt").modifiers(Modifier.PRIVATE | Modifier.FINAL)
|
||||
.value(1337).returning("int"));
|
||||
test.addFieldDeclaration(
|
||||
JavaFieldDeclaration.field("testDouble").modifiers(Modifier.PRIVATE).value("3.14").returning("Double"));
|
||||
test.addFieldDeclaration(
|
||||
JavaFieldDeclaration.field("testLong").modifiers(Modifier.PRIVATE).value("1986L").returning("Long"));
|
||||
test.addFieldDeclaration(
|
||||
JavaFieldDeclaration.field("testFloat").modifiers(Modifier.PUBLIC).value("99.999f").returning("float"));
|
||||
test.addFieldDeclaration(JavaFieldDeclaration.field("testBool").value("true").returning("boolean"));
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.java");
|
||||
assertThat(lines).containsExactly("package com.example;", "", "public class Test {", "",
|
||||
" private String testString = \"Test String\";", "",
|
||||
" private transient char testChar = '\\u03a9';", "", " private final int testInt = 1337;", "",
|
||||
" private Double testDouble = 3.14;", "", " private Long testLong = 1986L;", "",
|
||||
" public float testFloat = 99.999f;", "", " boolean testBool = true;", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void springBootApplication() throws IOException {
|
||||
JavaSourceCode sourceCode = new JavaSourceCode();
|
||||
|
@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link KotlinSourceCodeWriter}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Matt Berteaux
|
||||
*/
|
||||
class KotlinSourceCodeWriterTests {
|
||||
|
||||
@ -109,6 +110,121 @@ class KotlinSourceCodeWriterTests {
|
||||
" open override fun toString(): String {", " return super.toString()", " }", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void valProperty() throws IOException {
|
||||
KotlinSourceCode sourceCode = new KotlinSourceCode();
|
||||
KotlinCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
KotlinTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addPropertyDeclaration(
|
||||
KotlinPropertyDeclaration.val("testProp").returning("java.lang.String").emptyValue());
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
|
||||
assertThat(lines).containsExactly("package com.example", "", "class Test {", "", " val testProp: String", "",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void valPropertyImport() throws IOException {
|
||||
KotlinSourceCode sourceCode = new KotlinSourceCode();
|
||||
KotlinCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
KotlinTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addPropertyDeclaration(
|
||||
KotlinPropertyDeclaration.val("testProp").returning("com.example.One").emptyValue());
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
|
||||
assertThat(lines).containsExactly("package com.example", "", "import com.example.One", "", "class Test {", "",
|
||||
" val testProp: One", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void valGetterProperty() throws IOException {
|
||||
KotlinSourceCode sourceCode = new KotlinSourceCode();
|
||||
KotlinCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
KotlinTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addPropertyDeclaration(
|
||||
KotlinPropertyDeclaration.val("testProp").returning("java.lang.String").value("\"This is a TEST\""));
|
||||
test.addPropertyDeclaration(KotlinPropertyDeclaration.val("withGetter").returning("java.lang.String").getter()
|
||||
.withBody(new KotlinExpressionStatement(new KotlinFunctionInvocation("testProp", "toLowerCase")))
|
||||
.buildAccessor().emptyValue());
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
|
||||
assertThat(lines).containsExactly("package com.example", "", "class Test {", "",
|
||||
" val testProp: String = \"This is a TEST\"", "", " val withGetter: String",
|
||||
" get() = testProp.toLowerCase()", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void varProperty() throws IOException {
|
||||
KotlinSourceCode sourceCode = new KotlinSourceCode();
|
||||
KotlinCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
KotlinTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addPropertyDeclaration(
|
||||
KotlinPropertyDeclaration.var("testProp").returning("java.lang.String").value("\"This is a test\""));
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
|
||||
assertThat(lines).containsExactly("package com.example", "", "class Test {", "",
|
||||
" var testProp: String = \"This is a test\"", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void varSetterProperty() throws IOException {
|
||||
KotlinSourceCode sourceCode = new KotlinSourceCode();
|
||||
KotlinCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
KotlinTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addPropertyDeclaration(KotlinPropertyDeclaration.var("testProp").returning("java.lang.String").setter()
|
||||
.buildAccessor().value("\"This is a test\""));
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
|
||||
assertThat(lines).containsExactly("package com.example", "", "class Test {", "",
|
||||
" var testProp: String = \"This is a test\"", " set", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void varAnnotateSetterProperty() throws IOException {
|
||||
KotlinSourceCode sourceCode = new KotlinSourceCode();
|
||||
KotlinCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
KotlinTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addPropertyDeclaration(KotlinPropertyDeclaration.var("testProp").returning("java.lang.String").setter()
|
||||
.withAnnotation(Annotation.name("org.springframework.beans.factory.annotation.Autowired"))
|
||||
.buildAccessor().value("\"This is a test\""));
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
|
||||
assertThat(lines).containsExactly("package com.example", "", "class Test {", "",
|
||||
" var testProp: String = \"This is a test\"", " @Autowired set", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void varProperties() throws IOException {
|
||||
KotlinSourceCode sourceCode = new KotlinSourceCode();
|
||||
KotlinCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
KotlinTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addPropertyDeclaration(KotlinPropertyDeclaration.var("testProp").returning("Int").value(42));
|
||||
test.addPropertyDeclaration(KotlinPropertyDeclaration.var("testDouble").returning("Double").value("1986.0"));
|
||||
test.addPropertyDeclaration(KotlinPropertyDeclaration.var("testFloat").value("99.999f"));
|
||||
test.addPropertyDeclaration(KotlinPropertyDeclaration.var("testLong").returning("Long").value("1986L"));
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
|
||||
assertThat(lines).containsExactly("package com.example", "", "class Test {", "", " var testProp: Int = 42",
|
||||
"", " var testDouble: Double = 1986.0", "", " var testFloat = 99.999f", "",
|
||||
" var testLong: Long = 1986L", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void varEmptyProperty() throws IOException {
|
||||
KotlinSourceCode sourceCode = new KotlinSourceCode();
|
||||
KotlinCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
KotlinTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addPropertyDeclaration(KotlinPropertyDeclaration.var("testProp").returning("Int").empty());
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
|
||||
assertThat(lines).containsExactly("package com.example", "", "class Test {", "", " var testProp: Int", "",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void varLateinitProperty() throws IOException {
|
||||
KotlinSourceCode sourceCode = new KotlinSourceCode();
|
||||
KotlinCompilationUnit compilationUnit = sourceCode.createCompilationUnit("com.example", "Test");
|
||||
KotlinTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
|
||||
test.addPropertyDeclaration(
|
||||
KotlinPropertyDeclaration.var("testProp").modifiers(KotlinModifier.LATEINIT).returning("Int").empty());
|
||||
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
|
||||
assertThat(lines).containsExactly("package com.example", "", "class Test {", "",
|
||||
" lateinit var testProp: Int", "", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void springBootApplication() throws IOException {
|
||||
KotlinSourceCode sourceCode = new KotlinSourceCode();
|
||||
|
Loading…
Reference in New Issue
Block a user