org.eclipse.xtext.common.types.JvmGenericType Java Examples

The following examples show how to use org.eclipse.xtext.common.types.JvmGenericType. 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: InferredJvmModelTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testDispatchFunction_01() throws Exception {
	XtendFile xtendFile = file("class Foo { def dispatch foo(Object x, String y) {null} def dispatch foo(String x) {null}}");
	JvmGenericType inferredType = getInferredType(xtendFile);
	
	// two dispatch methods
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", Object.class);
	assertEquals("java.lang.Object", dispatch.getReturnType().getIdentifier());
	assertEquals(2, dispatch.getParameters().size());
	
	dispatch = findByNameAndFirstParameterType(operations, "foo", String.class);
	assertEquals("java.lang.Object", dispatch.getReturnType().getIdentifier());
	assertEquals(1, dispatch.getParameters().size());
	
	// two internal case methods
	findByNameAndFirstParameterType(operations, "_foo", Object.class);
	findByNameAndFirstParameterType(operations, "_foo", String.class);
}
 
Example #2
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_Inner_05() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodVArray_01()");
	JvmTypeReference listT = methodV.getReturnType();
	assertEquals("java.util.List<? extends V>[]", listT.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) ((JvmGenericArrayTypeReference) listT)
			.getComponentType();
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertTrue(typeArgument instanceof JvmWildcardTypeReference);
	JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument;
	assertEquals("? extends V", wildcardTypeArgument.getIdentifier());
	assertEquals(1, wildcardTypeArgument.getConstraints().size());
	JvmUpperBound upperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0);
	JvmTypeParameter v = type.getTypeParameters().get(3);
	assertSame(v, upperBound.getTypeReference().getType());
}
 
Example #3
Source File: XtendCompiler.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
	boolean result = super.isVariableDeclarationRequired(expr, b, recursive);
	if (result && expr instanceof XConstructorCall) {
		EObject container = expr.eContainer();
		if (container instanceof AnonymousClass) {
			AnonymousClass anonymousClass = (AnonymousClass) container;
			result = isVariableDeclarationRequired(anonymousClass, b, recursive);
			if (result) {
				JvmConstructor constructor = anonymousClass.getConstructorCall().getConstructor();
				JvmDeclaredType type = constructor.getDeclaringType();
				if (((JvmGenericType) type).isAnonymous()) {
					return false;
				}
			}
		}
	}
	return result;
}
 
Example #4
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_Inner_05() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodVArray_01()");
	JvmTypeReference listT = methodV.getReturnType();
	assertEquals("java.util.List<? extends V>[]", listT.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) ((JvmGenericArrayTypeReference) listT)
			.getComponentType();
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertTrue(typeArgument instanceof JvmWildcardTypeReference);
	JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument;
	assertEquals("? extends V", wildcardTypeArgument.getIdentifier());
	assertEquals(1, wildcardTypeArgument.getConstraints().size());
	JvmUpperBound upperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0);
	JvmTypeParameter v = type.getTypeParameters().get(3);
	assertSame(v, upperBound.getTypeReference().getType());
}
 
Example #5
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_06() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodVArray_02()");
	JvmTypeReference listV = methodV.getReturnType();
	assertEquals("java.util.List<? extends V[]>", listV.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listV;
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertTrue(typeArgument instanceof JvmWildcardTypeReference);
	JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument;
	assertEquals("? extends V[]", wildcardTypeArgument.getIdentifier());
	assertEquals(1, wildcardTypeArgument.getConstraints().size());
	JvmUpperBound upperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0);
	JvmType upperBoundType = upperBound.getTypeReference().getType();
	assertTrue(upperBoundType instanceof JvmArrayType);
	assertTrue(((JvmArrayType) upperBoundType).getComponentType() instanceof JvmTypeParameter);
	JvmTypeParameter v = type.getTypeParameters().get(3);
	assertSame(v, ((JvmArrayType) upperBoundType).getComponentType());
}
 
