org.eclipse.xtext.xbase.XCastedExpression Java Examples

The following examples show how to use org.eclipse.xtext.xbase.XCastedExpression. 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: JavaInlineExpressionCompiler.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Append the inline code for the given XCastedExpression.
 *
 * @param expression the expression of the operation.
 * @param parentExpression is the expression that contains this one, or {@code null} if the current expression is
 *     the root expression.
 * @param feature the feature that contains the expression.
 * @param output the output.
 * @return {@code true} if a text was appended.
 */
protected Boolean _generate(XCastedExpression expression, XExpression parentExpression, XtendExecutable feature,
		InlineAnnotationTreeAppendable output) {
	final InlineAnnotationTreeAppendable child = newAppendable(output.getImportManager());
	boolean bool = generate(expression.getTarget(), expression, feature, child);
	final String childContent = child.getContent();
	if (!Strings.isEmpty(childContent)) {
		output.append("("); //$NON-NLS-1$
		output.append(expression.getType().getType());
		output.append(")"); //$NON-NLS-1$
		output.append(childContent);
		output.setConstant(child.isConstant());
		bool = true;
	}
	return bool;
}
 
Example #2
Source File: AbstractConstantExpressionsInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public Object internalEvaluate(final XExpression it, final Context ctx) {
  if (it instanceof XBinaryOperation) {
    return _internalEvaluate((XBinaryOperation)it, ctx);
  } else if (it instanceof XUnaryOperation) {
    return _internalEvaluate((XUnaryOperation)it, ctx);
  } else if (it instanceof XBooleanLiteral) {
    return _internalEvaluate((XBooleanLiteral)it, ctx);
  } else if (it instanceof XCastedExpression) {
    return _internalEvaluate((XCastedExpression)it, ctx);
  } else if (it instanceof XStringLiteral) {
    return _internalEvaluate((XStringLiteral)it, ctx);
  } else if (it instanceof XTypeLiteral) {
    return _internalEvaluate((XTypeLiteral)it, ctx);
  } else if (it instanceof XAnnotation) {
    return _internalEvaluate((XAnnotation)it, ctx);
  } else if (it != null) {
    return _internalEvaluate(it, ctx);
  } else if (it == null) {
    return _internalEvaluate((Void)null, ctx);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, ctx).toString());
  }
}
 
Example #3
Source File: TypeConvertingCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
private boolean mustInsertTypeCast(XExpression expression, LightweightTypeReference actualType) {
	IResolvedTypes resolvedTypes = getResolvedTypes(expression);
	if (mustCheckForMandatoryTypeCast(resolvedTypes, expression)) {
		if (expression instanceof XAbstractFeatureCall) {
			LightweightTypeReference featureType = resolvedTypes.getActualType(((XAbstractFeatureCall) expression).getFeature());
			if (featureType != null && !featureType.isMultiType() && actualType.isAssignableFrom(featureType)) {
				return false;
			}
			if (featureType != null && featureType.isMultiType()) {
				JvmTypeReference compliantTypeReference = featureType.toJavaCompliantTypeReference();
				if (actualType.isAssignableFrom(featureType.getOwner().toLightweightTypeReference(compliantTypeReference))) {
					return false;
				}
			}
		}
		if (expression.eContainer() instanceof XCastedExpression) {
			XCastedExpression castedExpression = (XCastedExpression) expression.eContainer();
			LightweightTypeReference castedExpressionType = getResolvedTypes(castedExpression).getActualType(castedExpression);
			if (castedExpressionType != null) {
				return actualType.getType() != castedExpressionType.getType();	
			}
		}
		return true;
	}
	return false;
}
 
Example #4
Source File: TypeConvertingCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
private void convertArrayToList(final LightweightTypeReference left, final ITreeAppendable appendable, XExpression context,
		final Later expression) {
	if (!(context.eContainer() instanceof XCastedExpression)) {
		if (context.eContainer() instanceof XAbstractFeatureCall) {
			appendable.append("((");
		} else {
			appendable.append("(");
		}
		appendable.append(left);
		appendable.append(")");
	}
	appendable.append(Conversions.class);
	appendable.append(".doWrapArray(");
	expression.exec(appendable);
	if (!(context.eContainer() instanceof XCastedExpression)) {
		if (context.eContainer() instanceof XAbstractFeatureCall) {
			appendable.append("))");
		} else {
			appendable.append(")");
		}
	} else {
		appendable.append(")");
	}
}
 
Example #5
Source File: SerializerTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testSerialize_02() throws Exception {
	Resource resource = newResource("'foo' as String");
	XCastedExpression casted = (XCastedExpression) resource.getContents().get(0);
	
	XbaseFactory factory = XbaseFactory.eINSTANCE;
	XIfExpression ifExpression = factory.createXIfExpression();
	ifExpression.setIf(factory.createXBooleanLiteral());
	XStringLiteral stringLiteral = factory.createXStringLiteral();
	stringLiteral.setValue("value");
	ifExpression.setThen(stringLiteral);
	XInstanceOfExpression instanceOfExpression = factory.createXInstanceOfExpression();
	instanceOfExpression.setExpression(ifExpression);
	instanceOfExpression.setType(EcoreUtil.copy(casted.getType()));
	resource.getContents().clear();
	resource.getContents().add(instanceOfExpression);
	ISerializer serializer = get(ISerializer.class);
	String string = serializer.serialize(instanceOfExpression);
	// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=464846
	assertEquals("( if(false) \"value\" ) instanceof String", string);
	
	XInstanceOfExpression parsedExpression = parseHelper.parse(string);
	assertTrue(EcoreUtil.equals(instanceOfExpression, parsedExpression));
}
 
Example #6
Source File: SerializerTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testSerialize_01() throws Exception {
	Resource resource = newResource("'foo' as String");
	XCastedExpression casted = (XCastedExpression) resource.getContents().get(0);
	
	XbaseFactory factory = XbaseFactory.eINSTANCE;
	XClosure closure = factory.createXClosure();
	XStringLiteral stringLiteral = factory.createXStringLiteral();
	stringLiteral.setValue("value");
	XBlockExpression blockExpression = factory.createXBlockExpression();
	blockExpression.getExpressions().add(stringLiteral);
	closure.setExpression(blockExpression);
	closure.setExplicitSyntax(true);
	XInstanceOfExpression instanceOfExpression = factory.createXInstanceOfExpression();
	instanceOfExpression.setExpression(closure);
	instanceOfExpression.setType(EcoreUtil.copy(casted.getType()));
	resource.getContents().clear();
	resource.getContents().add(instanceOfExpression);
	ISerializer serializer = get(ISerializer.class);
	String string = serializer.serialize(instanceOfExpression);
	assertEquals("[|\"value\"] instanceof String", string);
	
	XInstanceOfExpression parsedExpression = parseHelper.parse(string);
	assertTrue(EcoreUtil.equals(instanceOfExpression, parsedExpression));
}
 
Example #7
Source File: ConstantExpressionValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public boolean isConstant(final XExpression expression) {
  if (expression instanceof XAbstractFeatureCall) {
    return _isConstant((XAbstractFeatureCall)expression);
  } else if (expression instanceof XBooleanLiteral) {
    return _isConstant((XBooleanLiteral)expression);
  } else if (expression instanceof XCastedExpression) {
    return _isConstant((XCastedExpression)expression);
  } else if (expression instanceof XNumberLiteral) {
    return _isConstant((XNumberLiteral)expression);
  } else if (expression instanceof XStringLiteral) {
    return _isConstant((XStringLiteral)expression);
  } else if (expression instanceof XTypeLiteral) {
    return _isConstant((XTypeLiteral)expression);
  } else if (expression != null) {
    return _isConstant(expression);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(expression).toString());
  }
}
 
Example #8
Source File: XbaseQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Fix(IssueCodes.OBSOLETE_CAST)
public void fixObsoletCast(final Issue issue, IssueResolutionAcceptor acceptor) {
	String fixup = "Remove unnecessary cast";
	acceptor.accept(issue, fixup, fixup, null, new IModification() {
		@Override
		public void apply(IModificationContext context) throws Exception {
			final IXtextDocument document = context.getXtextDocument();
			ReplaceRegion replacement = document.tryReadOnly(new IUnitOfWork<ReplaceRegion, XtextResource>() {

				@Override
				public ReplaceRegion exec(XtextResource state) throws Exception {
					EObject type = state.getEObject(issue.getUriToProblem().fragment());
					XCastedExpression cast = EcoreUtil2.getContainerOfType(type, XCastedExpression.class);
					INode castNode = NodeModelUtils.findActualNodeFor(cast);
					INode targetNode = NodeModelUtils.findActualNodeFor(cast.getTarget());
					return new ReplaceRegion(castNode.getTotalTextRegion(), targetNode.getText());
				}
			});
			if (replacement != null) {
				document.replace(replacement.getOffset(), replacement.getLength(), replacement.getText());
			}
		}
	});
}
 
