public class ConstructorGenericsContext extends GenericsContext
class A {
<T> A(T arg);
}
Such generics are not known in type and, as a result, any operation on type with such generic will lead to
unknown generic exception.
Also, context contains special methods for parameters analysis.
GenericsContext.RootContextAwareTypeWriterallTypeGenerics, CURRENT_POSITION_MARKER, ownerGenerics, ownerTypecurrentType, genericsInfo, typeGenerics| Constructor and Description |
|---|
ConstructorGenericsContext(GenericsInfo genericsInfo,
java.lang.reflect.Constructor constructor,
GenericsContext root) |
| Modifier and Type | Method and Description |
|---|---|
ConstructorGenericsContext |
constructor(java.lang.reflect.Constructor constructor)
Navigates current context to specific constructor (type context is switched to constructor declaring class).
|
java.util.Map<java.lang.String,java.lang.reflect.Type> |
constructorGenericsMap()
class A<E>{
<T, K extends E> A(T arg1, K arg2);
}
class B extends A<Serializable> {}
context.constructor(A.class.getConstructor(Object.class, Object.class)).constructorGenericsMap() ==
["T": Object.class, "K": Serializable.class]
For constructor generics it's impossible to know actual type (available only in time of constructor call),
so generics resolved as upper bound. |
java.util.List<java.lang.reflect.Type> |
constructorGenericTypes()
<T extends Serializable> Some(T arg);. |
protected java.util.Map<java.lang.String,java.lang.reflect.Type> |
contextGenerics() |
java.lang.reflect.Constructor |
currentConstructor() |
GenericDeclarationScope |
getGenericsScope()
For example,
CLASS scope could resolve only class level generics, whereas
METHOD could see method context generics. |
java.lang.reflect.GenericDeclaration |
getGenericsSource()
Declaration source could be: class, method or constructor.
|
GenericsContext |
parameterType(int pos)
Create generics context for parameter type (with correctly resolved root generics).
|
GenericsContext |
parameterTypeAs(int pos,
java.lang.Class<?> asType)
Create generics context for actual class, passed into parameter type (assuming you have access to that instance
or know exact type).
|
java.util.List<java.lang.Class<?>> |
resolveParameters()
Useful for introspection, to know exact parameter types.
|
java.util.List<java.lang.reflect.Type> |
resolveParametersTypes()
Returns parameter types with resolved generic variables.
|
java.lang.reflect.Type |
resolveParameterType(int pos) |
java.lang.String |
toString() |
java.lang.String |
toStringConstructor() |
chooseContext, inlyingType, inlyingTypeAs, isInlying, method, ownerClass, ownerGenericsMap, rootContext, switchContext, typecurrentClass, fieldType, fieldTypeAs, generic, generic, genericAsString, genericAsString, generics, genericsAsString, genericsMap, genericType, genericType, genericTypes, getGenericsInfo, resolveClass, resolveFieldClass, resolveFieldGeneric, resolveFieldGenerics, resolveFieldType, resolveGenericOf, resolveGenericsOf, resolveType, resolveTypeGenerics, toStringCurrentClass, toStringCurrentClassDeclaration, toStringType, visibleGenericsMappublic ConstructorGenericsContext(GenericsInfo genericsInfo, java.lang.reflect.Constructor constructor, GenericsContext root)
public java.lang.reflect.Constructor currentConstructor()
public java.util.List<java.lang.reflect.Type> constructorGenericTypes()
<T extends Serializable> Some(T arg);.
context.constructor(Some.class.getConstructor(Object.class)).constructorGenericTypes() ==
[Class<Serializable>]public java.util.Map<java.lang.String,java.lang.reflect.Type> constructorGenericsMap()
class A<E>{
<T, K extends E> A(T arg1, K arg2);
}
class B extends A<Serializable> {}
context.constructor(A.class.getConstructor(Object.class, Object.class)).constructorGenericsMap() ==
["T": Object.class, "K": Serializable.class]
For constructor generics it's impossible to know actual type (available only in time of constructor call),
so generics resolved as upper bound.public java.util.List<java.lang.Class<?>> resolveParameters()
class A extends B<Long> {}
class B<T> {
B(T a, Integer b);
}
Resolving parameters in context of root class:
constructor(B.class.getConstructor(Object.class)).resolveParameters() ==
[Long.class, Integer.class]
Note: may return primitives because it might be important to differentiate actual value.
Use TypeUtils.wrapPrimitive(Class) to box possible primitive,
if required.
resolveParametersTypes()public java.util.List<java.lang.reflect.Type> resolveParametersTypes()
class A extends B<Long> {}
class B<T>{
B(List<T> arg);
}
Resolving parameters types in context of root class:
constructor(B.class.getConstructor(List.class)).resolveParametersTypes() == [List<Long>]resolveParameters()public java.lang.reflect.Type resolveParameterType(int pos)
pos - parameter position (form 0)java.lang.IllegalArgumentException - if parameter index is incorrectpublic GenericsContext parameterType(int pos)
class A<T> {
A(B<T> arg);
}
class C extends A<String> {}
Build generics context for parameter type (to continue analyzing parameter type fields):
(context of C).constructor(A.class.getConstructor(B.class)).parameterType(0)
== generics context of B<String>
Note that, in contrast to direct resolution GenericsResolver.resolve(B.class), actual root generic
would be counted for hierarchy resolution.
pos - parameter position (from 0)java.lang.IllegalArgumentException - if parameter index is incorrectGenericsContext.inlyingType(Type)public GenericsContext parameterTypeAs(int pos, java.lang.Class<?> asType)
Other than target type, method is the same as parameterType(int).
pos - parameter position (from 0)asType - required target type to build generics context for (must include declared type as base class)java.lang.IllegalArgumentException - if parameter index is incorrectGenericsContext.inlyingTypeAs(Type, Class)public java.lang.String toStringConstructor()
public java.lang.String toString()
toString in class GenericsContextpublic GenericDeclarationScope getGenericsScope()
AbstractGenericsContextCLASS scope could resolve only class level generics, whereas
METHOD could see method context generics. May be used for current context differentiation
(instead of instanceof).getGenericsScope in class GenericsContextAbstractGenericsContext.getGenericsSource()public java.lang.reflect.GenericDeclaration getGenericsSource()
AbstractGenericsContextgetGenericsSource in class GenericsContextAbstractGenericsContext.getGenericsScope()public ConstructorGenericsContext constructor(java.lang.reflect.Constructor constructor)
AbstractGenericsContext<T> MyType(T arg).
Use context to work with constructor arguments.
constructor in class GenericsContextconstructor - constructor in current class hierarchy to navigate to (may be constructor from subclass,
relative to currently selected, in this case context type is automatically switched)protected java.util.Map<java.lang.String,java.lang.reflect.Type> contextGenerics()
contextGenerics in class GenericsContext