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

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference#isPrimitive() . 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: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
void checkNullSafeFeatureCallWithPrimitives(XMemberFeatureCall featureCall) {
	if (featureCall.isNullSafe()) {
		if (getActualType(featureCall.getMemberCallTarget()).isPrimitive()) {
			error("Cannot use null-safe feature call on primitive receiver", featureCall,
					Literals.XMEMBER_FEATURE_CALL__NULL_SAFE, NULL_SAFE_FEATURE_CALL_ON_PRIMITIVE);
			return;
		}
		LightweightTypeReference type = getActualType(featureCall);
		if (type.isPrimitive() && isValueExpectedRecursive(featureCall)) {
			addIssue("Null-safe call of primitive-valued feature " + featureCall.getConcreteSyntaxFeatureName() 
					+ ", default value "+ getDefaultValue(type) +" will be used", 
					featureCall, NULL_SAFE_FEATURE_CALL_OF_PRIMITIVE_VALUED_FEATURE);
		}
	}
}
 
Example 2
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 3
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void process(ITypeComputationResult result) {
	resultProcessed = true;
	if (result.getConformanceFlags() != 0 /* equivalent to getActualExpressionType != null */) {
		nonNullResultProcessed = true;
	}
	LightweightTypeReference expressionReturnType = result.getReturnType();
	if (expressionReturnType != null) {
		boolean isExit = (result.getCheckedConformanceFlags() & ConformanceFlags.NO_IMPLICIT_RETURN) != 0;
		if (earlyExit && isExit && !expressionReturnType.isPrimitiveVoid()) {
			earlyExit = false;
		}
		if (allPrimitive && isExit && !expressionReturnType.isPrimitive()) {
			allPrimitive = false;
		}
		if (allVoid || allPrimitive) {
			LightweightTypeReference expressionType = result.getActualExpressionType();
			if (allVoid && !(expressionType != null && expressionType.isPrimitiveVoid())) {
				allVoid = false;
			}
			if (allPrimitive && !(expressionType != null && expressionType.isPrimitive() || expressionReturnType.isPrimitive())) {
				allPrimitive = false;
			}
		}
	}
}
 
Example 4
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _computeTypes(XSynchronizedExpression expr, ITypeComputationState state) {
	ITypeComputationState paramState = state.withExpectation(state.getReferenceOwner().newReferenceToObject());
	ITypeComputationResult paramType = paramState.computeTypes(expr.getParam());
	LightweightTypeReference actualParamType = paramType.getActualExpressionType();
	if (actualParamType != null && (actualParamType.isPrimitive() || actualParamType.isAny())) {
		state.addDiagnostic(new EObjectDiagnosticImpl(
				Severity.ERROR,
				IssueCodes.INCOMPATIBLE_TYPES,
				actualParamType.getHumanReadableName() +  " is not a valid type's argument for the synchronized expression.",
				expr.getParam(),
				null,
				-1,
				new String[] { 
				}));
	}
	state.computeTypes(expr.getExpression());
}
 
Example 5
Source File: RawTypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected int doIsConformant(LightweightTypeReference left, UnboundTypeReference right, int flags) {
	if (left.getType() == right.getType() || left.isType(Object.class)) {
		return flags | SUCCESS;
	}
	if ((flags & ALLOW_UNBOXING) == 0 && left.isPrimitive()) {
		return flags;
	}
	boolean doesNotHaveSignificantHints = false;
	if (((flags & RAW_TYPE) == 0) && (right.canResolveTo(left) || (flags & AS_TYPE_ARGUMENT) != 0 && (doesNotHaveSignificantHints = !right.hasSignificantHints()))) {
		if ((flags & UNBOUND_COMPUTATION_ADDS_HINTS) != 0 && doesNotHaveSignificantHints) {
			right.acceptHint(left, BoundTypeArgumentSource.INFERRED_LATER, left, VarianceInfo.INVARIANT, VarianceInfo.INVARIANT);
		}
		return flags | SUCCESS;
	}
	right.tryResolve(false);
	LightweightTypeReference resolvedTo = right.getResolvedTo();
	if (resolvedTo != null) {
		return doIsConformant(left, resolvedTo, flags);
	}
	return flags;
}
 
Example 6
Source File: CommonSuperTypeTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isPrimitiveOrVoid(final LightweightTypeReference computedSuperType) {
  boolean _xblockexpression = false;
  {
    if ((computedSuperType == null)) {
      return false;
    }
    _xblockexpression = (computedSuperType.isPrimitiveVoid() || computedSuperType.isPrimitive());
  }
  return _xblockexpression;
}
 
Example 7
Source File: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected String getDefaultValueLiteral(XExpression expr) {
	LightweightTypeReference type = getTypeForVariableDeclaration(expr);
	if (type.isPrimitive()) {
		if (type.getPrimitiveKind() == Primitives.Primitive.Boolean) {
			return "false";
		} else {
			return "(" + type.getSimpleName() + ") 0";
		}
	}
	return "null";
}
 
Example 8
Source File: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendDefaultLiteral(ITreeAppendable b, /* @Nullable */ LightweightTypeReference type) {
	if (type != null && type.isPrimitive()) {
		Primitive primitiveKind = type.getPrimitiveKind();
		switch (primitiveKind) {
			case Boolean:
				b.append("false");
				break;
			default:
				b.append("0");
				break;
		}
	} else {
		b.append("null");
	}
}
 
Example 9
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendNullValueUntyped(LightweightTypeReference type, @SuppressWarnings("unused") EObject context, ITreeAppendable b) {
	if (!type.isPrimitive()) {
		b.append("null");
	} else {
		b.append(getDefaultLiteral((JvmPrimitiveType) type.getType()));
	}
}
 
Example 10
Source File: TypeConvertingCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void doConversion(final LightweightTypeReference left, final LightweightTypeReference right,
		final ITreeAppendable appendable, XExpression context, final Later expression) {
	if(left.isPrimitive() && !right.isPrimitive()) {
		if (right.isAny()) {
			convertWrapperToPrimitive(left, left, context, appendable, expression);
		} else {
			convertWrapperToPrimitive(right, right.getPrimitiveIfWrapperType(), context, appendable, expression);
		}
	} else if (right.isPrimitive() && !left.isPrimitive()) {
		convertPrimitiveToWrapper(right, right.getWrapperTypeIfPrimitive(), appendable, expression);
	} else if (right.isMultiType()) {
		convertMultiType(left, (CompoundTypeReference) right, context, appendable, expression);
	} else if (right.isArray() && !left.isArray() && left.isSubtypeOf(Iterable.class)) {
		convertArrayToList(left, appendable, context, expression);
	} else if (isJavaConformant(left, right)) {
		if (mustInsertTypeCast(context, left)) {
			doCastConversion(left, appendable, expression);
		} else {
			expression.exec(appendable);
		}
	} else if (left.isArray() && !right.isArray() && right.isSubtypeOf(Iterable.class)) {
		convertListToArray(left, appendable, expression);
	} else if ((isFunction(left) && isFunction(right) || isProcedure(left) && isProcedure(right)) && left.getType() == right.getType()) {
		doCastConversion(left, appendable, expression);
	} else if (isFunction(right) || (isFunction(left) && findImplementingOperation(right) != null)) {
		convertFunctionType(left, right, appendable, expression);
	} else if (isProcedure(right) || (isProcedure(left) && findImplementingOperation(right) != null)) {
		convertFunctionType(left, right, appendable, expression);
	} else if (mustInsertTypeCast(context, left)) {
		doCastConversion(left, appendable, expression);
	} else {
		expression.exec(appendable);
	}
}
 
Example 11
Source File: CreateMemberQuickfixes.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.3
 */
