Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List conversion does not honor specified type #6

Open
juraj-blahunka opened this issue Jul 26, 2011 · 5 comments
Open

List conversion does not honor specified type #6

juraj-blahunka opened this issue Jul 26, 2011 · 5 comments

Comments

@juraj-blahunka
Copy link

Conversion succeeds, but objects stored in ids list are String instances "1", "2", "3" not 1L, 2L, 3L.

private Form {
    List<Long> ids = Lists.newArrayList();
    .. setters, getters
}

<form>
    <input type="text" name="ids" value="1" />
    <input type="text" name="ids" value="2" />
    <input type="text" name="ids" value="3" />
</form>
@wuhaixing
Copy link
Contributor

There is a type cast exception when use ArrayList Long

@juraj-blahunka
Copy link
Author

When I replace List<Long> with ArrayList<Long>, no exception gets raised

I'm using sitebricks 0.8.5

I'm getting ClassCastException only when I explicitly iterate over the posted collection (String cannot be Long), however when there is no usage of the ids collection inside the Form instance, only in the the template, no exception is thrown.

for (Long ex : ids) {
    System.out.println(ex);
}

@wuhaixing
Copy link
Contributor

I think following code from mvel org.mvel2.PropertyAccessor is the exception source:

if (curr instanceof Map) {
            //noinspection unchecked
            ((Map) curr).put(eval(ex, this.ctx, this.variableFactory), value);
          }
          else if (curr instanceof List) {
            //noinspection unchecked
            ((List) curr).set(eval(ex, this.ctx, this.variableFactory, Integer.class), value);
          }
          else if (hasPropertyHandler(curr.getClass())) {
            getPropertyHandler(curr.getClass()).setProperty(ex, ctx, variableFactory, value);
          }
          else if (curr.getClass().isArray()) {
            Array.set(curr, eval(ex, this.ctx, this.variableFactory, Integer.class), convert(value, getBaseComponentType(curr.getClass())));
          }

But I'm not sure yet

@wuhaixing
Copy link
Contributor

I just add a test to PropertyAccessTests in MVEL

  public class Datas {
         private List<Long> ids = new ArrayList<Long>();
         public void setIds(List<Long> ids) {
             this.ids = ids;
         }
         public List<Long> getIds() {
             return this.ids;
         }

  }
  public void testBindingConvert() {

      Datas datas = new Datas();
      List values = new ArrayList();
      values.add("1");
      values.add("2");
      values.add("3");
      MVEL.setProperty(datas, "ids", values);

      assertEquals(new Long(1),datas.getIds().get(0));

  }

and got this:

testBindingConvert(org.mvel2.tests.core.PropertyAccessTests): expected:<1> but was:<1>

@dhanji
Copy link
Owner

dhanji commented Apr 20, 2012

Have you tried registering a converter? Is this issue still happening?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants