How to use registerTypeVariablesOn method of org.mockito.internal.util.reflection.GenericMetadataSupport class

Best Mockito code snippet using org.mockito.internal.util.reflection.GenericMetadataSupport.registerTypeVariablesOn

Source:GenericMetadataSupport.java Github

copy

Full Screen

...64 */65 protected Map<TypeVariable, Type> contextualActualTypeParameters = new HashMap<TypeVariable, Type>();666768 protected void registerTypeVariablesOn(Type classType) {69 if (!(classType instanceof ParameterizedType)) {70 return;71 }72 ParameterizedType parameterizedType = (ParameterizedType) classType;73 TypeVariable[] typeParameters = ((Class<?>) parameterizedType.getRawType()).getTypeParameters();74 Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();75 for (int i = 0; i < actualTypeArguments.length; i++) {76 TypeVariable typeParameter = typeParameters[i];77 Type actualTypeArgument = actualTypeArguments[i];7879 if (actualTypeArgument instanceof WildcardType) {80 contextualActualTypeParameters.put(typeParameter, boundsOf((WildcardType) actualTypeArgument));81 } else {82 contextualActualTypeParameters.put(typeParameter, actualTypeArgument);83 }84 // logger.log("For '" + parameterizedType + "' found type variable : { '" + typeParameter + "(" + System.identityHashCode(typeParameter) + ")" + "' : '" + actualTypeArgument + "(" + System.identityHashCode(typeParameter) + ")" + "' }");85 }86 }8788 protected void registerTypeParametersOn(TypeVariable[] typeParameters) {89 for (TypeVariable typeParameter : typeParameters) {90 contextualActualTypeParameters.put(typeParameter, boundsOf(typeParameter));91 // logger.log("For '" + typeParameter.getGenericDeclaration() + "' found type variable : { '" + typeParameter + "(" + System.identityHashCode(typeParameter) + ")" + "' : '" + boundsOf(typeParameter) + "' }");92 }93 }9495 /**96 * @param typeParameter The TypeVariable parameter97 * @return A {@link BoundedType} for easy bound information, if first bound is a TypeVariable98 * then retrieve BoundedType of this TypeVariable99 */100 private BoundedType boundsOf(TypeVariable typeParameter) {101 if (typeParameter.getBounds()[0] instanceof TypeVariable) {102 return boundsOf((TypeVariable) typeParameter.getBounds()[0]);103 }104 return new TypeVarBoundedType(typeParameter);105 }106107 /**108 * @param wildCard The WildCard type109 * @return A {@link BoundedType} for easy bound information, if first bound is a TypeVariable110 * then retrieve BoundedType of this TypeVariable111 */112 private BoundedType boundsOf(WildcardType wildCard) {113 /*114 * According to JLS(http://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html#4.5.1):115 * - Lower and upper can't coexist: (for instance, this is not allowed: <? extends List<String> & super MyInterface>)116 * - Multiple bounds are not supported (for instance, this is not allowed: <? extends List<String> & MyInterface>)117 */118119 WildCardBoundedType wildCardBoundedType = new WildCardBoundedType(wildCard);120 if (wildCardBoundedType.firstBound() instanceof TypeVariable) {121 return boundsOf((TypeVariable) wildCardBoundedType.firstBound());122 }123124 return wildCardBoundedType;125 }126127128129 /**130 * @return Raw type of the current instance.131 */132 public abstract Class<?> rawType();133134135136 /**137 * @return Returns extra interfaces <strong>if relevant</strong>, otherwise empty List.138 */139 public List<Type> extraInterfaces() {140 return Collections.emptyList();141 }142143 /**144 * @return Returns an array with the raw types of {@link #extraInterfaces()} <strong>if relevant</strong>.145 */146 public Class<?>[] rawExtraInterfaces() {147 return new Class[0];148 }149150151152 /**153 * @return Actual type arguments matching the type variables of the raw type represented by this {@link GenericMetadataSupport} instance.154 */155 public Map<TypeVariable, Type> actualTypeArguments() {156 TypeVariable[] typeParameters = rawType().getTypeParameters();157 LinkedHashMap<TypeVariable, Type> actualTypeArguments = new LinkedHashMap<TypeVariable, Type>();158159 for (TypeVariable typeParameter : typeParameters) {160161 Type actualType = getActualTypeArgumentFor(typeParameter);162163 actualTypeArguments.put(typeParameter, actualType);164 // logger.log("For '" + rawType().getCanonicalName() + "' returning explicit TypeVariable : { '" + typeParameter + "(" + System.identityHashCode(typeParameter) + ")" + "' : '" + actualType +"' }");165 }166167 return actualTypeArguments;168 }169170 protected Type getActualTypeArgumentFor(TypeVariable typeParameter) {171 Type type = this.contextualActualTypeParameters.get(typeParameter);172 if (type instanceof TypeVariable) {173 TypeVariable typeVariable = (TypeVariable) type;174 return getActualTypeArgumentFor(typeVariable);175 }176177 return type;178 }179180181182 /**183 * Resolve current method generic return type to a {@link GenericMetadataSupport}.184 *185 * @param method Method to resolve the return type.186 * @return {@link GenericMetadataSupport} representing this generic return type.187 */188 public GenericMetadataSupport resolveGenericReturnType(Method method) {189 Type genericReturnType = method.getGenericReturnType();190 // logger.log("Method '" + method.toGenericString() + "' has return type : " + genericReturnType.getClass().getInterfaces()[0].getSimpleName() + " : " + genericReturnType);191192 if (genericReturnType instanceof Class) {193 return new NotGenericReturnTypeSupport(genericReturnType);194 }195 if (genericReturnType instanceof ParameterizedType) {196 return new ParameterizedReturnType(this, method.getTypeParameters(), (ParameterizedType) method.getGenericReturnType());197 }198 if (genericReturnType instanceof TypeVariable) {199 return new TypeVariableReturnType(this, method.getTypeParameters(), (TypeVariable) genericReturnType);200 }201202 throw new MockitoException("Ouch, it shouldn't happen, type '" + genericReturnType.getClass().getCanonicalName() + "' on method : '" + method.toGenericString() + "' is not supported : " + genericReturnType);203 }204205 /**206 * Create an new instance of {@link GenericMetadataSupport} inferred from a {@link Type}.207 *208 * <p>209 * At the moment <code>type</code> can only be a {@link Class} or a {@link ParameterizedType}, otherwise210 * it'll throw a {@link MockitoException}.211 * </p>212 *213 * @param type The class from which the {@link GenericMetadataSupport} should be built.214 * @return The new {@link GenericMetadataSupport}.215 * @throws MockitoException Raised if type is not a {@link Class} or a {@link ParameterizedType}.216 */217 public static GenericMetadataSupport inferFrom(Type type) {218 Checks.checkNotNull(type, "type");219 if (type instanceof Class) {220 return new FromClassGenericMetadataSupport((Class<?>) type);221 }222 if (type instanceof ParameterizedType) {223 return new FromParameterizedTypeGenericMetadataSupport((ParameterizedType) type);224 }225226 throw new MockitoException("Type meta-data for this Type (" + type.getClass().getCanonicalName() + ") is not supported : " + type);227 }228229230 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////231 //// Below are specializations of GenericMetadataSupport that could handle retrieval of possible Types232 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////233234 /**235 * Generic metadata implementation for {@link Class}.236 *237 * Offer support to retrieve generic metadata on a {@link Class} by reading type parameters and type variables on238 * the class and its ancestors and interfaces.239 */240 private static class FromClassGenericMetadataSupport extends GenericMetadataSupport {241 private Class<?> clazz;242243 public FromClassGenericMetadataSupport(Class<?> clazz) {244 this.clazz = clazz;245 readActualTypeParametersOnDeclaringClass();246 }247248 private void readActualTypeParametersOnDeclaringClass() {249 registerTypeParametersOn(clazz.getTypeParameters());250 registerTypeVariablesOn(clazz.getGenericSuperclass());251 for (Type genericInterface : clazz.getGenericInterfaces()) {252 registerTypeVariablesOn(genericInterface);253 }254 }255256 @Override257 public Class<?> rawType() {258 return clazz;259 }260 }261262263 /**264 * Generic metadata implementation for "standalone" {@link ParameterizedType}.265 *266 * Offer support to retrieve generic metadata on a {@link ParameterizedType} by reading type variables of267 * the related raw type and declared type variable of this parameterized type.268 *269 * This class is not designed to work on ParameterizedType returned by {@link Method#getGenericReturnType()}, as270 * the ParameterizedType instance return in these cases could have Type Variables that refer to type declaration(s).271 * That's what meant the "standalone" word at the beginning of the Javadoc.272 * Instead use {@link ParameterizedReturnType}.273 */274 private static class FromParameterizedTypeGenericMetadataSupport extends GenericMetadataSupport {275 private ParameterizedType parameterizedType;276277 public FromParameterizedTypeGenericMetadataSupport(ParameterizedType parameterizedType) {278 this.parameterizedType = parameterizedType;279 readActualTypeParameters();280 }281282 private void readActualTypeParameters() {283 registerTypeVariablesOn(parameterizedType.getRawType());284 registerTypeVariablesOn(parameterizedType);285 }286287 @Override288 public Class<?> rawType() {289 return (Class<?>) parameterizedType.getRawType();290 }291 }292293294 /**295 * Generic metadata specific to {@link ParameterizedType} returned via {@link Method#getGenericReturnType()}.296 */297 private static class ParameterizedReturnType extends GenericMetadataSupport {298 private final ParameterizedType parameterizedType;299 private final TypeVariable[] typeParameters;300301 public ParameterizedReturnType(GenericMetadataSupport source, TypeVariable[] typeParameters, ParameterizedType parameterizedType) {302 this.parameterizedType = parameterizedType;303 this.typeParameters = typeParameters;304 this.contextualActualTypeParameters = source.contextualActualTypeParameters;305306 readTypeParameters();307 readTypeVariables();308 }309310 private void readTypeParameters() {311 registerTypeParametersOn(typeParameters);312 }313314 private void readTypeVariables() {315 registerTypeVariablesOn(parameterizedType);316 }317318 @Override319 public Class<?> rawType() {320 return (Class<?>) parameterizedType.getRawType();321 }322323 }324325326 /**327 * Generic metadata for {@link TypeVariable} returned via {@link Method#getGenericReturnType()}.328 */329 private static class TypeVariableReturnType extends GenericMetadataSupport {330 private final TypeVariable typeVariable;331 private final TypeVariable[] typeParameters;332 private Class<?> rawType;333334335336 public TypeVariableReturnType(GenericMetadataSupport source, TypeVariable[] typeParameters, TypeVariable typeVariable) {337 this.typeParameters = typeParameters;338 this.typeVariable = typeVariable;339 this.contextualActualTypeParameters = source.contextualActualTypeParameters;340341 readTypeParameters();342 readTypeVariables();343 }344345 private void readTypeParameters() {346 registerTypeParametersOn(typeParameters);347 }348349 private void readTypeVariables() {350 for (Type type : typeVariable.getBounds()) {351 registerTypeVariablesOn(type);352 }353 registerTypeVariablesOn(getActualTypeArgumentFor(typeVariable));354 }355356 @Override357 public Class<?> rawType() {358 if (rawType == null) {359 rawType = extractRawTypeOf(typeVariable);360 }361 return rawType;362 }363364 private Class<?> extractRawTypeOf(Type type) {365 if (type instanceof Class) {366 return (Class<?>) type;367 } ...

Full Screen

Full Screen

registerTypeVariablesOn

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.util.reflection.GenericMetadataSupport;2import org.mockito.internal.util.reflection.GenericTypeExtractor;3import org.mockito.internal.util.reflection.TypeResolver;4import java.lang.reflect.ParameterizedType;5import java.lang.reflect.Type;6import java.util.List;7class GenericMetadataSupportTest {8 public static void main(String[] args) {9 new GenericMetadataSupportTest().test();10 }11 private void test() {12 GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();13 Class<?> aClass = A.class;14 Type genericSuperclass = aClass.getGenericSuperclass();15 ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;16 Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();17 TypeResolver typeResolver = new TypeResolver();18 Type[] typeVariablesMap = typeResolver.resolve(actualTypeArguments, aClass);19 GenericTypeExtractor genericTypeExtractor = new GenericTypeExtractor();20 genericMetadataSupport.registerTypeVariablesOn(genericTypeExtractor, typeVariablesMap);21 List<String> list = genericTypeExtractor.getTypeVariable("T", List.class);22 System.out.println(list);23 }24 private static class A<T> extends B<T, String> {25 }26 private static class B<T, S> {27 }28}29Output: [Ljava.lang.String;@1b6d358630import org.mockito.internal.util.reflection.GenericMetadataSupport;31import org.mockito.internal.util.reflection.GenericTypeExtractor;32import org.mockito.internal.util.reflection.TypeResolver;33import java.lang.reflect.ParameterizedType;34import java.lang.reflect.Type;35import java.util.List;36class GenericMetadataSupportTest {37 public static void main(String[] args) {38 new GenericMetadataSupportTest().test();39 }40 private void test() {41 GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();42 Class<?> aClass = A.class;43 Type genericSuperclass = aClass.getGenericSuperclass();44 ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;45 Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();46 TypeResolver typeResolver = new TypeResolver();47 Type[] typeVariablesMap = typeResolver.resolve(actualTypeArguments, aClass);48 GenericTypeExtractor genericTypeExtractor = new GenericTypeExtractor();49 genericMetadataSupport.registerTypeVariablesOn(g

Full Screen

Full Screen

registerTypeVariablesOn

Using AI Code Generation

copy

Full Screen

1GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();2genericMetadataSupport.registerTypeVariablesOn( new TypeLiteral< Pair< String, List< Integer >>>() {}.getType());3GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();4genericMetadataSupport.registerTypeVariablesOn( new TypeLiteral< Pair< String, List< Integer >>>() {}.getType());5GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();6genericMetadataSupport.registerTypeVariablesOn( new TypeLiteral< Pair< String, List< Integer >>>() {}.getType());7GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();8genericMetadataSupport.registerTypeVariablesOn( new TypeLiteral< Pair< String, List< Integer >>>() {}.getType());9GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();10genericMetadataSupport.registerTypeVariablesOn( new TypeLiteral< Pair< String, List< Integer >>>() {}.getType());11GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();12genericMetadataSupport.registerTypeVariablesOn( new TypeLiteral< Pair< String, List< Integer >>>() {}.getType());13GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();14genericMetadataSupport.registerTypeVariablesOn( new TypeLiteral< Pair< String, List< Integer >>>() {}.getType());15GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();16genericMetadataSupport.registerTypeVariablesOn( new TypeLiteral< Pair< String, List< Integer >>>() {}.getType());

Full Screen

Full Screen

registerTypeVariablesOn

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.util.reflection.GenericMetadataSupport2import org.mockito.internal.util.reflection.FieldInitializer3class GenericMetadataSupportTest {4 static void main(String[] args) {5 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(A)6 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(B)7 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(C)8 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(D)9 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(E)10 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(F)11 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(G)12 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(H)13 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(I)14 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(J)15 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(K)16 println "output: " + GenericMetadataSupport.registerTypeVariablesOn(L)

Full Screen

Full Screen

registerTypeVariablesOn

Using AI Code Generation

copy

Full Screen

1import java.util.List;2import java.util.Map;3import java.util.Set;4import org.mockito.internal.util.reflection.GenericMetadataSupport;5public class TestMockitoGenericMetadataSupport {6 public static void main(String[] args) throws Exception {7 GenericMetadataSupport.registerTypeVariablesOn(Concrete.class, Concrete.class);8 Concrete concrete = new Concrete();9 concrete.method(null);10 }11 static class Concrete extends Abstract<Map<String, List<Set<String>>>> {12 public void method(Map<String, List<Set<String>>> map) {13 System.out.println(map);14 }15 }16 static abstract class Abstract<T> {17 public abstract void method(T

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful