Java Code Examples for org.eclipse.xtext.xbase.typesystem.IResolvedTypes#getActualType()

The following examples show how to use org.eclipse.xtext.xbase.typesystem.IResolvedTypes#getActualType() . 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: CreateMemberQuickfixes.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected List<LightweightTypeReference> getResolvedArgumentTypes(EObject context, JvmIdentifiableElement logicalContainer, List<XExpression> arguments) {
	List<LightweightTypeReference> argumentTypes = newArrayList();
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(context);
	for(XExpression argument: arguments) {
		LightweightTypeReference resolved = resolvedTypes.getActualType(argument);
		if(resolved == null) {
			StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(services, context);
			argumentTypes.add(owner.newReferenceToObject());
		} else {
			LocalTypeSubstitutor substitutor = new LocalTypeSubstitutor(resolved.getOwner(), logicalContainer);
			argumentTypes.add(substitutor.withoutLocalTypes(resolved));
		}
	}
	return argumentTypes;

}
 
Example 2
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 3
Source File: Oven.java    From xtext-eclipse 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 4
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 5
Source File: AbstractBatchTypeResolverTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void assertExpressionTypeIsResolved(XExpression expression, IResolvedTypes types) {
	LightweightTypeReference type = types.getActualType(expression);
	Assert.assertNotNull("Type is not resolved. Expression: " + expression.toString(), type);
	Assert.assertNotNull(expression.toString() + " / " + type, type.getIdentifier());
	LightweightTypeReference expectedType = types.getExpectedType(expression);
	Assert.assertNotNull(expression.toString(), String.valueOf(expectedType));
}
 
Example 6
Source File: CompoundReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public LightweightTypeReference getActualType(JvmIdentifiableElement identifiable) {
	for(int i = 0; i < resolvers.size(); i++) {
		AbstractRootedReentrantTypeResolver resolver = resolvers.get(i);
		if (resolver.isHandled(identifiable)) {
			IResolvedTypes delegate = getDelegate(i);
			return delegate.getActualType(identifiable);
		}
	}
	return null;
}
 
Example 7
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 8
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 9
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 10
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 11
Source File: SARLCodeMiningProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the inferred type for the given expression.
 *
 * @param expr the expression.
 * @return the type of the expression.
 */
protected LightweightTypeReference getLightweightType(XExpression expr) {
	final IResolvedTypes resolvedTypes = getResolvedTypes(expr);
	final LightweightTypeReference expressionType = resolvedTypes.getActualType(expr);
	if (expr instanceof AnonymousClass) {
		final List<LightweightTypeReference> superTypes = expressionType.getSuperTypes();
		if (superTypes.size() == 1) {
			return superTypes.get(0);
		}
	}
	return expressionType;
}
 
Example 12
Source File: FeatureScopes.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected IScope createImplicitExtensionScope(QualifiedName implicitName, EObject featureCall,
		IFeatureScopeSession session, IResolvedTypes resolvedTypes, IScope parent) {
	IEObjectDescription thisDescription = session.getLocalElement(implicitName);
	if (thisDescription != null) {
		JvmIdentifiableElement thisElement = (JvmIdentifiableElement) thisDescription.getEObjectOrProxy();
		LightweightTypeReference type = resolvedTypes.getActualType(thisElement);
		if (type != null && !type.isUnknown()) {
			XFeatureCall implicitReceiver = xbaseFactory.createXFeatureCall();
			implicitReceiver.setFeature(thisElement);
			return createStaticExtensionsScope(featureCall, implicitReceiver, type, true, parent, session);
		}
	}
	return parent;
}
 
Example 13
Source File: SARLReentrantTypeResolver.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the type of the field that represents a SARL capacity buffer.
 *
 * @param resolvedTypes the resolver of types.
 * @param field the field.
 * @return the type, never {@code null}.
 */
protected LightweightTypeReference getSarlCapacityFieldType(IResolvedTypes resolvedTypes, JvmField field) {
	// For capacity call redirection
	LightweightTypeReference fieldType = resolvedTypes.getActualType(field);
	final JvmAnnotationReference capacityAnnotation = this.annotationLookup.findAnnotation(field,
			ImportedCapacityFeature.class);
	if (capacityAnnotation != null) {
		final JvmTypeReference ref = ((JvmTypeAnnotationValue) capacityAnnotation.getValues().get(0)).getValues().get(0);
		fieldType = resolvedTypes.getActualType(ref.getType());
	}
	return fieldType;
}
 
Example 14
Source File: StringLiteralTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void resolvesStringLiteralsTo(final String expression, final String... types) {
  final String expressionWithQualifiedNames = expression.replace("$$", "org::eclipse::xtext::xbase::lib::");
  final List<XStringLiteral> featureCalls = this.findLiterals(expressionWithQualifiedNames);
  Assert.assertFalse(featureCalls.isEmpty());
  Assert.assertEquals(((List<String>)Conversions.doWrapArray(types)).size(), featureCalls.size());
  final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XStringLiteral>head(featureCalls));
  final Procedure2<XStringLiteral, Integer> _function = (XStringLiteral featureCall, Integer index) -> {
    final LightweightTypeReference type = resolvedTypes.getActualType(featureCall);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("failed for literal at ");
    _builder.append(index);
    Assert.assertEquals(_builder.toString(), types[(index).intValue()], type.getSimpleName());
  };
  IterableExtensions.<XStringLiteral>forEach(featureCalls, _function);
}
 
Example 15
Source File: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void completeXMemberFeatureCall_Feature(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	if (model instanceof XMemberFeatureCall) {
		XExpression memberCallTarget = ((XMemberFeatureCall) model).getMemberCallTarget();
		IResolvedTypes resolvedTypes = typeResolver.resolveTypes(memberCallTarget);
		LightweightTypeReference memberCallTargetType = resolvedTypes.getActualType(memberCallTarget);
		Iterable<JvmFeature> featuresToImport = getFavoriteStaticFeatures(model, input -> {
			if(input instanceof JvmOperation && input.isStatic()) {
				List<JvmFormalParameter> parameters = ((JvmOperation) input).getParameters();
				if(parameters.size() > 0) {
					JvmFormalParameter firstParam = parameters.get(0);
					JvmTypeReference parameterType = firstParam.getParameterType();
					if(parameterType != null) {
						LightweightTypeReference lightweightTypeReference = memberCallTargetType.getOwner().toLightweightTypeReference(parameterType);
						if(lightweightTypeReference != null) {
							return memberCallTargetType.isAssignableFrom(lightweightTypeReference);
						}
					}
				}
			}
			return false;
		});
		// Create StaticExtensionFeatureDescriptionWithImplicitFirstArgument instead of SimpleIdentifiableElementDescription since we want the Proposal to show parameters
		Iterable<IEObjectDescription> scopedFeatures = Iterables.transform(featuresToImport, feature -> {
			QualifiedName qualifiedName = QualifiedName.create(feature.getSimpleName());
			return new StaticExtensionFeatureDescriptionWithImplicitFirstArgument(qualifiedName, feature, memberCallTarget, memberCallTargetType, 0, true);
		});
		// Scope for all static features
		IScope staticMemberScope = new SimpleScope(IScope.NULLSCOPE, scopedFeatures);
		proposeFavoriteStaticFeatures(model, context, acceptor, staticMemberScope);
		// Regular proposals
		createReceiverProposals(((XMemberFeatureCall) model).getMemberCallTarget(), (CrossReference) assignment.getTerminal(),
				context, acceptor);
	} else if (model instanceof XAssignment) {
		createReceiverProposals(((XAssignment) model).getAssignable(), (CrossReference) assignment.getTerminal(),
				context, acceptor);
	}
}
 
Example 16
Source File: AbstractExpressionGenerator.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Compute the expected type of the given expression.
 *
 * @param expr the expression.
 * @return the expected type of the argument.
 */
protected LightweightTypeReference getExpectedType(XExpression expr) {
	final IResolvedTypes resolvedTypes = getTypeResolver().resolveTypes(expr);
	final LightweightTypeReference actualType = resolvedTypes.getActualType(expr);
	return actualType;
}
 
Example 17
Source File: ExtractMethodRefactoring.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public RefactoringStatus checkInitialConditions(final IProgressMonitor pm) throws CoreException,
		OperationCanceledException {
	StatusWrapper status = statusProvider.get();
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(firstExpression, new CancelIndicator() {
		@Override
		public boolean isCanceled() {
			return pm.isCanceled();
		}
	});
	try {
		Set<String> calledExternalFeatureNames = newHashSet();
		returnType = calculateReturnType(resolvedTypes);
		if (returnType != null && !equal("void", returnType.getIdentifier()))
			returnExpression = lastExpression;
		boolean isReturnAllowed = isEndOfOriginalMethod(); 
		for (EObject element : EcoreUtil2.eAllContents(originalMethod.getExpression())) {
			if (pm.isCanceled()) {
				throw new OperationCanceledException();
			}
			boolean isLocalExpression = EcoreUtil.isAncestor(expressions, element);
			if (element instanceof XFeatureCall) {
				XFeatureCall featureCall = (XFeatureCall) element;
				JvmIdentifiableElement feature = featureCall.getFeature();
				LightweightTypeReference featureType = resolvedTypes.getActualType(featureCall);
				boolean isLocalFeature = EcoreUtil.isAncestor(expressions, feature);
				if (!isLocalFeature && isLocalExpression) {
					// call-out
					if (feature instanceof JvmFormalParameter || feature instanceof XVariableDeclaration) {
						if (!calledExternalFeatureNames.contains(feature.getSimpleName())) {
							calledExternalFeatureNames.add(feature.getSimpleName());
							ParameterInfo parameterInfo = new ParameterInfo(featureType.getIdentifier(), 
									feature.getSimpleName(), 
									parameterInfos.size());
							parameterInfos.add(parameterInfo);
							parameter2type.put(parameterInfo, featureType);
						}
						externalFeatureCalls.put(feature.getSimpleName(), featureCall);
					}
				} else if (isLocalFeature && !isLocalExpression) {
					// call-in
					if (returnExpression != null) {
						status.add(RefactoringStatus.FATAL,
								"Ambiguous return value: Multiple local variables are accessed in subsequent code.");
						break;
					}
					returnExpression = featureCall;
					returnType = featureType;
				}
			} else if(isLocalExpression) {
				if(element instanceof XReturnExpression && !isReturnAllowed) {
					status.add(RefactoringStatus.FATAL,
						"Extracting method would break control flow due to return statements.");
					break;
				} else if (element instanceof JvmTypeReference) {
					JvmType type = ((JvmTypeReference) element).getType();
					if (type instanceof JvmTypeParameter) {
						JvmOperation operation = associations.getDirectlyInferredOperation(originalMethod);
						if (operation != null) {
							List<JvmTypeParameter> typeParameters = operation.getTypeParameters();
							if (typeParameters.contains(type)) 
								neededTypeParameters.add((JvmTypeParameter) type);
						}
					}
				} else if (element instanceof JvmFormalParameter)
					localFeatureNames.add(((JvmFormalParameter) element).getName());
				else if (element instanceof XVariableDeclaration)
					localFeatureNames.add(((XVariableDeclaration) element).getIdentifier());
			}
		}
	} catch (OperationCanceledException e) {
		throw e;
	} catch (Exception exc) {
		handleException(exc, status);
	}
	return status.getRefactoringStatus();
}
 
Example 18
Source File: CompoundReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public LightweightTypeReference getActualType(XExpression expression) {
	IResolvedTypes delegate = getDelegate(expression);
	return delegate.getActualType(expression);
}
 
Example 19
Source File: AbstractBatchTypeResolverTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public void assertIdentifiableTypeIsResolved(JvmIdentifiableElement identifiable, IResolvedTypes types) {
	LightweightTypeReference type = types.getActualType(identifiable);
	Assert.assertNotNull(identifiable.toString(), type);
	Assert.assertNotNull(identifiable.toString() + " / " + type, type.getIdentifier());
}
 
Example 20
Source File: BatchLinkingTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public void assertExpressionTypeIsResolved(XExpression expression, IResolvedTypes types) {
	LightweightTypeReference type = types.getActualType(expression);
	Assert.assertNotNull(expression.toString(), type);
	Assert.assertNotNull(expression.toString() + " / " + type, type.getIdentifier());
}