failing test for property in class

This commit is contained in:
Dmitry Jemerov
2011-04-21 15:42:51 +02:00
parent ec9018f209
commit aba13415c7
4 changed files with 52 additions and 7 deletions
@@ -45,7 +45,7 @@ public class PropertyCodegen {
}
public void gen(JetProperty p, OwnerKind kind) {
if (kind == OwnerKind.NAMESPACE) {
if (kind == OwnerKind.NAMESPACE || kind == OwnerKind.IMPLEMENTATION) {
final VariableDescriptor descriptor = context.getVariableDescriptor(p);
if (!(descriptor instanceof PropertyDescriptor)) {
throw new UnsupportedOperationException("expect a property to have a property descriptor");
@@ -59,8 +59,11 @@ public class PropertyCodegen {
value = ((JetConstantExpression) initializer).getValue();
}
}
v.visitField(Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE,
p.getName(),
int modifiers = Opcodes.ACC_PRIVATE;
if (kind == OwnerKind.NAMESPACE) {
modifiers |= Opcodes.ACC_STATIC;
}
v.visitField(modifiers, p.getName(),
mapper.mapType(descriptor.getOutType()).getDescriptor(),
null, value);
}
+6
View File
@@ -0,0 +1,6 @@
class PrivateVar {
private var x = 0;
fun setValueOfX(val: Int) { x = val }
fun getValueOfX() = x
}
@@ -16,6 +16,20 @@ import java.util.List;
* @author yole
*/
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) {
myFixture.configureByText(JetFileType.INSTANCE, text);
}
@@ -55,8 +69,7 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
for (String file : files) {
if (file.equals(fqName.replace('.', '/') + ".class")) {
final byte[] data = state.asBytes(file);
MyClassLoader classLoader = new MyClassLoader(NamespaceGenTest.class.getClassLoader());
return classLoader.doDefineClass(fqName, data);
return myClassLoader.doDefineClass(fqName, data);
}
}
@@ -81,6 +94,10 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
protected Method generateFunction(String name) {
Class aClass = generateNamespaceClass();
return findMethodByName(aClass, name);
}
protected static Method findMethodByName(Class aClass, String name) {
for (Method method : aClass.getMethods()) {
if (method.getName().equals(name)) {
return method;
@@ -89,11 +106,16 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
throw new IllegalArgumentException("couldn't find method " + name);
}
protected void assertIsCurrentTime(long returnValue) {
protected static void assertIsCurrentTime(long returnValue) {
long currentTime = System.currentTimeMillis();
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 {
public MyClassLoader(ClassLoader parent) {
super(parent);
@@ -11,7 +11,21 @@ public class PropertyGenTest extends CodegenTestCase {
public void testPrivateVal() throws Exception {
loadFile("privateVal.jet");
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 {