org.eclipse.xtext.xbase.XbaseFactory Java Examples

The following examples show how to use org.eclipse.xtext.xbase.XbaseFactory. 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: 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 #2
Source File: SARLReentrantTypeResolver.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Create the extension provider dedicated to the access to the used capacity functions.
 *
 * @param thisFeature the current object.
 * @param field the extension field.
 * @return the SARL capacity extension provider, or {@code null}.
 */
protected XAbstractFeatureCall createSarlCapacityExtensionProvider(JvmIdentifiableElement thisFeature, JvmField field) {
	// For capacity call redirection
	if (thisFeature instanceof JvmDeclaredType) {
		final JvmAnnotationReference capacityAnnotation = this.annotationLookup.findAnnotation(field,
				ImportedCapacityFeature.class);
		if (capacityAnnotation != null) {
			final String methodName = Utils.createNameForHiddenCapacityImplementationCallingMethodFromFieldName(
					field.getSimpleName());
			final JvmOperation callerOperation = findOperation((JvmDeclaredType) thisFeature, methodName);
			if (callerOperation != null) {
				final XbaseFactory baseFactory = getXbaseFactory();
				final XMemberFeatureCall extensionProvider = baseFactory.createXMemberFeatureCall();
				extensionProvider.setFeature(callerOperation);
				final XFeatureCall thisAccess = baseFactory.createXFeatureCall();
				thisAccess.setFeature(thisFeature);
				extensionProvider.setMemberCallTarget(thisAccess);
				return extensionProvider;
			}
		}
	}
	return null;
}
 
Example #3
Source File: ExpressionBuilderImpl.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Create a reference to "super" object or to the super type.
 *
 * @return the reference.
 */
public XFeatureCall createReferenceToSuper() {
	final XExpression expr = getXExpression();
	XtendTypeDeclaration type = EcoreUtil2.getContainerOfType(expr, XtendTypeDeclaration.class);
	JvmType jvmObject = getAssociatedElement(JvmType.class, type, expr.eResource());
	final XFeatureCall superFeature = XbaseFactory.eINSTANCE.createXFeatureCall();
	JvmIdentifiableElement feature;
	if (jvmObject instanceof JvmDeclaredType) {
		feature = ((JvmDeclaredType) jvmObject).getExtendedClass().getType();
	} else {
		feature = findType(expr, getQualifiedName(type)).getType();
		if (feature instanceof JvmDeclaredType) {
			feature = ((JvmDeclaredType) feature).getExtendedClass().getType();
		} else {
			feature = null;
		}
	}
	if (feature == null) {
		return null;
	}
	superFeature.setFeature(feature);
	return superFeature;

}
 
Example #4
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 #5
Source File: AbstractBatchTypeResolverTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testProxy() throws Exception {
	XFeatureCall proxy = XbaseFactory.eINSTANCE.createXFeatureCall();
	((InternalEObject) proxy).eSetProxyURI(URI.createURI("path#fragment"));
	IResolvedTypes typeResolution = getTypeResolver().resolveTypes(proxy);
	Assert.assertNotNull(typeResolution);
	Assert.assertEquals(IResolvedTypes.NULL, typeResolution);
}
 
Example #6
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 #7
Source File: BlockExpressionBuilderImpl.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Create the XBlockExpression.
 */
public void eInit(IJvmTypeProvider context) {
	setTypeResolutionContext(context);
	if (this.block == null) {
		this.block = XbaseFactory.eINSTANCE.createXBlockExpression();
	}
}
 
Example #8
Source File: StaticFeatureDescriptionWithImplicitReceiver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public StaticFeatureDescriptionWithImplicitReceiver(QualifiedName qualifiedName, JvmFeature feature, 
		LightweightTypeReference receiverType,
		int bucketId, boolean visible) {
	super(qualifiedName, feature, bucketId, visible);
	this.implicitReceiverType = receiverType;
	this.implicitReceiver = XbaseFactory.eINSTANCE.createXFeatureCall();
}
 
Example #9
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected <T extends Object> void genericTestInitializeSafely(final Function3<? super EObject, ? super String, ? super Procedure1<? super T>, ? extends EObject> create) {
  final XNullLiteral expr = XbaseFactory.eINSTANCE.createXNullLiteral();
  final Wrapper<Boolean> initialized = new Wrapper<Boolean>(Boolean.valueOf(false));
  final Procedure1<T> _function = (T it) -> {
    initialized.set(Boolean.valueOf(true));
    throw new RuntimeException();
  };
  final EObject element = create.apply(expr, "foo", _function);
  Assert.assertTrue((initialized.get()).booleanValue());
  Assert.assertNotNull(element);
}
 