Example #9
Source File: SwitchConstantExpressionsInterpreter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public Object internalEvaluate(final XExpression it, final Context ctx) {
  if (it instanceof XBinaryOperation) {
    return _internalEvaluate((XBinaryOperation)it, ctx);
  } else if (it instanceof XUnaryOperation) {
    return _internalEvaluate((XUnaryOperation)it, ctx);
  } else if (it instanceof XAbstractFeatureCall) {
    return _internalEvaluate((XAbstractFeatureCall)it, ctx);
  } else if (it instanceof XBooleanLiteral) {
    return _internalEvaluate((XBooleanLiteral)it, ctx);
  } else if (it instanceof XCastedExpression) {
    return _internalEvaluate((XCastedExpression)it, ctx);
  } else if (it instanceof XNumberLiteral) {
    return _internalEvaluate((XNumberLiteral)it, ctx);
  } else if (it instanceof XStringLiteral) {
    return _internalEvaluate((XStringLiteral)it, ctx);
  } else if (it instanceof XTypeLiteral) {
    return _internalEvaluate((XTypeLiteral)it, ctx);
  } else if (it instanceof XAnnotation) {
    return _internalEvaluate((XAnnotation)it, ctx);
  } else if (it != null) {
    return _internalEvaluate(it, ctx);
  } else if (it == null) {
    return _internalEvaluate((Void)null, ctx);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, ctx).toString());
  }
}
 
Example #10
Source File: ErrorSafeExtensionsTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testSerializeSafely_2() throws Exception {
	XCastedExpression e = (XCastedExpression) validatedExpression("null as String");
	TreeAppendable app = createTreeAppendable(e);
	Assert.assertFalse(errorSafeExtensions.hasErrors(e));
	errorSafeExtensions.serializeSafely(e.getType(), app);
	Assert.assertEquals("String", app.getContent());
}
 
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: UIStringsTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testReferenceToString_2() throws Exception {
	XExpression expr = expression("null as foo.String", true);
	assertTrue(expr instanceof XCastedExpression);
	XCastedExpression operator = (XCastedExpression) expr;
	JvmTypeReference reference = operator.getType();
	assertEquals("String", this.uiStrings.referenceToString(reference, "the-default-value"));
}
 
Example #13
Source File: StandardTypeParameterSubstitutorTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReference toTypeReference(CharSequence reference) {
	try {
		String expression = "null as " + reference;
		XCastedExpression castExpression = (XCastedExpression) expression(expression);
		IResolvedTypes resolvedTypes = typeResolver.resolveTypes(castExpression);
		return resolvedTypes.getActualType(castExpression);
	} catch (Exception e) {
		throw Exceptions.sneakyThrow(e);
	}
}
 
Example #14
Source File: AbstractExpectationTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug379531_02() {
  ExpectationTestingTypeComputer _typeComputer = this.getTypeComputer();
  final Function1<XExpression, Boolean> _function = (XExpression it) -> {
    return Boolean.valueOf((it instanceof XCastedExpression));
  };
  _typeComputer.setPredicate(_function);
  this.expects("newArrayList(\'\', \'\', null as CharSequence)").types("Unbound[T]").finalizedAs("CharSequence");
}
 
Example #15
Source File: AbstractExpectationTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug379531_01() {
  ExpectationTestingTypeComputer _typeComputer = this.getTypeComputer();
  final Function1<XExpression, Boolean> _function = (XExpression it) -> {
    return Boolean.valueOf((it instanceof XCastedExpression));
  };
  _typeComputer.setPredicate(_function);
  this.expects("newArrayList(\'\', \'\', null as String)").types("Unbound[T]").finalizedAs("String");
}
 
Example #16
Source File: AbstractExpectationTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testExtension() {
  ExpectationTestingTypeComputer _typeComputer = this.getTypeComputer();
  final Function1<XExpression, Boolean> _function = (XExpression it) -> {
    return Boolean.valueOf((it instanceof XCastedExpression));
  };
  _typeComputer.setPredicate(_function);
  this.expects("(null as String[]).size").types(((String) null)).finalizedAs(((String) null)).queriedAs("List<String>");
}
 
