samedi 23 février 2019

Scala extend trait field not found

I have a scala trait with a public UUID that has a default value:

trait pet {
    var uuid_ : UUID = UUID.randomUUID
}

now I am creating multiple classes, also in scala:

class dog extends pet {
    var foo = 1
}
class cat extends pet {
}
class fish extends pet {
}

After that I created a method in Java (old project with both languages mixed).
Here the snipped with my problem. In the variable somePet is an instance of dog, cat or fish. But it is not clear what of them exactly:

// printing all variables in the console for human testing
Serializer.printAllFields(somePet);

// The somePet Variable must be a pet
if(!pet.class.isAssignableFrom(somePet.getClass()))
    throw new Exception("Not a pet.");

// get the UUID of the pet
UUID uuid_;
try {
    Field f = pet.class.getField("uuid_");
    f.setAccessible(true);
    uuid_ = (UUID) f.get(somePet);
}catch(Exception e){
    // no uuid found
    throw e;
}

But when I run the code I get the following error:

Exception in thread "main" java.lang.NoSuchFieldException: uuid_

And the stacktrace points on the line with Field f = pet.class.getField("uuid_");.
But what is wrong with the code?
An alternative I thought was replacing this exact line with:

Field f = ntObj.getClass().getField("uuid_");

But this also fails.
So where is the variable uuid_?
Because when I print out all variables in the console of the current somePet with a Serializer, I get something like

* cat.uuid_ = 34d7a781-472d-4d98-861e-7cff08045445;

or

* dog.foo = 1
* dog.uuid_ = 34d7a781-472d-4d98-861e-7cff08045445;

in the console.
So the variable uuid_ is there with a default value.
(I am using the serializer Serializer.printAllFields(somePet); of the java util.)

So how do I get the uuid_ variable in my java snippet?





Aucun commentaire:

Enregistrer un commentaire