Skip to content

Commit 11fc146

Browse files
Fail fast if no codec for Json.Document (#7359)
1 parent 22ac624 commit 11fc146

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/rpc/DynamicDispatchRpcCodec.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ public abstract class DynamicDispatchRpcCodec<T> implements RpcCodec<T> {
3737
}
3838
}
3939

40+
/**
41+
* Verifies that at least one {@link DynamicDispatchRpcCodec} is registered for the
42+
* given source file type. Call this at startup to fail fast when the ServiceLoader
43+
* has not discovered the expected codec implementations (e.g. missing module on the
44+
* classpath).
45+
*
46+
* @param sourceFileType The fully-qualified class name of the source file type
47+
* (e.g. {@code "org.openrewrite.json.tree.Json$Document"}).
48+
* @throws IllegalStateException if no codec is registered for the given type.
49+
*/
50+
public static void requireCodecFor(String sourceFileType) {
51+
if (!CODEC_BY_TYPE.containsKey(sourceFileType)) {
52+
throw new IllegalStateException(
53+
"No DynamicDispatchRpcCodec registered for '" + sourceFileType + "'. " +
54+
"The ServiceLoader found zero DynamicDispatchRpcCodec implementations for this type. " +
55+
"Ensure that the module providing this codec is on the classpath.");
56+
}
57+
}
58+
4059
@SuppressWarnings("unchecked")
4160
public static <T> @Nullable RpcCodec<T> getCodec(Object t, @Nullable String sourceFileType) {
4261
if (sourceFileType == null) {

rewrite-javascript/src/main/java/org/openrewrite/javascript/rpc/JavaScriptRewriteRpc.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
import org.openrewrite.javascript.JavaScriptParser;
2424
import org.openrewrite.javascript.internal.rpc.JavaScriptValidator;
2525
import org.openrewrite.javascript.tree.JS;
26+
import org.openrewrite.json.tree.Json;
2627
import org.openrewrite.marker.Markers;
2728
import org.openrewrite.tree.ParseError;
2829
import org.openrewrite.marketplace.RecipeBundleResolver;
2930
import org.openrewrite.marketplace.RecipeMarketplace;
31+
import org.openrewrite.rpc.DynamicDispatchRpcCodec;
3032
import org.openrewrite.rpc.RewriteRpc;
3133
import org.openrewrite.rpc.RewriteRpcProcess;
3234
import org.openrewrite.rpc.RewriteRpcProcessManager;
@@ -344,6 +346,8 @@ public Builder workingDirectory(@Nullable Path workingDirectory) {
344346

345347
@Override
346348
public JavaScriptRewriteRpc get() {
349+
DynamicDispatchRpcCodec.requireCodecFor(Json.Document.class.getName());
350+
347351
Stream<@Nullable String> cmd;
348352

349353
if (inspectBrk != null) {

0 commit comments

Comments
 (0)