Package org.openrewrite.java.internal
Class DefaultJavaTypeFactory
java.lang.Object
org.openrewrite.java.internal.DefaultJavaTypeFactory
- All Implemented Interfaces:
JavaTypeFactory
Default implementation of
JavaTypeFactory: each method looks up by
key in a single JavaTypeCache. On hit, the cached instance is
returned. On miss, the compute* family constructs a stub, registers
it, then runs the initializer; the *For family invokes the supplier
atomically and caches the result.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionarrayFor(String signature, Supplier<JavaType.Array> builder) Atomic build forJavaType.Array.computeClass(String fqn, long flags, JavaType.FullyQualified.Kind kind, Consumer<JavaType.Class> initializer) Cache-or-build for a fully-attributedJavaType.Class.computeGenericTypeVariable(String signature, String name, JavaType.GenericTypeVariable.Variance variance, Consumer<JavaType.GenericTypeVariable> initializer) Cache-or-build forJavaType.GenericTypeVariable.computeParameterized(String signature, Consumer<JavaType.Parameterized> initializer) Cache-or-build forJavaType.Parameterized.intersectionFor(String signature, Supplier<JavaType.Intersection> builder) Atomic build forJavaType.Intersection.protected <T> @Nullable TProtected lookup hook for subclasses that need to read the cache before routing to the chain (e.g., the partition-backed factory's chain hit pre-registration).methodFor(String signature, Supplier<JavaType.Method> stub, Consumer<JavaType.Method> initializer) Build forJavaType.Methodwith cycle-breaking on the same key.protected voidProtected write hook for subclasses that pre-register chain instances into the signature-keyed cache so subsequent*Forcalls return canonical instances.variableFor(String signature, Supplier<JavaType.Variable> builder) Atomic build forJavaType.Variable.
-
Constructor Details
-
DefaultJavaTypeFactory
-
-
Method Details
-
lookup
Protected lookup hook for subclasses that need to read the cache before routing to the chain (e.g., the partition-backed factory's chain hit pre-registration). -
register
Protected write hook for subclasses that pre-register chain instances into the signature-keyed cache so subsequent*Forcalls return canonical instances. -
computeClass
public JavaType.Class computeClass(String fqn, long flags, JavaType.FullyQualified.Kind kind, Consumer<JavaType.Class> initializer) Description copied from interface:JavaTypeFactoryCache-or-build for a fully-attributedJavaType.Class.If a Class is already cached for
fqn, the cached instance is returned andinitializerdoes not run. Otherwise a stub is constructed withflagsandkindpopulated, registered in the cache, and the initializer runs to populate the remaining fields (typically viaJavaType.Class.unsafeSet(java.util.List<org.openrewrite.java.tree.JavaType>, org.openrewrite.java.tree.JavaType.FullyQualified, org.openrewrite.java.tree.JavaType.FullyQualified, java.util.List<org.openrewrite.java.tree.JavaType.FullyQualified>, java.util.List<org.openrewrite.java.tree.JavaType.FullyQualified>, java.util.List<org.openrewrite.java.tree.JavaType.Variable>, java.util.List<org.openrewrite.java.tree.JavaType.Method>)).- Specified by:
computeClassin interfaceJavaTypeFactory- Parameters:
fqn- the fully-qualified name of the class, in the formJavaType.FullyQualified.getFullyQualifiedName()returns.
-
computeParameterized
public JavaType.Parameterized computeParameterized(String signature, Consumer<JavaType.Parameterized> initializer) Description copied from interface:JavaTypeFactoryCache-or-build forJavaType.Parameterized. The initializer's recursivetype(...)calls on type arguments can re-encounter the same signature (e.g. F-bounded polymorphism, Kotlin parameterized self-refs). The pre-registered stub lets those recursive lookups observe a usable partially-populated instance rather than re-entering the builder and looping. The initializer typically seeds the stub with its raw class viaJavaType.Parameterized.unsafeSet(org.openrewrite.java.tree.JavaType.FullyQualified, java.util.List<org.openrewrite.java.tree.JavaType>)before resolving type arguments.- Specified by:
computeParameterizedin interfaceJavaTypeFactory- Parameters:
signature- an opaque per-factory key fromJavaTypeSignatureBuilder.parameterizedSignature(java.lang.Object)— canonical shapejava.util.List<java.util.List<Integer>>.
-
computeGenericTypeVariable
public JavaType.GenericTypeVariable computeGenericTypeVariable(String signature, String name, JavaType.GenericTypeVariable.Variance variance, Consumer<JavaType.GenericTypeVariable> initializer) Description copied from interface:JavaTypeFactoryCache-or-build forJavaType.GenericTypeVariable. Type variable bounds can reference the variable itself — e.g.<T extends Comparable<T>>whereT's upper bound is aJavaType.Parameterizedthat referencesTitself. Pre-registering the stub before the initializer runs makes the recursive lookup find the stub instead of looping.- Specified by:
computeGenericTypeVariablein interfaceJavaTypeFactory- Parameters:
signature- an opaque per-factory key fromJavaTypeSignatureBuilder.genericSignature(java.lang.Object)— canonical shapeGeneric{U extends java.lang.Comparable}(covariant) orGeneric{U super java.lang.Comparable}(contravariant).
-
methodFor
public JavaType.Method methodFor(String signature, Supplier<JavaType.Method> stub, Consumer<JavaType.Method> initializer) Description copied from interface:JavaTypeFactoryBuild forJavaType.Methodwith cycle-breaking on the same key. UnlikeJavaTypeFactory.variableFor(java.lang.String, java.util.function.Supplier<org.openrewrite.java.tree.JavaType.Variable>)/JavaTypeFactory.arrayFor(java.lang.String, java.util.function.Supplier<org.openrewrite.java.tree.JavaType.Array>)/JavaTypeFactory.intersectionFor(java.lang.String, java.util.function.Supplier<org.openrewrite.java.tree.JavaType.Intersection>), a method's attribution can recurse back to its own signature: an annotation-element method whose own annotations reference an annotation whose element is that same method (the Spring@AliasForshape —value()is annotated@AliasFor, and@AliasFor's element isvalue()). So this mirrorsJavaTypeFactory.computeClass(java.lang.String, long, org.openrewrite.java.tree.JavaType.FullyQualified.Kind, java.util.function.Consumer<org.openrewrite.java.tree.JavaType.Class>):stubconstructs the bare instance from non-recursive header fields, which is cached beforeinitializerruns the (possibly self-referential) attribution onto that same instance. A re-entrant lookup on the same signature then hits the cached stub instead of recursing — the signature and cache are the cycle-breaker, the same way they are forJavaTypeFactory.computeClass(java.lang.String, long, org.openrewrite.java.tree.JavaType.FullyQualified.Kind, java.util.function.Consumer<org.openrewrite.java.tree.JavaType.Class>).- Specified by:
methodForin interfaceJavaTypeFactory- Parameters:
signature- an opaque per-factory key from aJavaTypeSignatureBuilder— canonical shapecom.MyThing{name=add,return=void,parameters=[Integer]}.stub- constructs the bare method from non-recursive header fields (name, flags, parameter names, default value, declared formal type names); must not resolve any type that could recurse to this signature.initializer- populates the cached stub's recursive attribution (declaring type, return type, parameter types, thrown exceptions, annotations) viaunsafeSet.
-
variableFor
Description copied from interface:JavaTypeFactoryAtomic build forJavaType.Variable.- Specified by:
variableForin interfaceJavaTypeFactory- Parameters:
signature- an opaque per-factory key from aJavaTypeSignatureBuilder— canonical shapecom.MyThing{name=MY_FIELD}.
-
arrayFor
Description copied from interface:JavaTypeFactoryAtomic build forJavaType.Array.- Specified by:
arrayForin interfaceJavaTypeFactory- Parameters:
signature- an opaque per-factory key fromJavaTypeSignatureBuilder.arraySignature(java.lang.Object)— canonical shapeInteger[].
-
intersectionFor
public JavaType.Intersection intersectionFor(String signature, Supplier<JavaType.Intersection> builder) Description copied from interface:JavaTypeFactoryAtomic build forJavaType.Intersection.- Specified by:
intersectionForin interfaceJavaTypeFactory- Parameters:
signature- an opaque per-factory key from aJavaTypeSignatureBuilderbuilt from the intersection's component bounds.
-