Java Code Examples for org.eclipse.xtext.common.types.JvmTypeReference#getType()

The following examples show how to use org.eclipse.xtext.common.types.JvmTypeReference#getType() . 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: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_Inner_07() {
	String typeName = ParameterizedTypes.Inner.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodZArray_01()");
	JvmTypeReference listZ = methodV.getReturnType();
	assertEquals("java.util.List<Z[][]>", listZ.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listZ;
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	JvmType argumentType = typeArgument.getType();
	assertTrue(argumentType instanceof JvmArrayType);
	assertTrue(((JvmArrayType) argumentType).getComponentType() instanceof JvmArrayType);
	JvmComponentType componentType = ((JvmArrayType) ((JvmArrayType) argumentType).getComponentType())
			.getComponentType();
	JvmTypeParameter z = type.getTypeParameters().get(2);
	assertSame(z, componentType);
}
 
Example 2
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testTypeParameterShadowsType_3() throws Exception {
	final XtendFile file = file("class A {} class B extends A  { def <A> A foo(A x) {x}}");
	final XtendClass xtendClass = ((XtendClass) file.getXtendTypes().get(1));
	
	final JvmType extendedType = xtendClass.getExtends().getType();
	assertTrue(extendedType instanceof JvmGenericType);
	
	XtendFunction func = (XtendFunction) xtendClass.getMembers().get(0);
	
	JvmTypeReference returnType = func.getReturnType();
	JvmTypeParameter typeParamDecl = (JvmTypeParameter) returnType.getType();
	assertEquals("A", typeParamDecl.getIdentifier());
	
	JvmTypeParameter param = (JvmTypeParameter) func.getParameters().get(0).getParameterType().getType();
	assertSame(typeParamDecl, param);
	assertNotSame(extendedType, param);
}
 
Example 3
Source File: SARLValidator.java    From sarl with Apache License 2.0 6 votes vote down vote up
private Collection<ActionParameterTypes> doGetConstructorParameterTypes(Class<?> type, Notifier context) {
	final Collection<ActionParameterTypes> parameters = new ArrayList<>();
	final JvmTypeReference typeReference = this.typeReferences.getTypeForName(type, context);
	final JvmType jvmType = typeReference.getType();
	if (jvmType instanceof JvmDeclaredType) {
		final JvmDeclaredType declaredType = (JvmDeclaredType) jvmType;
		for (final JvmConstructor constructor : declaredType.getDeclaredConstructors()) {
			final ActionParameterTypes types = this.sarlActionSignatures.createParameterTypesFromJvmModel(
					constructor.isVarArgs(), constructor.getParameters());
			if (types != null) {
				parameters.add(types);
			}
		}
	}
	if (parameters.isEmpty()) {
		parameters.add(this.sarlActionSignatures.createParameterTypesForVoid());
	}
	return parameters;
}
 
Example 4
Source File: ExtensionScopeHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isMatchingFirstParameter(JvmOperation feature) {
	List<JvmFormalParameter> parameters = feature.getParameters();
	JvmFormalParameter firstParameter = parameters.get(0);
	JvmTypeReference type = firstParameter.getParameterType();
	if (type == null)
		return false;
	JvmType rawParameterType = type.getType();
	if (rawParameterType == null || rawParameterType.eIsProxy())
		return false;
	if (!(rawParameterType instanceof JvmTypeParameter)) {
		if (rawArgumentType.isResolved()) {
			// short circuit - limit extension scope entries to real candidates
			LightweightTypeReference parameterTypeReference = rawArgumentType.getOwner().toPlainTypeReference(rawParameterType);
			if (parameterTypeReference.isResolved() && !parameterTypeReference.isAssignableFrom(rawArgumentType)) {
				if (parameterTypeReference.isArray() && !rawArgumentType.isArray() && rawArgumentType.isSubtypeOf(Iterable.class)) {
					return true;
				}
				return false;
			}
			if (parameterTypeReference.isArray() && !rawArgumentType.isArray() && !rawArgumentType.isSubtypeOf(Iterable.class)) {
				return false;
			}
		} else if (isArrayTypeMismatch(rawArgumentType, rawParameterType)) {
			return false;
		}
	}
	return true;
}
 
