org.eclipse.xtext.xbase.typesystem.IResolvedTypes Java Examples

The following examples show how to use org.eclipse.xtext.xbase.typesystem.IResolvedTypes. 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: SynonymTypesProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void assertSynonymTypes(final String expression, String ...expectedSynonymTypes)
		throws Exception {
	XExpression parsedExpression = expression(expression);
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(parsedExpression);
	LightweightTypeReference type = resolvedTypes.getActualType(parsedExpression);
	final List<LightweightTypeReference> synonyms = Lists.newArrayList();
	synonymTypeProvider.collectSynonymTypes(type, new SynonymTypesProvider.Acceptor() {
		
		@Override
		protected boolean accept(/* @NonNull */ LightweightTypeReference synonym, int flags) {
			synonyms.add(synonym);
			return true;
		}
	});
	Iterator<LightweightTypeReference> iterator = synonyms.iterator();
	for (String clazz: expectedSynonymTypes) {
		assertEquals(clazz,iterator.next().getIdentifier());
	}
	assertFalse(iterator.hasNext());
}
 
Example #2
Source File: UIStrings.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected String expressionTypes(Iterable<XExpression> expressions) {
	Iterator<XExpression> iterator = expressions.iterator();
	if (iterator.hasNext()) {
		XExpression expression = iterator.next();
		IResolvedTypes resolvedTypes = typeResolver.resolveTypes(expression);
		LightweightTypeReference reference = resolvedTypes.getActualType(expression);
		if (!iterator.hasNext()) {
			return referenceToString(reference);
		}
		StringBuilder result = new StringBuilder(reference.toString());
		while (iterator.hasNext()) {
			reference = resolvedTypes.getActualType(iterator.next());
			result.append(", ");
			result.append(referenceToString(reference));
		}
		return result.toString();
	} else {
		return "";
	}
}
 
Example #3
Source File: SARLReentrantTypeResolver.java    From sarl with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("all")
private EObject getNearestClosureOrTypeDeclaration(EObject object, IResolvedTypes resolvedTypes) {
	// Overriding for proper lambda expression.
	EObject candidate = object;
	while(candidate != null) {
		if (candidate instanceof XClosure) {
			return candidate;
		}
		if (candidate instanceof XConstructorCall) {
			// skip anonymous class constructors themselves
			if (candidate.eContainingFeature() == XtendPackage.Literals.ANONYMOUS_CLASS__CONSTRUCTOR_CALL) {
				candidate = candidate.eContainer();
			}
		} else if (candidate instanceof XtendTypeDeclaration) {
			return candidate;
		}
		if (candidate instanceof RichString) {
			LightweightTypeReference type = resolvedTypes.getActualType((RichString)candidate);
			if (type != null && type.isType(StringConcatenationClient.class)) {
				return candidate;
			}
		}
		candidate = candidate.eContainer();
	}
	return null;
}
 
Example #4
Source File: AbstractClosureTypeHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void applyToModel(IResolvedTypes resolvedTypes) {
	if (!closure.isExplicitSyntax()) {
		List<JvmFormalParameter> parametersToAdd = getParameters();
		InternalEList<JvmFormalParameter> targetList = (InternalEList<JvmFormalParameter>) closure.getImplicitFormalParameters();
		if (!targetList.isEmpty()) {
			// things are already available, do nothing
			return;
		}
		for(int i = 0; i < parametersToAdd.size(); i++) {
			JvmFormalParameter parameter = parametersToAdd.get(i);
			LightweightTypeReference parameterType = resolvedTypes.getActualType(parameter);
			if (parameterType == null) {
				throw new IllegalStateException("Cannot find type for parameter " + parameter.getSimpleName());
			}
			JvmTypeReference typeReference = parameterType.toTypeReference();
			parameter.setParameterType(typeReference);
			targetList.addUnique(parameter);
		}
	}
	((XClosureImplCustom) closure).setLinked(true);
}
 
Example #5
Source File: CompoundReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected IResolvedTypes getDelegate(int idx) {
	if (!sealed) {
		reentrantResolve(getMonitor());
	}
	if (idx < delegates.length) {
		IResolvedTypes result = delegates[idx];
		if (result == null) {
			if (next != idx) {
				return delegates[idx] = resolvers.get(idx).reentrantResolve(getMonitor());
			}
			return IResolvedTypes.NULL;
		}
		return result;
	}
	throw new IndexOutOfBoundsException("Index: "+idx+", Size: "+delegates.length);
}
 
Example #6
Source File: CachingBatchTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
/* @NonNull */
protected IResolvedTypes doResolveTypes(final /* @Nullable */ EObject object, final /* @Nullable */ CancelIndicator monitor) {
	// TODO: remove when we switch to an Xtend scope provider without artificial feature calls  
	EObject nonArtificialObject = object;
	if(object.eResource() == null && object instanceof XAbstractFeatureCall) {
		nonArtificialObject = ((XAbstractFeatureCall) object).getFeature();
	}
	// TODO
	final Resource resource = nonArtificialObject.eResource();
	final LazyResolvedTypes result = cache.get(CachingBatchTypeResolver.class, resource, new Provider<LazyResolvedTypes>() {
		@Override
		public LazyResolvedTypes get() {
			final IReentrantTypeResolver resolver = delegate.getTypeResolver(object);
			return new LazyResolvedTypes(resolver, resource);
		}
	});
	cache.execWithoutCacheClear(resource, new IUnitOfWork.Void<Resource>() {
		@Override
		public void process(Resource state) throws Exception {
			// trigger the actual resolution after the thing was cached
			result.resolveTypes(monitor == null ? CancelIndicator.NullImpl : monitor); 
		}
	});
	return result;
}
 
Example #7
Source File: FeatureScopes.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected IScope createStaticExtensionsScope(XExpression receiver, LightweightTypeReference receiverType, EObject featureCall, IScope parent, IFeatureScopeSession session, IResolvedTypes resolvedTypes) {
	IFeatureScopeSession nextCaptureLayer = session.getNextCaptureLayer();
	if (nextCaptureLayer != null) {
		parent = createStaticExtensionsScope(receiver, receiverType, featureCall, parent, nextCaptureLayer, resolvedTypes);
	}
	IScope result = parent;
	if (receiver == null) {
		// 'this' is a valid implicit first argument, e.g. implementations of Iterable may use #filter on themselves
		result = createImplicitExtensionScope(THIS, featureCall, session, resolvedTypes, result);
		// 'it' has a higher priority than 'this' as implicit first argument
		result = createImplicitExtensionScope(IT, featureCall, session, resolvedTypes, result);
		return result;
	} else {
		if (receiverType == null) {
			throw new IllegalStateException("Unknown receiver type");
		}
		result = createStaticExtensionsScope(featureCall, receiver, receiverType, false, parent, session);
	}
	return result;
}
 
Example #8
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
private EObject getNearestClosureOrTypeDeclaration(EObject object, IResolvedTypes resolvedTypes) {
	EObject candidate = object;
	while(candidate != null) {
		if (candidate instanceof XClosure) {
			return candidate;
		}
		if (candidate instanceof XConstructorCall) {
			// skip anonymous class constructors themselves
			if (candidate.eContainingFeature() == XtendPackage.Literals.ANONYMOUS_CLASS__CONSTRUCTOR_CALL) {
				candidate = candidate.eContainer();
			}
		} else if (candidate instanceof XtendTypeDeclaration) {
			return candidate;
		}
		if (candidate instanceof RichString) {
			LightweightTypeReference type = resolvedTypes.getActualType((RichString)candidate);
			if (type != null && type.isType(StringConcatenationClient.class)) {
				return candidate;
			}
		}
		candidate = candidate.eContainer();
	}
	return null;
}
 
Example #9
Source File: XSwitchExpressions.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public boolean isJavaCaseExpression(XSwitchExpression it, XCasePart casePart) {
	if (casePart.getTypeGuard() != null) {
		return false;
	}
	XExpression caseExpression = casePart.getCase();
	if (caseExpression == null) {
		return false;
	}
	IResolvedTypes resolvedTypes = batchTypeResolver.resolveTypes(it);
	LightweightTypeReference caseType = resolvedTypes.getActualType(caseExpression);
	if (caseType == null) {
		return false;
	}
	LightweightTypeReference switchType = getSwitchVariableType(it);
	if (!switchType.isAssignableFrom(caseType)) {
		return false;
	}
	return true;
}
 
Example #10
Source File: ErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testErrorModel_011() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("annotation { ");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("val inferred = \'bar\'");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final XtendFile file = this.processWithoutException(_builder);
  final XtendTypeDeclaration annotation = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
  XtendMember _head = IterableExtensions.<XtendMember>head(annotation.getMembers());
  final XtendField field = ((XtendField) _head);
  final XExpression initializer = field.getInitialValue();
  final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(initializer);
  Assert.assertNotNull(resolvedTypes.getActualType(initializer));
}
 
Example #11
Source File: ErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testErrorModel_077() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("class C {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def m() {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("try return \'literal\' as as Boolean");
  _builder.newLine();
  _builder.append("\t\t  ");
  _builder.append("catch(NullPointerException e) return \'second thing is thrown\'\t\t  ");
  _builder.newLine();
  _builder.append("\t\t  ");
  _builder.append("catch(ClassCastException e) throw new NullPointerException()");
  _builder.newLine();
  _builder.append("    ");
  _builder.append("}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final XtendFile file = this.processWithoutException(_builder);
  final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(file);
  final XCastedExpression casted = IteratorExtensions.<XCastedExpression>last(Iterators.<XCastedExpression>filter(file.eAllContents(), XCastedExpression.class));
  Assert.assertNull(casted.getType());
  Assert.assertNotNull(resolvedTypes.getActualType(casted));
}
 
Example #12
Source File: ErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testErrorModel_008() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("annotation Bar { @");
  _builder.newLine();
  final XtendFile file = this.processWithoutException(_builder);
  final XtendTypeDeclaration typeDeclaration = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
  final EList<XAnnotation> annotations = IterableExtensions.<XtendMember>last(typeDeclaration.getMembers()).getAnnotations();
  final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XAnnotation>head(annotations));
  Assert.assertNotNull(resolvedTypes.getActualType(IterableExtensions.<XAnnotation>head(annotations)));
}
 
Example #13
Source File: XSwitchExpressions.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public LightweightTypeReference getSwitchVariableType(XSwitchExpression it) {
	IResolvedTypes resolvedTypes = batchTypeResolver.resolveTypes(it);
	JvmFormalParameter declaredParam = it.getDeclaredParam();
	if (declaredParam == null) {
		return resolvedTypes.getActualType(it.getSwitch());
	}
	LightweightTypeReference paramType = resolvedTypes.getActualType(declaredParam);
	if (paramType != null) {
		return paramType;
	} else {
		return resolvedTypes.getActualType(it.getSwitch());
	}
}
 
Example #14
Source File: AbstractBatchReturnTypeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testProxy() throws Exception {
  final XFeatureCall proxy = XbaseFactory.eINSTANCE.createXFeatureCall();
  ((InternalEObject) proxy).eSetProxyURI(URI.createURI("path#fragment"));
  final IResolvedTypes typeResolution = this.getTypeResolver().resolveTypes(proxy);
  Assert.assertNotNull(typeResolution);
  Assert.assertEquals(IResolvedTypes.NULL, typeResolution);
}
 
Example #15
Source File: TypeConvertingCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private boolean mustCheckForMandatoryTypeCast(IResolvedTypes resolvedTypes, XExpression expression) {
	if (resolvedTypes.isRefinedType(expression)) {
		return true;
	}
	LightweightTypeReference actualType = resolvedTypes.getActualType(expression);
	if (actualType != null && actualType.isMultiType()) {
		return true;
	}
	return false;
}
 
Example #16
Source File: FeatureCallAsTypeLiteralHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if the given feature call can be a type literal (structurally). 
 * Otherwise <code>false</code>.
 */
public boolean isPotentialTypeLiteral(XExpression featureCall, /* @Nullable */ IResolvedTypes resolvedTypes) {
	if (featureCall instanceof XMemberFeatureCall) {
		return isPotentialTypeLiteralImpl(featureCall, resolvedTypes, ((XMemberFeatureCall) featureCall).isExplicitStatic());	
	}
	return isPotentialTypeLiteralImpl(featureCall, resolvedTypes, false);
}
 
Example #17
Source File: XbaseCopyQualifiedNameService.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String toQualifiedName(XExpression expression, IResolvedExecutable resolvedExecutable, JvmExecutable executable,
		IResolvedTypes resolvedTypes, List<XExpression> arguments) {
	LightweightTypeReference actualType = resolvedTypes.getActualType(expression);
	if (actualType != null && !actualType.isAny() && !actualType.isUnknown()) {
		return actualType.getHumanReadableName();
	}

	int index = arguments.indexOf(expression);
	if (resolvedExecutable == null) {
		return executable.getParameters().get(index).getParameterType().getSimpleName();
	}
	return resolvedExecutable.getResolvedParameterTypes().get(index).getSimpleName();
}
 
Example #18
Source File: Oven.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void assertIdentifiableTypeIsResolved(JvmIdentifiableElement identifiable, IResolvedTypes types) {
	if (identifiable.getSimpleName() == null) {
		return;
	}
	LightweightTypeReference type = types.getActualType(identifiable);
	Assert.assertNotNull(identifiable.toString(), type);
	Assert.assertNotNull(identifiable.toString() + " / " + type, type.getIdentifier());
}
 
Example #19
Source File: TypeResolverPerformanceTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public LightweightTypeReference resolvesTo(String expression, String type) {
	try {
		XExpression xExpression = expression(expression.replace("$$", "org::eclipse::xtext::xbase::lib::"), false);
		IResolvedTypes resolvedTypes = getTypeResolver().resolveTypes(xExpression);
		LightweightTypeReference lightweight = resolvedTypes.getActualType(xExpression);
		Assert.assertEquals(type, lightweight.getSimpleName());
		return lightweight;
	} catch (Exception e) {
		throw Exceptions.sneakyThrow(e);
	}
}
 
Example #20
Source File: ExtractVariableRefactoring.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendDeclaration(ISourceAppender section, ITextRegion expressionRegion) throws BadLocationException {
	section
		.append((isFinal) ? "val" : "var")
		.append(" ")
		.append(variableName)
		.append(" = ");
	String expressionAsString = document.get(expressionRegion.getOffset(), expressionRegion.getLength());
	if(expression instanceof XClosure) {
		XClosure closure = (XClosure) expression;
		if(expressionAsString.startsWith("[") && expressionAsString.endsWith("]")) {
			expressionAsString = expressionAsString.substring(1, expressionAsString.length()-1);
		} 
		section.append("[");
		boolean isFirst = true;
		if(!closure.getFormalParameters().isEmpty()) {
			IResolvedTypes types = typeResolver.resolveTypes(closure);
			for(JvmFormalParameter parameter: closure.getFormalParameters()) {
				if(!isFirst)
					section.append(", ");
				isFirst = false;
				LightweightTypeReference parameterType = types.getActualType(parameter);
				section.append(parameterType);
				section
					.append(" ")
					.append(parameter.getIdentifier());
			}
		}
		section.append(" | ");
		if(!closure.getDeclaredFormalParameters().isEmpty())
			section.append(expressionAsString.substring(expressionAsString.indexOf("|")+1));
		else 
			section.append(expressionAsString);
		section.append("]");
	} else {
		section.append(expressionAsString);
	}
}
 
Example #21
Source File: InvokedResolvedOperation.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected InvokedResolvedOperation resolve(XAbstractFeatureCall featureCall, IResolvedTypes resolvedTypes, ITypeReferenceOwner owner) {
	JvmIdentifiableElement feature = featureCall.getFeature();
	if (feature instanceof JvmOperation) {
		LightweightTypeReference contextType = getReceiverType(featureCall, resolvedTypes, owner);
		return new InvokedResolvedOperation((JvmOperation) feature, contextType, resolvedTypes.getActualTypeArguments(featureCall), overrideTester);
	} else {
		throw new IllegalArgumentException();
	}
}
 
