Skip to content

Commit

Permalink
Generate code with JSpecify annotations (openrewrite#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Aug 16, 2024
1 parent e713ca6 commit fe43c07
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
recipe.append("/**\n * OpenRewrite recipe created for Refaster template {@code ").append(refasterRuleClassName).append("}.\n */\n");
String recipeName = templateFqn.substring(templateFqn.lastIndexOf('.') + 1);
recipe.append("@SuppressWarnings(\"all\")\n");
recipe.append("@NonNullApi\n");
recipe.append("@NullMarked\n");
recipe.append("@Generated(\"").append(GENERATOR_NAME).append("\")\n");
recipe.append(descriptor.classDecl.sym.outermostClass() == descriptor.classDecl.sym ?
"public class " : "public static class ").append(recipeName).append(" extends Recipe {\n\n");
Expand Down Expand Up @@ -344,11 +344,11 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
out.write("package " + pkg.fullname + ";\n");
out.write("\n");
}
out.write("import org.jspecify.annotations.NullMarked;\n");
out.write("import org.openrewrite.ExecutionContext;\n");
out.write("import org.openrewrite.Preconditions;\n");
out.write("import org.openrewrite.Recipe;\n");
out.write("import org.openrewrite.TreeVisitor;\n");
out.write("import org.openrewrite.internal.lang.NonNullApi;\n");
out.write("import org.openrewrite.java.JavaParser;\n");
out.write("import org.openrewrite.java.JavaTemplate;\n");
out.write("import org.openrewrite.java.JavaVisitor;\n");
Expand Down Expand Up @@ -712,7 +712,7 @@ public RuleDescriptor(JCTree.JCClassDecl classDecl, JCCompilationUnit cu, Contex
this.context = context;
}

private @Nullable RefasterTemplateProcessor.RuleDescriptor validate() {
private RefasterTemplateProcessor.@Nullable RuleDescriptor validate() {
if (beforeTemplates.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import org.intellij.lang.annotations.Language;
import org.jspecify.annotations.NullMarked;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.java.template.processor.RefasterTemplateProcessor;
import org.openrewrite.java.template.processor.TypeAwareProcessor;

import javax.annotation.Generated;
import javax.tools.JavaFileObject;
import java.io.File;
import java.net.URL;
Expand Down Expand Up @@ -120,6 +120,7 @@ static Compilation compileSource(String fqn, @Language("java") String source) {
return compile(JavaFileObjects.forSourceString(fqn, source), new RefasterTemplateProcessor());
}

@SuppressWarnings("unused") // use when text blocks are available
static Compilation compileSource(String fqn, @Language("java") String source, TypeAwareProcessor processor) {
return compile(JavaFileObjects.forSourceString(fqn, source), processor);
}
Expand All @@ -134,7 +135,8 @@ static Compilation compile(JavaFileObject javaFileObject, TypeAwareProcessor pro
fileForClass(org.openrewrite.Recipe.class),
fileForClass(org.openrewrite.java.JavaTemplate.class),
fileForClass(org.slf4j.Logger.class),
fileForClass(Primitive.class)
fileForClass(Primitive.class),
fileForClass(NullMarked.class)
))
.compile(javaFileObject);
}
Expand Down

0 comments on commit fe43c07

Please sign in to comment.