Skip to content

Commit

Permalink
[xbase][validation] Fixing the name replies by UIStrings class for ty…
Browse files Browse the repository at this point in the history
…pe references.

The UIStrings class is able to reply the human-readable string for a
JvmTypereference.
When the JvmTypeReference is resolved, the simple name of the referred
type is always replied.
But, when the JvmTypeReference is not resolved, the replied name is
directly extracted from the text of associated code.
Consequently, the replied value may be a simple name or a fully
qualified name of the type.
This patch ensures that UIStrings class always reply the simple name of
the type in all cases.

see https://bugs.eclipse.org/bugs/show_bug.cgi?id=443131

Change-Id: Ida292ba87e3ab1408f03c17973db0bdb3901c8de
Signed-off-by: sgalland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg authored and Sven Efftinge committed Sep 10, 2014
1 parent aab8672 commit 50f5da7
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 1 deletion.
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -47,6 +47,7 @@
* UI presentable string representation of Xbase elements.
*
* @author Jan Koehnlein - Initial contribution and API
* @author Stéphane Galland
*/
public class UIStrings {

Expand Down Expand Up @@ -167,6 +168,14 @@ public String referenceToString(JvmTypeReference typeRef, String defaultLabel) {
String text = node.getRootNode().getText();
ITextRegion textRegion = node.getTextRegion();
String result = text.substring(textRegion.getOffset(), textRegion.getLength() + textRegion.getOffset());

if (result != null) {
int pointIndex = result.lastIndexOf('.');
if (pointIndex >= 0 && pointIndex < (result.length() - 1)) {
return result.substring(pointIndex + 1);
}
}

return result;
} else {
return defaultLabel;
Expand Down
@@ -0,0 +1,149 @@
/*******************************************************************************
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtend.core.tests.validation;

import org.eclipse.xtend.core.tests.AbstractXtendTestCase;
import org.eclipse.xtend.core.xtend.XtendClass;
import org.eclipse.xtend.core.xtend.XtendFile;
import org.eclipse.xtend.core.xtend.XtendFunction;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.xbase.XBlockExpression;
import org.eclipse.xtext.xbase.XConstructorCall;
import org.eclipse.xtext.xbase.XVariableDeclaration;
import org.eclipse.xtext.xbase.validation.UIStrings;
import org.junit.Test;

import com.google.inject.Inject;

/**
* @author Stéphane Galland - Initial contribution and API
*/
public class UIStringsTest extends AbstractXtendTestCase {

@Inject
protected UIStrings uiStrings;

/** Only the simple name of the type is specified.
* The JvmTypeReference is not a proxy.
*/
@Test
public void testReferenceToString_0() throws Exception {
XtendFile file = file(
"package org.eclipse.xtend.core.tests.validation.uistrings\n"
+ "public interface InterfaceA { }\n"
+ "public class ClassB implements InterfaceA { }\n"
+ "public class ClassC extends ClassB {\n"
+ "}\n"
+ "class XtendClass1 {\n"
+ " def test() {\n"
+ " val x = new List<ClassC>\n"
+ " }\n"
+ "}\n");
XtendClass clazz = (XtendClass) file.getXtendTypes().get(3);
XBlockExpression block = (XBlockExpression) ((XtendFunction) clazz.getMembers().get(0)).getExpression();
XVariableDeclaration declaration = (XVariableDeclaration) block.getExpressions().get(0);
XConstructorCall cons = (XConstructorCall) declaration.getRight();
JvmTypeReference reference = cons.getTypeArguments().get(0);
assertNotNull(reference);
assertNotNull(reference.getType());
assertFalse(reference.getType().eIsProxy());
assertNotNull(reference.eResource());
assertEquals("ClassC", this.uiStrings.referenceToString(reference, "the-default-label"));
}

/** Only the simple name of the type is specified.
* The JvmTypeReference is proxy.
*/
@Test
public void testReferenceToString_1() throws Exception {
XtendFile file1 = file(
"package org.eclipse.xtend.core.tests.validation.uistrings\n"
+ "public interface InterfaceA { }\n"
+ "public class ClassB implements InterfaceA { }\n"
+ "public class ClassC extends ClassB {\n"
+ "}\n");
assertNotNull(file1);
XtendFile file2 = file(
"package org.eclipse.xtend.core.tests.validation.uistrings\n"
+ "class XtendClass1 {\n"
+ " def test() {\n"
+ " val x = new List<ClassC>\n"
+ " }\n"
+ "}\n");
XtendClass clazz = (XtendClass) file2.getXtendTypes().get(0);
XBlockExpression block = (XBlockExpression) ((XtendFunction) clazz.getMembers().get(0)).getExpression();
XVariableDeclaration declaration = (XVariableDeclaration) block.getExpressions().get(0);
XConstructorCall cons = (XConstructorCall) declaration.getRight();
JvmTypeReference reference = cons.getTypeArguments().get(0);
assertNotNull(reference);
assertNotNull(reference.getType());
assertTrue(reference.getType().eIsProxy());
assertNotNull(reference.eResource());
assertEquals("ClassC", this.uiStrings.referenceToString(reference, "the-default-label"));
}

/** The qualified name of the type is specified.
* The JvmTypeReference is not a proxy.
*/
@Test
public void testReferenceToString_2() throws Exception {
XtendFile file = file(
"package org.eclipse.xtend.core.tests.validation.uistrings\n"
+ "public interface InterfaceA { }\n"
+ "public class ClassB implements InterfaceA { }\n"
+ "public class ClassC extends ClassB {\n"
+ "}\n"
+ "class XtendClass1 {\n"
+ " def test() {\n"
+ " val x = new List<org.eclipse.xtend.core.tests.validation.uistrings.ClassC>\n"
+ " }\n"
+ "}\n");
XtendClass clazz = (XtendClass) file.getXtendTypes().get(3);
XBlockExpression block = (XBlockExpression) ((XtendFunction) clazz.getMembers().get(0)).getExpression();
XVariableDeclaration declaration = (XVariableDeclaration) block.getExpressions().get(0);
XConstructorCall cons = (XConstructorCall) declaration.getRight();
JvmTypeReference reference = cons.getTypeArguments().get(0);
assertNotNull(reference);
assertNotNull(reference.getType());
assertFalse(reference.getType().eIsProxy());
assertNotNull(reference.eResource());
assertEquals("ClassC", this.uiStrings.referenceToString(reference, "the-default-label"));
}

/** The qualified name of the type is specified.
* The JvmTypeReference is proxy.
*/
@Test
public void testReferenceToString_3() throws Exception {
XtendFile file1 = file(
"package org.eclipse.xtend.core.tests.validation.uistrings\n"
+ "public interface InterfaceA { }\n"
+ "public class ClassB implements InterfaceA { }\n"
+ "public class ClassC extends ClassB {\n"
+ "}\n");
assertNotNull(file1);
XtendFile file2 = file(
"package org.eclipse.xtend.core.tests.validation.uistrings\n"
+ "class XtendClass1 {\n"
+ " def test() {\n"
+ " val x = new List<org.eclipse.xtend.core.tests.validation.uistrings.ClassC>\n"
+ " }\n"
+ "}\n");
XtendClass clazz = (XtendClass) file2.getXtendTypes().get(0);
XBlockExpression block = (XBlockExpression) ((XtendFunction) clazz.getMembers().get(0)).getExpression();
XVariableDeclaration declaration = (XVariableDeclaration) block.getExpressions().get(0);
XConstructorCall cons = (XConstructorCall) declaration.getRight();
JvmTypeReference reference = cons.getTypeArguments().get(0);
assertNotNull(reference);
assertNotNull(reference.getType());
assertTrue(reference.getType().eIsProxy());
assertNotNull(reference.eResource());
assertEquals("ClassC", this.uiStrings.referenceToString(reference, "the-default-label"));
}

}
@@ -0,0 +1,73 @@
/*******************************************************************************
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.xbase.tests.validation;

import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.common.types.util.TypeReferences;
import org.eclipse.xtext.xbase.XBlockExpression;
import org.eclipse.xtext.xbase.XCastedExpression;
import org.eclipse.xtext.xbase.XExpression;
import org.eclipse.xtext.xbase.XTypeLiteral;
import org.eclipse.xtext.xbase.XVariableDeclaration;
import org.eclipse.xtext.xbase.tests.AbstractXbaseTestCase;
import org.eclipse.xtext.xbase.validation.UIStrings;
import org.junit.Test;

import com.google.inject.Inject;

/**
* @author Stéphane Galland - Initial contribution and API
*/
public class UIStringsTest extends AbstractXbaseTestCase {

@Inject
protected UIStrings uiStrings;

@Inject
protected TypeReferences typeReferences;

@Test
public void testReferenceToString_0() throws Exception {
XExpression expr = expression("typeof(foo.String)", true);
assertTrue(expr instanceof XTypeLiteral);
XTypeLiteral operator = (XTypeLiteral) expr;
JvmType type = operator.getType();
JvmTypeReference reference = this.typeReferences.createTypeRef(type);
assertEquals("String", this.uiStrings.referenceToString(reference, "the-default-value"));
}

@Test
public void testReferenceToString_1() throws Exception {
XExpression expr = expression("typeof(String)", true);
assertTrue(expr instanceof XTypeLiteral);
XTypeLiteral operator = (XTypeLiteral) expr;
JvmType type = operator.getType();
JvmTypeReference reference = this.typeReferences.createTypeRef(type);
assertEquals("String", this.uiStrings.referenceToString(reference, "the-default-value"));
}

@Test
public void testReferenceToString_2() throws Exception {
XExpression expr = expression("null as foo.String", true);
assertTrue(expr instanceof XCastedExpression);
XCastedExpression operator = (XCastedExpression) expr;
JvmTypeReference reference = operator.getType();
assertEquals("String", this.uiStrings.referenceToString(reference, "the-default-value"));
}

@Test
public void testReferenceToString_3() throws Exception {
XExpression expr = expression("{ var foo.String x }", false);
assertNotNull(expr);
XVariableDeclaration declaration = (XVariableDeclaration) (((XBlockExpression) expr).getExpressions().get(0));
JvmTypeReference reference = declaration.getType();
assertEquals("String", this.uiStrings.referenceToString(reference, "the-default-value"));
}

}

0 comments on commit 50f5da7

Please sign in to comment.