Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further Support Null Analysis #47

Merged
merged 42 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
6eac7c8
allow empty records
SentryMan Jan 7, 2024
ed4d471
support unnamed package records
SentryMan Jan 7, 2024
d77701b
Merge branch 'main' into empty
SentryMan Jan 7, 2024
b713d7f
defaults
SentryMan Jan 8, 2024
4521d86
Update README.md
SentryMan Jan 8, 2024
b380c83
Update README.md
SentryMan Jan 8, 2024
d9c9166
Update README.md
SentryMan Jan 8, 2024
f78e719
start
SentryMan Jan 8, 2024
cd7d4ad
done
SentryMan Jan 8, 2024
16b33b4
Merge branch 'main' into nullability
SentryMan Jan 8, 2024
fd46274
Update module-info.java
SentryMan Jan 8, 2024
7c2bd5f
Merge branch 'nullability' of https://github.com/SentryMan/avaje-reco…
SentryMan Jan 8, 2024
ae09ed3
Update RecordBuilder.java
SentryMan Jan 8, 2024
a6a316f
try checker framework
SentryMan Jan 8, 2024
fd82e31
Revert "try checker framework"
SentryMan Jan 8, 2024
9ce8fb8
get checker to work
SentryMan Jan 8, 2024
50956e6
make some progess
SentryMan Jan 8, 2024
d547e45
finally get it working
SentryMan Jan 8, 2024
990a565
null away and error prone
SentryMan Jan 8, 2024
70d71fd
Update pom.xml
SentryMan Jan 8, 2024
491d116
nullaway
SentryMan Jan 8, 2024
4a05add
Update README.md
SentryMan Jan 8, 2024
8f430a1
format
SentryMan Jan 8, 2024
de4d8bf
helpful error message
SentryMan Jan 9, 2024
eb1a17e
Merge remote-tracking branch 'upstream/main' into nullability
SentryMan Jan 9, 2024
3053401
Update ImportPrism.java
SentryMan Jan 9, 2024
202c2d9
support partial compile
SentryMan Jan 9, 2024
a738aa2
Merge branch 'main' into nullability
SentryMan Jan 13, 2024
cf89a7e
support @NullMarked
SentryMan Jan 16, 2024
76ac62e
support nullUnmarked
SentryMan Jan 16, 2024
a314998
Merge branch 'main' into nullability
SentryMan Jan 29, 2024
3d9d497
Merge branch 'main' into nullability
SentryMan Feb 22, 2024
2652118
Merge branch 'main' into nullability
SentryMan Jun 15, 2024
041f59b
Merge branch 'main' into nullability
SentryMan Jul 15, 2024
c463949
Merge branch 'main' into nullability
rbygrave Aug 30, 2024
0aec148
follow pr suggestions
SentryMan Aug 30, 2024
f5fa4ad
Merge branch 'nullability' of https://github.com/SentryMan/avaje-reco…
SentryMan Aug 30, 2024
0912aed
disable EA
SentryMan Aug 30, 2024
d447400
Update ImportPrism.java
SentryMan Aug 30, 2024
6ad8afd
Update pom.xml
SentryMan Aug 30, 2024
5e06fd2
fix nonnull detection
SentryMan Aug 30, 2024
5737afc
Update Utils.java
SentryMan Aug 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .mvn/jvm.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ Uses Annotation processing to generate builders for records.
## Distinguishing features
- By default, Collection/Optional Types will not be null (an empty collection/optional will be provided)
- We can choose the default value of a record component in the generated builder
- Copies nullability annotations to the generated setters to aid in static analysis

- Support for generating Checker/NullAway compliant builders for static null analysis.
## Usage
### 1. Add dependency:
```xml
Expand All @@ -22,7 +21,7 @@ Uses Annotation processing to generate builders for records.
</dependency>
```

When working with Java modules you need to add the annotation module as a static dependency.
Add the annotation module as a static dependency when working with Java modules.
```java
module my.module {
requires static io.avaje.recordbuilder;
Expand All @@ -39,7 +38,9 @@ public record ArmoredCore(
```

The following builder class will be generated for the above:
```java
<details>
<summary>Generated Class, (click to expand)</summary>
<pre content="java">
/** Builder class for {@link ArmoredCore} */
public class ArmoredCoreBuilder {
private String coreName = "Steel Haze";
Expand Down Expand Up @@ -105,5 +106,29 @@ public class ArmoredCoreBuilder {
this.ap = ap;
return this;
}
}
</pre>
</details>

## Default Values
Using `@DefaultValue` we can directly write the code to set the default value in the generated builder. This allows us to directly write a value or use static methods to set the default builder state.
```java
@RecordBuilder
public record Defaults(
@DefaultValue("List.of(1,2,3)") List<Integer> list,
@DefaultValue("24") int num,
@DefaultValue("\"string val\"") String str,
@DefaultValue("CustomClass.createDefault()") CustomClass custom) {}
```

This will generate:
```java
public class DefaultsBuilder {
private List<Integer> list = List.of(1,2,3);
private int num = 24;
private String str = "string val";
private CustomClass custom = CustomClass.createDefault();

...the rest of the builder
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.avaje.recordbuilder.internal;

import java.util.List;

import javax.lang.model.type.TypeMirror;

public interface BuilderPrism {

default boolean imported() {
return this instanceof ImportPrism;
}

/**
* Returns a Boolean representing the value of the {@code boolean public abstract boolean
* getters() } member of the Annotation.
*
* @see io.avaje.recordbuilder.RecordBuilder.Import#getters()
*/
Boolean getters();

/**
* Returns a Boolean representing the value of the {@code boolean public abstract boolean
* enforceNullSafety() } member of the Annotation.
*
* @see io.avaje.recordbuilder.RecordBuilder.Import#enforceNullSafety()
*/
Boolean enforceNullSafety();

/**
* Returns a TypeMirror representing the value of the {@code java.lang.Class<? extends
* java.lang.annotation.Annotation> public abstract Class<? extends
* java.lang.annotation.Annotation> nullableAnnotation() } member of the Annotation.
*
* @see io.avaje.recordbuilder.RecordBuilder.Import#nullableAnnotation()
*/
TypeMirror nullableAnnotation();

/**
* Returns a List&lt;TypeMirror&gt; representing the value of the {@code public abstract
* Class<?>[] builderInterfaces() } member of the Annotation.
*
* @see io.avaje.recordbuilder.RecordBuilder.Import#builderInterfaces()
*/
List<TypeMirror> builderInterfaces();
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package io.avaje.recordbuilder.internal;

import static io.avaje.recordbuilder.internal.APContext.elements;
import static io.avaje.recordbuilder.internal.Templates.classTemplate;
import static java.util.stream.Collectors.joining;

import java.text.MessageFormat;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import javax.lang.model.element.RecordComponentElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

// TODO better name?
public class ClassBodyBuilder {

static String createClassStart(TypeElement type, String typeParams, boolean isImported) {
private ClassBodyBuilder() {}

static String createClassStart(
BuilderPrism prism,
TypeElement type,
String typeParams,
boolean isImported,
String packageName) {

final var components = type.getRecordComponents();
final var packageName =
elements().getPackageOf(type).getQualifiedName().toString()
+ (isImported ? ".builder" : "");
final var shortName = type.getSimpleName().toString();
if (type.getEnclosingElement() instanceof TypeElement) {
isImported = true;
Expand All @@ -42,6 +43,14 @@ static String createClassStart(TypeElement type, String typeParams, boolean isIm

final RecordModel rm = new RecordModel(type, isImported, components, utype);
rm.initialImports();
rm.nullableAnnotation(GlobalSettings.nullableAnnotation().orElse(prism.nullableAnnotation().toString()));
var implementsStr =
prism.builderInterfaces().stream()
.map(TypeMirror::toString)
.peek(rm::addImport)
.map(ProcessorUtils::shortType)
.collect(joining(", "))
.transform(s -> s.isEmpty() ? s : "implements " + s);
final String fieldString = rm.fields();
final var imports = rm.importsFormat();
final var numberOfComponents = components.size();
Expand All @@ -51,11 +60,12 @@ static String createClassStart(TypeElement type, String typeParams, boolean isIm
final String builderFrom =
builderFrom(components).transform(s -> numberOfComponents > 5 ? "\n " + s : s);
final String build =
build(components).transform(s -> numberOfComponents > 6 ? "\n " + s : s);
build(components, prism).transform(s -> numberOfComponents > 6 ? "\n " + s : s);
return classTemplate(
packageName,
imports,
shortName,
implementsStr,
fieldString,
constructorParams,
constructorBody,
Expand Down Expand Up @@ -88,7 +98,16 @@ private static String builderFrom(List<? extends RecordComponentElement> compone
.collect(joining(", "));
}

private static String build(List<? extends RecordComponentElement> components) {
return components.stream().map(RecordComponentElement::getSimpleName).collect(joining(", "));
private static String build(
List<? extends RecordComponentElement> components, BuilderPrism prism) {

return components.stream()
.map(
element ->
!Utils.isNullableType(UType.parse(element.asType()).mainType())
&& Utils.isNonNullable(element, prism)
? "requireNonNull(%s)".formatted(element.getSimpleName())
: element.getSimpleName())
.collect(joining(", "));
}
}
Loading
Loading