Example #10
Source File: ExpressionBuilderImpl.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Create a reference to "this" object or to the current type.
 *
 * @return the reference.
 */
public XFeatureCall createReferenceToThis() {
	final XExpression expr = getXExpression();
	XtendTypeDeclaration type = EcoreUtil2.getContainerOfType(expr, XtendTypeDeclaration.class);
	JvmType jvmObject = getAssociatedElement(JvmType.class, type, expr.eResource());
	final XFeatureCall thisFeature = XbaseFactory.eINSTANCE.createXFeatureCall();
	thisFeature.setFeature(jvmObject);
	return thisFeature;
}
 
Example #11
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Add the given constructors to the Ecore container.
 *
 * @param codeBuilder the code builder to use.
 * @param superClassConstructors the constructors defined in the super class.
 * @param context the context of the constructors.
 * @throws JavaModelException if the Java model is invalid.
 */
public void createStandardConstructorsWith(
		ConstructorBuilder codeBuilder,
		Collection<IMethod> superClassConstructors,
		XtendTypeDeclaration context) throws JavaModelException {
	if (superClassConstructors != null) {
		for (final IMethod constructor : superClassConstructors) {
			if (!isGeneratedOperation(constructor)) {
				final ISarlConstructorBuilder cons = codeBuilder.addConstructor();
				// Create parameters
				final IFormalParameterBuilder[] sarlParams = createFormalParametersWith(
					name -> cons.addParameter(name), constructor);
				// Create the block
				final IBlockExpressionBuilder block = cons.getExpression();
				// Create thre super-call expression
				final IExpressionBuilder superCall = block.addExpression();
				final XFeatureCall call = XbaseFactory.eINSTANCE.createXFeatureCall();
				superCall.setXExpression(call);
				// Set the todo comment.
				superCall.setDocumentation(block.getAutoGeneratedActionString());
				// Create the super-call XExpression
				call.setFeature(getJvmConstructor(constructor, context));
				call.setExplicitOperationCall(true);
				final List<XExpression> arguments = call.getFeatureCallArguments();
				for (final IFormalParameterBuilder currentParam : sarlParams) {
					final XFeatureCall argumentSource = XbaseFactory.eINSTANCE.createXFeatureCall();
					arguments.add(argumentSource);
					currentParam.setReferenceInto(argumentSource);
				}
			}
		}
	}
}
 
Example #12
Source File: FeatureScopes.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected XbaseFactory getXbaseFactory() {
	return xbaseFactory;
}
 
Example #13
Source File: XbasePackageImpl.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public XbaseFactory getXbaseFactory()
{
	return (XbaseFactory)getEFactoryInstance();
}
 
Example #14
Source File: AbstractSARLRuntimeModule.java    From sarl with Apache License 2.0 4 votes vote down vote up
@SingletonBinding
public XbaseFactory bindXbaseFactoryToInstance() {
	return XbaseFactory.eINSTANCE;
}
 
Example #15
Source File: ExpressionBuilderImpl.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Replies the XExpression for the default value associated to the given type.
 * @param type the type for which the default value should be determined.
 * @return the default value.
 */
@Pure
public XExpression getDefaultXExpressionForType(String type) {
	//TODO: Check if a similar function exists in the Xbase library.
	XExpression expr = null;
	if (type != null && !"void".equals(type) && !Void.class.getName().equals(type)) {
		switch (type) {
		case "boolean":
		case "java.lang.Boolean":
			XBooleanLiteral booleanLiteral = XbaseFactory.eINSTANCE.createXBooleanLiteral();
			booleanLiteral.setIsTrue(false);
			expr = booleanLiteral;
			break;
		case "float":
		case "java.lang.Float":
			XNumberLiteral numberLiteral = XbaseFactory.eINSTANCE.createXNumberLiteral();
			numberLiteral.setValue("0.0f");
			expr = numberLiteral;
			break;
		case "double":
		case "java.lang.Double":
		case "java.lang.BigDecimal":
			numberLiteral = XbaseFactory.eINSTANCE.createXNumberLiteral();
			numberLiteral.setValue("0.0");
			expr = numberLiteral;
			break;
		case "int":
		case "long":
		case "java.lang.Integer":
		case "java.lang.Long":
		case "java.lang.BigInteger":
			numberLiteral = XbaseFactory.eINSTANCE.createXNumberLiteral();
			numberLiteral.setValue("0");
			expr = numberLiteral;
			break;
		case "byte":
		case "short":
		case "char":
		case "java.lang.Byte":
		case "java.lang.Short":
		case "java.lang.Character":
			numberLiteral = XbaseFactory.eINSTANCE.createXNumberLiteral();
			numberLiteral.setValue("0");
			XCastedExpression castExpression = XbaseFactory.eINSTANCE.createXCastedExpression();
			castExpression.setTarget(numberLiteral);
			castExpression.setType(newTypeRef(this.context, type));
			expr = numberLiteral;
			break;
		default:
			expr = XbaseFactory.eINSTANCE.createXNullLiteral();
			break;
		}
	}
	return expr;
}
 
