use of org.eclipse.xtext.xbase.XVariableDeclaration in project xtext-xtend by eclipse.
the class XtendTypeComputer method _computeTypes.
protected void _computeTypes(RichString object, ITypeComputationState state) {
List<XExpression> expressions = object.getExpressions();
if (!expressions.isEmpty()) {
for (XExpression expression : expressions) {
ITypeComputationState expressionState = state.withoutExpectation();
expressionState.computeTypes(expression);
if (expression instanceof XVariableDeclaration) {
addLocalToCurrentScope((XVariableDeclaration) expression, state);
}
}
}
for (ITypeExpectation expectation : state.getExpectations()) {
LightweightTypeReference expectedType = expectation.getExpectedType();
if (expectedType != null && expectedType.isType(StringConcatenation.class)) {
expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
} else if (expectedType != null && expectedType.isType(StringConcatenationClient.class)) {
expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
} else if (expectedType != null && expectedType.isType(String.class)) {
expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
// TODO this special treatment here should become obsolete as soon as the expectations are properly propagated
} else if (!(object.eContainer() instanceof XCastedExpression) && object.eContainingFeature() != XbasePackage.Literals.XMEMBER_FEATURE_CALL__MEMBER_CALL_TARGET && (expectedType != null && !expectedType.isResolved() || expectedType == null && !expectation.isVoidTypeAllowed())) {
LightweightTypeReference type = getRawTypeForName(String.class, state);
expectation.acceptActualType(type, ConformanceFlags.UNCHECKED | ConformanceFlags.DEMAND_CONVERSION);
} else {
LightweightTypeReference type = getRawTypeForName(CharSequence.class, state);
expectation.acceptActualType(type, ConformanceFlags.UNCHECKED);
}
}
}
use of org.eclipse.xtext.xbase.XVariableDeclaration in project xtext-xtend by eclipse.
the class ExtractMethodRefactoring method calculateExpressions.
protected List<XExpression> calculateExpressions(final List<XExpression> expressions) {
final XExpression firstExpression = expressions.get(0);
// instead of the complete XVariableDeclaration
if (expressions.size() == 1 && firstExpression instanceof XVariableDeclaration) {
final XtendFunction originalMethod = EcoreUtil2.getContainerOfType(firstExpression, XtendFunction.class);
for (final EObject element : Iterables.filter(EcoreUtil2.eAllContents(originalMethod.getExpression()), new Predicate<EObject>() {
@Override
public boolean apply(EObject input) {
return !EcoreUtil.isAncestor(expressions, input);
}
})) {
if (element instanceof XFeatureCall) {
XFeatureCall featureCall = (XFeatureCall) element;
JvmIdentifiableElement feature = featureCall.getFeature();
if (EcoreUtil.isAncestor(expressions, feature)) {
return singletonList(((XVariableDeclaration) firstExpression).getRight());
}
}
}
}
return expressions;
}
use of org.eclipse.xtext.xbase.XVariableDeclaration in project xtext-xtend by eclipse.
the class ExtractMethodRefactoring method checkInitialConditions.
@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();
}
use of org.eclipse.xtext.xbase.XVariableDeclaration in project xtext-xtend by eclipse.
the class XtendHoverSignatureProviderTest method testSignatureForXVariableDeclaration.
@Test
public void testSignatureForXVariableDeclaration() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package testPackage");
_builder.newLine();
_builder.append("class Foo {");
_builder.newLine();
_builder.append("\t");
_builder.append("def bar(List<String> list){");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val a = \"42\"");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet());
final XtendClass clazz = IterableExtensions.<XtendClass>head(Iterables.<XtendClass>filter(xtendFile.getXtendTypes(), XtendClass.class));
XtendMember _get = clazz.getMembers().get(0);
final XtendFunction xtendFunction = ((XtendFunction) _get);
XExpression _expression = xtendFunction.getExpression();
XExpression _get_1 = ((XBlockExpression) _expression).getExpressions().get(0);
final XVariableDeclaration variable = ((XVariableDeclaration) _get_1);
final String signature = this.signatureProvider.getSignature(variable);
Assert.assertEquals("String a", signature);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.xbase.XVariableDeclaration in project xtext-xtend by eclipse.
the class UIStringsTest method testReferenceToString_3.
/**
* The qualified name of the type is specified.
* The JvmTypeReference is proxy.
*/
@Test
public void testReferenceToString_3() throws Exception {
XtendFile file1 = file("package org.eclipse.xtend.core.tests.validation.uistrings\n" + "public interface InterfaceA { }\n" + "public class ClassB implements InterfaceA { }\n" + "public class ClassC extends ClassB {\n" + "}\n");
assertNotNull(file1);
XtendFile file2 = file("package org.eclipse.xtend.core.tests.validation.uistrings\n" + "class XtendClass1 {\n" + " def test() {\n" + " val x = new List<org.eclipse.xtend.core.tests.validation.uistrings.ClassC>\n" + " }\n" + "}\n");
XtendClass clazz = (XtendClass) file2.getXtendTypes().get(0);
XBlockExpression block = (XBlockExpression) ((XtendFunction) clazz.getMembers().get(0)).getExpression();
XVariableDeclaration declaration = (XVariableDeclaration) block.getExpressions().get(0);
XConstructorCall cons = (XConstructorCall) declaration.getRight();
JvmTypeReference reference = cons.getTypeArguments().get(0);
assertNotNull(reference);
assertNotNull(reference.getType());
assertTrue(reference.getType().eIsProxy());
assertNotNull(reference.eResource());
assertEquals("ClassC", this.uiStrings.referenceToString(reference, "the-default-label"));
}
Aggregations