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

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

Source:GenericMetadataSupport.java Github

copy

Full Screen

...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 } ...

Full Screen

Full Screen

registerTypeParametersOn

Using AI Code Generation

copy

Full Screen

1public class GenericMetadataSupportTest {2 private GenericMetadataSupport genericMetadataSupport;3 public void setUp() {4 genericMetadataSupport = new GenericMetadataSupport();5 }6 public void testRegisterTypeParametersOn() throws Exception {7 class A<T> {8 T t;9 }10 class B extends A<String> {11 }12 class C extends B {13 }14 class D extends C {15 }16 genericMetadataSupport.registerTypeParametersOn(A.class, new TypeToken<A<String>>() {17 }.getType());18 genericMetadataSupport.registerTypeParametersOn(B.class, new TypeToken<B>() {19 }.getType());20 genericMetadataSupport.registerTypeParametersOn(C.class, new TypeToken<C>() {21 }.getType());22 genericMetadataSupport.registerTypeParametersOn(D.class, new TypeToken<D>() {23 }.getType());24 assertEquals(String.class, genericMetadataSupport.typeOf(A.class, "t"));25 assertEquals(String.class, genericMetadataSupport.typeOf(B.class, "t"));26 assertEquals(String.class, genericMetadataSupport.typeOf(C.class, "t"));27 assertEquals(String.class, genericMetadataSupport.typeOf(D.class, "t"));28 }29}30public class GenericMetadataSupportTest {31 private GenericMetadataSupport genericMetadataSupport;32 public void setUp() {33 genericMetadataSupport = new GenericMetadataSupport();34 }35 public void testRegisterTypeParametersOn() throws Exception {36 class A<T> {37 T t;38 }39 class B extends A<String> {40 }41 class C extends B {42 }43 class D extends C {44 }45 genericMetadataSupport.registerTypeParametersOn(A.class, new TypeToken<A<String>>() {46 }.getType());47 genericMetadataSupport.registerTypeParametersOn(B.class, new TypeToken<B>() {48 }.getType());49 genericMetadataSupport.registerTypeParametersOn(C.class, new TypeToken<C>() {50 }.getType());51 genericMetadataSupport.registerTypeParametersOn(D.class, new TypeToken<D>() {52 }.getType());53 assertEquals(String.class, genericMetadataSupport.typeOf(A.class, "t"));54 assertEquals(String.class, genericMetadataSupport.typeOf(B.class, "t"));55 assertEquals(String.class, genericMetadataSupport.typeOf(C.class, "t"));56 assertEquals(String.class, genericMetadataSupport

Full Screen

Full Screen

registerTypeParametersOn

Using AI Code Generation

copy

Full Screen

1package com.baeldung.mockito.generics;2import static org.mockito.Mockito.mock;3import java.lang.reflect.Method;4import java.util.List;5import org.junit.Test;6import org.mockito.internal.util.reflection.GenericMetadataSupport;7import org.mockito.internal.util.reflection.GenericMetadataSupport.GenericTypeExtractor;8import org.mockito.internal.util.reflection.GenericMetadataSupport.GenericTypeExtractor.GenericType;9public class GenericMetadataSupportUnitTest {10 public void givenGenericMetadataSupport_whenRegisterTypeParametersOn_thenCorrect() throws Exception {11 GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();12 Method method = GenericMetadataSupportUnitTest.class.getMethod("method", List.class);13 genericMetadataSupport.registerTypeParametersOn(method, mock(List.class));14 GenericTypeExtractor genericTypeExtractor = genericMetadataSupport.typeExtractorFor(method);15 GenericType genericType = genericTypeExtractor.genericTypeOf(List.class);16 System.out.println(genericType);17 }18 public void method(List<String> list) {19 }20}

Full Screen

Full Screen

registerTypeParametersOn

Using AI Code Generation

copy

Full Screen

1package com.mycompany.app;2import java.util.List;3import java.util.Map;4import org.mockito.internal.util.reflection.GenericMetadataSupport;5public class App {6 public static void main(String[] args) {7 GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();8 genericMetadataSupport.registerTypeParametersOn(List.class, Map.class);9 System.out.println("Hello World!");10 }11}

Full Screen

Full Screen

registerTypeParametersOn

Using AI Code Generation

copy

Full Screen

1public class GenericMetadataSupportTest {2 public void shouldRegisterTypeParametersOn() {3 GenericMetadataSupport.registerTypeParametersOn(Child.class, Parent.class);4 GenericMetadataSupport.registerTypeParametersOn(Child.class, Child.class);5 GenericMetadataSupport.registerTypeParametersOn(Child.class, GrandParent.class);6 }7 public void shouldRegisterTypeParametersOn2() {8 GenericMetadataSupport.registerTypeParametersOn(Child.class, Parent.class);9 GenericMetadataSupport.registerTypeParametersOn(Child.class, Child.class);10 GenericMetadataSupport.registerTypeParametersOn(Child.class, GrandParent.class);11 }12 public void shouldRegisterTypeParametersOn3() {13 GenericMetadataSupport.registerTypeParametersOn(Child.class, Parent.class);14 GenericMetadataSupport.registerTypeParametersOn(Child.class, Child.class);15 GenericMetadataSupport.registerTypeParametersOn(Child.class, GrandParent.class);16 }17 public interface GrandParent<T> {18 }19 public interface Parent<T> extends GrandParent<T> {20 }21 public interface Child<T> extends Parent<T> {22 }23}24 at org.mockito.internal.util.reflection.GenericMetadataSupport.registerTypeParametersOn(GenericMetadataSupport.java:42)25 at org.mockito.internal.util.reflection.GenericMetadataSupportTest.shouldRegisterTypeParametersOn(GenericMetadataSupportTest.java:17)26 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)27 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)28 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)29 at java.lang.reflect.Method.invoke(Method.java:606)30 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)31 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)32 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)33 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)34 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)35 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)36 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47

Full Screen

Full Screen

registerTypeParametersOn

Using AI Code Generation

copy

Full Screen

1import spock.lang.Specification2import spock.lang.Unroll3import spock.util.matcher.HamcrestSupport4import spock.util.matcher.HamcrestSupport.*5import org.mockito.internal.util.reflection.GenericMetadataSupport6import org.mockito.internal.util.reflection.GenericMetadataSupport.*7import org.mockito.internal.util.reflection.ParameterizedTypeImpl8class GenericMetadataSupportTest extends Specification {9 def "should get generic type parameters for a generic class"() {10 def typeParameters = GenericMetadataSupport.registerTypeParametersOn(HasGenericParameterizedType)11 assert typeParameters.size() == 112 assert typeParameters.get(0) == String13 }14 def "should get generic type parameters for a generic class with multiple parameters"() {15 def typeParameters = GenericMetadataSupport.registerTypeParametersOn(HasGenericParameterizedType2)16 assert typeParameters.size() == 217 assert typeParameters.get(0) == String18 assert typeParameters.get(1) == Integer19 }20 def "should get generic type parameters for a generic class with multiple parameters and a wild card"() {21 def typeParameters = GenericMetadataSupport.registerTypeParametersOn(HasGenericParameterizedType3)22 assert typeParameters.size() == 223 assert typeParameters.get(0) == String24 assert typeParameters.get(1) == Integer25 }26 def "should get generic type parameters for a generic class with multiple parameters and a wild card and a generic"() {27 def typeParameters = GenericMetadataSupport.registerTypeParametersOn(HasGenericParameterizedType4)28 assert typeParameters.size() == 329 assert typeParameters.get(0) == String30 assert typeParameters.get(1) == Integer31 assert typeParameters.get(2) == List32 }33 def "should get generic type parameters for a generic class with multiple parameters and a wild card and a generic and a generic with parameters"() {34 def typeParameters = GenericMetadataSupport.registerTypeParametersOn(HasGenericParameterizedType5)35 assert typeParameters.size() == 436 assert typeParameters.get(0) == String37 assert typeParameters.get(1) == Integer38 assert typeParameters.get(2) == List

Full Screen

Full Screen

registerTypeParametersOn

Using AI Code Generation

copy

Full Screen

1public class TestClass<T> {2 public void testMethod(T t) {3 System.out.println(t);4 }5}6TestClass<Integer> testClass = new TestClass<Integer>();7GenericMetadataSupport.registerTypeParametersOn(TestClass.class, testClass, new Type[] { Integer.class });8testClass.testMethod(1);9 at org.mockito.internal.util.reflection.GenericMetadataSupport.registerTypeParametersOn(GenericMetadataSupport.java:50)10 at Test.main(Test.java:7)

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