Example #6
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMethods_publicStrictFpMethod_01() {
	String typeName = Methods.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation method = getMethodFromType(type, Methods.class, "publicStrictFpMethod()");
	assertSame(type, method.getDeclaringType());
	assertFalse(method.isAbstract());
	assertFalse(method.isFinal());
	assertFalse(method.isStatic());
	assertFalse(method.isSynchronized());
	assertTrue(method.isStrictFloatingPoint());
	assertFalse(method.isNative());
	assertEquals(JvmVisibility.PUBLIC, method.getVisibility());
	JvmType methodType = method.getReturnType().getType();
	assertEquals("void", methodType.getIdentifier());
}
 
Example #7
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_U_01() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeVariable = type.getTypeParameters().get(2);
	assertEquals("U", typeVariable.getIdentifier());
	assertSame(type, typeVariable.getDeclarator());
	assertEquals(1, typeVariable.getConstraints().size());
	JvmTypeConstraint typeConstraint = typeVariable.getConstraints().get(0);
	assertTrue(typeConstraint instanceof JvmUpperBound);
	JvmUpperBound upperBound = (JvmUpperBound) typeConstraint;
	assertNotNull(upperBound.getTypeReference());
	assertFalse(upperBound.getTypeReference().toString(), upperBound.getTypeReference().eIsProxy());
	assertEquals("java.util.List<S>", upperBound.getTypeReference().getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) upperBound.getTypeReference();
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	JvmTypeParameter s = type.getTypeParameters().get(0);
	assertSame(s, typeArgument.getType());
}
 
Example #8
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_W_02() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeParameterW = type.getTypeParameters().get(4);
	assertEquals("W", typeParameterW.getIdentifier());
	assertSame(type, typeParameterW.getDeclarator());
	assertEquals(2, typeParameterW.getConstraints().size());

	JvmTypeConstraint secondTypeConstraint = typeParameterW.getConstraints().get(1);
	assertTrue(secondTypeConstraint instanceof JvmUpperBound);
	JvmUpperBound secondUpperBound = (JvmUpperBound) secondTypeConstraint;
	assertNotNull(secondUpperBound.getTypeReference());
	assertFalse(secondUpperBound.getTypeReference().toString(), secondUpperBound.getTypeReference().eIsProxy());
	assertEquals("java.io.Serializable", secondUpperBound.getTypeReference().getIdentifier());
}
 
Example #9
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStringAnnotation() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(Inject.class, e);
    anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType));
    anno.setValue(e);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    EObject _head_1 = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue) _head).getValues());
    Assert.assertTrue((_head_1 instanceof XStringLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #10
Source File: OverrideProposalUtil.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isCandidate(LightweightTypeReference type, IResolvedExecutable executable,
		IVisibilityHelper visibilityHelper) {
	JvmDeclaredType declaringType = executable.getDeclaration().getDeclaringType();
	if (type.getType() != declaringType && isVisible(executable, visibilityHelper)) {
		JvmExecutable rawExecutable = executable.getDeclaration();
		if (rawExecutable instanceof JvmOperation) {
			JvmOperation operation = (JvmOperation) rawExecutable;
			if (operation.isFinal() || operation.isStatic()) {
				return false;
			} else {
				if (type.getType() instanceof JvmGenericType && ((JvmGenericType) type.getType()).isInterface()) {
					return  declaringType instanceof JvmGenericType
							&& ((JvmGenericType) declaringType).isInterface() && !operation.isAbstract();
				} else {
					return true;
				}
			}
		} else {
			return true;
		}
	}
	return false;
}
 
Example #11
Source File: InferredJvmModelTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testInferredFunction_02() throws Exception {
	XtendFile xtendFile = file("class Foo { def create result: newArrayList(s) newList(String s) {} }");
	JvmGenericType inferredType = getInferredType(xtendFile);
	XtendClass xtendClass = (XtendClass) xtendFile.getXtendTypes().get(0);
	EList<JvmMember> jvmMembers = inferredType.getMembers();
	assertEquals(4, jvmMembers.size());
	JvmMember jvmMember = jvmMembers.get(1);
	assertTrue(jvmMember instanceof JvmOperation);
	XtendFunction xtendFunction = (XtendFunction) xtendClass.getMembers().get(0);
	assertEquals(xtendFunction.getName(), jvmMember.getSimpleName());
	assertEquals(JvmVisibility.PUBLIC, jvmMember.getVisibility());
	assertEquals("java.util.ArrayList<java.lang.String>", ((JvmOperation) jvmMember).getReturnType().getIdentifier());
	
	JvmField cacheVar = (JvmField) jvmMembers.get(2);
	assertEquals("_createCache_" + xtendFunction.getName(), cacheVar.getSimpleName());
	assertEquals(JvmVisibility.PRIVATE, cacheVar.getVisibility());
	assertEquals("java.util.HashMap<java.util.ArrayList<? extends java.lang.Object>, java.util.ArrayList<java.lang.String>>", cacheVar.getType().getIdentifier());
	
	JvmOperation privateInitializer = (JvmOperation) jvmMembers.get(3);
	assertEquals("_init_"+xtendFunction.getName(), privateInitializer.getSimpleName());
	assertEquals(JvmVisibility.PRIVATE, privateInitializer.getVisibility());
	assertEquals("java.util.ArrayList<java.lang.String>", privateInitializer.getParameters().get(0).getParameterType().getIdentifier());
	assertEquals("java.lang.String", privateInitializer.getParameters().get(1).getParameterType().getIdentifier());
}
 
Example #12
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_04() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodY = getMethodFromType(type, ParameterizedTypes.class, "methodY(Y)");
	assertEquals(1, methodY.getParameters().size());
	JvmType parameterType = methodY.getParameters().get(0).getParameterType().getType();
	assertFalse(parameterType.eIsProxy());
	assertEquals("Y", parameterType.getIdentifier());
	assertTrue(parameterType instanceof JvmTypeParameter);
	assertSame(methodY, ((JvmTypeParameter) parameterType).getDeclarator());
	JvmTypeParameter y = (JvmTypeParameter) parameterType;
	assertEquals(1, y.getConstraints().size());
	JvmUpperBound upperBound = (JvmUpperBound) y.getConstraints().get(0);
	JvmTypeParameter t = type.getTypeParameters().get(1);
	assertSame(t, upperBound.getTypeReference().getType());
}
 
Example #13
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_W_01() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeParameterW = type.getTypeParameters().get(4);
	assertEquals("W", typeParameterW.getIdentifier());
	assertSame(type, typeParameterW.getDeclarator());
	assertEquals(2, typeParameterW.getConstraints().size());
	JvmTypeConstraint firstTypeConstraint = typeParameterW.getConstraints().get(0);
	assertTrue(firstTypeConstraint instanceof JvmUpperBound);
	JvmUpperBound firstUpperBound = (JvmUpperBound) firstTypeConstraint;
	assertNotNull(firstUpperBound.getTypeReference());
	assertFalse(firstUpperBound.getTypeReference().toString(), firstUpperBound.getTypeReference().eIsProxy());
	assertEquals("java.lang.Comparable<S>", firstUpperBound.getTypeReference().getIdentifier());
	JvmParameterizedTypeReference comparableType = (JvmParameterizedTypeReference) firstUpperBound
			.getTypeReference();
	assertEquals(1, comparableType.getArguments().size());
	JvmTypeReference typeArgument = comparableType.getArguments().get(0);
	assertEquals("S", typeArgument.getIdentifier());
	JvmTypeParameter s = type.getTypeParameters().get(0);
	assertSame(s, typeArgument.getType());
}
 
Example #14
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testIntegerAnnotation() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(TestAnnotation3.class, e);
    final JvmAnnotationType annotatiomType = ((JvmAnnotationType) _findDeclaredType);
    anno.setAnnotationType(annotatiomType);
    final XAnnotationElementValuePair pair = f.createXAnnotationElementValuePair();
    pair.setElement(IterableExtensions.<JvmOperation>head(annotatiomType.getDeclaredOperations()));
    pair.setValue(this.expression("10"));
    EList<XAnnotationElementValuePair> _elementValuePairs = anno.getElementValuePairs();
    this._jvmTypesBuilder.<XAnnotationElementValuePair>operator_add(_elementValuePairs, pair);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    Assert.assertEquals(1, IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues().size());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    final JvmCustomAnnotationValue value = ((JvmCustomAnnotationValue) _head);
    EObject _head_1 = IterableExtensions.<EObject>head(value.getValues());
    Assert.assertTrue((_head_1 instanceof XNumberLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #15
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testFindTypeByName_javaUtilList_01() {
	String typeName = List.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	assertNotNull(type);
	assertEquals(typeName, type.getIdentifier());
	assertEquals(1, type.getTypeParameters().size());
	JvmTypeParameter typeVariable = type.getTypeParameters().get(0);
	assertEquals("E", typeVariable.getName());
	assertEquals(1, typeVariable.getConstraints().size());
	JvmTypeConstraint typeConstraint = typeVariable.getConstraints().get(0);
	assertTrue(typeConstraint instanceof JvmUpperBound);
	JvmUpperBound upperBound = (JvmUpperBound) typeConstraint;
	assertSame(typeVariable, upperBound.getOwner());
	assertNotNull(upperBound.getTypeReference());
	assertFalse(upperBound.getTypeReference().getType().eIsProxy());
	assertEquals(Object.class.getName(), upperBound.getTypeReference().getIdentifier());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #16
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean hasCycleInHierarchy(JvmGenericType type, Set<JvmGenericType> processedSuperTypes) {
	JvmDeclaredType container = type;
	do {
		if (processedSuperTypes.contains(container))
			return true;
		container = container.getDeclaringType();
	} while (container != null);
	processedSuperTypes.add(type);
	for (JvmTypeReference superTypeRef : type.getSuperTypes()) {
		if (superTypeRef.getType() instanceof JvmGenericType) {
			if (hasCycleInHierarchy((JvmGenericType) superTypeRef.getType(), processedSuperTypes))
				return true;
		}
	}
	processedSuperTypes.remove(type);
	return false;
}
 
Example #17
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMemberCount_12() {
	String typeName = Fields.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	int constructorCount = Fields.class.getDeclaredConstructors().length;
	assertEquals(1, constructorCount); // default constructor
	int fieldCount = Fields.class.getDeclaredFields().length;
	assertEquals(7, fieldCount);
	int nestedCount = Fields.class.getDeclaredClasses().length;
	assertEquals(1, nestedCount);
	assertEquals(nestedCount + constructorCount + fieldCount, type.getMembers().size());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #18
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a map type reference that comes as close as possible / necessary to its expected type.
 */
protected LightweightTypeReference createMapTypeReference(JvmGenericType mapType, LightweightTypeReference pairType, LightweightTypeReference expectation, ITypeReferenceOwner owner) {
	List<LightweightTypeReference> leftAndRight = pairType.getTypeArguments();
	
	LightweightTypeReference left = leftAndRight.get(0).getInvariantBoundSubstitute();
	LightweightTypeReference right = leftAndRight.get(1).getInvariantBoundSubstitute();
	
	LightweightTypeReference mapExpectation = getMapExpectation(expectation);
	if (mapExpectation != null) {
		List<LightweightTypeReference> typeArguments = expectation.getTypeArguments();
		left = doNormalizeElementType(left, typeArguments.get(0));
		right = doNormalizeElementType(right, typeArguments.get(1));
	}
	ParameterizedTypeReference result = owner.newParameterizedTypeReference(mapType);
	result.addTypeArgument(left.copyInto(owner));
	result.addTypeArgument(right.copyInto(owner));
	if (mapExpectation != null && !expectation.isAssignableFrom(result)) {
		// expectation does not match the computed type, but looks good according to the element types:
		// use expected type
		if (matchesExpectation(left, mapExpectation.getTypeArguments().get(0)) && matchesExpectation(right, mapExpectation.getTypeArguments().get(1))) {
			return expectation;
		}
	}
	return result;
}
 
Example #19
Source File: ResourceDescriptionProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStubGeneration_01() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("public class MyTest {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("public String helloWorld() {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("return \"Hello\";");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<IResourceDescription> _function = (IResourceDescription it) -> {
    Assert.assertEquals("MyTest", IterableExtensions.<IEObjectDescription>head(it.getExportedObjects()).getQualifiedName().toString());
    EObject _eObjectOrProxy = IterableExtensions.<IEObjectDescription>head(it.getExportedObjects()).getEObjectOrProxy();
    Assert.assertFalse(((JvmGenericType) _eObjectOrProxy).isInterface());
    Assert.assertEquals(1, IterableExtensions.size(it.getExportedObjects()));
  };
  this.resultsIn(_builder, _function);
}
 
Example #20
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMemberCount_07() {
	String typeName = StaticNestedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	int methodCount = StaticNestedTypes.class.getDeclaredMethods().length;
	assertEquals(1, methodCount);
	int constructorCount = StaticNestedTypes.class.getDeclaredConstructors().length;
	assertEquals(1, constructorCount); // default constructor
	int nestedTypesCount = StaticNestedTypes.class.getClasses().length;
	assertEquals(1, nestedTypesCount);
	assertEquals(methodCount + constructorCount + nestedTypesCount, type.getMembers().size());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #21
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void doTestInnerType_WrappedIterator_02(JvmGenericType wrappedIterator) {
	assertEquals(3, Iterables.size(wrappedIterator.getDeclaredConstructors()));
	JvmConstructor constructor = (JvmConstructor) Iterables.find(wrappedIterator.getMembers(),
			new Predicate<JvmMember>() {
				@Override
				public boolean apply(JvmMember input) {
					return (input instanceof JvmConstructor) && input.getSimpleName().equals("WrappedIterator")
							&& ((JvmConstructor) input).getParameters().size() == 3;
				}
			});
	assertNotNull(constructor);
	JvmFormalParameter firstParameter = constructor.getParameters().get(0);
	assertEquals(1, firstParameter.getAnnotations().size());
	assertEquals("java.lang.String", firstParameter.getParameterType().getIdentifier());
	assertEquals(TestAnnotationWithDefaults.class.getName(),
			firstParameter.getAnnotations().get(0).getAnnotation().getQualifiedName());
	JvmFormalParameter secondParameter = constructor.getParameters().get(1);
	assertEquals(0, secondParameter.getAnnotations().size());
	assertEquals("int", secondParameter.getParameterType().getIdentifier());
	JvmFormalParameter thirdParameter = constructor.getParameters().get(2);
	assertEquals(1, thirdParameter.getAnnotations().size());
	assertEquals("java.util.Iterator<V>", thirdParameter.getParameterType().getIdentifier());
	assertEquals(TestAnnotation.NestedAnnotation.class.getName(),
			thirdParameter.getAnnotations().get(0).getAnnotation().getQualifiedName());
}
 
Example #22
Source File: XtendEObjectAtOffsetHelper.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected EObject resolveCrossReferencedElement(INode node) {
	EObject referencedElement = super.resolveCrossReferencedElement(node);
	EObject referenceOwner = NodeModelUtils.findActualSemanticObjectFor(node);
	if(referenceOwner instanceof XConstructorCall) {
		if (referenceOwner.eContainer() instanceof AnonymousClass) {
			AnonymousClass anon = (AnonymousClass) referenceOwner.eContainer();
			JvmGenericType superType = anonymousClassUtil.getSuperType(anon);
			if(superType != null) {
				if (referencedElement instanceof JvmGenericType)  
					return superType;
				else if(referencedElement instanceof JvmConstructor) {
					if(superType.isInterface())
						return superType;
					JvmConstructor superConstructor = anonymousClassUtil.getSuperTypeConstructor(anon);
					if(superConstructor != null)
						return superConstructor;
				}
			}
		}
	}
	return referencedElement;
}
 
Example #23
Source File: SARLJvmModelInferrer.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
protected JvmOperation deriveGenericDispatchOperationSignature(
		Iterable<JvmOperation> localOperations, JvmGenericType target) {
	final JvmOperation dispatcher = super.deriveGenericDispatchOperationSignature(localOperations, target);
	//
	// Fixing the behavior for determining the visibility of the dispatcher since
	// it does not fit the SARL requirements.
	//
	JvmVisibility higherVisibility = JvmVisibility.PRIVATE;
	for (final JvmOperation jvmOperation : localOperations) {
		final Iterable<XtendFunction> xtendFunctions = Iterables.filter(
				this.sarlAssociations.getSourceElements(jvmOperation), XtendFunction.class);
		for (final XtendFunction func : xtendFunctions) {
			JvmVisibility visibility = func.getVisibility();
			if (visibility == null) {
				visibility = this.defaultVisibilityProvider.getDefaultJvmVisibility(func);
			}
			if (this.visibilityComparator.compare(visibility, higherVisibility) > 0) {
				higherVisibility = visibility;
			}
		}
	}
	dispatcher.setVisibility(higherVisibility);

	return dispatcher;
}
 
Example #24
Source File: FindReferencesTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testFindReferencesToAnonymousSuperType() throws Exception {
	XtendClass classFoo = (XtendClass) testHelper.xtendFile("Foo", "class Foo {}").getXtendTypes().get(0);
	XtendClass classBar = (XtendClass) testHelper.xtendFile("Bar", "class Bar { val foo = new Foo{} }").getXtendTypes().get(0);
	waitForBuild();
	XtendField fieldFoo = (XtendField) classBar.getMembers().get(0);
	JvmGenericType inferredTypeFoo = associations.getInferredType(classFoo);

	final MockAcceptor mockAcceptor = new MockAcceptor();
	mockAcceptor.expect(((AnonymousClass) fieldFoo.getInitialValue()).getConstructorCall(), inferredTypeFoo, XCONSTRUCTOR_CALL__CONSTRUCTOR);
	findReferencesTester.checkFindReferences(inferredTypeFoo, "Java References to Foo (/test.project/src/Foo.xtend)", mockAcceptor);
}
 
Example #25
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_staticNestedTypes_method() {
	String typeName = StaticNestedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation operation = getMethodFromType(type, StaticNestedTypes.class, "method()");
	assertEquals("boolean", operation.getReturnType().getIdentifier());
}
 
Example #26
Source File: ReflectionTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testJvmTypeSimple() {
	Resource resource = resourceSet.createResource(URI.createURI("foo.typesRefactoring"));
	JvmGenericType expected = TypesFactory.eINSTANCE.createJvmGenericType();
	expected.setSimpleName("SimpleName");
	expected.setPackageName("package.name");
	resource.getContents().add(expected);
	JvmType actual = getTypeProvider().findTypeByName("package.name.SimpleName");
	assertEquals(expected, actual);
}
 
Example #27
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMemberCount_03() {
	String typeName = InitializerWithoutConstructor.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	int methodCount = InitializerWithoutConstructor.class.getDeclaredMethods().length;
	assertEquals(0, methodCount);
	int constructorCount = InitializerWithoutConstructor.class.getDeclaredConstructors().length;
	assertEquals(1, constructorCount); // default constructor
	assertEquals(methodCount + constructorCount, type.getMembers().size());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #28
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testTypeParamEndsWithDollar_02() {
	String typeName = "org.eclipse.xtext.common.types.testSetups.TypeParamEndsWithDollar";
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation function = (JvmOperation) type.findAllFeaturesByName("function2").iterator().next();
	JvmFormalParameter parameter = function.getParameters().get(0);
	JvmType parameterType = parameter.getParameterType().getType();
	assertEquals(function.getTypeParameters().get(0), parameterType);
	JvmFormalParameter secondParameter = function.getParameters().get(1);
	JvmType secondParameterType = secondParameter.getParameterType().getType();
	assertEquals(type.getTypeParameters().get(0), secondParameterType);
}
 
Example #29
Source File: SARLValidator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Check if all the fields are initialized in a SARL skill.
 *
 * @param skill the skill.
 */
@Check
public void checkFinalFieldInitialization(SarlSkill skill) {
	final JvmGenericType inferredType = this.associations.getInferredType(skill);
	if (inferredType != null) {
		checkFinalFieldInitialization(inferredType);
	}
}
 
Example #30
Source File: JvmGenericTypeItemProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * This returns the label text for the adapted class.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public String getText(Object object)
{
	String label = ((JvmGenericType)object).getSimpleName();
	return label == null || label.length() == 0 ?
		getString("_UI_JvmGenericType_type") :
		getString("_UI_JvmGenericType_type") + " " + label;
}