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

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

Source:GenericMetadataSupport.java Github

copy

Full Screen

...384 @Override385 public List<Type> extraInterfaces() {386 Type type = extractActualBoundedTypeOf(typeVariable);387 if (type instanceof BoundedType) {388 return Arrays.asList(((BoundedType) type).interfaceBounds());389 }390 if (type instanceof ParameterizedType) {391 return Collections.singletonList(type);392 }393 if (type instanceof Class) {394 return Collections.emptyList();395 }396 throw new MockitoException("Cannot extract extra-interfaces from '" + typeVariable + "' : '" + type + "'");397 }398399 /**400 * @return Returns an array with the extracted raw types of {@link #extraInterfaces()}.401 * @see #extractRawTypeOf(java.lang.reflect.Type)402 */403 public Class<?>[] rawExtraInterfaces() {404 List<Type> extraInterfaces = extraInterfaces();405 List<Class<?>> rawExtraInterfaces = new ArrayList<Class<?>>();406 for (Type extraInterface : extraInterfaces) {407 Class<?> rawInterface = extractRawTypeOf(extraInterface);408 // avoid interface collision with actual raw type (with typevariables, resolution ca be quite aggressive)409 if(!rawType().equals(rawInterface)) {410 rawExtraInterfaces.add(rawInterface);411 }412 }413 return rawExtraInterfaces.toArray(new Class[rawExtraInterfaces.size()]);414 }415416 private Type extractActualBoundedTypeOf(Type type) {417 if (type instanceof TypeVariable) {418 /*419 If type is a TypeVariable, then it is needed to gather data elsewhere. Usually TypeVariables are declared420 on the class definition, such as such as List<E>.421 */422 return extractActualBoundedTypeOf(contextualActualTypeParameters.get(type));423 }424 if (type instanceof BoundedType) {425 Type actualFirstBound = extractActualBoundedTypeOf(((BoundedType) type).firstBound());426 if (!(actualFirstBound instanceof BoundedType)) {427 return type; // avoid going one step further, ie avoid : O(TypeVar) -> K(TypeVar) -> Some ParamType428 }429 return actualFirstBound;430 }431 return type; // irrelevant, we don't manage other types as they are not bounded.432 }433 }434435436437 /**438 * Non-Generic metadata for {@link Class} returned via {@link Method#getGenericReturnType()}.439 */440 private static class NotGenericReturnTypeSupport extends GenericMetadataSupport {441 private final Class<?> returnType;442443 public NotGenericReturnTypeSupport(Type genericReturnType) {444 returnType = (Class<?>) genericReturnType;445 }446447 @Override448 public Class<?> rawType() {449 return returnType;450 }451 }452453454455 /**456 * Type representing bounds of a type457 *458 * @see TypeVarBoundedType459 * @see <a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html#4.4">http://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html#4.4</a>460 * @see WildCardBoundedType461 * @see <a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html#4.5.1">http://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html#4.5.1</a>462 */463 public static interface BoundedType extends Type {464 Type firstBound();465466 Type[] interfaceBounds();467 }468469 /**470 * Type representing bounds of a type variable, allows to keep all bounds information.471 *472 * <p>It uses the first bound in the array, as this array is never null and always contains at least473 * one element (Object is always here if no bounds are declared).</p>474 *475 * <p>If upper bounds are declared with SomeClass and additional interfaces, then firstBound will be SomeClass and476 * interfacesBound will be an array of the additional interfaces.477 *478 * i.e. <code>SomeClass</code>.479 * <pre class="code"><code class="java">480 * interface UpperBoundedTypeWithClass<E extends Comparable<E> & Cloneable> {481 * E get();482 * }483 * // will return Comparable type484 * </code></pre>485 * </p>486 *487 * @see <a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html#4.4">http://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html#4.4</a>488 */489 public static class TypeVarBoundedType implements BoundedType {490 private TypeVariable typeVariable;491492493 public TypeVarBoundedType(TypeVariable typeVariable) {494 this.typeVariable = typeVariable;495 }496497 /**498 * @return either a class or an interface (parameterized or not), if no bounds declared Object is returned.499 */500 public Type firstBound() {501 return typeVariable.getBounds()[0]; //502 }503504 /**505 * On a Type Variable (typeVar extends C_0 & I_1 & I_2 & etc), will return an array506 * containing I_1 and I_2.507 *508 * @return other bounds for this type, these bounds can only be only interfaces as the JLS says,509 * empty array if no other bound declared.510 */511 public Type[] interfaceBounds() {512 Type[] interfaceBounds = new Type[typeVariable.getBounds().length - 1];513 System.arraycopy(typeVariable.getBounds(), 1, interfaceBounds, 0, typeVariable.getBounds().length - 1);514 return interfaceBounds;515 }516517 @Override518 public boolean equals(Object o) {519 if (this == o) return true;520 if (o == null || getClass() != o.getClass()) return false;521522 return typeVariable.equals(((TypeVarBoundedType) o).typeVariable);523524 }525526 @Override527 public int hashCode() {528 return typeVariable.hashCode();529 }530531 @Override532 public String toString() {533 final StringBuilder sb = new StringBuilder();534 sb.append("{firstBound=").append(firstBound());535 sb.append(", interfaceBounds=").append(Arrays.deepToString(interfaceBounds()));536 sb.append('}');537 return sb.toString();538 }539540 public TypeVariable typeVariable() {541 return typeVariable;542 }543 }544545 /**546 * Type representing bounds of a wildcard, allows to keep all bounds information.547 *548 * <p>The JLS says that lower bound and upper bound are mutually exclusive, and that multiple bounds549 * are not allowed.550 *551 * @see <a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html#4.4">http://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html#4.4</a>552 */553 public static class WildCardBoundedType implements BoundedType {554 private WildcardType wildcard;555556557 public WildCardBoundedType(WildcardType wildcard) {558 this.wildcard = wildcard;559 }560561 /**562 * @return The first bound, either a type or a reference to a TypeVariable563 */564 public Type firstBound() {565 Type[] lowerBounds = wildcard.getLowerBounds();566 Type[] upperBounds = wildcard.getUpperBounds();567568 return lowerBounds.length != 0 ? lowerBounds[0] : upperBounds[0];569 }570571 /**572 * @return An empty array as, wildcard don't support multiple bounds.573 */574 public Type[] interfaceBounds() {575 return new Type[0];576 }577578 @Override579 public boolean equals(Object o) {580 if (this == o) return true;581 if (o == null || getClass() != o.getClass()) return false;582583 return wildcard.equals(((TypeVarBoundedType) o).typeVariable);584585 }586587 @Override588 public int hashCode() {589 return wildcard.hashCode();590 }591592 @Override593 public String toString() {594 final StringBuilder sb = new StringBuilder();595 sb.append("{firstBound=").append(firstBound());596 sb.append(", interfaceBounds=[]}");597 return sb.toString();598 }599600 public WildcardType wildCard() {601 return wildcard;602 }603 }604605}606607 ...

Full Screen

Full Screen

interfaceBounds

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.util.reflection;2import org.mockito.internal.util.collections.ListUtil;3import java.lang.reflect.Method;4import java.lang.reflect.Type;5public class GenericMetadataSupport {6 private final static GenericMetadataSupport INSTANCE = new GenericMetadataSupport();7 public static GenericMetadataSupport instance() {8 return INSTANCE;9 }10 private GenericMetadataSupport() {11 }12 public Type[] interfaceBounds(Type type) {13 if (type instanceof Class) {14 return ((Class<?>) type).getGenericInterfaces();15 }16 if (type instanceof java.lang.reflect.ParameterizedType) {17 return ((java.lang.reflect.ParameterizedType) type).getRawType().getGenericInterfaces();18 }19 return new Type[0];20 }21 public static boolean isAvailable() {22 try {23 Class<?> genericMetadataSupportClass = Class.forName(GENERIC_METADATA_SUPPORT_CLASS_NAME);24 Method interfaceBoundsMethod = genericMetadataSupportClass.getMethod(INTERFACE_BOUNDS_METHOD_NAME, Class.forName("java.lang.reflect.Type"));25 return interfaceBoundsMethod != null && interfaceBoundsMethod.getReturnType().equals(Class.forName("java.lang.reflect.Type[]"));26 } catch (Exception e) {27 return false;28 }29 }30}31package org.mockito.internal.util.reflection;32import org.junit.Before;33import org.junit.Test;34import org.mockito.internal.util.collections.ListUtil;35import java.lang.reflect.Type;36import java.util.List;37import static org.junit.Assert.assertEquals;38import static org.junit.Assert.assertTrue;39public class GenericMetadataSupportTest {40 private GenericMetadataSupport genericMetadataSupport;41 public void setup() {42 genericMetadataSupport = GenericMetadataSupport.instance();43 }44 public void should_return_empty_list_for_non_parameterized_type() {45 Type[] types = genericMetadataSupport.interfaceBounds(List.class);46 assertEquals(0, types.length);47 }

Full Screen

Full Screen

interfaceBounds

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.util.reflection.GenericMetadataSupport2import org.mockito.internal.util.reflection.GenericMetadataSupport.InterfaceBounds3def gms = new GenericMetadataSupport()4def bounds = gms.interfaceBounds(A.class, B.class)5assert bounds.size() == 16assert bounds[0].isGeneric()7assert bounds[0].getRawType() == Map8assert bounds[0].getTypeParameters().size() == 29assert bounds[0].getTypeParameters()[0] == String10assert bounds[0].getTypeParameters()[1] == Integer11assert bounds[0].getUpperBounds().size() == 112assert bounds[0].getUpperBounds()[0] == Object13assert bounds[0].getLowerBounds().size() == 014def bounds = gms.interfaceBounds(B.class, C.class)15assert bounds.size() == 116assert bounds[0].isGeneric()17assert bounds[0].getRawType() == Map18assert bounds[0].getTypeParameters().size() == 219assert bounds[0].getTypeParameters()[0] == String20assert bounds[0].getTypeParameters()[1] == Integer21assert bounds[0].getUpperBounds().size() == 122assert bounds[0].getUpperBounds()[0] == Object23assert bounds[0].getLowerBounds().size() == 024def bounds = gms.interfaceBounds(B.class, D.class)25assert bounds.size() == 126assert bounds[0].isGeneric()27assert bounds[0].getRawType() == Map28assert bounds[0].getTypeParameters().size() == 229assert bounds[0].getTypeParameters()[0] == String30assert bounds[0].getTypeParameters()[1] == Integer31assert bounds[0].getUpperBounds().size() == 132assert bounds[0].getUpperBounds()[0] == Object33assert bounds[0].getLowerBounds().size() == 034def bounds = gms.interfaceBounds(B.class, E.class)35assert bounds.size() == 136assert bounds[0].isGeneric()37assert bounds[0].getRawType() == Map38assert bounds[0].getTypeParameters().size() == 239assert bounds[0].getTypeParameters()[0] == String40assert bounds[0].getTypeParameters()[1] ==

Full Screen

Full Screen

interfaceBounds

Using AI Code Generation

copy

Full Screen

1 public void testGetGenericType() throws Exception {2 GenericMetadataSupport genericMetadataSupport = new GenericMetadataSupport();3 Class<?>[] genericTypes = genericMetadataSupport.interfaceBounds(InterfaceWithGenericType.class, InterfaceWithGenericType.class.getMethods()[0]);4 assertThat(genericTypes).hasSize(1);5 assertThat(genericTypes[0]).isEqualTo(String.class);6 }7 interface InterfaceWithGenericType {8 List<String> method();9 }10 interface InterfaceWithoutGenericType {11 List method();12 }13}

Full Screen

Full Screen

interfaceBounds

Using AI Code Generation

copy

Full Screen

1 public static <T> T interfaceBounds(Class<T> clazz) {2 return new GenericMetadataSupport().interfaceBounds(clazz, 0);3 }4}5public interface InterfaceWithBounds<T extends Number> { }6public interface InterfaceWithMultipleBounds<T extends Number & Comparable<T>> { }7public interface InterfaceWithBounds<T extends Number> { }8public interface InterfaceWithMultipleBounds<T extends Number & Comparable<T>> { }9public interface InterfaceWithBounds<T extends Number> { }10public interface InterfaceWithMultipleBounds<T extends Number & Comparable<T>> { }11public interface InterfaceWithBounds<T extends Number> { }12public interface InterfaceWithMultipleBounds<T extends Number & Comparable<T>> { }13public interface InterfaceWithBounds<T extends Number> { }

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