Java Code Examples for org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference#isWrapper()

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference#isWrapper() . 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: SynonymTypesProvider.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void collectSynonymTypes(/* @Nullable */ LightweightTypeReference type, /* @NonNull */ Acceptor acceptor) {
	if (type == null || type.isPrimitiveVoid() || type.isType(Void.class)) {
		return;
	}
	if (type.isWrapper()) {
		if (!acceptor.accept(type.getPrimitiveIfWrapperType(), ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.UNBOXING)) {
			return;
		}
		// a primitive type is never an array or list
		collectCustomSynonymTypes(type, acceptor);
		return;
	} else if (type.isPrimitive()) {
		if (!acceptor.accept(type.getWrapperTypeIfPrimitive(), ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.BOXING)) {
			return;
		}
		// a primitive type is never an array or list
		collectCustomSynonymTypes(type, acceptor);
		return;
	}
	if (addArrayAndListSynonyms(type, acceptor)) {
		collectCustomSynonymTypes(type, acceptor);
	}
}
 
Example 2
Source File: ObjectAndPrimitiveBasedCastOperationCandidateSelector.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Constructor.
 *
 * @param state the current state of the type computation.
 * @param castType the target type.
 * @param expressionType the type of the expression to cast.
 */
protected ObjectAndPrimitiveBasedSelector(AbstractTypeComputationState state,
		LightweightTypeReference castType, LightweightTypeReference expressionType) {
	this.state = state;
	this.castType = castType.getWrapperTypeIfPrimitive();
	this.primitiveCastType = castType.getPrimitiveIfWrapperType();
	this.expressionType = expressionType;
	this.primitiveCast = castType.isPrimitive() || castType.isWrapper();
}
 
Example 3
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void checkCast(JvmTypeReference concreteSyntax, LightweightTypeReference toType, LightweightTypeReference fromType) {
		if (toType == null || fromType == null)
			return;
		if (fromType.getType() instanceof JvmDeclaredType || fromType.isPrimitive()) {
			// if one of the types is an interface and the other is a non final class (or interface) there always can be a subtype
			if ((!isInterface(fromType) || isFinal(toType)) && (!isInterface(toType) || isFinal(fromType))) { 
				if (!toType.isAssignableFrom(fromType)) {
					if (   isFinal(fromType) || isFinal(toType)
						|| isClass(fromType) && isClass(toType)) {
						if (!fromType.isAssignableFrom(toType)) { // no upcast
							error("Cannot cast from " + getNameOfTypes(fromType) + " to "
									+ canonicalName(toType), concreteSyntax, null, ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
									INVALID_CAST);
						}
					}
				}
			}
		} else if (fromType.isPrimitiveVoid()) {
			error("Cannot cast from void to "
					+ canonicalName(toType), concreteSyntax, null, ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
					INVALID_CAST);
		}
		if(toType.isPrimitive() && !(fromType.isPrimitive() || fromType.isWrapper())) {
				error("Cannot cast from " + getNameOfTypes(fromType) + " to "
						+ canonicalName(toType), concreteSyntax, null, ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
						INVALID_CAST);
		}
		/*
		 * TODO: JDT reports no unnecessary cast of List<?> but for List<String> ... why is there an exception?
		 * 
		 * 
		 */
//			List<? extends String> list = Collections.emptyList();
//			if (((List<? extends String>)list).isEmpty()) {}
//			List<String> list = Collections.emptyList();
//			if (((List<String>)list).isEmpty()) {}
//			List list = Collections.emptyList();
//			if (((List)list).isEmpty()) {}
		if (toType.getIdentifier().equals(fromType.getIdentifier())) {
			addIssue("Unnecessary cast from " + fromType.getHumanReadableName() + " to " + toType.getHumanReadableName(), concreteSyntax, IssueCodes.OBSOLETE_CAST);
		}
	}