TypeInfo.getClassObject(), for now implemented via reflection
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
class Point() {
|
||||
class object {
|
||||
fun foo(): String = "bar"
|
||||
}
|
||||
}
|
||||
|
||||
fun foo() = Point()
|
||||
@@ -104,6 +104,16 @@ public class TypeInfoTest extends CodegenTestCase {
|
||||
blackBoxFile("typeInfo/forwardTypeParameter.jet");
|
||||
}
|
||||
|
||||
public void testClassObjectInTypeInfo() throws Exception {
|
||||
loadFile();
|
||||
Method foo = generateFunction();
|
||||
JetObject jetObject = (JetObject) foo.invoke(null);
|
||||
TypeInfo<?> typeInfo = jetObject.getTypeInfo();
|
||||
final Object object = typeInfo.getClassObject();
|
||||
final Method classObjFoo = object.getClass().getMethod("foo");
|
||||
assertNotNull(classObjFoo);
|
||||
}
|
||||
|
||||
private Runnable newRunnable() {
|
||||
return new Runnable() {
|
||||
@Override
|
||||
|
||||
@@ -2,6 +2,7 @@ package jet.typeinfo;
|
||||
|
||||
import jet.JetObject;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
@@ -26,6 +27,17 @@ public class TypeInfo<T> implements JetObject {
|
||||
this.typeParameters = typeParameters;
|
||||
}
|
||||
|
||||
public Object getClassObject() {
|
||||
try {
|
||||
final Class implClass = theClass.getClassLoader().loadClass(theClass.getCanonicalName() + "$$Impl");
|
||||
final Field classobj = implClass.getField("$classobj");
|
||||
return classobj.get(null);
|
||||
}
|
||||
catch(Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInstance(Object obj) {
|
||||
if (obj instanceof JetObject) {
|
||||
return isSubtypeOf(((JetObject) obj).getTypeInfo());
|
||||
|
||||
Reference in New Issue
Block a user