@Override public Class<?> rawType() { Class<?> rawComponentType = genericArrayType.rawType(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < arity; i++) { stringBuilder.append("["); } try { return Class.forName(stringBuilder.append("L").append(rawComponentType.getName()).append(";").toString(), false, rawComponentType.getClassLoader()); } catch (ClassNotFoundException e) { throw new IllegalStateException("This was not supposed to happend", e); } } }
/** * @return Actual type arguments matching the type variables of the raw type represented by this {@link GenericMetadataSupport} instance. */ public Map<TypeVariable<?>, Type> actualTypeArguments() { TypeVariable<?>[] typeParameters = rawType().getTypeParameters(); LinkedHashMap<TypeVariable<?>, Type> actualTypeArguments = new LinkedHashMap<TypeVariable<?>, Type>(); for (TypeVariable<?> typeParameter : typeParameters) { Type actualType = getActualTypeArgumentFor(typeParameter); actualTypeArguments.put(typeParameter, actualType); // logger.log("For '" + rawType().getCanonicalName() + "' returning explicit TypeVariable : { '" + typeParameter + "(" + System.identityHashCode(typeParameter) + ")" + "' : '" + actualType +"' }"); } return actualTypeArguments; }
/** * Creates a mock using the Generics Metadata. * * <li>Finally as we want to mock the actual type, but we want to pass along the contextual generics meta-data * that was resolved for the current return type, for this to happen we associate to the mock an new instance of * {@link ReturnsDeepStubs} answer in which we will store the returned type generic metadata. * * @param returnTypeGenericMetadata The metadata to use to create the new mock. * @param parentMock The parent of the current deep stub mock. * @return The mock */ private Object newDeepStubMock(GenericMetadataSupport returnTypeGenericMetadata, Object parentMock) { MockCreationSettings parentMockSettings = MockUtil.getMockSettings(parentMock); return mockitoCore().mock( returnTypeGenericMetadata.rawType(), withSettingsUsing(returnTypeGenericMetadata, parentMockSettings) ); }
public Object answer(InvocationOnMock invocation) throws Throwable { GenericMetadataSupport returnTypeGenericMetadata = actualParameterizedType(invocation.getMock()).resolveGenericReturnType(invocation.getMethod()); Class<?> rawType = returnTypeGenericMetadata.rawType(); if (!mockitoCore().isTypeMockable(rawType)) { return delegate().returnValueFor(rawType); } return deepStub(invocation, returnTypeGenericMetadata); }
/** * Retrieve the expected type when it came from a primitive. If the type cannot be retrieve, return null. * * @param invocation the current invocation * @param returnType the expected return type * @return the type or null if not found */ private Class<?> findTypeFromGeneric(final InvocationOnMock invocation, final TypeVariable returnType) { // Class level final MockCreationSettings mockSettings = MockUtil.getMockHandler(invocation.getMock()).getMockSettings(); final GenericMetadataSupport returnTypeSupport = GenericMetadataSupport .inferFrom(mockSettings.getTypeToMock()) .resolveGenericReturnType(invocation.getMethod()); final Class<?> rawType = returnTypeSupport.rawType(); // Method level if (rawType == Object.class) { return findTypeFromGenericInArguments(invocation, returnType); } return rawType; }