Example #17
Source File: ResolvedFeaturesTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public ResolvedFeatures toResolvedOperations(final String castExpression) {
  try {
    XExpression _expression = this.expression(castExpression);
    final XCastedExpression cast = ((XCastedExpression) _expression);
    final ResolvedFeatures result = this.overrideHelper.getResolvedFeatures(cast.getType());
    return result;
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #18
Source File: ErrorSafeExtensionsTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testSerializeSafely() throws Exception {
	XCastedExpression e = (XCastedExpression) validatedExpression("null as Unresolved");
	TreeAppendable app = createTreeAppendable(e);
	Assert.assertTrue(errorSafeExtensions.hasErrors(e));
	errorSafeExtensions.serializeSafely(e.getType(), app);
	Assert.assertEquals("/* Unresolved */", app.getContent());
}
 
Example #19
Source File: ErrorSafeExtensionsTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testSerializeSafely_1() throws Exception {
	XCastedExpression e = (XCastedExpression) validatedExpression("null as Unresolved");
	TreeAppendable app = createTreeAppendable(e);
	Assert.assertTrue(errorSafeExtensions.hasErrors(e));
	errorSafeExtensions.serializeSafely(e.getType(), "Object", app);
	Assert.assertEquals("/* Unresolved */Object", app.getContent());
}
 
Example #20
Source File: ConstantConditionsInterpreter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public EvaluationResult internalEvaluate(final XExpression it, final EvaluationContext context) {
  if (it instanceof XBinaryOperation) {
    return _internalEvaluate((XBinaryOperation)it, context);
  } else if (it instanceof XUnaryOperation) {
    return _internalEvaluate((XUnaryOperation)it, context);
  } else if (it instanceof XAbstractFeatureCall) {
    return _internalEvaluate((XAbstractFeatureCall)it, context);
  } else if (it instanceof XBooleanLiteral) {
    return _internalEvaluate((XBooleanLiteral)it, context);
  } else if (it instanceof XCastedExpression) {
    return _internalEvaluate((XCastedExpression)it, context);
  } else if (it instanceof XNullLiteral) {
    return _internalEvaluate((XNullLiteral)it, context);
  } else if (it instanceof XNumberLiteral) {
    return _internalEvaluate((XNumberLiteral)it, context);
  } else if (it instanceof XStringLiteral) {
    return _internalEvaluate((XStringLiteral)it, context);
  } else if (it instanceof XTypeLiteral) {
    return _internalEvaluate((XTypeLiteral)it, context);
  } else if (it != null) {
    return _internalEvaluate(it, context);
  } else if (it == null) {
    return _internalEvaluate((Void)null, context);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, context).toString());
  }
}
 
Example #21
Source File: XbaseFormatter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _format(final XCastedExpression expr, @Extension final IFormattableDocument doc) {
  final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
    it.oneSpace();
  };
  doc.surround(this.textRegionExtensions.regionFor(expr).keyword("as"), _function);
  doc.<XExpression>format(expr.getTarget());
  doc.<JvmTypeReference>format(expr.getType());
}
 
Example #22
Source File: ConstantExpressionsInterpreter.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public Object internalEvaluate(final XExpression it, final Context ctx) {
  if (it instanceof XBinaryOperation) {
    return _internalEvaluate((XBinaryOperation)it, ctx);
  } else if (it instanceof XFeatureCall) {
    return _internalEvaluate((XFeatureCall)it, ctx);
  } else if (it instanceof XListLiteral) {
    return _internalEvaluate((XListLiteral)it, ctx);
  } else if (it instanceof XMemberFeatureCall) {
    return _internalEvaluate((XMemberFeatureCall)it, ctx);
  } else if (it instanceof XUnaryOperation) {
    return _internalEvaluate((XUnaryOperation)it, ctx);
  } else if (it instanceof XBooleanLiteral) {
    return _internalEvaluate((XBooleanLiteral)it, ctx);
  } else if (it instanceof XCastedExpression) {
    return _internalEvaluate((XCastedExpression)it, ctx);
  } else if (it instanceof XNumberLiteral) {
    return _internalEvaluate((XNumberLiteral)it, ctx);
  } else if (it instanceof XStringLiteral) {
    return _internalEvaluate((XStringLiteral)it, ctx);
  } else if (it instanceof XTypeLiteral) {
    return _internalEvaluate((XTypeLiteral)it, ctx);
  } else if (it instanceof XAnnotation) {
    return _internalEvaluate((XAnnotation)it, ctx);
  } else if (it != null) {
    return _internalEvaluate(it, ctx);
  } else if (it == null) {
    return _internalEvaluate((Void)null, ctx);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, ctx).toString());
  }
}
 
