Skip to content

Fix flag defaulting to Javax Singleton/Add more tests #121

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

Merged
merged 2 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;

@SupportedOptions({"useJavax"})
@SupportedOptions({"useJavax","useSingleton"})
public abstract class BaseProcessor extends AbstractProcessor {

protected ProcessingContext ctx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,34 @@ public class ProcessingContext {
private final String diAnnotation;

public ProcessingContext(ProcessingEnvironment env, PlatformAdapter readAdapter) {

this.readAdapter = readAdapter;
this.messager = env.getMessager();
this.filer = env.getFiler();
this.elements = env.getElementUtils();
this.types = env.getTypeUtils();
this.openApiAvailable = isTypeAvailable(Constants.OPENAPIDEFINITION);
this.docContext = new DocContext(env, openApiAvailable);
this.useComponent = isTypeAvailable(Constants.COMPONENT);

final var options = env.getOptions();
final var singletonOverride = options.get("useSingleton");

if (singletonOverride != null) {
this.useComponent = !Boolean.parseBoolean(singletonOverride);
} else {
this.useComponent = isTypeAvailable(Constants.COMPONENT);
}

this.diAnnotation = useComponent ? "@Component" : "@Singleton";

final var javax = isTypeAvailable(Constants.SINGLETON_JAVAX);
final var jakarta = isTypeAvailable(Constants.SINGLETON_JAKARTA);
final var override = Boolean.getBoolean(env.getOptions().get("useJavax"));
final var override = env.getOptions().get("useJavax");

if (override || (javax && jakarta)) {
this.useJavax = override;
} else if (javax) {
this.useJavax = true;
if (override != null || (javax && jakarta)) {
this.useJavax = Boolean.parseBoolean(override);
} else if (javax && !jakarta) {
useJavax = javax;
} else {
useJavax = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public static String prettyPrintJson(Writer writer, String json) throws IOExcept
writer.append(current).append("\n");
addIndents(writer, indentLevel);
break;
case ':':
writer.append(" :");
break;
default:
writer.append(current);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static String serialize(Object obj) throws IllegalAccessException {
}
sb.append("\"");
sb.append(entry.getKey());
sb.append("\": ");
sb.append("\" : ");

write(sb, entry.getValue());
firstElement = false;
Expand All @@ -75,7 +75,7 @@ static String serialize(Object obj) throws IllegalAccessException {
}
sb.append("\"");
sb.append(field.getName());
sb.append("\": ");
sb.append("\" : ");
write(sb, value);
firstField = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void deleteGeneratedFiles() throws IOException {
}

@Test
public void runAnnoationProcessor() throws Exception {
public void runAnnotationProcessor() throws Exception {
final var source = Paths.get("src").toAbsolutePath().toString();

final var files = getSourceFiles(source);
Expand All @@ -54,10 +54,13 @@ public void runAnnoationProcessor() throws Exception {
task.setProcessors(List.of(new JavalinProcessor()));

assertThat(task.call()).isTrue();
assert Files.readString(
Paths.get("org/example/myapp/web/BarController$Route.java").toAbsolutePath())
.contains("io.avaje.inject.Component");
}

@Test
public void runAnnoationProcessorJsonB() throws Exception {
public void runAnnotationProcessorJsonB() throws Exception {
final var source = Paths.get("src").toAbsolutePath().toString();

final var files = getSourceFiles(source);
Expand All @@ -67,9 +70,62 @@ public void runAnnoationProcessorJsonB() throws Exception {
final var task =
compiler.getTask(
new PrintWriter(System.out), null, null, List.of("--release=11"), null, files);
task.setProcessors(List.of(new JavalinProcessor(true), new Processor()));

assertThat(task.call()).isTrue();
assert Files.readString(
Paths.get("org/example/myapp/web/BarController$Route.java").toAbsolutePath())
.contains("io.avaje.jsonb.Jsonb");
}

@Test
public void runAnnotationProcessorJavax() throws Exception {
final var source = Paths.get("src").toAbsolutePath().toString();

final var files = getSourceFiles(source);

final var compiler = ToolProvider.getSystemJavaCompiler();

final var task =
compiler.getTask(
new PrintWriter(System.out),
null,
null,
List.of("--release=11", "-AuseJavax=true", "-AuseSingleton=true"),
null,
files);
task.setProcessors(List.of(new JavalinProcessor(false), new Processor()));
// we don't have javax on the cp
assertThat(task.call()).isFalse();

assert Files.readString(
Paths.get("org/example/myapp/web/BarController$Route.java").toAbsolutePath())
.contains("javax.inject.Singleton");
}

@Test
public void runAnnotationProcessorJakarta() throws Exception {
final var source = Paths.get("src").toAbsolutePath().toString();

final var files = getSourceFiles(source);

final var compiler = ToolProvider.getSystemJavaCompiler();

final var task =
compiler.getTask(
new PrintWriter(System.out),
null,
null,
List.of("--release=11", "-AuseJavax=false", "-AuseSingleton=true"),
null,
files);
task.setProcessors(List.of(new JavalinProcessor(false), new Processor()));

assertThat(task.call()).isTrue();

assert Files.readString(
Paths.get("org/example/myapp/web/BarController$Route.java").toAbsolutePath())
.contains("jakarta.inject.Singleton");
}

@Test
Expand Down