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

The following examples show how to use org.eclipse.xtext.common.types.JvmGenericType#getDeclaredOperations() . 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
@Test public void testDispatchFunction_00() throws Exception {
	XtendFile xtendFile = file("class Foo { def dispatch foo(Object x) {null} def dispatch foo(String x) {null}}");
	JvmGenericType inferredType = getInferredType(xtendFile);

	// one main dispatch
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", Object.class);
	assertEquals("java.lang.Object", dispatch.getReturnType().getIdentifier());
	
	// two internal case methods
	findByNameAndFirstParameterType(operations, "_foo", Object.class);
	findByNameAndFirstParameterType(operations, "_foo", String.class);
	for(JvmMember member: inferredType.getMembers()) {
		if (member instanceof JvmExecutable) {
			if (member.getSimpleName().startsWith("_"))
				assertEquals(JvmVisibility.PROTECTED, member.getVisibility());
			else
				assertEquals(JvmVisibility.PUBLIC, member.getVisibility());
		} else {
			assertEquals(JvmVisibility.PRIVATE, member.getVisibility());
		}
	}
}
 
Example 2
@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 3
@Test public void testDispatchFunction_04() throws Exception {
	XtendFile xtendFile = file("class Foo { def dispatch foo(Integer x) {x} def dispatch foo(Double x) {x}}");
	JvmGenericType inferredType = getInferredType(xtendFile);
	
	// one main dispatch
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", Number.class);
	// return type is specialized
	assertEquals("java.lang.Number", dispatch.getReturnType().getIdentifier());
	
	// two internal case methods
	JvmOperation internal = findByNameAndFirstParameterType(operations, "_foo", Double.class);
	assertEquals(dispatch.getReturnType().getIdentifier(), internal.getReturnType().getIdentifier());
	
	internal = findByNameAndFirstParameterType(operations, "_foo", Integer.class);
	assertEquals(dispatch.getReturnType().getIdentifier(), internal.getReturnType().getIdentifier());
}
 
Example 4
@Test public void testBug_340611() throws Exception {
	XtendFile xtendFile = file(
			"class Bug340611 {\n" + 
			"    def dispatch foo(String string) {\n" + 
			"        string\n" + 
			"    }\n" + 
			"    def dispatch foo(Object o) {\n" + 
			"        null\n" + 
			"    }\n" + 
			"}", false);
	
	JvmGenericType inferredType = getInferredType(xtendFile);
	assertEquals(3, IterableExtensions.size(inferredType.getDeclaredOperations()));
	
	// one main dispatch
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", Object.class);
	assertEquals("java.lang.String", dispatch.getReturnType().getIdentifier());

	// two internal case methods
	JvmOperation stringParam = findByNameAndFirstParameterType(operations, "_foo", String.class);
	assertEquals("java.lang.String", stringParam.getReturnType().getIdentifier());
	
	JvmOperation objectParam = findByNameAndFirstParameterType(operations, "_foo", Object.class);
	assertEquals("java.lang.String", objectParam.getReturnType().getIdentifier());
}
 
Example 5
@Test public void testBug_340611_02() throws Exception {
	XtendFile xtendFile = file(
			"class Bug340611 {\n" + 
			"    def dispatch foo(String string) {\n" + 
			"        string\n" + 
			"    }\n" + 
			"    def dispatch foo(CharSequence charSeq) {\n" + 
			"        charSeq\n" + 
			"    }\n" + 
			"}", false);
	
	JvmGenericType inferredType = getInferredType(xtendFile);
	assertEquals(3, IterableExtensions.size(inferredType.getDeclaredOperations()));
	
	// one main dispatch
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", CharSequence.class);
	assertEquals("java.lang.CharSequence", dispatch.getReturnType().getIdentifier());

	// two internal case methods
	JvmOperation stringParam = findByNameAndFirstParameterType(operations, "_foo", String.class);
	assertEquals("java.lang.CharSequence", stringParam.getReturnType().getIdentifier());
	
	JvmOperation objectParam = findByNameAndFirstParameterType(operations, "_foo", CharSequence.class);
	assertEquals("java.lang.CharSequence", objectParam.getReturnType().getIdentifier());
}
 
Example 6
@Test public void testBug_340611_03() throws Exception {
	XtendFile xtendFile = file(
			"class Bug340611 {\n" + 
			"    def dispatch foo(Appendable appendable) {\n" + 
			"        appendable\n" + 
			"    }\n" + 
			"    def dispatch foo(CharSequence charSeq) {\n" + 
			"        charSeq\n" + 
			"    }\n" + 
			"}", false);
	
	JvmGenericType inferredType = getInferredType(xtendFile);
	assertEquals(3, IterableExtensions.size(inferredType.getDeclaredOperations()));
	
	// one main dispatch
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", Object.class);
	assertEquals("java.lang.Object", dispatch.getReturnType().getIdentifier());

	// two internal case methods
	JvmOperation stringParam = findByNameAndFirstParameterType(operations, "_foo", Appendable.class);
	assertEquals("java.lang.Object", stringParam.getReturnType().getIdentifier());
	
	JvmOperation objectParam = findByNameAndFirstParameterType(operations, "_foo", CharSequence.class);
	assertEquals("java.lang.Object", objectParam.getReturnType().getIdentifier());
}
 
Example 7
@Test public void testBug_340611_04() throws Exception {
	XtendFile xtendFile = file(
			"class Bug340611 {\n" + 
			"    def dispatch Appendable foo(Appendable appendable) {\n" + 
			"        appendable\n" + 
			"    }\n" + 
			"    def dispatch foo(CharSequence charSeq) {\n" + 
			"        charSeq\n" + 
			"    }\n" + 
			"}", false);
	
	JvmGenericType inferredType = getInferredType(xtendFile);
	assertEquals(3, IterableExtensions.size(inferredType.getDeclaredOperations()));
	
	// one main dispatch
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", Object.class);
	assertEquals("java.lang.Object", dispatch.getReturnType().getIdentifier());

	// two internal case methods
	JvmOperation stringParam = findByNameAndFirstParameterType(operations, "_foo", Appendable.class);
	assertEquals("java.lang.Appendable", stringParam.getReturnType().getIdentifier());
	
	JvmOperation objectParam = findByNameAndFirstParameterType(operations, "_foo", CharSequence.class);
	assertEquals("java.lang.Object", objectParam.getReturnType().getIdentifier());
}
 
Example 8
@Test public void testBug_340611_05() throws Exception {
	XtendFile xtendFile = file(
			"class Bug340611 extends test.Dispatching {\n" + 
			"    override dispatch doDispatch(StringBuilder sb) {\n" + 
			"        null\n" + 
			"    }\n" + 
			"    def dispatch doDispatch(String s) {\n" + 
			"        s\n" + 
			"    }\n" + 
			"}", false);
	
	JvmGenericType inferredType = getInferredType(xtendFile);
	assertEquals(3, IterableExtensions.size(inferredType.getDeclaredOperations()));
	
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "doDispatch", Object.class);
	assertEquals("java.lang.CharSequence", dispatch.getReturnType().getIdentifier());
	
	// two internal case methods
	JvmOperation stringParam = findByNameAndFirstParameterType(operations, "_doDispatch", StringBuilder.class);
	assertEquals("java.lang.StringBuilder", stringParam.getReturnType().getIdentifier());
	
	JvmOperation objectParam = findByNameAndFirstParameterType(operations, "_doDispatch", String.class);
	assertEquals("java.lang.CharSequence", objectParam.getReturnType().getIdentifier());
}
 
Example 9
protected JvmOperation findOperation(Class<?> declaringType, String methodName, int numberOfParameters) {
	JvmGenericType type = (JvmGenericType) typeProvider.findTypeByName(declaringType.getCanonicalName());
	for(JvmOperation operation: type.getDeclaredOperations()) {
		if (methodName.equals(operation.getSimpleName()) && operation.getParameters().size() == numberOfParameters) {
			return operation;
		}
	}
	return null;
}
 
Example 10
@Test public void testDispatchFunction_02() throws Exception {
	XtendFile xtendFile = file("class Foo { def dispatch foo(Object x) {'foo'} def dispatch foo(String x) {'bar'}}");
	JvmGenericType inferredType = getInferredType(xtendFile);
	
	// one main dispatch
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", Object.class);
	assertEquals(String.class.getName(), dispatch.getReturnType().getIdentifier());
	
	// two internal case methods
	findByNameAndFirstParameterType(operations, "_foo", Object.class);
	findByNameAndFirstParameterType(operations, "_foo", String.class);
}
 
Example 11
protected void testDispatchFunction_03_impl(boolean validation) throws Exception {
	XtendFile xtendFile = file(
			"class Dispatcher {\n" + 
			"	def dispatch doStuff(org.eclipse.emf.ecore.EClass model) {\n" + 
			"		model.ETypeParameters.map(e|doStuff(e))\n" + 
			"	}\n" + 
			"	def dispatch doStuff(org.eclipse.emf.ecore.EPackage packageDecl) {\n" + 
			"		packageDecl.EClassifiers.map(e|doStuff(e))\n" + 
			"	}\n" + 
			"	def dispatch doStuff(org.eclipse.emf.ecore.EStructuralFeature feature) {\n" + 
			"		newArrayList(feature)\n" + 
			"	}\n" + 
			"}", validation);
	
	JvmGenericType inferredType = getInferredType(xtendFile);
	
	// one main dispatch
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "doStuff", ENamedElement.class);
	// TODO ultimately this should be List<? extends NamedElement>
	assertEquals("java.util.List<? extends java.lang.Object>", dispatch.getReturnType().getIdentifier());

	// three internal case methods
	JvmOperation eClassParam = findByNameAndFirstParameterType(operations, "_doStuff", EClass.class);
	assertEquals("java.util.List<? extends java.lang.Object>", eClassParam.getReturnType().getIdentifier());
	
	JvmOperation ePackageParam = findByNameAndFirstParameterType(operations, "_doStuff", EPackage.class);
	assertEquals("java.util.List<? extends java.lang.Object>", ePackageParam.getReturnType().getIdentifier());
	
	JvmOperation eStructuralFeatureParam = findByNameAndFirstParameterType(operations, "_doStuff", EStructuralFeature.class);
	assertEquals("java.util.List<? extends java.lang.Object>", eStructuralFeatureParam.getReturnType().getIdentifier());
}
 
Example 12
@Test public void testDispatchFunction_05() throws Exception {
	XtendFile xtendFile = file("class Foo {" + 
			"  def private dispatch private_private  (Integer x) {} def private   dispatch private_private  (Double x) {}" +
			"  def private dispatch private_protected(Integer x) {} def protected dispatch private_protected(Double x) {}" +
			"  def private dispatch private_public   (Integer x) {} def public    dispatch private_public   (Double x) {}" +
			"  def private dispatch private_default  (Integer x) {} def           dispatch private_default  (Double x) {}" +
			"  def protected dispatch protected_private  (Integer x) {} def private   dispatch protected_private  (Double x) {}" +
			"  def protected dispatch protected_protected(Integer x) {} def protected dispatch protected_protected(Double x) {}" +
			"  def protected dispatch protected_public   (Integer x) {} def public    dispatch protected_public   (Double x) {}" +
			"  def protected dispatch protected_default  (Integer x) {} def           dispatch protected_default  (Double x) {}" +
			"  def public dispatch public_private  (Integer x) {} def private   dispatch public_private  (Double x) {}" +
			"  def public dispatch public_protected(Integer x) {} def protected dispatch public_protected(Double x) {}" +
			"  def public dispatch public_public   (Integer x) {} def public    dispatch public_public   (Double x) {}" +
			"  def public dispatch public_default  (Integer x) {} def           dispatch public_default  (Double x) {}" +
			"  def dispatch default_private  (Integer x) {} def private   dispatch default_private  (Double x) {}" +
			"  def dispatch default_protected(Integer x) {} def protected dispatch default_protected(Double x) {}" +
			"  def dispatch default_public   (Integer x) {} def public    dispatch default_public   (Double x) {}" +
			"  def dispatch default_default  (Integer x) {} def           dispatch default_default  (Double x) {}" +
			"}");
	JvmGenericType inferredType = getInferredType(xtendFile);
	for(JvmOperation op: inferredType.getDeclaredOperations()) {
		String[] split = op.getSimpleName().toUpperCase().split("_");
		JvmVisibility expectedVisibility = null;
		if(!op.getSimpleName().startsWith("_")) {
			if(equal(split[0], split[1]))
				expectedVisibility = "DEFAULT".equals(split[0]) ? JvmVisibility.PUBLIC : JvmVisibility.get(split[0]);
			else 	
				expectedVisibility = JvmVisibility.PUBLIC;
		}
		else if(equal(op.getParameters().get(0).getParameterType().getIdentifier(), "java.lang.Integer"))
			expectedVisibility = "DEFAULT".equals(split[1]) ? JvmVisibility.PROTECTED : JvmVisibility.get(split[1]);
		else
			expectedVisibility = "DEFAULT".equals(split[2]) ? JvmVisibility.PROTECTED : JvmVisibility.get(split[2]);
		assertEquals(op.getIdentifier(), expectedVisibility, op.getVisibility());
	}
}
 
Example 13
@Test public void testDispatchFunction_10() throws Exception {
	XtendFile xtendFile = file(
		"class Foo {\n" +
		"	def dispatch String someMethod(String o) {\n" +
		"		\"String\".someMethod\n" +
		"	}\n" +
		"	def dispatch someMethod(Object o) {\n" +
		"		\"String\"" +
		"	}\n" +
		"	def dispatch someMethod(Double o) {\n" +
		"		\"String\""+
		"	}\n" +
		"}");
	JvmGenericType inferredType = getInferredType(xtendFile);
	
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "someMethod", Object.class);
	assertEquals("java.lang.String", dispatch.getReturnType().getIdentifier());

	JvmOperation dispatchCase = findByNameAndFirstParameterType(operations, "_someMethod", Object.class);
	assertEquals("java.lang.String", dispatchCase.getReturnType().getIdentifier());
	
	JvmOperation dispatchCase2 = findByNameAndFirstParameterType(operations, "_someMethod", String.class);
	assertEquals("java.lang.String", dispatchCase2.getReturnType().getIdentifier());
	
	JvmOperation dispatchCase3 = findByNameAndFirstParameterType(operations, "_someMethod", Double.class);
	assertEquals("java.lang.String", dispatchCase3.getReturnType().getIdentifier());
}
 
Example 14
@Test public void testDispatchFunction_11() throws Exception {
	XtendFile xtendFile = file(
		"class Foo {\n" +
		"	def dispatch someMethod(Object o) {\n" +
		"		\"String\"" +
		"	}\n" +
		"	def dispatch String someMethod(String o) {\n" +
		"		\"String\".someMethod\n" +
		"	}\n" +
		"	def dispatch someMethod(Double o) {\n" +
		"		\"String\""+
		"	}\n" +
		"}");
	JvmGenericType inferredType = getInferredType(xtendFile);
	
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "someMethod", Object.class);
	assertEquals("java.lang.String", dispatch.getReturnType().getIdentifier());

	JvmOperation dispatchCase = findByNameAndFirstParameterType(operations, "_someMethod", Object.class);
	assertEquals("java.lang.String", dispatchCase.getReturnType().getIdentifier());
	
	JvmOperation dispatchCase2 = findByNameAndFirstParameterType(operations, "_someMethod", String.class);
	assertEquals("java.lang.String", dispatchCase2.getReturnType().getIdentifier());
	
	JvmOperation dispatchCase3 = findByNameAndFirstParameterType(operations, "_someMethod", Double.class);
	assertEquals("java.lang.String", dispatchCase3.getReturnType().getIdentifier());
}
 
Example 15
@Test public void testFinalFunction() throws Exception {
	XtendFile xtendFile = file("final class C { final String s = '' def final void m() {} }");
	JvmGenericType inferredType = getInferredType(xtendFile);
	assertTrue(inferredType.isFinal());
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation operation = operations.iterator().next();
	assertTrue(operation.isFinal());
	Iterable<JvmField> fields = inferredType.getDeclaredFields();
	JvmField field = fields.iterator().next();
	assertTrue(field.isFinal());	
}
 
Example 16
@Test public void testInferredFunction_03() throws Exception {
	XtendFile xtendFile = file("class Foo {def publicMethod(Object dummy) {} def public publicMethod() {} def protected protectedMethod() {} def private privateMethod() {} }");
	JvmGenericType inferredType = getInferredType(xtendFile);
	for(JvmOperation op: inferredType.getDeclaredOperations()) {
		assertEquals(JvmVisibility.get(op.getSimpleName().replace("Method", "").toUpperCase()), op.getVisibility());
	}
}