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 all 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
28 changes: 17 additions & 11 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
version: 2
updates:
- package-ecosystem: maven
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
groups:
dev-deps:
dependency-type: "development"
prod-deps:
dependency-type: "production"
- package-ecosystem: "maven"
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
groups:
dependencies:
patterns:
- "*"
labels:
- "dependencies"
target-branch: "master"

- package-ecosystem: "github-actions"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
Expand All @@ -21,3 +23,7 @@ updates:
labels:
- "dependencies"
target-branch: "master"
groups:
dependencies:
patterns:
- "*"
8 changes: 4 additions & 4 deletions .github/workflows/jdk-ea-stable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
name: JDK EA Stable

on:
push:
pull_request:
# push:
# pull_request:
workflow_dispatch:
schedule:
- cron: '39 1 * * 1,3,5'
# schedule:
# - cron: '39 1 * * 1,3,5'

jobs:
build:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/jdk-ea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
name: JDK EA

on:
pull_request:
# pull_request:
workflow_dispatch:
schedule:
- cron: '39 6 * * 1,3,5'
# schedule:
# - cron: '39 6 * * 1,3,5'

jobs:
build:
Expand Down
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
5 changes: 2 additions & 3 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 Down
7 changes: 7 additions & 0 deletions avaje-record-builder-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<version>1.0.0</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.avaje.recordbuilder.internal;

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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ public class ClassBodyBuilder {
private ClassBodyBuilder() {}

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

final var components = type.getRecordComponents();

final var shortName = type.getSimpleName().toString();
if (type.getEnclosingElement() instanceof TypeElement) {
isImported = true;
Expand Down Expand Up @@ -85,7 +87,18 @@ 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) {

return components.stream()
.map(
element -> {
final var simpleName = element.getSimpleName();
return !Utils.isNullableType(UType.parse(element.asType()).mainType())
&& Utils.isNonNullable(element)
? "requireNonNull(%s, \"%s\")".formatted(simpleName, simpleName)
: simpleName;
})
.collect(joining(", "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
* A Prism representing a {@link io.avaje.recordbuilder.RecordBuilder.Import @Import} annotation.
*/
@Generated("avaje-prism-generator")
final class ImportPrism {
final class ImportPrism implements BuilderPrism {
/** store prism value of value */
private final List<TypeMirror> _value;

/** store prism value of getters */
private final Boolean _getters;

public static final String PRISM_TYPE = "io.avaje.recordbuilder.RecordBuilder.Import";

/**
Expand Down Expand Up @@ -71,7 +74,7 @@ static ImportPrism getInstanceOn(Element element) {
/**
* Return a Optional representing a nullable {@link
* io.avaje.recordbuilder.RecordBuilder.Import @Import} annotation on the given element. similar
* to {@link element.getAnnotation(io.avaje.recordbuilder.RecordBuilder.Import.class)} except that
* to {@code element.getAnnotation(io.avaje.recordbuilder.RecordBuilder.Import.class)} except that
* an Optional of this class rather than an instance of {@link
* io.avaje.recordbuilder.RecordBuilder.Import} is returned.
*
Expand Down Expand Up @@ -99,7 +102,7 @@ static ImportPrism getInstance(AnnotationMirror mirror) {

/**
* Return an Optional representing a nullable {@link ImportPrism @ImportPrism} from an annotation
* mirror. similar to {@link e.getAnnotation(io.avaje.recordbuilder.RecordBuilder.Import.class)}
* mirror. similar to {@code e.getAnnotation(io.avaje.recordbuilder.RecordBuilder.Import.class)}
* except that an Optional of this class rather than an instance of {@link
* io.avaje.recordbuilder.RecordBuilder.Import @Import} is returned.
*
Expand All @@ -122,21 +125,33 @@ private ImportPrism(AnnotationMirror mirror) {
defaults.put(member.getSimpleName().toString(), member.getDefaultValue());
}
_value = getArrayValues("value", TypeMirror.class);
_getters = getValue("getters", Boolean.class);
this.values = new Values(memberValues);
this.mirror = mirror;
this.isValid = valid;
}

/**
* Returns a List&lt;TypeMirror&gt; representing the value of the {@code value()} member of the
* Annotation.
* Returns a List&lt;TypeMirror&gt; representing the value of the {@code public abstract Class<?
* extends java.lang.Record>[] value() } member of the Annotation.
*
* @see io.avaje.recordbuilder.RecordBuilder.Import#value()
*/
public List<TypeMirror> value() {
return _value;
}

/**
* 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()
*/
@Override
public Boolean getters() {
return _getters;
}

/**
* Determine whether the underlying AnnotationMirror has no errors. True if the underlying
* AnnotationMirror has no errors. When true is returned, none of the methods will return null.
Expand All @@ -150,6 +165,7 @@ public List<TypeMirror> value() {
* to support using Messager.
*/
final AnnotationMirror mirror;

/**
* A class whose members corespond to those of {@link
* io.avaje.recordbuilder.RecordBuilder.Import @Import} but which each return the AnnotationValue
Expand All @@ -162,19 +178,34 @@ static final class Values {
private Values(Map<String, AnnotationValue> values) {
this.values = values;
}

/**
* Return the AnnotationValue corresponding to the value() member of the annotation, or null
* when the default value is implied.
*/
AnnotationValue value() {
return values.get("value");
}

/**
* Return the AnnotationValue corresponding to the getters() member of the annotation, or null
* when the default value is implied.
*/
AnnotationValue getters() {
return values.get("getters");
}
}

private final Map<String, AnnotationValue> defaults = new HashMap<>(10);
private final Map<String, AnnotationValue> memberValues = new HashMap<>(10);
private boolean valid = true;

private <T> T getValue(String name, Class<T> clazz) {
final T result = ImportPrism.getValue(memberValues, defaults, name, clazz);
if (result == null) valid = false;
return result;
}

private <T> List<T> getArrayValues(String name, final Class<T> clazz) {
final List<T> result = ImportPrism.getArrayValues(memberValues, defaults, name, clazz);
if (result == null) valid = false;
Expand All @@ -190,25 +221,43 @@ private static AnnotationMirror getMirror(Element target) {
return null;
}

private static <T> T getValue(
Map<String, AnnotationValue> memberValues,
Map<String, AnnotationValue> defaults,
String name,
Class<T> clazz) {
AnnotationValue av = memberValues.get(name);
if (av == null) av = defaults.get(name);
if (av == null) {
return null;
}
if (clazz.isInstance(av.getValue())) return clazz.cast(av.getValue());
return null;
}

private static <T> List<T> getArrayValues(
Map<String, AnnotationValue> memberValues,
Map<String, AnnotationValue> defaults,
String name,
final Class<T> clazz) {
AnnotationValue av = memberValues.get(name);
if (av == null) av = defaults.get(name);
if ((av == null) || !(av.getValue() instanceof List)) {
if (av == null) {
return List.of();
}
final List<T> result = new ArrayList<>();
for (final var v : getValueAsList(av)) {
if (clazz.isInstance(v.getValue())) {
result.add(clazz.cast(v.getValue()));
} else {
return List.of();
if (av.getValue() instanceof List) {
final List<T> result = new ArrayList<>();
for (final var v : getValueAsList(av)) {
if (clazz.isInstance(v.getValue())) {
result.add(clazz.cast(v.getValue()));
} else {
return List.of();
}
}
return result;
} else {
return List.of();
}
return result;
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ public class InitMap {
private static final Map<String, String> defaultsMap = new HashMap<>();

static {
put("byte", "0");
put("short", "0");
put("int", "0");
put("long", "0L");
put("float", "0.0f");
put("double", "0.0d");
put("char", "\u0000");
put("boolean", "false");

// TODO add the rest of the collections
final var util = "java.util.";
final var initDiamond = "new java.util.%s<>()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static void read(BufferedReader reader) {
.lines()
.forEach(
line -> {
if (line.isBlank()) {
if (line.isBlank()|| line.contains("import")) {
return;
}

Expand Down
Loading
Loading