Skip to content

Commit

Permalink
Merge pull request #881 from mattyb678
Browse files Browse the repository at this point in the history
* pr/881:
  Polish "Adding field declaration for code generation"
  Adding field declaration for code generation

Closes gh-881
  • Loading branch information
snicoll committed Jul 25, 2019
2 parents d8a5be7 + c7093c6 commit 74e0bcc
Show file tree
Hide file tree
Showing 13 changed files with 969 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@
* 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 {
Expand All @@ -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");
Expand Down Expand Up @@ -107,6 +119,14 @@ private void writeTo(Path directory, GroovyCompilationUnit compilationUnit) thro
}
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(() -> {
Expand Down Expand Up @@ -165,6 +185,20 @@ private String formatValues(List<String> values, Function<String, String> format
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());
Expand Down Expand Up @@ -230,6 +264,12 @@ private Set<String> determineImports(GroovyCompilationUnit compilationUnit) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -44,6 +46,14 @@ public int getModifiers() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

}
Loading

0 comments on commit 74e0bcc

Please sign in to comment.