+ private final LoadingCache<Class<?>, ContainerLikeCodecContext<?>> rpcDataByClass =
+ CacheBuilder.newBuilder().build(new CacheLoader<>() {
+ @Override
+ public ContainerLikeCodecContext<?> load(final Class<?> key) {
+ final Function<RpcRuntimeType, ContainerLikeRuntimeType<?, ?>> lookup;
+ if (RpcInput.class.isAssignableFrom(key)) {
+ lookup = RpcRuntimeType::input;
+ } else if (RpcOutput.class.isAssignableFrom(key)) {
+ lookup = RpcRuntimeType::output;
+ } else {
+ throw new IllegalArgumentException(key + " does not represent an RPC container");
+ }
+
+ final QName qname = BindingReflections.findQName(key);
+ final QNameModule qnameModule = qname.getModule();
+ final Module module = context.getEffectiveModelContext().findModule(qnameModule)
+ .orElseThrow(() -> new IllegalArgumentException("Failed to find module for " + qnameModule));
+ final String className = Naming.getClassName(qname);
+
+ for (var potential : module.getRpcs()) {
+ final QName potentialQName = potential.getQName();
+ /*
+ * Check if rpc and class represents data from same module and then checks if rpc local name
+ * produces same class name as class name appended with Input/Output based on QName associated
+ * with binding class.
+ *
+ * FIXME: Rework this to have more precise logic regarding Binding Specification.
+ */
+ if (key.getSimpleName().equals(Naming.getClassName(potentialQName) + className)) {
+ final ContainerLike schema = getRpcDataSchema(potential, qname);
+ checkArgument(schema != null, "Schema for %s does not define input / output.", potentialQName);
+
+ final var runtimeType = context.getTypes().schemaTreeChild(potentialQName);
+ if (runtimeType instanceof RpcRuntimeType rpcType) {
+ // FIXME: accurate type
+ return new ContainerLikeCodecContext(key, lookup.apply(rpcType), BindingCodecContext.this);
+ }
+ throw new IllegalArgumentException("Cannot find runtime type for " + key);
+ }
+ }
+
+ throw new IllegalArgumentException("Supplied class " + key + " is not valid RPC class.");
+ }
+
+ /**
+ * Returns RPC input or output schema based on supplied QName.
+ *
+ * @param rpc RPC Definition
+ * @param qname input or output QName with namespace same as RPC
+ * @return input or output schema. Returns null if RPC does not have input/output specified.
+ */
+ private static @Nullable ContainerLike getRpcDataSchema(final @NonNull RpcDefinition rpc,
+ final @NonNull QName qname) {
+ requireNonNull(rpc, "Rpc Schema must not be null");
+ return switch (requireNonNull(qname, "QName must not be null").getLocalName()) {
+ case "input" -> rpc.getInput();
+ case "output" -> rpc.getOutput();
+ default -> throw new IllegalArgumentException(
+ "Supplied qname " + qname + " does not represent rpc input or output.");
+ };
+ }
+ });
+ private final LoadingCache<Absolute, RpcInputCodec<?>> rpcDataByPath =
+ CacheBuilder.newBuilder().build(new CacheLoader<>() {
+ @Override
+ public RpcInputCodec<?> load(final Absolute key) {
+ final var rpcName = key.firstNodeIdentifier();
+
+ final Class<? extends DataContainer> container = switch (key.lastNodeIdentifier().getLocalName()) {
+ case "input" -> context.getRpcInput(rpcName);
+ case "output" -> context.getRpcOutput(rpcName);
+ default -> throw new IllegalArgumentException("Unhandled path " + key);
+ };
+
+ return getRpc(container);
+ }
+ });
+
+ private final @NonNull BindingClassLoader loader =
+ BindingClassLoader.create(BindingCodecContext.class, BYTECODE_DIRECTORY);