Skip to content

Commit

Permalink
Record support
Browse files Browse the repository at this point in the history
  • Loading branch information
Randgalt committed Mar 24, 2024
1 parent 57bf756 commit cd14576
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ Please see [JavaPoet](https://github.com/square/javapoet) for documentation, lic

# Changes From JavaPoet

### March 24, 2024
- Require Java 17
- Add record support from https://github.com/square/javapoet/pull/981
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<groupId>io.soabase.java-composer</groupId>
<artifactId>java-composer</artifactId>
<version>1.0-SNAPSHOT</version>

Expand Down
64 changes: 50 additions & 14 deletions src/main/java/com/squareup/javapoet/TypeSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@
*/
package com.squareup.javapoet;

import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.NoType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand All @@ -39,6 +30,14 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.NoType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;

import static com.squareup.javapoet.Util.checkArgument;
import static com.squareup.javapoet.Util.checkNotNull;
Expand Down Expand Up @@ -133,6 +132,15 @@ public static Builder classBuilder(ClassName className) {
return classBuilder(checkNotNull(className, "className == null").simpleName());
}

public static Builder recordBuilder(String name) {
return new Builder(Kind.RECORD, checkNotNull(name, "name == null"), null);
}

public static Builder recordBuilder(ClassName className) {
return recordBuilder(checkNotNull(className, "className == null").simpleName());
}


public static Builder interfaceBuilder(String name) {
return new Builder(Kind.INTERFACE, checkNotNull(name, "name == null"), null);
}
Expand Down Expand Up @@ -229,6 +237,26 @@ void emit(CodeWriter codeWriter, String enumName, Set<Modifier> implicitModifier
if (kind == Kind.INTERFACE) {
extendsTypes = superinterfaces;
implementsTypes = Collections.emptyList();
} else if (kind == Kind.RECORD) {
extendsTypes = Collections.emptyList();
implementsTypes = superinterfaces;

// Record constructor
boolean firstParameter = true;
codeWriter.emit("(");
int fieldSpecsLength = fieldSpecs.size();
for (int i = 0; i < fieldSpecsLength; i++) {
FieldSpec fieldSpec = fieldSpecs.get(i);

if (fieldSpec.hasModifier(Modifier.STATIC))
continue;
ParameterSpec parameter = ParameterSpec.builder(fieldSpec.type, fieldSpec.name).build();
if (!firstParameter)
codeWriter.emit(",").emitWrappingSpace();
parameter.emit(codeWriter, !(i < fieldSpecsLength));
firstParameter = false;
}
codeWriter.emit(")");
} else {
extendsTypes = superclass.equals(ClassName.OBJECT)
? Collections.emptyList()
Expand Down Expand Up @@ -296,11 +324,16 @@ void emit(CodeWriter codeWriter, String enumName, Set<Modifier> implicitModifier
}

// Non-static fields.
for (FieldSpec fieldSpec : fieldSpecs) {
if (fieldSpec.hasModifier(Modifier.STATIC)) continue;
if (!firstMember) codeWriter.emit("\n");
fieldSpec.emit(codeWriter, kind.implicitFieldModifiers);
firstMember = false;
// If kind RECORD, ignore generate Non-static field
if (!(Kind.RECORD == kind)) {
for (FieldSpec fieldSpec : fieldSpecs) {
if (fieldSpec.hasModifier(Modifier.STATIC))
continue;
if (!firstMember)
codeWriter.emit("\n");
fieldSpec.emit(codeWriter, kind.implicitFieldModifiers);
firstMember = false;
}
}

// Initializer block.
Expand Down Expand Up @@ -375,6 +408,9 @@ public enum Kind {
Collections.emptySet(),
Collections.emptySet()),

RECORD(Collections.emptySet(), Collections.emptySet(), Collections.emptySet(),
Collections.emptySet()),

INTERFACE(
Util.immutableSet(Arrays.asList(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)),
Util.immutableSet(Arrays.asList(Modifier.PUBLIC, Modifier.ABSTRACT)),
Expand Down
38 changes: 28 additions & 10 deletions src/test/java/com/squareup/javapoet/JavaFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,22 @@
*/
package com.squareup.javapoet;

import com.google.testing.compile.CompilationRule;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;

import java.io.File;
import com.google.testing.compile.CompilationRule;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static com.google.common.truth.Truth.assertThat;

Expand Down Expand Up @@ -287,6 +285,26 @@ private TypeSpec importStaticTypeSpec(String name) {
+ "}\n");
}

@Test
public void recordNoField() {
String source = JavaFile.builder("com.squareup.tacos", TypeSpec.recordBuilder("Taco").build())
.skipJavaLangImports(true).build().toString();
assertThat(source).isEqualTo(
"" + "package com.squareup.tacos;\n" + "\n" + "record Taco() {\n" + "}\n" + "");
}

@Test
public void recordTwoField() {
String source = JavaFile
.builder("com.squareup.tacos",
TypeSpec.recordBuilder("Taco")
.addField(FieldSpec.builder(String.class, "name").build())
.addField(FieldSpec.builder(Integer.class, "code").build()).build())
.skipJavaLangImports(true).build().toString();
assertThat(source).isEqualTo("" + "package com.squareup.tacos;\n" + "\n"
+ "record Taco(String name, Integer code) {\n" + "}\n" + "");
}

@Test public void conflictingImports() throws Exception {
String source = JavaFile.builder("com.squareup.tacos",
TypeSpec.classBuilder("Taco")
Expand Down

0 comments on commit cd14576

Please sign in to comment.