private MetaClass getMethodReturnType(final MetaMethod method) { final MetaType genericType = method.getGenericReturnType(); return getMetaClassFromGeneric(genericType).orElseGet(method::getReturnType); }
@Override public MetaClass getType() { MetaClass returnType = method.getReturnType(); if (method.getGenericReturnType() != null && method.getGenericReturnType() instanceof MetaTypeVariable) { typeVariables = new HashMap<String, MetaClass>(); resolveTypeVariables(); final MetaTypeVariable typeVar = (MetaTypeVariable) method.getGenericReturnType(); if (typeVariables.containsKey(typeVar.getName())) { returnType = typeVariables.get(typeVar.getName()); } else if (writer.getTypeParm(typeVar.getName()) != null) { returnType = writer.getTypeParm(typeVar.getName()); } else { // returning NullType as a stand-in for an unbounded wildcard type since this is a parameterized method // and there is not RHS qualification for the parameter. // // ie when calling GWT.create() and assigning it to a concrete type. // // TODO: might be worth flushing this out for clarify in the future. return MetaClassFactory.get(NullType.class); } } assert returnType != null; return returnType; }
@Override public MetaClass getType() { MetaClass returnType = method.getReturnType(); if (method.getGenericReturnType() != null && method.getGenericReturnType() instanceof MetaTypeVariable) { typeVariables = new HashMap<String, MetaClass>(); resolveTypeVariables(); final MetaTypeVariable typeVar = (MetaTypeVariable) method.getGenericReturnType(); if (typeVariables.containsKey(typeVar.getName())) { returnType = typeVariables.get(typeVar.getName()); } else if (writer.getTypeParm(typeVar.getName()) != null) { returnType = writer.getTypeParm(typeVar.getName()); } else { // returning NullType as a stand-in for an unbounded wildcard type since this is a parameterized method // and there is not RHS qualification for the parameter. // // ie when calling GWT.create() and assigning it to a concrete type. // // TODO: might be worth flushing this out for clarify in the future. return MetaClassFactory.get(NullType.class); } } assert returnType != null; return returnType; }
final MetaType returnType = method.getGenericReturnType(); if (returnType == null) { throw new GenerationException("The method 'getAssociatedType()' in the event [" + eventType.getName()
final MetaType returnType = method.getGenericReturnType(); if (returnType == null) { throw new GenerationException("The method 'getAssociatedType()' in the event [" + eventType.getName()
@Test public void testFieldWithSingleUpperBoundedTypeVarParam() throws Exception { final MetaClass metaClass = getMetaClass(ClassWithGenericCollections.class); final MetaMethod field = metaClass.getDeclaredMethod("hasSingleBoundedTypeVarFromSelf", new Class[] {}); assertNotNull(field); final MetaTypeVariable returnType = (MetaTypeVariable) field.getGenericReturnType(); assertEquals("B", returnType.getName()); assertEquals("Should have a single upper bound", Arrays.asList(getMetaClass(Serializable.class)), Arrays.asList(returnType.getBounds())); }
final String returnValName = ensureSafeLocalVariableName("returnValue", method); final MetaClass returnType = getTypeOrFirstUpperBound(method.getGenericReturnType(), method); if (returnType == null) return;
final String returnValName = ensureSafeLocalVariableName("returnValue", method); final MetaClass returnType = getTypeOrFirstUpperBound(method.getGenericReturnType(), method); if (returnType == null) return;
@Test public void testFieldWithTwoUpperBoundedTypeVarParam() throws Exception { final MetaClass metaClass = getMetaClass(ClassWithGenericCollections.class); final MetaMethod field = metaClass.getDeclaredMethod("hasDoubleBoundedTypeVarFromSelf", new Class[] {}); assertNotNull(field); final MetaTypeVariable returnType = (MetaTypeVariable) field.getGenericReturnType(); assertEquals("B", returnType.getName()); assertEquals("Should have two upper bounds", Arrays.asList(getMetaClass(Collection.class), getMetaClass(Serializable.class)), Arrays.asList(returnType.getBounds())); }
@Test public void testMethodObjectReturnType() { final MetaClass c = getMetaClass(ClassWithGenericMethods.class); final MetaMethod method = c.getMethod("methodReturningObject", new Class[] {}); assertEquals("java.lang.Object", method.getReturnType().getFullyQualifiedNameWithTypeParms()); assertEquals(getTypeOfMetaClassBeingTested(), method.getReturnType().getClass()); // the generic return type should be the same: plain old Object assertEquals(getMetaClass(Object.class), method.getGenericReturnType()); }
if (method.getGenericReturnType() instanceof MetaTypeVariable) { final MetaTypeVariable typeVariable = (MetaTypeVariable) method.getGenericReturnType(); final MetaClass tVarVal = getTypeVariableValue(typeVariable, buildMetaClass); if (tVarVal != null) { else if (method.getGenericReturnType() instanceof MetaParameterizedType) { final MetaParameterizedType metaParameterizedType = (MetaParameterizedType) method.getGenericReturnType();
if (method.getGenericReturnType() instanceof MetaTypeVariable) { final MetaTypeVariable typeVariable = (MetaTypeVariable) method.getGenericReturnType(); final MetaClass tVarVal = getTypeVariableValue(typeVariable, buildMetaClass); if (tVarVal != null) { else if (method.getGenericReturnType() instanceof MetaParameterizedType) { final MetaParameterizedType metaParameterizedType = (MetaParameterizedType) method.getGenericReturnType();
@Test public void testMethodReturnTypeWithUpperBoundedWildcardParameter() { final MetaClass c = getMetaClass(ClassWithGenericMethods.class); final MetaMethod method = c.getMethod("methodReturningUpperBoundedWildcardCollection", new Class[] {}); // TODO (ERRAI-459) decide whether it's correct to have the type param present or not // then adjust this assertion to strict equality rather than startsWith() assertTrue(method.getReturnType().getFullyQualifiedNameWithTypeParms().startsWith("java.util.Collection")); final MetaType genericReturnType = method.getGenericReturnType(); assertNotNull(genericReturnType); assertTrue("Got unexpected return type type " + genericReturnType.getClass(), genericReturnType instanceof MetaParameterizedType); final MetaParameterizedType mpReturnType = (MetaParameterizedType) genericReturnType; assertEquals(1, mpReturnType.getTypeParameters().length); // Sole type parameter should be <? extends String> assertTrue(mpReturnType.getTypeParameters()[0] instanceof MetaWildcardType); final MetaWildcardType typeParam = (MetaWildcardType) mpReturnType.getTypeParameters()[0]; assertArrayEquals(new MetaType[] {}, typeParam.getLowerBounds()); assertArrayEquals(new MetaType[] { getMetaClass(String.class) }, typeParam.getUpperBounds()); }
@Test public void testMethodReturnTypeWithWildcardParameter() { final MetaClass c = getMetaClass(ClassWithGenericMethods.class); final MetaMethod method = c.getMethod("methodReturningUnboundedWildcardCollection", new Class[] {}); // TODO (ERRAI-459) decide whether it's correct to have the type param present or not // then adjust this assertion to strict equality rather than startsWith() assertTrue(method.getReturnType().getFullyQualifiedNameWithTypeParms().startsWith("java.util.Collection")); final MetaType genericReturnType = method.getGenericReturnType(); assertNotNull(genericReturnType); assertTrue("Got unexpected return type type " + genericReturnType.getClass(), genericReturnType instanceof MetaParameterizedType); final MetaParameterizedType mpReturnType = (MetaParameterizedType) genericReturnType; assertEquals(1, mpReturnType.getTypeParameters().length); // Sole type parameter should be <?> assertTrue(mpReturnType.getTypeParameters()[0] instanceof MetaWildcardType); final MetaWildcardType typeParam = (MetaWildcardType) mpReturnType.getTypeParameters()[0]; assertArrayEquals(new MetaType[] {}, typeParam.getLowerBounds()); assertArrayEquals(new MetaType[] { getMetaClass(Object.class) }, typeParam.getUpperBounds()); }
@Test public void testMethodReturnTypeWithLowerBoundedWildcardParameter() { final MetaClass c = getMetaClass(ClassWithGenericMethods.class); final MetaMethod method = c.getMethod("methodReturningLowerBoundedWildcardCollection", new Class[] {}); // TODO (ERRAI-459) decide whether it's correct to have the type param present or not // then adjust this assertion to strict equality rather than startsWith() assertTrue(method.getReturnType().getFullyQualifiedNameWithTypeParms().startsWith("java.util.Collection")); final MetaType genericReturnType = method.getGenericReturnType(); assertNotNull(genericReturnType); assertTrue("Got unexpected return type type " + genericReturnType.getClass(), genericReturnType instanceof MetaParameterizedType); final MetaParameterizedType mpReturnType = (MetaParameterizedType) genericReturnType; assertEquals(1, mpReturnType.getTypeParameters().length); // Sole type parameter should be <? extends String> assertTrue(mpReturnType.getTypeParameters()[0] instanceof MetaWildcardType); final MetaWildcardType typeParam = (MetaWildcardType) mpReturnType.getTypeParameters()[0]; assertArrayEquals(new MetaType[] { getMetaClass(String.class) }, typeParam.getLowerBounds()); assertArrayEquals(new MetaType[] { getMetaClass(Object.class)}, typeParam.getUpperBounds()); }