This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Using Scala and Click framework

6 replies
blueneogeo
Joined: 2009-01-18,
User offline. Last seen 42 years 45 weeks ago.

Hi, I am unable to get scala to work with the Java Click Framework, and I
wondered if this is because scala does not support reflection?

I am just getting started with Scala, and I was already using the Click
framework (http://incubator.apache.org/click/) for a Java application. Since
Scala is Java backwards compatible I figured it should be possible to create
a Click Webpage using a Scala class.

However Click uses public members in a Page class to recieve URL parameters
and to pass members to a velocity template. So a Click webpage looks
something like this in Java:

public class TestPage extends Page {
public int id;
// other code
}

and the id gets filled in with 12 via reflection if I request
http://...../test.htm?id=12 . It will also pass this property to the
velocity template so in the template I am able to display it.

I did something like this in scala:

class TestPage extends Page {
var id
}

This does not seem to work at all, I get no errors but nothing gets set when
I get the URL, and if I hardcode the id, I cannot request it from the
template.

So does this mean I cannot use Scala to develop in click, and if it is
reflection, is this coming soon in Scala?

Best regards,

Christian

James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: Using Scala and Click framework
Scala supports the same way Java does.  That's not the issue.

The problem is that Scala does not emit public fields in its generated byte code.  If  you have a public var then Scala emits a private field and a public getter/setter pair.  Is there no way to make click read an annotation to determine which fields are accessible?  Or can it read Java bean style getX/setX methods generated by @BeanProperty?  If not, you may have to do that portion of your application in Java.  For instance

// Java
public abstract class TestPageBase extends Page {
  public int id;
}

// scala
class TestPageImpl extends TestPageBase {
  // other code
}

Note that other frameworks work just fine with Scala

http://fanf42.blogspot.com/2009/01/t5-scala-first-injection-and-property.html
http://javajmc.blogspot.com/2008/02/simple-scala-spring-and-webworkstruts2.html

On Sun, Jan 18, 2009 at 8:10 AM, blueneogeo <christian [dot] vogel [dot] private [at] gmail [dot] com> wrote:

Hi, I am unable to get scala to work with the Java Click Framework, and I
wondered if this is because scala does not support reflection?

I am just getting started with Scala, and I was already using the Click
framework (http://incubator.apache.org/click/) for a Java application. Since
Scala is Java backwards compatible I figured it should be possible to create
a Click Webpage using a Scala class.

However Click uses public members in a Page class to recieve URL parameters
and to pass members to a velocity template. So a Click webpage looks
something like this in Java:

public class TestPage extends Page {
  public int id;
  // other code
}

and the id gets filled in with 12 via reflection if I request
http://...../test.htm?id=12 . It will also pass this property to the
velocity template so in the template I am able to display it.

I did something like this in scala:

class TestPage extends Page {
  var id
}

This does not seem to work at all, I get no errors but nothing gets set when
I get the URL, and if I hardcode the id, I cannot request it from the
template.

So does this mean I cannot use Scala to develop in click, and if it is
reflection, is this coming soon in Scala?

Best regards,

Christian


--
View this message in context: http://www.nabble.com/Using-Scala-and-Click-framework-tp21529439p21529439.html
Sent from the Scala - User mailing list archive at Nabble.com.


blueneogeo
Joined: 2009-01-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Using Scala and Click framework

Right... I see your point. I don't believe Click can do any of the things you
mentioned. I've been hunting through the source code and didn't see any such
options. The BasePage like option you mentioned isn't a solution here
either, since properties differ for each page. So to use Click in Scala I
would have to hack Click itself. Either that or give up Scala for Click.

Thanks for the reply, at least now I know where to look :-)

James Iry-2 wrote:
>
> Scala supports the same way Java does. That's not the issue.
>
> The problem is that Scala does not emit public fields in its generated
> byte
> code. If you have a public var then Scala emits a private field and a
> public getter/setter pair. Is there no way to make click read an
> annotation
> to determine which fields are accessible? Or can it read Java bean style
> getX/setX methods generated by @BeanProperty?
>

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Using Scala and Click framework


On Sun, Jan 18, 2009 at 6:30 PM, blueneogeo <christian [dot] vogel [dot] private [at] gmail [dot] com> wrote:

Right... I see your point. I don't believe Click can do any of the things you
mentioned. I've been hunting through the source code and didn't see any such
options. The BasePage like option you mentioned isn't a solution here
either, since properties differ for each page. So to use Click in Scala I
would have to hack Click itself. Either that or give up Scala for Click.

Since Click! is open source, you should really spend 30-40 minutes adding BeanProperty support to it. (if field X exists, use that, if method setX exists, use that)
That way Click!, Scala and you have benefited.

Cheers,
Viktor
 


Thanks for the reply, at least now I know where to look :-)



James Iry-2 wrote:
>
> Scala supports the same way Java does.  That's not the issue.
>
> The problem is that Scala does not emit public fields in its generated
> byte
> code.  If  you have a public var then Scala emits a private field and a
> public getter/setter pair.  Is there no way to make click read an
> annotation
> to determine which fields are accessible?  Or can it read Java bean style
> getX/setX methods generated by @BeanProperty?
>

--
View this message in context: http://www.nabble.com/Using-Scala-and-Click-framework-tp21529439p21530474.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Viktor Klang
Senior Systems Analyst
James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: Using Scala and Click framework
Sorry, wasn't clear.  What I mean was have a Java base class per page plus a Scala extension per page, or at least per page that has "interesting" enough code to be worth the effort.

Still, the best answer, IMHO, is to hack on Click and submit back to the community.  Requiring public fields is too strict and inflexible.

On Sun, Jan 18, 2009 at 9:30 AM, blueneogeo <christian [dot] vogel [dot] private [at] gmail [dot] com> wrote:

  The BasePage like option you mentioned isn't a solution here
either, since properties differ for each page. So to use Click in Scala I
would have to hack Click itself. Either that or give up Scala for Click.


blueneogeo
Joined: 2009-01-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Using Scala and Click framework

I was thinking that, and knowing that I can access the getters and setters
via reflection helps a lot. Thanks :-)

Viktor Klang wrote:
>
> On Sun, Jan 18, 2009 at 6:30 PM, blueneogeo <
> christian [dot] vogel [dot] private [at] gmail [dot] com> wrote:
>
>>
>> Right... I see your point. I don't believe Click can do any of the things
>> you
>> mentioned. I've been hunting through the source code and didn't see any
>> such
>> options. The BasePage like option you mentioned isn't a solution here
>> either, since properties differ for each page. So to use Click in Scala I
>> would have to hack Click itself. Either that or give up Scala for Click.
>
>
> Since Click! is open source, you should really spend 30-40 minutes adding
> BeanProperty support to it. (if field X exists, use that, if method setX
> exists, use that)
> That way Click!, Scala and you have benefited.
>
> Cheers,
> Viktor
>
>
>>
>>
>> Thanks for the reply, at least now I know where to look :-)
>>
>>
>>
>> James Iry-2 wrote:
>> >
>> > Scala supports the same way Java does. That's not the issue.
>> >
>> > The problem is that Scala does not emit public fields in its generated
>> > byte
>> > code. If you have a public var then Scala emits a private field and a
>> > public getter/setter pair. Is there no way to make click read an
>> > annotation
>> > to determine which fields are accessible? Or can it read Java bean
>> style
>> > getX/setX methods generated by @BeanProperty?
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Using-Scala-and-Click-framework-tp21529439p2153047...
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Using Scala and Click framework

On Sun, Jan 18, 2009 at 03:46:37PM -0800, James Iry wrote:
> Still, the best answer, IMHO, is to hack on Click and submit back to the
> community. Requiring public fields is too strict and inflexible.

Incidentally, this is how JNA (https://jna.dev.java.net/) works too. I wrote FUSE bindings in
scala but I had to write the classes which bridge between C and the jvm in java and extend them
in scala because it determines the size of structures by counting up the sizes of your public
fields. Made me sort of wish there was a secret scala backdoor to publicness (hmm, I was going
to write "publicity" there.)

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland