提交 aba13415 编写于 作者: D Dmitry Jemerov

failing test for property in class

上级 ec9018f2
...@@ -45,7 +45,7 @@ public class PropertyCodegen { ...@@ -45,7 +45,7 @@ public class PropertyCodegen {
} }
public void gen(JetProperty p, OwnerKind kind) { public void gen(JetProperty p, OwnerKind kind) {
if (kind == OwnerKind.NAMESPACE) { if (kind == OwnerKind.NAMESPACE || kind == OwnerKind.IMPLEMENTATION) {
final VariableDescriptor descriptor = context.getVariableDescriptor(p); final VariableDescriptor descriptor = context.getVariableDescriptor(p);
if (!(descriptor instanceof PropertyDescriptor)) { if (!(descriptor instanceof PropertyDescriptor)) {
throw new UnsupportedOperationException("expect a property to have a property descriptor"); throw new UnsupportedOperationException("expect a property to have a property descriptor");
...@@ -59,8 +59,11 @@ public class PropertyCodegen { ...@@ -59,8 +59,11 @@ public class PropertyCodegen {
value = ((JetConstantExpression) initializer).getValue(); value = ((JetConstantExpression) initializer).getValue();
} }
} }
v.visitField(Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE, int modifiers = Opcodes.ACC_PRIVATE;
p.getName(), if (kind == OwnerKind.NAMESPACE) {
modifiers |= Opcodes.ACC_STATIC;
}
v.visitField(modifiers, p.getName(),
mapper.mapType(descriptor.getOutType()).getDescriptor(), mapper.mapType(descriptor.getOutType()).getDescriptor(),
null, value); null, value);
} }
......
class PrivateVar {
private var x = 0;
fun setValueOfX(val: Int) { x = val }
fun getValueOfX() = x
}
...@@ -16,6 +16,20 @@ import java.util.List; ...@@ -16,6 +16,20 @@ import java.util.List;
* @author yole * @author yole
*/ */
public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase { public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
private MyClassLoader myClassLoader;
@Override
protected void setUp() throws Exception {
super.setUp();
myClassLoader = new MyClassLoader(NamespaceGenTest.class.getClassLoader());
}
@Override
protected void tearDown() throws Exception {
myClassLoader = null;
super.tearDown();
}
protected void loadText(final String text) { protected void loadText(final String text) {
myFixture.configureByText(JetFileType.INSTANCE, text); myFixture.configureByText(JetFileType.INSTANCE, text);
} }
...@@ -55,8 +69,7 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase { ...@@ -55,8 +69,7 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
for (String file : files) { for (String file : files) {
if (file.equals(fqName.replace('.', '/') + ".class")) { if (file.equals(fqName.replace('.', '/') + ".class")) {
final byte[] data = state.asBytes(file); final byte[] data = state.asBytes(file);
MyClassLoader classLoader = new MyClassLoader(NamespaceGenTest.class.getClassLoader()); return myClassLoader.doDefineClass(fqName, data);
return classLoader.doDefineClass(fqName, data);
} }
} }
...@@ -81,6 +94,10 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase { ...@@ -81,6 +94,10 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
protected Method generateFunction(String name) { protected Method generateFunction(String name) {
Class aClass = generateNamespaceClass(); Class aClass = generateNamespaceClass();
return findMethodByName(aClass, name);
}
protected static Method findMethodByName(Class aClass, String name) {
for (Method method : aClass.getMethods()) { for (Method method : aClass.getMethods()) {
if (method.getName().equals(name)) { if (method.getName().equals(name)) {
return method; return method;
...@@ -89,11 +106,16 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase { ...@@ -89,11 +106,16 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
throw new IllegalArgumentException("couldn't find method " + name); throw new IllegalArgumentException("couldn't find method " + name);
} }
protected void assertIsCurrentTime(long returnValue) { protected static void assertIsCurrentTime(long returnValue) {
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
assertTrue(Math.abs(returnValue - currentTime) <= 1L); assertTrue(Math.abs(returnValue - currentTime) <= 1L);
} }
protected Class loadImplementationClass(Codegens codegens, final String name) {
loadClass(name, codegens);
return loadClass(name + "$$Impl", codegens);
}
private static class MyClassLoader extends ClassLoader { private static class MyClassLoader extends ClassLoader {
public MyClassLoader(ClassLoader parent) { public MyClassLoader(ClassLoader parent) {
super(parent); super(parent);
......
...@@ -11,7 +11,21 @@ public class PropertyGenTest extends CodegenTestCase { ...@@ -11,7 +11,21 @@ public class PropertyGenTest extends CodegenTestCase {
public void testPrivateVal() throws Exception { public void testPrivateVal() throws Exception {
loadFile("privateVal.jet"); loadFile("privateVal.jet");
System.out.println(generateToText()); System.out.println(generateToText());
// TODO final Class aClass = loadImplementationClass(generateClassesInFile(), "PrivateVal");
final Field[] fields = aClass.getDeclaredFields();
assertEquals(1, fields.length);
final Field field = fields[0];
assertEquals("prop", field.getName());
}
public void testPrivateVar() throws Exception {
loadFile("privateVar.jet");
final Class aClass = loadImplementationClass(generateClassesInFile(), "PrivateVar");
final Object instance = aClass.newInstance();
Method setter = findMethodByName(aClass, "setValueOfX");
setter.invoke(instance, 239);
Method getter = findMethodByName(aClass, "getValueOfX");
assertEquals(239, ((Integer) getter.invoke(instance)).intValue());
} }
public void testPropertyInNamespace() throws Exception { public void testPropertyInNamespace() throws Exception {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册