Example #23
Source File: SARLHoverSignatureProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the hover for a SARL casted expression.
 *
 * @param castExpression the casted expression.
 * @param typeAtEnd indicates if the type should be put at end.
 * @return the string representation into the hover.
 */
protected String _signature(XCastedExpression castExpression, boolean typeAtEnd) {
	if (castExpression instanceof SarlCastedExpression) {
		final JvmOperation delegate = ((SarlCastedExpression) castExpression).getFeature();
		if (delegate != null) {
			return _signature(delegate, typeAtEnd);
		}
	}
	return MessageFormat.format(Messages.SARLHoverSignatureProvider_0,
			getTypeName(castExpression.getType()));
}
 
Example #24
Source File: SARLValidator.java    From sarl with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("checkstyle:nestedifdepth")
@Override
@Check
public void checkCasts(XCastedExpression cast) {
	// Validation of the casting according to the SARL specification of this operator.
	if (cast instanceof SarlCastedExpression) {
		final SarlCastedExpression sarlCast = (SarlCastedExpression) cast;
		final JvmOperation operation = sarlCast.getFeature();
		if (operation != null) {
			// We have to test the unnecessary cast because because it is tested into the standard process
			final JvmTypeReference concreteSyntax = sarlCast.getType();
			if (concreteSyntax != null) {
				final LightweightTypeReference toType = toLightweightTypeReference(cast.getType());
				reportCastWarnings(
						concreteSyntax,
						toType,
						getActualType(cast.getTarget()));

				if (!isIgnored(POTENTIAL_INEFFICIENT_VALUE_CONVERSION)) {
					final LightweightTypeReference fromType = toLightweightTypeReference(operation.getReturnType());
					final String message;
					if (Strings.equal(concreteSyntax.getIdentifier(), fromType.getIdentifier())) {
						message = MessageFormat.format(Messages.SARLValidator_97, operation.getSimpleName());
					} else {
						message = MessageFormat.format(Messages.SARLValidator_98, operation.getSimpleName(),
								toType.getHumanReadableName(), fromType.getHumanReadableName());
					}
					addIssue(message, concreteSyntax, POTENTIAL_INEFFICIENT_VALUE_CONVERSION);
				}
			}

			// Break to avoid the standard validation for casted expressions
			return;
		}
	}
	// Standard check of the types.
	super.checkCasts(cast);
}
 
Example #25
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _toJavaExpression(XCastedExpression expr, ITreeAppendable b) {
	b.append("((");
	serialize(expr.getType(), expr, b);
	b.append(") ");
	internalToConvertedExpression(expr.getTarget(), b, getLightweightType(expr));
	b.append(")");
}
 
Example #26
Source File: CastOperatorLinkingCandidate.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void applyToModel(IResolvedTypes resolvedTypes) {
	final XCastedExpression expr = getExpression();
	if (expr.eClass().isSuperTypeOf(SarlPackage.eINSTANCE.getSarlCastedExpression())) {
		// Feature
		setStructuralFeature(expr, SarlPackage.Literals.SARL_CASTED_EXPRESSION__FEATURE, getFeature());
		// Receiver
		setStructuralFeature(expr, SarlPackage.Literals.SARL_CASTED_EXPRESSION__RECEIVER, getReceiver());
		// Argument
		setStructuralFeature(expr, SarlPackage.Literals.SARL_CASTED_EXPRESSION__ARGUMENT, getArgument());
	}
}
 
