Skip to content

Commit

Permalink
adding the clone implementation, which currently doesn't check to see…
Browse files Browse the repository at this point in the history
… if the object is actually clone-able but does work on Arrays at least (the object vs. array implementations are different). This makes all unit tests pass along with the JDK, except for HashTable Test since it requires more complete reflection (getDeclaredFields).
  • Loading branch information
Drew Zagieboylo committed Aug 20, 2018
1 parent 146dc64 commit 09459b5
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
.settings/
.cproject
.project
.classpath
*~
22 changes: 21 additions & 1 deletion runtime/native/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "rep.h"
#include "gc.h"


#define ARRAY_CLS "polyllvm.runtime.Array"
// The name must match that used in polyllvm.runtime.Factory,
// and the mangling and calling conventions must match those used by PolyLLVM.
extern "C" {
Expand Down Expand Up @@ -49,3 +49,23 @@ jobject CreateJavaObject(jclass clazz) {
new_obj->SetCdv(reinterpret_cast<DispatchVector*>(info->cdv));
return new_obj->Wrap();
}

jobject CloneJavaObject(jobject obj) {
//TODO set exception if class is not cloneable
auto objRep = Unwrap(obj);
auto cdv = objRep->Cdv();
auto cls = cdv->Class()->Wrap();
auto info = GetJavaClassInfo(cls);
int size = 0;
jobject new_obj;
if (strcmp(info->name, ARRAY_CLS) == 0 || isArrayClass(cls)) {
JArrayRep* array = Unwrap(reinterpret_cast<jarray>(obj));
size = info->obj_size + (array->Length() * array->ElemSize());
new_obj = (jobject) GC_MALLOC(size);
} else {
new_obj = CreateJavaObject(cls);
size = info->obj_size;
}
memcpy(new_obj, obj, size);
return new_obj;
}
2 changes: 1 addition & 1 deletion runtime/native/factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobjectArray CreateJavaObjectArray (jint len);
jstring CreateJavaString(jcharArray chars);
// Objects.
jobject CreateJavaObject(jclass clazz);

jobject CloneJavaObject(jobject obj);
4 changes: 2 additions & 2 deletions runtime/native/jvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ JVM_MonitorNotifyAll(JNIEnv *env, jobject obj) {

jobject
JVM_Clone(JNIEnv *env, jobject obj) {
JvmUnimplemented("JVM_Clone");
return CloneJavaObject(obj);
}

jstring
Expand Down Expand Up @@ -781,7 +781,7 @@ JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused) {

jboolean
JVM_SupportsCX8(void) {
JvmUnimplemented("JVM_SupportsCX8");
return JNI_FALSE;
}

const char*
Expand Down
18 changes: 18 additions & 0 deletions tests/AnonymousSubClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.test.AbstractTest;
import java.test.Test;
import java.test.SubTest;

public class AnonymousSubClass {

public static void main(String[] args) {
AbstractTest at = new Test();
at.printMessage();
at = new SubTest();
at.printMessage();
SubTest st = new SubTest() {
public void doNothing(){ System.out.print(""); };
};
st.printMessage();

}
}
44 changes: 44 additions & 0 deletions tests/isolated/CastNull.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class CastNull {

static class InnerOne {

}

static interface InnerInterOne {
public Object objectify();
}

static class InnerTwo implements InnerInterOne {
public Object objectify() {
return this;
}


}

static class InnerThree implements InnerInterOne {
public Object objectify() {
return null;
}
}

public static void main(String[] args) {

InnerTwo t = null;
System.out.println(t == null);
t = new InnerTwo();
InnerInterOne in = (InnerInterOne)t.objectify();
System.out.println(in == null);
in = (InnerInterOne) new InnerThree().objectify();
System.out.println(in == null);
InnerOne ino = (InnerOne) new InnerThree().objectify();
System.out.println(ino == null);
try {
Object o = new InnerTwo();
InnerOne o1 = (InnerOne) new InnerTwo().objectify();
} catch (ClassCastException e) {
System.out.println("caught exception");
}
}

}
7 changes: 7 additions & 0 deletions tests/isolated/ClassTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class ClassTest {

public static void main(String[] args) throws Exception {
// Class<?> clazz = Class.forName("java.lang.Object");
System.out.println(Object.class.getName());
}
}

0 comments on commit 09459b5

Please sign in to comment.