Example 5
Source File: ParameterizedTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isWrapper(JvmTypeParameter typeParameter, /* @Nullable */ RecursionGuard<JvmTypeParameter> stack) {
	for(JvmTypeConstraint constraint: typeParameter.getConstraints()) {
		if (constraint.eClass() == TypesPackage.Literals.JVM_UPPER_BOUND) {
			JvmTypeReference upperBound = constraint.getTypeReference();
			if (upperBound != null) {
				JvmType upperBoundType = upperBound.getType();
				if (upperBoundType == null) {
					return false;
				}
				if (upperBoundType.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
					return isWrapper((JvmGenericType)upperBoundType);
				}
				// guard against recursive deps
				if (upperBoundType.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
					if (typeParameter == upperBoundType || stack != null && !stack.tryNext((JvmTypeParameter) upperBoundType)) {
						return false;
					}
					if (stack == null) {
						stack = new RecursionGuard<JvmTypeParameter>();
						stack.tryNext(typeParameter);
						stack.tryNext((JvmTypeParameter) upperBoundType);
					}
					return isWrapper((JvmTypeParameter) upperBoundType, stack);
				}
			}
		}
	}
	return false;
}
 
Example 6
Source File: ParameterizedTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private Primitive getPrimitiveKind(JvmTypeParameter type, /* @Nullable */ RecursionGuard<JvmTypeParameter> guard) {
	if (type.eIsProxy())
		return null;
	for(JvmTypeConstraint constraint: type.getConstraints()) {
		if (constraint.eClass() == TypesPackage.Literals.JVM_UPPER_BOUND) {
			JvmTypeReference upperBound = constraint.getTypeReference();
			if (upperBound != null) {
				JvmType upperBoundType = upperBound.getType();
				if (upperBoundType.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
					return getPrimitiveKind((JvmGenericType) upperBoundType);
				}
				if (type == upperBoundType) {
					return null;
				}
				// guard against recursive deps
				if (upperBoundType.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
					JvmTypeParameter upperBoundTypeParameter = (JvmTypeParameter) upperBoundType;
					if (guard == null) {
						guard = new RecursionGuard<JvmTypeParameter>();
						guard.tryNext(type);
					}
					if (guard.tryNext(upperBoundTypeParameter)) {
						return getPrimitiveKind(upperBoundTypeParameter, guard);
					}
					return null;
				}
			}
		}
	}
	return null;
}
 
Example 7
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkSuperTypes(XtendClass xtendClass) {
	JvmTypeReference superClass = xtendClass.getExtends();
	if (superClass != null && superClass.getType() != null) {
		if (!(superClass.getType() instanceof JvmGenericType)
				|| ((JvmGenericType) superClass.getType()).isInterface()) {
			error("Superclass must be a class", XTEND_CLASS__EXTENDS, CLASS_EXPECTED);
		} else {
			if (((JvmGenericType) superClass.getType()).isFinal()) {
				error("Attempt to override final class", XTEND_CLASS__EXTENDS, OVERRIDDEN_FINAL);
			}
			checkWildcardSupertype(xtendClass, superClass, XTEND_CLASS__EXTENDS, INSIGNIFICANT_INDEX);
		}
	}
	for (int i = 0; i < xtendClass.getImplements().size(); ++i) {
		JvmTypeReference implementedType = xtendClass.getImplements().get(i);
		if (!isInterface(implementedType.getType()) && !isAnnotation(implementedType.getType())) {
			error("Implemented interface must be an interface", XTEND_CLASS__IMPLEMENTS, i, INTERFACE_EXPECTED);
		}
		checkWildcardSupertype(xtendClass, implementedType, XTEND_CLASS__IMPLEMENTS, i);
	}
	JvmGenericType inferredType = associations.getInferredType(xtendClass);
	if (inferredType != null && hasCycleInHierarchy(inferredType, Sets.<JvmGenericType> newHashSet())) {
		error("The inheritance hierarchy of " + notNull(xtendClass.getName()) + " contains cycles",
				XTEND_TYPE_DECLARATION__NAME, CYCLIC_INHERITANCE);
	}
}
 
Example 8
Source File: JavaInlineExpressionCompiler.java    From sarl with Apache License 2.0 5 votes vote down vote up
private JvmTypeReference getFunctionTypeReference(XtendFunction function) {
	if (function != null) {
		final JvmTypeReference reference = function.getReturnType();
		if (reference != null) {
			final JvmType type = reference.getType();
			if (type != null) {
				return reference;
			}
		}
	}
	return this.typeReferences.getTypeForName(Object.class, function);
}
 
