+ private Entry<GeneratedType, WithStatus> findTypeWithSchema(final String className) {
+ // All we have is a straight FQCN, which we need to split into a hierarchical JavaTypeName. This involves
+ // some amount of guesswork -- we do that by peeling components at the dot and trying out, e.g. given
+ // "foo.bar.baz.Foo.Bar.Baz" we end up trying:
+ // "foo.bar.baz.Foo.Bar" + "Baz"
+ // "foo.bar.baz.Foo" + Bar" + "Baz"
+ // "foo.bar.baz" + Foo" + Bar" + "Baz"
+ //
+ // And see which one sticks. We cannot rely on capital letters, as they can be used in package names, too.
+ // Nested classes are not common, so we should be arriving at the result pretty quickly.
+ final List<String> components = new ArrayList<>();
+ String packageName = className;
+
+ for (int lastDot = packageName.lastIndexOf(DOT); lastDot != -1; lastDot = packageName.lastIndexOf(DOT)) {
+ components.add(packageName.substring(lastDot + 1));
+ packageName = packageName.substring(0, lastDot);
+
+ final Iterator<String> it = components.iterator();
+ JavaTypeName name = JavaTypeName.create(packageName, it.next());
+ while (it.hasNext()) {
+ name = name.createEnclosed(it.next());
+ }
+
+ final Type type = new ReferencedTypeImpl(name);
+ final Optional<WithStatus> optSchema = runtimeTypes.findSchema(type);
+ if (!optSchema.isPresent()) {
+ continue;
+ }
+
+ final WithStatus schema = optSchema.get();
+ final Optional<Type> optDefinedType = runtimeTypes.findType(schema);
+ if (!optDefinedType.isPresent()) {
+ continue;
+ }
+
+ final Type definedType = optDefinedType.get();
+ if (definedType instanceof GeneratedTypeBuilder) {
+ return new SimpleEntry<>(((GeneratedTypeBuilder) definedType).build(), schema);
+ }
+ checkArgument(definedType instanceof GeneratedType, "Type %s is not a GeneratedType", className);
+ return new SimpleEntry<>((GeneratedType) definedType, schema);
+ }
+
+ throw new IllegalArgumentException("Failed to find type for " + className);
+ }
+