protected String getDefaultValueLiteral(/* @Nullable */ LightweightTypeReference type) {
	if (type != null && type.isPrimitive()) {
		Primitive primitiveKind = primitives.primitiveKind((JvmPrimitiveType) type.getType());
		if (primitiveKind == Primitive.Boolean)
			return "false";
		else
			return "0 as " + type.getSimpleName();
	}
	return "null";
}
 
Example 12
Source File: AbstractResolvedFeature.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReference getResolvedReference(/* @Nullable */ JvmTypeReference unresolved) {
	ITypeReferenceOwner owner = getContextType().getOwner();
	if (unresolved == null) {
		return owner.newReferenceToObject();
	}
	LightweightTypeReference unresolvedLightweight = owner.toLightweightTypeReference(unresolved);
	if (unresolvedLightweight.isPrimitive() || unresolvedLightweight.isPrimitiveVoid())
		return unresolvedLightweight;
	TypeParameterSubstitutor<?> substitutor = getSubstitutor();
	LightweightTypeReference result = substitutor.substitute(unresolvedLightweight);
	return result;
}
 
Example 13
Source File: TypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean containsPrimitiveOrAnyReferences(List<LightweightTypeReference> types) {
	for(LightweightTypeReference type: types) {
		if (type.isPrimitive() || type.isAny())
			return true;
	}
	return false;
}
 
Example 14
Source File: Utils.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies if the given reference is referencing a final type.
 *
 * @param expressionTypeRef - the type reference to test.
 * @return {@code true} if the given type is final.
 */
public static boolean isFinal(LightweightTypeReference expressionTypeRef) {
	if (expressionTypeRef.isArray()) {
		return isFinal(expressionTypeRef.getComponentType());
	}
	if (expressionTypeRef.isPrimitive()) {
		return true;
	}
	return expressionTypeRef.getType() instanceof JvmDeclaredType
			&& ((JvmDeclaredType) expressionTypeRef.getType()).isFinal();
}
 
Example 15
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isFinal(LightweightTypeReference expressionTypeRef) {
	if (expressionTypeRef.isArray()) {
		return isFinal(expressionTypeRef.getComponentType());
	}
	if (expressionTypeRef.isPrimitive())
		return true;
	return expressionTypeRef.getType() instanceof JvmDeclaredType
			&& ((JvmDeclaredType) expressionTypeRef.getType()).isFinal();
}
 
Example 16
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkInstanceOf(XInstanceOfExpression instanceOfExpression) {
	LightweightTypeReference leftType = getActualType(instanceOfExpression.getExpression());
	final LightweightTypeReference rightType = toLightweightTypeReference(instanceOfExpression.getType(), true);
	if (leftType == null || rightType == null || rightType.getType() == null || rightType.getType().eIsProxy()) {
		return;
	}
	if (containsTypeArgs(rightType)) {
		error("Cannot perform instanceof check against parameterized type " + getNameOfTypes(rightType), null, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INVALID_INSTANCEOF);
		return;
	}
	if (leftType.isAny() || leftType.isUnknown()) {
		return; // null / unknown is ok
	}
	if (rightType.isPrimitive()) {
		error("Cannot perform instanceof check against primitive type " + this.getNameOfTypes(rightType), null, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INVALID_INSTANCEOF);
		return;
	}
	if (leftType.isPrimitive() 
		|| rightType.isArray() && !(leftType.isArray() || leftType.isType(Object.class) || leftType.isType(Cloneable.class) || leftType.isType(Serializable.class))
		|| isFinal(rightType) && !memberOfTypeHierarchy(rightType, leftType)
		|| isFinal(leftType) && !memberOfTypeHierarchy(leftType, rightType)) {
		error("Incompatible conditional operand types " + this.getNameOfTypes(leftType)+" and "+this.getNameOfTypes(rightType), null, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INVALID_INSTANCEOF);
		return;
	}
	if (!isIgnored(OBSOLETE_INSTANCEOF) && rightType.isAssignableFrom(leftType, new TypeConformanceComputationArgument(false, false, true, true, false, false))) {
		// check that we do not have a type parameter usage on the rhs of the instanceof 
		if (rightType.getConstraintSubstitute() == rightType) {
			addIssueToState(OBSOLETE_INSTANCEOF, "The expression of type " + getNameOfTypes(leftType) + " is already of type " + canonicalName(rightType), null);	
		}
	}
}
 
