/** * {@inheritDoc} */ public String getSymbol() { return typeVariable.getName(); }
@Override public int hashCode() { return Objects.hashCode(var.getGenericDeclaration(), var.getName()); }
/** * Checks whether two types are type variables describing the same. */ public static boolean sameTypeVars(Type t1, Type t2) { return t1 instanceof TypeVariable && t2 instanceof TypeVariable && ((TypeVariable<?>) t1).getName().equals(((TypeVariable<?>) t2).getName()) && ((TypeVariable<?>) t1).getGenericDeclaration().equals(((TypeVariable<?>) t2).getGenericDeclaration()); }
public static TypeBindings create(Class<?> erasedType, JavaType typeArg1, JavaType typeArg2) { // 30-Oct-2015, tatu: Minor optimization for relatively common cases TypeVariable<?>[] vars = TypeParamStash.paramsFor2(erasedType); int varLen = (vars == null) ? 0 : vars.length; if (varLen != 2) { throw new IllegalArgumentException("Cannot create TypeBindings for class "+erasedType.getName() +" with 2 type parameters: class expects "+varLen); } return new TypeBindings(new String[] { vars[0].getName(), vars[1].getName() }, new JavaType[] { typeArg1, typeArg2 }, null); }
public static TypeBindings create(Class<?> erasedType, JavaType typeArg1, JavaType typeArg2) { // 30-Oct-2015, tatu: Minor optimization for relatively common cases TypeVariable<?>[] vars = TypeParamStash.paramsFor2(erasedType); int varLen = (vars == null) ? 0 : vars.length; if (varLen != 2) { throw new IllegalArgumentException("Cannot create TypeBindings for class "+erasedType.getName() +" with 2 type parameters: class expects "+varLen); } return new TypeBindings(new String[] { vars[0].getName(), vars[1].getName() }, new JavaType[] { typeArg1, typeArg2 }, null); }
@Override public int hashCode() { return Objects.hashCode(var.getGenericDeclaration(), var.getName()); }
/** * Format a {@link TypeVariable} as a {@link String}. * @param v {@code TypeVariable} to format * @return String * @since 3.2 */ private static String typeVariableToString(final TypeVariable<?> v) { final StringBuilder buf = new StringBuilder(v.getName()); final Type[] bounds = v.getBounds(); if (bounds.length > 0 && !(bounds.length == 1 && Object.class.equals(bounds[0]))) { buf.append(" extends "); appendAllTo(buf, " & ", v.getBounds()); } return buf.toString(); }
@Override public boolean equals(Object obj) { if (NativeTypeVariableEquals.NATIVE_TYPE_VARIABLE_ONLY) { // equal only to our TypeVariable implementation with identical bounds if (obj != null && Proxy.isProxyClass(obj.getClass()) && Proxy.getInvocationHandler(obj) instanceof TypeVariableInvocationHandler) { TypeVariableInvocationHandler typeVariableInvocationHandler = (TypeVariableInvocationHandler) Proxy.getInvocationHandler(obj); TypeVariableImpl<?> that = typeVariableInvocationHandler.typeVariableImpl; return name.equals(that.getName()) && genericDeclaration.equals(that.getGenericDeclaration()) && bounds.equals(that.bounds); } return false; } else { // equal to any TypeVariable implementation regardless of bounds if (obj instanceof TypeVariable) { TypeVariable<?> that = (TypeVariable<?>) obj; return name.equals(that.getName()) && genericDeclaration.equals(that.getGenericDeclaration()); } return false; } } }
TypeParameterSignature(TypeVariable<?> typeParameter) { name = typeParameter.getName(); bounds = Arrays.asList(typeParameter.getBounds()); }
public static TypeBindings create(Class<?> erasedType, JavaType typeArg1) { // 30-Oct-2015, tatu: Minor optimization for relatively common cases TypeVariable<?>[] vars = TypeParamStash.paramsFor1(erasedType); int varLen = (vars == null) ? 0 : vars.length; if (varLen != 1) { throw new IllegalArgumentException("Cannot create TypeBindings for class "+erasedType.getName() +" with 1 type parameter: class expects "+varLen); } return new TypeBindings(new String[] { vars[0].getName() }, new JavaType[] { typeArg1 }, null); }
public static TypeBindings create(Class<?> erasedType, JavaType typeArg1) { // 30-Oct-2015, tatu: Minor optimization for relatively common cases TypeVariable<?>[] vars = TypeParamStash.paramsFor1(erasedType); int varLen = (vars == null) ? 0 : vars.length; if (varLen != 1) { throw new IllegalArgumentException("Cannot create TypeBindings for class "+erasedType.getName() +" with 1 type parameter: class expects "+varLen); } return new TypeBindings(new String[] { vars[0].getName() }, new JavaType[] { typeArg1 }, null); }
private static <D extends GenericDeclaration> TypeVariable<D> withBounds( TypeVariable<D> typeVariable, Type... bounds) { return Types.newArtificialTypeVariable( typeVariable.getGenericDeclaration(), typeVariable.getName(), bounds); }
private static void assertEqualTypeVariable(TypeVariable<?> expected, TypeVariable<?> actual) { assertEquals(expected.toString(), actual.toString()); assertEquals(expected.getName(), actual.getName()); assertEquals(expected.getGenericDeclaration(), actual.getGenericDeclaration()); if (!Types.NativeTypeVariableEquals.NATIVE_TYPE_VARIABLE_ONLY) { assertEquals(actual.toString(), expected.hashCode(), actual.hashCode()); } assertThat(actual.getBounds()) .asList() .containsExactlyElementsIn(asList(expected.getBounds())) .inOrder(); }
public void testStaticMethod_typeParameters() throws Exception { Invokable<?, ?> delegate = Prepender.method("prepend", String.class, Iterable.class); TypeVariable<?>[] variables = delegate.getTypeParameters(); assertThat(variables).hasLength(1); assertEquals("T", variables[0].getName()); }
public void testConstructor_typeParameters() throws Exception { TypeVariable<?>[] variables = Prepender.constructor().getTypeParameters(); assertThat(variables).hasLength(1); assertEquals("A", variables[0].getName()); }
@Test public void testLowerBoundedWildcardType() { final WildcardType lowerBounded = TypeUtils.wildcardType().withLowerBounds(java.sql.Date.class).build(); assertEquals(String.format("? super %s", java.sql.Date.class.getName()), TypeUtils.toString(lowerBounded)); assertEquals(String.format("? super %s", java.sql.Date.class.getName()), lowerBounded.toString()); final TypeVariable<Class<Iterable>> iterableT0 = Iterable.class.getTypeParameters()[0]; final WildcardType lowerTypeVariable = TypeUtils.wildcardType().withLowerBounds(iterableT0).build(); assertEquals(String.format("? super %s", iterableT0.getName()), TypeUtils.toString(lowerTypeVariable)); assertEquals(String.format("? super %s", iterableT0.getName()), lowerTypeVariable.toString()); }
@Test public void resolveTypeWithCustomVariableResolver() throws Exception { VariableResolver variableResolver = mock(VariableResolver.class); given(variableResolver.getSource()).willReturn(this); ResolvableType longType = ResolvableType.forClass(Long.class); given(variableResolver.resolveVariable(any())).willReturn(longType); ResolvableType variable = ResolvableType.forType( Fields.class.getField("typeVariableType").getGenericType(), variableResolver); ResolvableType parameterized = ResolvableType.forType( Fields.class.getField("parameterizedType").getGenericType(), variableResolver); assertThat(variable.resolve(), equalTo((Class) Long.class)); assertThat(parameterized.resolve(), equalTo((Class) List.class)); assertThat(parameterized.resolveGeneric(), equalTo((Class) Long.class)); verify(variableResolver, atLeastOnce()).resolveVariable(this.typeVariableCaptor.capture()); assertThat(this.typeVariableCaptor.getValue().getName(), equalTo("T")); }