Re: Cannot reflectively create enum objects?

408 views
Skip to first unread message

Arnav Roy

unread,
Jul 18, 2011, 2:33:14 PM7/18/11
to play-framework
Forgot to mention a few things, my conf/dependencies.yaml looks like the following:

# Application dependencies

require:
    - play -> play [1.2.1,)
    - play -> siena [2.0.0,)
    - play -> gae 1.4


On Mon, Jul 18, 2011 at 11:59 PM, Arnav <roy....@gmail.com> wrote:
Hi Players,

I'm new to playframework. I am trying to do the following:

// code snippet begins

public class Learner extends Model {
       @Id(Generator.AUTO_INCREMENT)
       public Long id;
       public String email;
       public String password;
       public String fullname;
       public boolean isAdmin;

       @Embedded
       public List<Language> languagesKnown;

   public Learner(String email, String password, String fullname) {
               this.email = email;
               this.password = password;
               this.fullname = fullname;
               languagesKnown = new ArrayList<Language>();
       }
}

@EmbeddedMap
public enum Language {
       ENGLISH,
       FRENCH;
}

public class BasicTest extends UnitTest {

   @Test
   public void createAndRetrieveUser() {
       // Create a new user and save it
       Learner bob = new Learner("bo...@gmail.com", "secret", "Bob");
       bob.languagesKnown.add(Language.ENGLISH);
       bob.languagesKnown.add(Language.FRENCH);
       bob.save();
       Learner bobToBeTested =
Model.all(Learner.class).filter("email",
                       "bo...@gmail.com").get();

       // Test
       assertNotNull(bobToBeTested);
       assertEquals("Bob", bobToBeTested.fullname);
       assertNotNull(bobToBeTested.languagesKnown);
       assertEquals(Language.ENGLISH,
(Language)bobToBeTested.languagesKnown.get(0));
   }

}

// code snippet ends

The test fails at  assertEquals(Language.ENGLISH,
(Language)bobToBeTested.languagesKnown.get(0));
saying :

A siena.SienaException has been caught, siena.SienaException:
siena.SienaException: java.lang.IllegalArgumentException: Cannot
reflectively create enum objects
@ "bo...@gmail.com").get();

How do I fix this?

Thanks in advance.


Arnav

unread,
Jul 18, 2011, 2:29:47 PM7/18/11
to play-framework

Pascal Voitot Dev

unread,
Jul 18, 2011, 3:44:34 PM7/18/11
to play-fr...@googlegroups.com
Hi,

First of all,

assertEquals(Language.ENGLISH,  (Language)bobToBeTested.languagesKnown.get(0));

I think it tries to cast bobToBeTested to a Language enum so you get the Exception encapsulated in A runtime SienaException.
Try like that:
assertEquals(Language.ENGLISH,  (Language)(bobToBeTested.languagesKnown.get(0)));

But, you should also remove the @EmbeddedMap in your Enum because I don't think it's useful. an enum can't be represented as a map IMHO.
Basically, I'm not even sure of the result of using an enum in an embedded list (no unit test of this case in Siena).
So if you still have a problem, I will exam this precisely!

regards
Pascal

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Arnav Roy

unread,
Jul 19, 2011, 12:39:47 PM7/19/11
to play-fr...@googlegroups.com
Hi Pascal,

assertEquals(Language.ENGLISH,  (Language)(bobToBeTested.languagesKnown.get(0)));
fails with the same exception.

Can't use @EmbeddedList because it requires @At(...) annotation.

Probably switching to something other than an enum for now.

Thanks.

Pascal Voitot Dev

unread,
Jul 19, 2011, 12:43:34 PM7/19/11
to play-fr...@googlegroups.com
in fact, no annotation at all would be better IMO but I don't know what it does!
I should try this case to know :)

Did you try to debug to know what you get in bobToBeTested.languagesKnow ?

Pascal

Arnav Roy

unread,
Jul 19, 2011, 1:12:19 PM7/19/11
to play-fr...@googlegroups.com

Without the annotations, when I try to save:
A java.lang.IllegalArgumentException has been caught, languagesKnown: models.Language is not a supported property type.
Reply all
Reply to author
Forward
0 new messages