Example 9
Source File: ConstructorLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected List<JvmTypeParameter> getDeclaredTypeParameters() {
	if (isAnonymousClassConstructorCall()) {
		JvmDeclaredType anonymousType = getConstructor().getDeclaringType();
		JvmTypeReference superType = Iterables.getLast(anonymousType.getSuperTypes());
		JvmType rawSuperType = superType.getType();
		if (rawSuperType instanceof JvmTypeParameterDeclarator) {
			return ((JvmTypeParameterDeclarator) rawSuperType).getTypeParameters();
		}
		return Collections.emptyList();
	}
	return new FeatureLinkHelper().getDeclaredTypeParameters(getConstructor());
}
 
Example 10
Source File: ExpressionScope.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReference getParameterType(JvmFormalParameter p) {
	JvmTypeReference parameterType = p.getParameterType();
	if (parameterType == null) {
		return null;
	}
	JvmType type = parameterType.getType();
	if (type == null)
		return null;
	return owner.toPlainTypeReference(type).getRawTypeReference();
}
 
Example 11
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isReferenceToTypeParameter(JvmTypeReference type) {
	if (type != null) {
		if (type.getType() instanceof JvmTypeParameter) {
			return true;
		}
		if (type instanceof JvmGenericArrayTypeReference) {
			return isReferenceToTypeParameter(((JvmGenericArrayTypeReference) type).getComponentType());
		}
	}
	return false;
}
 
Example 12
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_twoListParamsListResult_03() {
	JvmOperation twoListParamsListResult = getMethodFromParameterizedMethods(
			"twoListParamsListResult(java.util.List,java.util.List)");
	JvmTypeReference returnType = twoListParamsListResult.getReturnType();
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) returnType;
	assertEquals(1, parameterized.getArguments().size());
	JvmTypeReference typeParameter = parameterized.getArguments().get(0);
	JvmType referencedType = typeParameter.getType();
	assertFalse(referencedType.eIsProxy());
	assertTrue(referencedType instanceof JvmTypeParameter);
	JvmTypeParameter typeVar = (JvmTypeParameter) referencedType;
	assertEquals("T", typeVar.getName());
	assertSame(twoListParamsListResult, typeVar.getDeclarator());
}
 
Example 13
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_arrayParameterized_02() {
	JvmOperation arrayParameterized = getMethodFromParameterizedMethods("arrayParameterized(java.util.List[])");
	JvmTypeReference paramType = arrayParameterized.getParameters().get(0).getParameterType();
	assertEquals("java.util.List<T>[]", paramType.getIdentifier());
	assertTrue(paramType.getType() instanceof JvmArrayType);
	JvmArrayType arrayType = (JvmArrayType) paramType.getType();
	assertTrue(arrayType.getComponentType() instanceof JvmDeclaredType);
	assertTrue(paramType instanceof JvmGenericArrayTypeReference);
	assertTrue(
			((JvmGenericArrayTypeReference) paramType).getComponentType() instanceof JvmParameterizedTypeReference);
}
 
Example 14
Source File: JvmSpecializedTypeReferenceImplCustom.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public JvmType getType() {
	JvmTypeReference resolvedEquivalent = getEquivalent();
	if (resolvedEquivalent != null)
		return resolvedEquivalent.getType();
	return null;
}
 
Example 15
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_arrayWildcard_02() {
	JvmOperation arrayWildcard = getMethodFromParameterizedMethods("arrayWildcard(java.util.List[])");
	JvmTypeReference paramType = arrayWildcard.getParameters().get(0).getParameterType();
	assertEquals("java.util.List<? extends java.lang.Object>[]", paramType.getIdentifier());
	assertTrue(paramType.getType() instanceof JvmArrayType);
	JvmArrayType arrayType = (JvmArrayType) paramType.getType();
	assertTrue(arrayType.getComponentType() instanceof JvmDeclaredType);
	assertTrue(paramType instanceof JvmGenericArrayTypeReference);
	assertTrue(
			((JvmGenericArrayTypeReference) paramType).getComponentType() instanceof JvmParameterizedTypeReference);
}
 
Example 16
Source File: Utils.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Extract the mapping between the type parameters declared within the super types and the
 * type parameters arguments that are declared within the given type.
 *
 * <p>For example, consider the following code:
 * <pre><code>
 * interface X&lt;T&gt; {
 *   def a(p1 : T, p2 : U) with U
 * }
 * interface Y&lt;T&gt; {
 * }
 * class Z&lt;TT&gt; implements X&lt;TT&gt;, Y&lt;TT&gt; {
 *   def a(p1 : TT, p2 : W) with W { }
 * }
 * </code></pre>
 * The mapping is:
 * <pre><code>
 * X.T =&gt; TT
 * Y.T =&gt; TT
 * </code></pre>
 *
 * @param type the type to analyze.
 * @param mapping the map to fill with the mapping.
 * @since 0.7
 */
public static void getSuperTypeParameterMap(JvmDeclaredType type, Map<String, JvmTypeReference> mapping) {
	for (final JvmTypeReference superTypeReference : type.getSuperTypes()) {
		if (superTypeReference instanceof JvmParameterizedTypeReference) {
			final JvmParameterizedTypeReference parameterizedTypeReference = (JvmParameterizedTypeReference) superTypeReference;
			final JvmType st = superTypeReference.getType();
			if (st instanceof JvmTypeParameterDeclarator) {
				final JvmTypeParameterDeclarator superType = (JvmTypeParameterDeclarator) st;
				int i = 0;
				for (final JvmTypeParameter typeParameter : superType.getTypeParameters()) {
					mapping.put(typeParameter.getIdentifier(), parameterizedTypeReference.getArguments().get(i));
					++i;
				}
			}
		}
	}
}
 
Example 17
Source File: ValidationBug433213Test.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void test_01() {
  try {
    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("new Object {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("def <T> T m2() {}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile file = this.parser.parse(_builder);
    final XtendTypeDeclaration c = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
    XtendMember _head = IterableExtensions.<XtendMember>head(c.getMembers());
    final XtendFunction m = ((XtendFunction) _head);
    XExpression _expression = m.getExpression();
    final XBlockExpression body = ((XBlockExpression) _expression);
    XExpression _head_1 = IterableExtensions.<XExpression>head(body.getExpressions());
    final AnonymousClass anon = ((AnonymousClass) _head_1);
    XtendMember _head_2 = IterableExtensions.<XtendMember>head(anon.getMembers());
    final XtendFunction m2 = ((XtendFunction) _head_2);
    final JvmTypeReference returnType = m2.getReturnType();
    final JvmType t = returnType.getType();
    Assert.assertNotNull("notNull", t);
    Assert.assertFalse("t.eIsProxy", t.eIsProxy());
    this.helper.assertNoErrors(file);
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 18
Source File: JvmDeclaredTypeImplCustom.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected JvmType getRawType(JvmTypeReference reference) {
	RawTypeReferenceComputer strategy = new RawTypeReferenceComputer(TypesFactory.eINSTANCE);
	JvmTypeReference result = strategy.getRawTypeReference(reference, eResource());
	return result == null ? null : result.getType();
}
 
Example 19
Source File: Primitives.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public boolean isPrimitive(JvmTypeReference type) {
	return type!=null && type.getType() instanceof JvmPrimitiveType && !type.getType().eIsProxy();
}
 
Example 20
Source File: ValidationBug433213Test.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void test_06() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def <K> m() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("new Object {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("def <V> m2() {");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("new java.util.AbstractMap<K, V> {");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("def Entry<K, V> m() {}");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("override entrySet() {}");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile file = this.parser.parse(_builder);
    final XtendTypeDeclaration c = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
    XtendMember _head = IterableExtensions.<XtendMember>head(c.getMembers());
    final XtendFunction m = ((XtendFunction) _head);
    XExpression _expression = m.getExpression();
    final XBlockExpression body = ((XBlockExpression) _expression);
    XExpression _head_1 = IterableExtensions.<XExpression>head(body.getExpressions());
    final AnonymousClass anon = ((AnonymousClass) _head_1);
    XtendMember _head_2 = IterableExtensions.<XtendMember>head(anon.getMembers());
    final XtendFunction m2 = ((XtendFunction) _head_2);
    XExpression _expression_1 = m2.getExpression();
    final XBlockExpression body2 = ((XBlockExpression) _expression_1);
    XExpression _head_3 = IterableExtensions.<XExpression>head(body2.getExpressions());
    final AnonymousClass anon2 = ((AnonymousClass) _head_3);
    XtendMember _head_4 = IterableExtensions.<XtendMember>head(anon2.getMembers());
    final XtendFunction m3 = ((XtendFunction) _head_4);
    final JvmTypeReference returnType = m3.getReturnType();
    final JvmType t = returnType.getType();
    Assert.assertNotNull("notNull", t);
    Assert.assertFalse("t.eIsProxy", t.eIsProxy());
    this.helper.assertNoErrors(file);
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}