Example 17
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkTypeGuards(XCasePart casePart) {
	if (casePart.getTypeGuard() == null)
		return;
	LightweightTypeReference typeGuard = toLightweightTypeReference(casePart.getTypeGuard());
	if (typeGuard.isPrimitive()) {
		error("Primitives are not allowed as type guards", Literals.XCASE_PART__TYPE_GUARD, INVALID_USE_OF_TYPE);
		return;
	}
	LightweightTypeReference targetTypeRef = getActualType(((XSwitchExpression) casePart.eContainer()).getSwitch());
	checkCast(casePart.getTypeGuard(), typeGuard, targetTypeRef);
}
 
Example 18
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void ensureNotPrimitiveNorWildcard(JvmTypeReference typeRef) {
	LightweightTypeReference reference = toLightweightTypeReference(typeRef);
	if (reference.isPrimitive()) {
		error("Primitives cannot be used as type arguments.", typeRef, null, INVALID_USE_OF_TYPE);
	}
	if (reference.isWildcard()) {
		error("Wildcard types are not allowed in this context", typeRef, null, INSIGNIFICANT_INDEX,
				INVALID_USE_OF_WILDCARD);
	}
}
 
Example 19
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected LightweightTypeReference getAndEnhanceIterableOrArrayFromComponent(LightweightTypeReference parameterType, JvmGenericType iterableType,
		final CompoundTypeReference compoundResult) {
	if (parameterType.isUnknown()) {
		compoundResult.addComponent(parameterType);
		return parameterType;
	}
	ITypeReferenceOwner owner = compoundResult.getOwner();
	LightweightTypeReference iterableOrArray = null;
	LightweightTypeReference addAsArrayComponentAndIterable = null;
	if (parameterType.isPrimitive()) {
		iterableOrArray = owner.newArrayTypeReference(parameterType);
		compoundResult.addComponent(iterableOrArray);
		addAsArrayComponentAndIterable = parameterType.getWrapperTypeIfPrimitive();
	} else if (parameterType.isAny()) {
		addAsArrayComponentAndIterable = parameterType.getOwner().newReferenceToObject();
	} else {
		addAsArrayComponentAndIterable = parameterType;
	}
	if (iterableType != null) {
		ParameterizedTypeReference reference = owner.newParameterizedTypeReference(iterableType);
		WildcardTypeReference wildcard = owner.newWildcardTypeReference();
		wildcard.addUpperBound(addAsArrayComponentAndIterable);
		reference.addTypeArgument(wildcard);
		compoundResult.addComponent(reference);
		if (iterableOrArray == null) {
			iterableOrArray = reference;
			LightweightTypeReference potentialPrimitive = addAsArrayComponentAndIterable.getPrimitiveIfWrapperType();
			if (potentialPrimitive != addAsArrayComponentAndIterable) {
				compoundResult.addComponent(owner.newArrayTypeReference(potentialPrimitive));
			}
		}
		compoundResult.addComponent(owner.newArrayTypeReference(addAsArrayComponentAndIterable));
	} else if (iterableOrArray == null) { // no JRE on the CP
		if (addAsArrayComponentAndIterable != null) {
			iterableOrArray = owner.newArrayTypeReference(addAsArrayComponentAndIterable);
			compoundResult.addComponent(iterableOrArray);
		} else {
			compoundResult.addComponent(parameterType);
			return parameterType;
		}
	}
	return iterableOrArray;
}
 
Example 20
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);
		}
	}