Example #16
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Create the operations into the SARL feature container.
 *
 * @param codeBuilder the builder of the script.
 * @param methods the operations to create.
 * @param context the context of the actions. If {@code null}, the block of the actions are not generated.
 * @throws JavaModelException if the Java model is invalid.
 * @throws IllegalArgumentException if the signature is not syntactically correct.
 */
public void createActionsWith(
		ActionBuilder codeBuilder,
		Collection<IMethod> methods,
		XtendTypeDeclaration context) throws JavaModelException, IllegalArgumentException {
	if (methods != null) {
		for (final IMethod operation : methods) {
			if (!isGeneratedOperation(operation)) {
				final ISarlActionBuilder action = codeBuilder.addAction(operation.getElementName());
				action.setReturnType(Signature.toString(operation.getReturnType()));
				final IFormalParameterBuilder[] sarlParams = createFormalParametersWith(
					name -> action.addParameter(name), operation);
				if (context != null) {
					final JvmType type = this.typeReferences.findDeclaredType(
							operation.getDeclaringType().getFullyQualifiedName(),
							context);
					final JvmOperation superOperation = getJvmOperation(operation, type);
					// Create the block
					final IBlockExpressionBuilder block = action.getExpression();
					if ((type.eClass() != TypesPackage.Literals.JVM_GENERIC_TYPE
							|| !((JvmGenericType) type).isInterface())
							&& superOperation != null
							&& !superOperation.isAbstract()) {
						// Create the super-call expression
						final IExpressionBuilder superCall = block.addExpression();
						final XMemberFeatureCall call = XbaseFactory.eINSTANCE.createXMemberFeatureCall();
						superCall.setXExpression(call);
						superCall.setDocumentation(block.getAutoGeneratedActionString());
						call.setFeature(superOperation);
						call.setMemberCallTarget(superCall.createReferenceToSuper());
						final List<XExpression> arguments = call.getMemberCallArguments();
						for (final IFormalParameterBuilder currentParam : sarlParams) {
							final XFeatureCall argumentSource = XbaseFactory.eINSTANCE.createXFeatureCall();
							arguments.add(argumentSource);
							currentParam.setReferenceInto(argumentSource);
						}
					} else {
						final JvmTypeReference ret = superOperation != null ? superOperation.getReturnType() : null;
						block.setDefaultAutoGeneratedContent(
								ret == null ? null : ret.getIdentifier());
					}
				}
			}
		}
	}
}
 
Example #17
Source File: XtendRuntimeModule.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public XbaseFactory bindXbaseFactory() {
	return XbaseFactory.eINSTANCE;
}
 
Example #18
Source File: DefaultReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected XbaseFactory getXbaseFactory() {
	return xbaseFactory;
}
 
Example #19
Source File: XbaseTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testProxy() throws Exception {
	XFeatureCall proxy = XbaseFactory.eINSTANCE.createXFeatureCall();
	((InternalEObject) proxy).eSetProxyURI(URI.createURI("path#fragment"));
	assertNull(getType(proxy));
}
 
Example #20
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected UnboundTypeReference createUnboundTypeReference(JvmTypeParameter type) {
	PublicResolvedTypes resolvedTypes = getResolvedTypes();
	return resolvedTypes.createUnboundTypeReference(XbaseFactory.eINSTANCE.createXNullLiteral(), type);
}
 
Example #21
Source File: XbasePackageImpl.java    From xtext-extras with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Creates an instance of the model <b>Package</b>, registered with
 * {@link org.eclipse.emf.ecore.EPackage.Registry EPackage.Registry} by the package
 * package URI value.
 * <p>Note: the correct way to create the package is via the static
 * factory method {@link #init init()}, which also performs
 * initialization of the package, or returns the registered package,
 * if one already exists.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @see org.eclipse.emf.ecore.EPackage.Registry
 * @see org.eclipse.xtext.xbase.XbasePackage#eNS_URI
 * @see #init()
 * @generated
 */
private XbasePackageImpl()
{
	super(eNS_URI, XbaseFactory.eINSTANCE);
}