Java Code Examples for org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner#newReferenceTo()

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner#newReferenceTo() . 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: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _computeTypes(XTypeLiteral object, ITypeComputationState state) {
	JvmType type = object.getType();
	if (type == null) {
		return;
	}
	checkTypeParameterNotAllowedAsLiteral(object, type, state);
	ITypeReferenceOwner owner = state.getReferenceOwner();
	LightweightTypeReference clazz = owner.newParameterizedTypeReference(type);
	for (int i = 0; i < object.getArrayDimensions().size(); i++) {
		clazz = owner.newArrayTypeReference(clazz);
	}
	if (object.getArrayDimensions().isEmpty()) {
		if (clazz.isPrimitiveVoid()) {
			clazz = state.getReferenceOwner().newReferenceTo(Void.class);
		} else {
			clazz = clazz.getWrapperTypeIfPrimitive();
		}
	}
	LightweightTypeReference result = owner.newReferenceTo(Class.class);
	if (result instanceof ParameterizedTypeReference) {
		ParameterizedTypeReference parameterizedTypeReference = (ParameterizedTypeReference) result;
		parameterizedTypeReference.addTypeArgument(clazz);
	}
	state.acceptActualType(result);
}
 
Example 2
Source File: TypeLiteralHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected LightweightTypeReference getAsClassLiteral(final JvmIdentifiableElement feature) {
	if (feature instanceof JvmType) {
		final ITypeReferenceOwner owner = state.getReferenceOwner();
		return owner.newReferenceTo(Class.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {
			@Override
			public LightweightTypeReference enhance(ParameterizedTypeReference reference) {
				LightweightTypeReference argumentType = owner.newParameterizedTypeReference((JvmType) feature);
				if (argumentType.isPrimitiveVoid()) {
					argumentType = owner.newReferenceTo(Void.class);
				} else {
					argumentType = argumentType.getWrapperTypeIfPrimitive();
				}
				reference.addTypeArgument(argumentType);
				return reference;
			}
		});
	}
	throw new IllegalArgumentException(String.valueOf(feature));
}
 
Example 3
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
/* @Nullable */
protected JvmTypeReference doGetTypeReference(XComputedTypeReferenceImplCustom context) {
	final ITypeReferenceOwner owner = resolvedTypes.getReferenceOwner();
	LightweightTypeReference hashMapReference = owner.newReferenceTo(HashMap.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {
		@Override
		public LightweightTypeReference enhance(ParameterizedTypeReference ref) {
			ref.addTypeArgument(owner.newReferenceTo(ArrayList.class, new TypeReferenceInitializer<ParameterizedTypeReference>(){
				@Override
				public LightweightTypeReference enhance(ParameterizedTypeReference keyType) {
					keyType.addTypeArgument(owner.newWildcardExtendsObject());
					return keyType;
				}
			}));
			ref.addTypeArgument(owner.toLightweightTypeReference(createOperation.getReturnType()));
			return ref;
		}
	});
	return toJavaCompliantTypeReference(hashMapReference, session);
}
 
Example 4
Source File: AbstractTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @deprecated use {@link ITypeReferenceOwner#newReferenceTo(Class)} instead.
 */
/* @NotNull */
@Deprecated
protected LightweightTypeReference getRawTypeForName(Class<?> clazz, ITypeReferenceOwner owner) {
	return owner.newReferenceTo(clazz);
}
 
Example 5
Source File: TypeLiteralHelper.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @deprecated use {@link ITypeReferenceOwner#newReferenceTo(Class)} instead.
 */
@Deprecated
/* @Nullable */
protected LightweightTypeReference getRawTypeForName(Class<?> clazz, ITypeReferenceOwner owner) {
	return owner.newReferenceTo(clazz);
}
 
Example 6
Source File: CacheMethodCompileStrategy.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void apply(ITreeAppendable appendable) {
	JvmOperation cacheMethod = (JvmOperation) logicalContainerProvider.getLogicalContainer(createExtensionInfo.getCreateExpression());
	JvmDeclaredType containerType = cacheMethod.getDeclaringType();
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(containerType);
	final ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, containerType);
	LightweightTypeReference listType = owner.newReferenceTo(ArrayList.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {
		@Override
		public LightweightTypeReference enhance(ParameterizedTypeReference reference) {
			reference.addTypeArgument(owner.newWildcardTypeReference());
			return reference;
		}
	});
	String cacheVarName = cacheField.getSimpleName();
	String cacheKeyVarName = appendable.declareSyntheticVariable("CacheKey", "_cacheKey");
	appendable.append("final ").append(listType).append(" ").append(cacheKeyVarName)
		.append(" = ").append(CollectionLiterals.class).append(".newArrayList(");
	List<JvmFormalParameter> list = cacheMethod.getParameters();
	for (Iterator<JvmFormalParameter> iterator = list.iterator(); iterator.hasNext();) {
		JvmFormalParameter jvmFormalParameter = iterator.next();
		appendable.append(getVarName(jvmFormalParameter));
		if (iterator.hasNext()) {
			appendable.append(", ");
		}
	}
	appendable.append(");");
	// declare result variable
	LightweightTypeReference returnType = resolvedTypes.getActualType(initializerMethod.getParameters().get(0));
	if (returnType != null) {
		appendable.newLine().append("final ").append(returnType);
	} else {
		appendable.newLine().append("final Object");
	}
	String resultVarName = "_result";
	appendable.append(" ").append(resultVarName).append(";");
	// open synchronize block
	appendable.newLine().append("synchronized (").append(cacheVarName).append(") {");
	appendable.increaseIndentation();
	// if the cache contains the key return the previously created object.
	appendable.newLine().append("if (").append(cacheVarName).append(".containsKey(").append(cacheKeyVarName)
			.append(")) {");
	appendable.increaseIndentation();
	appendable.newLine().append("return ").append(cacheVarName).append(".get(").append(cacheKeyVarName).append(");");
	appendable.decreaseIndentation().newLine().append("}");
	
	// execute the creation
	compiler.toJavaStatement(createExtensionInfo.getCreateExpression(), appendable, true);
	appendable.newLine();
	appendable.append(resultVarName).append(" = ");
	compiler.toJavaExpression(createExtensionInfo.getCreateExpression(), appendable);
	appendable.append(";");

	// store the newly created object in the cache
	appendable.newLine().append(cacheVarName).append(".put(").append(cacheKeyVarName).append(", ");
	LightweightTypeReference fieldType = resolvedTypes.getActualType(cacheField);
	LightweightTypeReference declaredResultType = fieldType.getTypeArguments().get(1);
	boolean castRequired = false;
	if (!declaredResultType.isAssignableFrom(returnType)) {
		castRequired = true;
		appendable.append("(").append(declaredResultType).append(")");
	}
	appendable.append(resultVarName).append(");");

	// close synchronize block
	appendable.decreaseIndentation();
	appendable.newLine().append("}");
	appendable.newLine().append(initializerMethod.getSimpleName()).append("(").append(resultVarName);
	for (JvmFormalParameter parameter : cacheMethod.getParameters()) {
		appendable.append(", ").append(parameter.getName());
	}
	appendable.append(");");
	// return the result
	appendable.newLine().append("return ");
	if (castRequired) {
		appendable.append("(").append(declaredResultType).append(")");
	}
	appendable.append(resultVarName).append(";");
}