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

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference#isSubtypeOf() . 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: CastOperatorLinkingCandidate.java    From sarl with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("checkstyle:magicnumber")
private static int computeCompliance(LightweightTypeReference target, LightweightTypeReference current) {
	if (isSame(target, current)) {
		return 0;
	}
	final LightweightTypeReference target2 = target.getWrapperTypeIfPrimitive();
	if (target2.isSubtypeOf(Number.class)) {
		final LightweightTypeReference current2 = current.getWrapperTypeIfPrimitive();
		if (current2.isSubtypeOf(Number.class)) {
			return 1 + Math.max(getNumberPrecision(target) - getNumberPrecision(current), 0);
		}
		if (target.isAssignableFrom(current)) {
			return 8;
		}
		return 9;
	}
	if (target.isAssignableFrom(current)) {
		return 1;
	}
	return 2;
}
 
Example 2
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param context unused in this context but required for dispatching
 * @param indicator unused in this context but required for dispatching
 */
@SuppressWarnings("unchecked")
protected Object _doEvaluate(XNumberLiteral literal, IEvaluationContext context, CancelIndicator indicator) {
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(literal);
	LightweightTypeReference expectedType = resolvedTypes.getExpectedType(literal);
	Class<? extends Number> type = numberLiterals.getJavaType(literal);
	if (expectedType != null && expectedType.isSubtypeOf(Number.class)) {
		try {
			Class<?> expectedClassType = getJavaType(expectedType.toJavaCompliantTypeReference().getType());
			if (expectedClassType.isPrimitive()) {
				expectedClassType = ReflectionUtil.getObjectType(expectedClassType);
			}
			if (Number.class != expectedClassType && Number.class.isAssignableFrom(expectedClassType)) {
				type = (Class<? extends Number>) expectedClassType;
			}
		} catch (ClassNotFoundException e) {
		}
	}
	return numberLiterals.numberValue(literal, type);
}
 
Example 3
Source File: OverrideTester.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void addExceptionDetails(AbstractResolvedOperation overriding, AbstractResolvedOperation overridden,
		EnumSet<OverrideCheckDetails> result) {
	List<LightweightTypeReference> exceptions = overriding.getResolvedExceptions();
	if (exceptions.isEmpty()) {
		return;
	}
	List<LightweightTypeReference> inheritedExceptions = overridden.getResolvedExceptions();
	for(LightweightTypeReference exception: exceptions) {
		if (!exception.isSubtypeOf(RuntimeException.class) && !exception.isSubtypeOf(Error.class)) {
			boolean isDeclared = false;
			for(LightweightTypeReference inheritedException: inheritedExceptions) {
				if (inheritedException.isAssignableFrom(exception)) {
					isDeclared = true;
					break;
				}
			}
			if (!isDeclared) {
				result.add(OverrideCheckDetails.EXCEPTION_MISMATCH);
				return;
			}
		}
	}
}
 
Example 4
Source File: AbstractResolvedOperation.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public List<LightweightTypeReference> getIllegallyDeclaredExceptions() {
	if (getDeclaration().getExceptions().isEmpty())
		return Collections.emptyList();
	List<IResolvedOperation> overriddenAndImplemented = getOverriddenAndImplementedMethods();
	if (overriddenAndImplemented.isEmpty())
		return Collections.emptyList();
	List<LightweightTypeReference> exceptions = getResolvedExceptions();
	List<LightweightTypeReference> result = Lists.newArrayListWithCapacity(exceptions.size());
	for(LightweightTypeReference exception: exceptions) {
		if (!exception.isSubtypeOf(RuntimeException.class) && !exception.isSubtypeOf(Error.class)) {
			if (isIllegallyDeclaredException(exception, overriddenAndImplemented)) {
				result.add(exception);
			}
		}
	}
	return result;
}
 
Example 5
Source File: XSwitchExpressions.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Determine whether the given switch expression is valid for Java version 7
 * or higher.
 */
public boolean isJava7SwitchExpression(XSwitchExpression it) {
	LightweightTypeReference switchType = getSwitchVariableType(it);
	if (switchType == null) {
		return false;
	}
	if (switchType.isSubtypeOf(Integer.TYPE)) {
		return true;
	}
	if (switchType.isSubtypeOf(Enum.class)) {
		return true;
	}
	if (switchType.isSubtypeOf(String.class)) {
		return true;
	}
	return false;
}
 
Example 6
Source File: AbstractBuilder.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies if the first parameter is a subtype of the second parameter.
 *
 * @param context the context.
 * @param subType the subtype to test.
 * @param superType the expected super type.
 * @return the type reference.
 */
@Pure
protected boolean isSubTypeOf(EObject context, JvmTypeReference subType, JvmTypeReference superType) {
	if (isTypeReference(superType) && isTypeReference(subType)) {
		StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(services, context);
		LightweightTypeReferenceFactory factory = new LightweightTypeReferenceFactory(owner, false);
		LightweightTypeReference reference = factory.toLightweightReference(subType);
		return reference.isSubtypeOf(superType.getType());
	}
	return false;
}
 