Example #27
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void internalToConvertedExpression(XExpression obj, ITreeAppendable appendable) {
	if (obj instanceof XBlockExpression) {
		_toJavaExpression((XBlockExpression) obj, appendable);
	} else if (obj instanceof XCastedExpression) {
		_toJavaExpression((XCastedExpression) obj, appendable);
	} else if (obj instanceof XClosure) {
		_toJavaExpression((XClosure) obj, appendable);
	} else if (obj instanceof XAnnotation) {
		_toJavaExpression((XAnnotation) obj, appendable);
	} else if (obj instanceof XConstructorCall) {
		_toJavaExpression((XConstructorCall) obj, appendable);
	} else if (obj instanceof XIfExpression) {
		_toJavaExpression((XIfExpression) obj, appendable);
	} else if (obj instanceof XInstanceOfExpression) {
		_toJavaExpression((XInstanceOfExpression) obj, appendable);
	} else if (obj instanceof XSwitchExpression) {
		_toJavaExpression((XSwitchExpression) obj, appendable);
	} else if (obj instanceof XTryCatchFinallyExpression) {
		_toJavaExpression((XTryCatchFinallyExpression) obj, appendable);
	} else if (obj instanceof XListLiteral) {
		_toJavaExpression((XListLiteral) obj, appendable);
	} else if (obj instanceof XSetLiteral) {
		_toJavaExpression((XSetLiteral) obj, appendable);
	} else if (obj instanceof XSynchronizedExpression) {
		_toJavaExpression((XSynchronizedExpression) obj, appendable);
	} else if (obj instanceof XReturnExpression) {
		_toJavaExpression((XReturnExpression) obj, appendable);
	} else if (obj instanceof XThrowExpression) {
		_toJavaExpression((XThrowExpression) obj, appendable);
	} else {
		super.internalToConvertedExpression(obj, appendable);
	}
}
 
Example #28
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _computeTypes(XCastedExpression object, ITypeComputationState state) {
	// TODO: should we hold on the previously known expression?
	/* 
	 * ('foo' as CharSequence) as NullPointerException
	 * In this case, we know - even though it's CharSequence on the Java side - 
	 * that the type of ('foo' as CharSequence) is still a String
	 * which is not conformant to NPE. The subsequent cast will always fail at
	 * runtime. This could be detected.
	 * 
	 * It could be interesting to have a subtype of MultiTypeReference, e.g. CastedTypeReference
	 * that still knows about the original type. This would be similar to a nested switch
	 * with the difference, that we want to know which type to use on the Java side in order
	 * to disambiguate overloaded methods:
	 * 
	 * m(Object o) {} // 1
	 * m(String s) {}
	 * 
	 * {
	 *   val o = '' as Object
	 *   m(o) // calls 1
	 *   o.substring(1) // valid, too - compiler could insert the cast back to String
	 * }
	 */
	JvmTypeReference type = object.getType();
	if (type != null) {
		state.withNonVoidExpectation().computeTypes(object.getTarget());
		state.acceptActualType(state.getReferenceOwner().toLightweightTypeReference(type));
	} else {
		state.computeTypes(object.getTarget());
	}
}
 
Example #29
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkCasts(XCastedExpression cast) {
	if (cast.getType() == null) return;
	LightweightTypeReference toType = toLightweightTypeReference(cast.getType());
	LightweightTypeReference fromType = getActualType(cast.getTarget());
	checkCast(cast.getType(), toType, fromType);
}
 
Example #30
Source File: JavaConverterTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testLambdaCase3() throws Exception {
  this.j2x.useRobustSyntax();
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("import java.util.Iterator;");
  _builder.newLine();
  _builder.append("class Clazz {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("Iterable<String> iter = new AbstractIterable<String>() {");
  _builder.newLine();
  _builder.append("\t\t\t");
  _builder.append("@Override");
  _builder.newLine();
  _builder.append("\t\t\t");
  _builder.append("public Iterator<String> internalIterator() {");
  _builder.newLine();
  _builder.append("\t\t\t\t");
  _builder.append("return null;");
  _builder.newLine();
  _builder.append("\t\t\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("};");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("abstract static class AbstractIterable<T> implements Iterable<T> {");
  _builder.newLine();
  _builder.append("\t\t\t");
  _builder.append("@Override");
  _builder.newLine();
  _builder.append("\t\t\t");
  _builder.append("public Iterator<T> iterator() {");
  _builder.newLine();
  _builder.append("\t\t\t\t");
  _builder.append("return internalIterator();");
  _builder.newLine();
  _builder.append("\t\t\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("\t\t\t");
  _builder.append("public abstract Iterator<T> internalIterator();");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("}");
  XtendClass clazz = this.toValidXtendClass(_builder);
  Assert.assertNotNull(clazz);
  XtendField xtendMember = this.field(clazz, 0);
  Assert.assertEquals("iter", xtendMember.getName());
  XExpression _initialValue = xtendMember.getInitialValue();
  Assert.assertTrue((_initialValue instanceof XCastedExpression));
  XExpression _initialValue_1 = xtendMember.getInitialValue();
  XExpression _target = ((XCastedExpression) _initialValue_1).getTarget();
  Assert.assertTrue((_target instanceof XClosure));
}