Example #22
Source File: AbstractBatchReturnTypeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void doResolvesTo(final String expression, final String type) {
  try {
    final XExpression parsedExpression = this.expression(expression, false);
    final IResolvedTypes resolvedTypes = this.getTypeResolver().resolveTypes(parsedExpression);
    final LightweightTypeReference resolvedType = resolvedTypes.getReturnType(parsedExpression);
    Assert.assertEquals(expression, type, resolvedType.getSimpleName());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #23
Source File: XbaseCopyQualifiedNameService.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected IResolvedExecutable _resolveExecutable(JvmConstructor constructor, XAbstractFeatureCall featureCall,
		IResolvedTypes resolvedTypes) {
	XExpression actualReceiver = featureCall.getActualReceiver();
	if (actualReceiver != null) {
		LightweightTypeReference actualType = resolvedTypes.getActualType(actualReceiver);
		return findDeclaredConstructor(constructor, actualType);
	}
	return null;
}
 
Example #24
Source File: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected JvmTypeReference doGetTypReferenceWithAnotherTypeReference() {
	IResolvedTypes resolveTypes = typeResolver.resolveTypes(expression);
	LightweightTypeReference actualType = returnType ? resolveTypes.getReturnType(expression) : resolveTypes.getActualType(expression);
	if (actualType == null) {
		actualType = returnType ? resolvedTypes.getExpectedReturnType(expression) : resolvedTypes.getExpectedType(expression);
	}
	if (actualType == null)
		return null;
	return toJavaCompliantTypeReference(convertLocalType(actualType), session);
}
 
Example #25
Source File: ErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testErrorModel_007() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("@Data class A {}");
  _builder.newLine();
  _builder.append("@Data class");
  _builder.newLine();
  final XtendFile file = this.processWithoutException(_builder);
  final XtendTypeDeclaration typeDeclaration = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
  final EList<XAnnotation> annotations = typeDeclaration.getAnnotations();
  final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XAnnotation>head(annotations));
  Assert.assertNotNull(resolvedTypes.getActualType(IterableExtensions.<XAnnotation>head(annotations)));
}
 
Example #26
Source File: AbstractBatchReturnTypeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void assertExpressionTypeIsResolved(final XExpression expression, final IResolvedTypes types) {
  final LightweightTypeReference type = types.getActualType(expression);
  String _string = expression.toString();
  String _plus = ("Type is not resolved. Expression: " + _string);
  Assert.assertNotNull(_plus, type);
  String _string_1 = expression.toString();
  String _plus_1 = (_string_1 + " / ");
  String _plus_2 = (_plus_1 + type);
  Assert.assertNotNull(_plus_2, type.getIdentifier());
}
 
Example #27
Source File: CompoundReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IConstructorLinkingCandidate getLinkingCandidate(/* @Nullable */ XConstructorCall constructorCall) {
	if (constructorCall == null)
		return null;
	IResolvedTypes delegate = getDelegate(constructorCall);
	return delegate.getLinkingCandidate(constructorCall);
}
 
Example #28
Source File: CompoundReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public JvmIdentifiableElement getLinkedFeature(/* @Nullable */ XConstructorCall constructorCall) {
	if (constructorCall == null)
		return null;
	IResolvedTypes delegate = getDelegate(constructorCall);
	return delegate.getLinkedFeature(constructorCall);
}
 
Example #29
Source File: CompoundReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public JvmIdentifiableElement getLinkedFeature(/* @Nullable */ XAbstractFeatureCall featureCall) {
	if (featureCall == null)
		return null;
	IResolvedTypes delegate = getDelegate(featureCall);
	return delegate.getLinkedFeature(featureCall);
}
 
Example #30
Source File: CompoundReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected IResolvedTypes getDelegate(EObject object) {
	for(int i = 0; i < resolvers.size(); i++) {
		AbstractRootedReentrantTypeResolver resolver = resolvers.get(i);
		if (resolver.isHandled(object)) {
			return getDelegate(i);
		}
	}
	return IResolvedTypes.NULL;
}