Example 7
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Object wrapOrUnwrapArray(Object result, LightweightTypeReference expectedType) {
	if (expectedType.isArray() && !(result instanceof Object[])) {
		Class<?> arrayType;
		try {
			arrayType = getJavaType(expectedType.getComponentType().getType());
			return Conversions.unwrapArray(result, arrayType);
		} catch (ClassNotFoundException e) {
			return result;
		}
	} else if (!expectedType.isArray() && expectedType.isSubtypeOf(Iterable.class)) {
		return Conversions.doWrapArray(result);
	}
	return result;
}
 
Example 8
Source File: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isUnhandledException(LightweightTypeReference thrownException, Collection<JvmTypeReference> declaredExceptions) {
	for(JvmTypeReference declaredException: declaredExceptions) {
		if (thrownException.isSubtypeOf(declaredException.getType())) {
			return false;
		}
	}
	return true;
}
 
Example 9
Source File: CastedExpressionTypeComputationState.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies if the linking to the cast operator functions is enabled.
 *
 * @param cast the cast operator.
 * @return {@code true} if the linking is enabled.
 */
public boolean isCastOperatorLinkingEnabled(SarlCastedExpression cast) {
	final LightweightTypeReference sourceType = getStackedResolvedTypes().getReturnType(cast.getTarget());
	final LightweightTypeReference destinationType = getReferenceOwner().toLightweightTypeReference(cast.getType());
	if (sourceType.isPrimitiveVoid() || destinationType.isPrimitiveVoid()) {
		return false;
	}
	if (sourceType.isPrimitive() && destinationType.isPrimitive()) {
		return false;
	}
	return !sourceType.isSubtypeOf(destinationType.getType());
}
 
Example 10
Source File: SarlCompiler.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean canCompileToJavaLambda(XClosure closure, LightweightTypeReference typeRef,
		JvmOperation operation) {
	// This function overrides the super one in order to avoid the generation of a Java 8 lambda when
	// the implemented interface is serializable. In this case, it will avoid issue for serialization and
	// deserialization of  the closure.
	return !typeRef.isSubtypeOf(Serializable.class) && super.canCompileToJavaLambda(closure, typeRef, operation);
}
 
Example 11
Source File: XSwitchExpressions.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Determine whether the given switch expression is valid for Java version 6
 * or lower.
 */
public boolean isJavaSwitchExpression(XSwitchExpression it) {
	LightweightTypeReference switchType = getSwitchVariableType(it);
	if (switchType == null) {
		return false;
	}
	if (switchType.isSubtypeOf(Integer.TYPE)) {
		return true;
	}
	if (switchType.isSubtypeOf(Enum.class)) {
		return true;
	}
	return false;
}
 
Example 12
Source File: ExtendedEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void accept(LightweightTypeReference type) {
	for(LightweightTypeReference caughtException: caughtExceptions) {
		if (type.isSubtypeOf(caughtException.getType())) {
			return;
		}
	}
	delegate.accept(type);
}
 
Example 13
Source File: ObjectAndPrimitiveBasedCastOperationCandidateSelector.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Validate the return type of the operation.
 *
 * @param expectedTypeName the expected simple name of the return type.
 * @param type the return type.
 * @return {@code true} if the return type is valid; otherwise {@code false}.
 */
protected boolean isValidReturnType(String expectedTypeName, LightweightTypeReference type) {
	if (type != null
			&& (type.isSubtypeOf(this.castType.getType())
			|| (this.primitiveCast && type.isSubtypeOf(this.primitiveCastType.getType())))) {
		return type.getSimpleName().equals(expectedTypeName);
	}
	return false;
}
 
Example 14
Source File: FeatureScopeSessionWithContext.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean isVisible(JvmMember member, /* @Nullable */ LightweightTypeReference receiverType, /* @Nullable */ JvmIdentifiableElement receiverFeature) {
	boolean result = isVisible(member);
	if (result && JvmVisibility.PROTECTED == member.getVisibility()) {
		if (receiverFeature != null) {
			// We bypass this check for qualified.this and qualified.super in the scope provider
			// they are considered to be always visible
			/*
			 * class A {
			 *   class B {
			 *     {
			 *       A.super.toString
			 *     }
			 *   }
			 * }
			 */
			if (isThisSuperOrTypeLiteral(receiverFeature)) {
				if (receiverType == null || !receiverType.isType(Class.class)) {
					return true;
				}
			}
		}
		JvmType contextType = visibilityHelper.getRawContextType();
		if (contextType instanceof JvmDeclaredType) {
			String packageName = visibilityHelper.getPackageName();
			JvmDeclaredType declaringType = member.getDeclaringType();
			String memberPackageName = declaringType.getPackageName();
			if (Strings.equal(packageName, memberPackageName)) {
				return true;
			}
		}
		if (receiverType != null) {
			if (receiverType.isSubtypeOf(contextType)) {
				return true;
			}
			EObject container = contextType.eContainer();
			while (container instanceof JvmType) {
				if (receiverType.isSubtypeOf((JvmType)container)) {
					return true;
				}
				container = container.eContainer();
			}
		}
		return false;
	}
	return result;
}
 
Example 15
Source File: AbstractPendingLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean validateUnhandledExceptions(JvmExecutable executable, IAcceptor<? super AbstractDiagnostic> result) {
	TypeParameterSubstitutor<?> substitutor = createArgumentTypeSubstitutor();
	List<LightweightTypeReference> unhandledExceptions = null;
	List<LightweightTypeReference> expectedExceptions = getState().getExpectedExceptions();
	ITypeReferenceOwner referenceOwner = getState().getReferenceOwner();
	outer: for(JvmTypeReference typeReference: executable.getExceptions()) {
		LightweightTypeReference exception = referenceOwner.toLightweightTypeReference(typeReference);
		LightweightTypeReference resolvedException = substitutor.substitute(exception);
		if (resolvedException.isSubtypeOf(Throwable.class) && !resolvedException.isSubtypeOf(RuntimeException.class) && !resolvedException.isSubtypeOf(Error.class)) {
			for (LightweightTypeReference expectedException : expectedExceptions) {
				if (expectedException.isAssignableFrom(resolvedException)) {
					continue outer;
				}
			}
			if (unhandledExceptions == null) {
				unhandledExceptions = Lists.newArrayList(resolvedException);
			} else {
				unhandledExceptions.add(resolvedException);
			}
		}
	}
	if (unhandledExceptions != null) {
		String message = null;
		int count = unhandledExceptions.size();
		if (count > 1) {
			message = String.format("Unhandled exception types %s and %s", 
					IterableExtensions.join(unhandledExceptions.subList(0, count - 1), ", "),
					unhandledExceptions.get(count - 1));
		} else {
			message = String.format("Unhandled exception type %s", unhandledExceptions.get(0).getHumanReadableName());
		}
		String[] data = new String[unhandledExceptions.size() + 1];
		for(int i = 0; i < data.length - 1; i++) {
			LightweightTypeReference unhandled = unhandledExceptions.get(i);
			data[i] = EcoreUtil.getURI(unhandled.getType()).toString();
		}
		data[data.length - 1] = EcoreUtil.getURI(getExpression()).toString();
		AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(
				getUnhandledExceptionSeverity(executable), 
				IssueCodes.UNHANDLED_EXCEPTION,
				message, 
				getExpression(),
				getDefaultValidationFeature(), 
				-1, 
				data);
		result.accept(diagnostic);
		return false;
	}
	return true;
}
 
Example 16
Source File: InheritanceHelper.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Replies if the given JVM element is a SARL agent.
 *
 * @param type the JVM type to test.
 * @return {@code true} if the given type is a SARL agent, or not.
 * @since 0.8
 */
public boolean isSarlAgent(LightweightTypeReference type) {
	return !type.isInterfaceType() && (getSarlElementEcoreType(type) == SarlPackage.SARL_AGENT
			|| type.isSubtypeOf(Agent.class));
}
 
Example 17
Source File: InheritanceHelper.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Replies if the given JVM element is a SARL skill.
 *
 * @param type the JVM type to test.
 * @return {@code true} if the given type is a SARL skill, or not.
 * @since 0.8
 */
public boolean isSarlSkill(LightweightTypeReference type) {
	return !type.isInterfaceType() && (getSarlElementEcoreType(type) == SarlPackage.SARL_SKILL
			|| type.isSubtypeOf(Skill.class));
}
 
Example 18
Source File: InheritanceHelper.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Replies if the given JVM element is a SARL capacity.
 *
 * @param type the JVM type to test.
 * @return {@code true} if the given type is a SARL capacity, or not.
 * @since 0.8
 */
public boolean isSarlCapacity(LightweightTypeReference type) {
	return type.isInterfaceType() && (getSarlElementEcoreType(type) == SarlPackage.SARL_CAPACITY
			|| type.isSubtypeOf(Capacity.class));
}
 
Example 19
Source File: InheritanceHelper.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Replies if the given JVM element is a SARL event.
 *
 * @param type the JVM type to test.
 * @return {@code true} if the given type is a SARL event, or not.
 * @since 0.8
 */
public boolean isSarlEvent(LightweightTypeReference type) {
	return !type.isInterfaceType() && (getSarlElementEcoreType(type) == SarlPackage.SARL_EVENT
			|| type.isSubtypeOf(Event.class));
}
 
Example 20
Source File: SarlCompiler.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Replies if the given closure could be represented by an not static anonymous class.
 *
 * @param closure the closure.
 * @param typeRef the type of the closure.
 * @param operation the operation to implement.
 * @return {@code true} if the given closure could be represented by a not-static anonymous class.
 * @since 0.8.6
 */
@SuppressWarnings("static-method")
protected boolean canBeNotStaticAnonymousClass(XClosure closure, LightweightTypeReference typeRef,
		JvmOperation operation) {
	return !typeRef.isSubtypeOf(Serializable.class);
}