Java Code Examples for org.eclipse.xtext.common.types.JvmParameterizedTypeReference#getType()

The following examples show how to use org.eclipse.xtext.common.types.JvmParameterizedTypeReference#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testFindTypeByName_javaUtilList_07() {
	String typeName = List.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	assertEquals(1, type.getSuperTypes().size());
	JvmParameterizedTypeReference superType = (JvmParameterizedTypeReference) type.getSuperTypes().get(0);
	assertFalse(superType.getType().eIsProxy());
	assertEquals("java.util.Collection<E>", superType.getIdentifier());
	assertEquals(1, type.getTypeParameters().size());
	JvmType rawType = superType.getType();
	assertFalse(rawType.eIsProxy());
	assertEquals(1, superType.getArguments().size());
	JvmTypeReference superTypeParameter = superType.getArguments().get(0);
	JvmType parameterType = superTypeParameter.getType();
	assertFalse(parameterType.eIsProxy());
	assertSame(parameterType, type.getTypeParameters().get(0));
}
 
Example 2
Source File: SARLJvmModelInferrer.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the implemented types for the given SARL statement.
 *
 * @param context the context of the generation.
 * @param owner the JVM element to change.
 * @param defaultJvmType the default JVM type.
 * @param defaultSarlType the default SARL type.
 * @param implementedtypes the implemented types.
 */
protected void appendConstrainedImplements(
		GenerationContext context,
		JvmGenericType owner, Class<?> defaultJvmType,
		Class<? extends XtendTypeDeclaration> defaultSarlType,
		List<? extends JvmParameterizedTypeReference> implementedtypes) {
	boolean explicitType = false;
	for (final JvmParameterizedTypeReference superType : implementedtypes) {
		if (!Objects.equal(owner.getIdentifier(), superType.getIdentifier())
				&& superType.getType() instanceof JvmGenericType
				/*&& this.inheritanceHelper.isProxyOrSubTypeOf(superType, defaultJvmType, defaultSarlType, true)*/) {
			owner.getSuperTypes().add(this.typeBuilder.cloneWithProxies(superType));
			context.incrementSerial(superType.getIdentifier().hashCode());
			explicitType = true;
		}
	}
	if (!explicitType) {
		final JvmTypeReference type = this._typeReferenceBuilder.typeRef(defaultJvmType);
		owner.getSuperTypes().add(type);
		context.incrementSerial(type.getIdentifier().hashCode());
	}
}
 
Example 3
Source File: NameConcatHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
private static void computeFor(JvmParameterizedTypeReference typeReference, char innerClassDelimiter,
		NameType nameType, StringBuilder result) {
	if (typeReference.eClass() == TypesPackage.Literals.JVM_INNER_TYPE_REFERENCE) {
		JvmParameterizedTypeReference outer = ((JvmInnerTypeReference) typeReference).getOuter();
		if (outer != null) {
			computeFor(outer, innerClassDelimiter, nameType, result);
			if (result.length() != 0) {
				JvmType type = typeReference.getType();
				result.append(innerClassDelimiter);
				result.append(type.getSimpleName());
			} else {
				appendType(typeReference, innerClassDelimiter, nameType, result);	
			}
		} else {
			appendType(typeReference, innerClassDelimiter, nameType, result);
		}
	} else {
		appendType(typeReference, innerClassDelimiter, nameType, result);
	}
	if (typeReference.eIsSet(TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__ARGUMENTS)) {
		result.append("<");
		appendArguments(result, typeReference.getArguments(), innerClassDelimiter, nameType);
		result.append(">");
	}
}
 
Example 4
Source File: RawTypeReferenceComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public JvmTypeReference doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference, Pair<Resource, Set<JvmType>> context) {
	JvmType type = reference.getType();
	if (type != null && !type.eIsProxy()) {
		if (type instanceof JvmTypeParameterDeclarator) {
			if (!((JvmTypeParameterDeclarator) type).getTypeParameters().isEmpty()) {
				JvmParameterizedTypeReference result = factory.createJvmParameterizedTypeReference();
				result.setType(type);
				return result;
			}
		} else if (type instanceof JvmTypeParameter) {
			Set<JvmType> recursionGuard = context.getSecond();
			if (recursionGuard.add(type)) {
				return getRawTypeFromConstraints(((JvmTypeParameter) type).getConstraints(), context);
			} else {
				return createObjectReference(context.getFirst());
			}
		}
	}
	return reference;
}
 
Example 5
Source File: RawTypeHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public JvmTypeReference doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference,
		Resource resource) {
	JvmType type = reference.getType();
	if (type != null && !type.eIsProxy()) {
		if (type instanceof JvmTypeParameterDeclarator) {
			if (!((JvmTypeParameterDeclarator) type).getTypeParameters().isEmpty()) {
				JvmParameterizedTypeReference result = factory.createJvmParameterizedTypeReference();
				result.setType(type);
				return result;
			}
		} else if (type instanceof JvmTypeParameter) {
			return getRawTypeFromConstraints(((JvmTypeParameter) type).getConstraints(), resource);
		}
	}
	return reference;
}
 
Example 6
Source File: TypeScopes.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public IScope createTypeScope(EObject context, EReference reference) {
	if (context.eClass() == TypesPackage.Literals.JVM_INNER_TYPE_REFERENCE) {
		JvmInnerTypeReference casted = (JvmInnerTypeReference) context;
		JvmParameterizedTypeReference outerType = casted.getOuter();
		JvmType outerRawType = outerType.getType();
		if (outerRawType instanceof JvmDeclaredType) {
			Iterable<JvmDeclaredType> nestedTypes = ((JvmDeclaredType) outerRawType).getAllNestedTypes();
			List<IEObjectDescription> descriptions = Lists.newArrayList();
			for(JvmDeclaredType nestedType: nestedTypes) {
				descriptions.add(EObjectDescription.create(nestedType.getSimpleName(), nestedType));
			}
			return new SimpleScope(descriptions);
		}
		return IScope.NULLSCOPE;
	} else {
		final IScope delegateScope = getDelegate().getScope(context, reference);
		return delegateScope;
	}
}
 
Example 7
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testFindTypeByName_javaUtilList_07() {
	String typeName = List.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	assertEquals(1, type.getSuperTypes().size());
	JvmParameterizedTypeReference superType = (JvmParameterizedTypeReference) type.getSuperTypes().get(0);
	assertFalse(superType.getType().eIsProxy());
	assertEquals("java.util.Collection<E>", superType.getIdentifier());
	assertEquals(1, type.getTypeParameters().size());
	JvmType rawType = superType.getType();
	assertFalse(rawType.eIsProxy());
	assertEquals(1, superType.getArguments().size());
	JvmTypeReference superTypeParameter = superType.getArguments().get(0);
	JvmType parameterType = superTypeParameter.getType();
	assertFalse(parameterType.eIsProxy());
	assertSame(parameterType, type.getTypeParameters().get(0));
}
 
Example 8
Source File: NameConcatHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private static void appendType(JvmParameterizedTypeReference typeReference, char innerClassDelimiter,
		NameType nameType, StringBuilder result) {
	JvmType type = typeReference.getType();
	if (type != null) {
		switch(nameType) {
			case ID: result.append(type.getIdentifier()); break;
			case QUALIFIED: result.append(type.getQualifiedName(innerClassDelimiter)); break;
			case SIMPLE: result.append(type.getSimpleName()); break;
			case TO_STRING: result.append(type.getIdentifier()); break;
		}
	}
}
 
Example 9
Source File: RawTypeHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public List<JvmType> doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference,
		Resource resource) {
	JvmType type = reference.getType();
	if (type != null && !type.eIsProxy()) {
		if (type instanceof JvmTypeParameter) {
			return getRawTypesFromConstraints(((JvmTypeParameter) type).getConstraints(), resource);
		}
		return Collections.singletonList(type);
	}
	return Collections.emptyList();
}
 
Example 10
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_twoListWildcardsNoResult_02() {
	JvmOperation twoListWildcardsNoResult = getMethodFromParameterizedMethods(
			"twoListWildcardsNoResult(java.util.List,java.util.List)");
	JvmFormalParameter firstParam = twoListWildcardsNoResult.getParameters().get(0);
	JvmTypeReference paramType = firstParam.getParameterType();
	assertNotNull(paramType);
	assertEquals("java.util.List<? extends java.lang.Object>", paramType.getIdentifier());
	assertTrue(paramType instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) paramType;
	JvmType rawType = parameterized.getType();
	assertFalse(rawType.eIsProxy());
	assertEquals("java.util.List", rawType.getIdentifier());
}
 
Example 11
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_twoListWildcardsNoResult_02() {
	JvmOperation twoListWildcardsNoResult = getMethodFromParameterizedMethods(
			"twoListWildcardsNoResult(java.util.List,java.util.List)");
	JvmFormalParameter firstParam = twoListWildcardsNoResult.getParameters().get(0);
	JvmTypeReference paramType = firstParam.getParameterType();
	assertNotNull(paramType);
	assertEquals("java.util.List<? extends java.lang.Object>", paramType.getIdentifier());
	assertTrue(paramType instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) paramType;
	JvmType rawType = parameterized.getType();
	assertFalse(rawType.eIsProxy());
	assertEquals("java.util.List", rawType.getIdentifier());
}
 
Example 12
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_twoListParamsListResult_02() {
	JvmOperation twoListParamsListResult = getMethodFromParameterizedMethods(
			"twoListParamsListResult(java.util.List,java.util.List)");
	JvmTypeReference returnType = twoListParamsListResult.getReturnType();
	assertNotNull(returnType);
	assertEquals("java.util.List<T>", returnType.getIdentifier());
	assertTrue(returnType instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) returnType;
	JvmType rawType = parameterized.getType();
	assertFalse(rawType.eIsProxy());
	assertEquals("java.util.List", rawType.getIdentifier());
}
 
Example 13
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_twoListParamsNoResult_02() {
	JvmOperation twoListParamsNoResult = getMethodFromParameterizedMethods(
			"twoListParamsNoResult(java.util.List,java.util.List)");
	JvmFormalParameter firstParam = twoListParamsNoResult.getParameters().get(0);
	JvmTypeReference paramType = firstParam.getParameterType();
	assertNotNull(paramType);
	assertFalse(paramType.getType().eIsProxy());
	assertEquals("java.util.List<T>", paramType.getIdentifier());
	assertTrue(paramType instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) paramType;
	JvmType rawType = parameterized.getType();
	assertFalse(rawType.eIsProxy());
	assertEquals("java.util.List", rawType.getIdentifier());
}
 
Example 14
Source File: DeferredTypeParameterHintCollector.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReference getStricterConstraint(final UnboundTypeReference typeParameter, LightweightTypeReference hint) {
	final JvmTypeParameter parameter = typeParameter.getTypeParameter();
	List<JvmTypeConstraint> constraints = parameter.getConstraints();
	for(JvmTypeConstraint constraint: constraints) {
		JvmTypeReference constraintReference = constraint.getTypeReference();
		if (constraintReference != null) {
			final boolean[] recursive = new boolean[] { false };
			LightweightTypeReferenceFactory factory = new LightweightTypeReferenceFactory(hint.getOwner()) {
				@Override
				public LightweightTypeReference doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference) {
					JvmType type = reference.getType();
					if (type == parameter) {// recursively bound
						recursive[0] = true;
					}
					return super.doVisitParameterizedTypeReference(reference);
				}
			};
			LightweightTypeReference lightweightReference = factory.toLightweightReference(constraintReference);
			if (!recursive[0]) {
				if (hint.isAssignableFrom(lightweightReference)) {
					hint = lightweightReference;	
				} else if (hint.isResolved() && !lightweightReference.getRawTypeReference().isAssignableFrom(hint, TypeConformanceComputationArgument.RAW)) {
					return null;
				}
			}
		}
	}
	return hint;
}
 
Example 15
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_twoListParamsListResult_02() {
	JvmOperation twoListParamsListResult = getMethodFromParameterizedMethods(
			"twoListParamsListResult(java.util.List,java.util.List)");
	JvmTypeReference returnType = twoListParamsListResult.getReturnType();
	assertNotNull(returnType);
	assertEquals("java.util.List<T>", returnType.getIdentifier());
	assertTrue(returnType instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) returnType;
	JvmType rawType = parameterized.getType();
	assertFalse(rawType.eIsProxy());
	assertEquals("java.util.List", rawType.getIdentifier());
}
 
