Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Add support for Java 16 records #829

Open
htsaustin opened this issue Mar 17, 2021 · 2 comments · May be fixed by #840
Open

Add support for Java 16 records #829

htsaustin opened this issue Mar 17, 2021 · 2 comments · May be fixed by #840

Comments

@htsaustin
Copy link

Java 16 has added support for records under JEP 395, of the form.

public record Point(int x, int y) {
}

It would be great to support these in JavaPoet, as among other things, they significantly reduce boilerplate.

@rbair23
Copy link

rbair23 commented Jul 8, 2022

I am taking a crack at a PR that would add record support. However, after forking and doing a fresh build/test, I'm seeing two tests fail in IntelliJ (when running through the IDE, not in Maven).

ClassNameTest::classNameFromTypeElementDoesntUseGetKind fails:

java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.code.Symbol$Completer.complete(com.sun.tools.javac.code.Symbol)" because "c" is null

	at jdk.compiler/com.sun.tools.javac.code.Symbol.complete(Symbol.java:682)
	at jdk.compiler/com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:1410)
	at jdk.compiler/com.sun.tools.javac.code.Symbol.apiComplete(Symbol.java:688)
	at jdk.compiler/com.sun.tools.javac.code.Symbol$ClassSymbol.getKind(Symbol.java:1476)
	at com.squareup.javapoet.ClassNameTest.preventGetKind(ClassNameTest.java:135)
	at com.squareup.javapoet.ClassNameTest.classNameFromTypeElementDoesntUseGetKind(ClassNameTest.java:122)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

and ParameterSpecTest::parameterVariableElement fails:

expected: java.lang.String arg0
but was : java.lang.String bar
Expected :java.lang.String arg0
Actual   :java.lang.String bar
<Click to see difference>


	at com.squareup.javapoet.ParameterSpecTest.parameterVariableElement(ParameterSpecTest.java:121)

Is this expected?

@vkhodygo
Copy link

vkhodygo commented Sep 3, 2022

@rbair23 It looks like the latter can be fixed by replacing

.isEqualTo("java.lang.String arg0");

with

.isEqualTo("java.lang.String bar");

It's not clear to me yet what causes the other one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants