summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakosyakov2013-07-04 08:31:50 (EDT)
committer akosyakov2013-07-08 11:33:54 (EDT)
commit91571314a9396c72044261e66c546c28989170bb (patch)
treef59d43ac07fc1cc12e0591f6f5f0deb9245359a7
parentf3df5d844e253cce28f3ad164cbbcb2b875c2e8d (diff)
downloadorg.eclipse.xtext-91571314a9396c72044261e66c546c28989170bb.zip
org.eclipse.xtext-91571314a9396c72044261e66c546c28989170bb.tar.gz
org.eclipse.xtext-91571314a9396c72044261e66c546c28989170bb.tar.bz2
[411973]: resolved - make XbaseCompiler generate anonymous Java classrefs/changes/56/14256/5
for XClosure without the variable in the scope Change-Id: I8eb38236c15e68f6d4baafe699a68fb3e9743b7e Signed-off-by: akosyakov <anton.kosyakov@itemis.de>
-rw-r--r--plugins/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/XbaseCompiler.java14
-rw-r--r--tests/org.eclipse.xtend.core.tests/src/org/eclipse/xtend/core/tests/compiler/CompilerTest.java16
2 files changed, 27 insertions, 3 deletions
diff --git a/plugins/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/XbaseCompiler.java b/plugins/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/XbaseCompiler.java
index 025c9ea..2e5ab6a 100644
--- a/plugins/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/XbaseCompiler.java
+++ b/plugins/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/XbaseCompiler.java
@@ -1089,6 +1089,10 @@ public class XbaseCompiler extends FeatureCallCompiler {
b.append(" ");
String variableName = b.declareSyntheticVariable(closure, "_function");
b.append(variableName).append(" = ");
+ _toJavaStatement(closure, b, type).append(";").decreaseIndentation();
+ }
+
+ protected ITreeAppendable _toJavaStatement(final XClosure closure, final ITreeAppendable b, JvmTypeReference type) {
b.append("new ");
// TODO parameters in type arguments are safe to be a wildcard
serialize(type, closure, b, false, false, true, false);
@@ -1130,7 +1134,7 @@ public class XbaseCompiler extends FeatureCallCompiler {
} finally {
b.closeScope();
}
- b.decreaseIndentation().newLine().append("};").decreaseIndentation();
+ return b.decreaseIndentation().newLine().append("}");
}
protected void appendClosureParameter(JvmFormalParameter closureParam, JvmTypeReference parameterType, final XClosure closure,
@@ -1214,8 +1218,12 @@ public class XbaseCompiler extends FeatureCallCompiler {
return new StandardTypeParameterSubstitutor(mapping, owner).substitute(parameterType).toJavaCompliantTypeReference();
}
- protected void _toJavaExpression(final XClosure call, final ITreeAppendable b) {
- b.trace(call, false).append(getVarName(call, b));
+ protected void _toJavaExpression(final XClosure closure, final ITreeAppendable b) {
+ if (b.hasName(closure)) {
+ b.trace(closure, false).append(getVarName(closure, b));
+ } else {
+ _toJavaStatement(closure, b.trace(closure, false), getType(closure));
+ }
}
@Override
diff --git a/tests/org.eclipse.xtend.core.tests/src/org/eclipse/xtend/core/tests/compiler/CompilerTest.java b/tests/org.eclipse.xtend.core.tests/src/org/eclipse/xtend/core/tests/compiler/CompilerTest.java
index 0679730..dd594ea 100644
--- a/tests/org.eclipse.xtend.core.tests/src/org/eclipse/xtend/core/tests/compiler/CompilerTest.java
+++ b/tests/org.eclipse.xtend.core.tests/src/org/eclipse/xtend/core/tests/compiler/CompilerTest.java
@@ -3403,4 +3403,20 @@ public class CompilerTest extends AbstractXtendTestCase {
assertNotNull("Example code does not compile to java", javaCode);
}
+ /**
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=411973
+ */
+ @Test
+ public void testBug411973() throws Exception {
+ String code = "package bug411973 class Bug {val ()=>String init new() { this([|\"Hello World!\"]) }"
+ + "new(()=>String init) { this.init = init } @Override override toString() { init.apply } }";
+ Class<?> javaCode = compileJavaCode("bug411973.Bug", code);
+ assertNotNull("Example code does not compile to java", javaCode);
+
+ Constructor<?> constructor = javaCode.getDeclaredConstructor();
+ Object newInstance = constructor.newInstance();
+ Method toString = javaCode.getDeclaredMethod("toString");
+ assertEquals("Hello World!", toString.invoke(newInstance));
+ }
+
}