Example 16
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void validateInferredType(JvmTypeReference inferredType, XtendMember member, String messagePrefix,
		EAttribute location) {
	if (inferredType != null) {
		TreeIterator<EObject> iterator = EcoreUtil2.eAll(inferredType);
		while(iterator.hasNext()) {
			EObject next = iterator.next();
			if (next instanceof JvmParameterizedTypeReference) {
				JvmParameterizedTypeReference candidate = (JvmParameterizedTypeReference) next;
				JvmType type = candidate.getType();
				if (type instanceof JvmGenericType && !((JvmGenericType) type).getTypeParameters().isEmpty()) {
					if (candidate.getArguments().isEmpty()) {
						StringBuilder message = new StringBuilder(messagePrefix);
						message = proxyAwareUIStrings.visit(inferredType, message);
						if (message != null) {
							message.append(" uses the raw type ");
							message.append(type.getSimpleName());
							message.append(". References to generic type ");
							message = proxyAwareUIStrings.appendTypeSignature(type, message);
							message.append(" should be parameterized");
							warning(message.toString(), member, location, org.eclipse.xtext.xbase.validation.IssueCodes.RAW_TYPE);
						}
						return;
					}
				}
			} else if (next instanceof XComputedTypeReference) {
				validateInferredType(((XComputedTypeReference) next).getEquivalent(), member, messagePrefix, location);
				iterator.prune();
			}
		}
	}
}
 
Example 17
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkDeprecated(JvmParameterizedTypeReference type) {
	if (!isIgnored(DEPRECATED_MEMBER_REFERENCE)) {
		JvmType jvmType = type.getType();
		checkDeprecated(
				jvmType,
				type,
				TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
	}
}
 
Example 18
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_twoListParamsNoResult_02() {
	JvmOperation twoListParamsNoResult = getMethodFromParameterizedMethods(
			"twoListParamsNoResult(java.util.List,java.util.List)");
	JvmFormalParameter firstParam = twoListParamsNoResult.getParameters().get(0);
	JvmTypeReference paramType = firstParam.getParameterType();
	assertNotNull(paramType);
	assertFalse(paramType.getType().eIsProxy());
	assertEquals("java.util.List<T>", paramType.getIdentifier());
	assertTrue(paramType instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) paramType;
	JvmType rawType = parameterized.getType();
	assertFalse(rawType.eIsProxy());
	assertEquals("java.util.List", rawType.getIdentifier());
}
 
Example 19
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_twoListWildcardsNoResult_02() {
	JvmOperation twoListWildcardsNoResult = getMethodFromParameterizedMethods(
			"twoListWildcardsNoResult(java.util.List,java.util.List)");
	JvmFormalParameter firstParam = twoListWildcardsNoResult.getParameters().get(0);
	JvmTypeReference paramType = firstParam.getParameterType();
	assertNotNull(paramType);
	assertEquals("java.util.List<? extends java.lang.Object>", paramType.getIdentifier());
	assertTrue(paramType instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) paramType;
	JvmType rawType = parameterized.getType();
	assertFalse(rawType.eIsProxy());
	assertEquals("java.util.List", rawType.getIdentifier());
}
 
Example 20
Source File: SARLValidator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** This function is overridden in order to add "isIgnore" invocation.
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("checkstyle:nestedifdepth")
protected void validateInferredType(JvmTypeReference inferredType, XtendMember member, String messagePrefix, EAttribute location) {
	if (inferredType != null) {
		final TreeIterator<EObject> iterator = EcoreUtil2.eAll(inferredType);
		while (iterator.hasNext()) {
			final EObject next = iterator.next();
			if (next instanceof JvmParameterizedTypeReference) {
				final JvmParameterizedTypeReference candidate = (JvmParameterizedTypeReference) next;
				final JvmType type = candidate.getType();
				if (type instanceof JvmGenericType && !((JvmGenericType) type).getTypeParameters().isEmpty()) {
					if (candidate.getArguments().isEmpty()) {
						StringBuilder message = new StringBuilder(messagePrefix);
						message = this.proxyAwareUIStrings.visit(inferredType, message);
						if (message != null && !isIgnored(org.eclipse.xtext.xbase.validation.IssueCodes.RAW_TYPE)) {
							StringBuilder msg2 = new StringBuilder();
							msg2 = this.proxyAwareUIStrings.appendTypeSignature(type, msg2);
							final String m = MessageFormat.format(Messages.SARLValidator_6,
									message.toString(),
									type.getSimpleName(),
									msg2.toString());
							addIssue(m.toString(), member, location, org.eclipse.xtext.xbase.validation.IssueCodes.RAW_TYPE);
						}
						return;
					}
				}
			} else if (next instanceof XComputedTypeReference) {
				validateInferredType(((XComputedTypeReference) next).getEquivalent(), member, messagePrefix, location);
				iterator.prune();
			}
		}
	}
}