Java Code Examples for org.eclipse.xtext.common.types.JvmGenericType#getTypeParameters()

The following examples show how to use org.eclipse.xtext.common.types.JvmGenericType#getTypeParameters() . 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: JvmModelTests.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testJvmTypeParameter_01() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo< {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(this.file(_builder.toString(), false, false).getXtendTypes());
    final JvmGenericType clazz = this._iXtendJvmAssociations.getInferredType(((XtendClass) _head));
    EList<JvmTypeParameter> _typeParameters = clazz.getTypeParameters();
    String _plus = ("" + _typeParameters);
    Assert.assertTrue(_plus, clazz.getTypeParameters().isEmpty());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 2
Source File: JvmModelTests.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testJvmTypeParameter_02() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo<T> {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(this.file(_builder.toString(), false, false).getXtendTypes());
    final JvmGenericType clazz = this._iXtendJvmAssociations.getInferredType(((XtendClass) _head));
    EList<JvmTypeParameter> _typeParameters = clazz.getTypeParameters();
    String _plus = ("" + _typeParameters);
    Assert.assertEquals(_plus, 1, clazz.getTypeParameters().size());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 3
Source File: SuperMemberImplementorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReference getContextType() {
	JvmGenericType inferredType = associations.getInferredType(xtendClass);
	ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, inferredType);
	ParameterizedTypeReference contextType = owner.newParameterizedTypeReference(inferredType);
	for(JvmTypeParameter typeParamter: inferredType.getTypeParameters()) {
		contextType.addTypeArgument(owner.newParameterizedTypeReference(typeParamter));
	}
	return contextType;
}
 
Example 4
Source File: TypeReferences.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @return a fresh {@link JvmParameterizedTypeReference} for the given {@link JvmType} parameterized with the given
 *         typeArgs. This method does not check whether the given type can handle the given type arguments.
 */
public JvmParameterizedTypeReference createTypeRef(JvmType type, JvmTypeReference... typeArgs) {
	if (type == null)
		throw new NullPointerException("type");
	List<JvmTypeReference> typeReferences = Collections.emptyList();
	if (typeArgs != null && typeArgs.length > 0) {
		typeReferences = Lists.newArrayListWithCapacity(typeArgs.length);
		for (int i = 0; i < typeArgs.length; i++) {
			JvmTypeReference jvmTypeReference = typeArgs[i];
			typeReferences.add(EcoreUtil2.cloneIfContained(jvmTypeReference));
		}
	}
	JvmParameterizedTypeReference reference;
	if (type instanceof JvmGenericType) {
		JvmGenericType casted = (JvmGenericType) type;
		List<JvmTypeParameter> list = casted.getTypeParameters();
		if (!typeReferences.isEmpty() && list.size() != typeReferences.size()) {
			throw new IllegalArgumentException("The type " + type.getIdentifier() + " expects " + list.size()
					+ " type arguments, but was " + typeReferences.size()
					+ ". Either pass zero arguments (raw type) or the correct number.");
		}
		// Raw type -> create type references to type param
		if (typeReferences.isEmpty() && !list.isEmpty()) {
			typeReferences = Lists.newArrayListWithCapacity(list.size());
			for (JvmTypeParameter typeParameter : list) {
				typeReferences.add(createTypeRef(typeParameter));
			}
		}
		if (!casted.isStatic() && casted.eContainer() instanceof JvmType) {
			JvmParameterizedTypeReference outer = createTypeRef((JvmType)casted.eContainer());
			reference = factory.createJvmInnerTypeReference();
			((JvmInnerTypeReference) reference).setOuter(outer);
		} else {
			reference = factory.createJvmParameterizedTypeReference();	
		}
	} else {
		reference = factory.createJvmParameterizedTypeReference();
	}
	reference.setType(type);
	if (!typeReferences.isEmpty())
		reference.getArguments().addAll(typeReferences);
	return reference;
}
 
Example 5
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.4
 */
protected JvmDeclaredType createType(ITypeBinding typeBinding, String handleIdentifier, List<String> path, StringBuilder fqn) {
	if (typeBinding.isAnonymous() || typeBinding.isSynthetic())
		throw new IllegalStateException("Cannot create type for anonymous or synthetic classes");

	// Creates the right type of instance based on the type of binding.
	//
	JvmGenericType jvmGenericType;
	JvmDeclaredType result;
	if (typeBinding.isAnnotation()) {
		jvmGenericType = null;
		result = TypesFactory.eINSTANCE.createJvmAnnotationType();
	} else if (typeBinding.isEnum()) {
		jvmGenericType = null;
		result = TypesFactory.eINSTANCE.createJvmEnumerationType();
	} else {
		result = jvmGenericType = TypesFactory.eINSTANCE.createJvmGenericType();
		jvmGenericType.setInterface(typeBinding.isInterface());
	}

	// Populate the information computed from the modifiers.
	//
	int modifiers = typeBinding.getModifiers();
	setTypeModifiers(result, modifiers);
	result.setDeprecated(typeBinding.isDeprecated());
	setVisibility(result, modifiers);

	// Determine the simple name and compose the fully qualified name and path, remembering the fqn length and path size so we can reset them.
	//
	String simpleName = typeBinding.getName();
	fqn.append(simpleName);
	int length = fqn.length();
	int size = path.size();
	path.add(simpleName);

	String qualifiedName = fqn.toString();
	result.internalSetIdentifier(qualifiedName);
	result.setSimpleName(simpleName);

	// Traverse the nested types using '$' as the qualified name separator.
	//
	fqn.append('$');
	createNestedTypes(typeBinding, result, handleIdentifier, path, fqn);

	// Traverse the methods using '.'as the qualifed name separator.
	//
	fqn.setLength(length);
	fqn.append('.');
	createMethods(typeBinding, handleIdentifier, path, fqn, result);
	
	createFields(typeBinding, fqn, result);

	// Set the super types.
	//
	setSuperTypes(typeBinding, qualifiedName, result);

	// If this is for a generic type, populate the type parameters.
	//
	if (jvmGenericType != null) {
		ITypeBinding[] typeParameterBindings = typeBinding.getTypeParameters();
		if (typeParameterBindings.length > 0) {
			InternalEList<JvmTypeParameter> typeParameters = (InternalEList<JvmTypeParameter>)jvmGenericType.getTypeParameters();
			for (ITypeBinding variable : typeParameterBindings) {
				typeParameters.addUnique(createTypeParameter(variable, result));
			}
		}
	}

	// Populate the annotation values.
	//
	createAnnotationValues(typeBinding, result);

	// Restore the path.
	//
	path.remove(size);

	return result;
}