tags:

views:

562

answers:

3

I'm trying to get a real equivalent for Java's public static final in Scala for using TwiP.

Creating a val in an object don't works for me, because it's part of a new generated class Example$.class and TwiP can't access it from class Example.class.

Here's an example of a Java Class I'm trying to port to Scala:

public static final String[] MY_STRINGS = { "A", "B", "C" };

@Test
public void myTest(@Values("MY_STRINGS") String string) {
  ...
}

But I don't know how to port the public static final to Scala. If it's a val in an object like here

@RunWith(classOf[TwiP])
class Foo {

  import Foo.MY_STRINGS

  @Test
  def testTwiP(@Values("MY_STRINGS") value: String): Unit = {
    println("I'm testing value " + value + ".")
  }

}

object Foo {
  val MY_STRINGS = Array("A", "B", "C")
}

I only get the following exception:

net.sf.twip.internal.TwipConfigurationError:
there is no method or field 'MY_STRINGS' named in the @Values annotation of Parameter#1

How can I solve the problem using Scala?

+1  A: 

If you use a var then you can create your own getter and setter, and if the value is already set, don't change it.

That may not be the best approach, but it would be helpful if you could explain why you want to use public static final on a variable, as a better solution might be more obvious then.

James Black
Hallo James, thanks for you're comment.I'm trying to make the following in Scala: public static final String[] MY_STRINGS = { "A", "B", "C" }; @Test public void myTest(@Values("MY_STRINGS") String string) { ... }http://twip.sourceforge.net/howto.htmlBut I don't know how to port the public static final to Scala. If it's a val in an object I only get the following: net.sf.twip.internal.TwipConfigurationError: there is no method or field 'myStrings' named in the @Values annotation of Parameter#1
renfis
@renfis - You may want to put this info in your question, formatted properly, to make it easier to read.
James Black
Thank you. I did it.
renfis
That is great. You may want to post your solution in case someone gets stuck.
James Black
+3  A: 
object Foo{
  val MY_STRINGS=Array("A","B","C")
}
class Foo{
  import Foo.MY_STRINGS
}

The val definition in the companion object creates your public static final variable, and the import declaration gives it a nice easy alias in the code you're using to write the class.

Note that the public static final variable in Scala still will compile to look like a static method call if you call this code from Java.

Edit: I'm slightly wrong because of a bug in Scala 2.7, which I demonstrate in detail in another answer.

Ken Bloom
Thank for the tipp. I tried it out and added it to my question. But it don't work for me. :(
renfis
This is because scala doesn't expose a variable as a variable -- it exposes it as a method. You need to find a version of the @values annotation that calls a method and uses the values returned by the method.
Ken Bloom
You mean, the actual version of TwiP will not work with Scala? It's a pity.
renfis
I'm going to give a new answer to this question, but it looks like this is http://lampsvn.epfl.ch/trac/scala/ticket/1735, and is fixed in Scala 2.8 snapshots.
Ken Bloom
+3  A: 

The following Scala code:

class Foo{
  import Bar.MY_STRINGS
}
object Bar{
  val MY_STRINGS=Array("A","B","C")
}

Generates the following Java classes:

public final class Bar extends java.lang.Object{
    public static final java.lang.String[] MY_STRINGS();
    public static final int $tag()       throws java.rmi.RemoteException;
}
public final class Bar$ extends java.lang.Object implements scala.ScalaObject{
    public static final Bar$ MODULE$;
    public static {};
    public Bar$();
    public java.lang.String[] MY_STRINGS();
    public int $tag()       throws java.rmi.RemoteException;
}
public class Foo extends java.lang.Object implements scala.ScalaObject{
    public Foo();
    public int $tag()       throws java.rmi.RemoteException;
}

The following Scala code:

class Foo{
  import Foo.MY_STRINGS
}
object Foo{
  val MY_STRINGS=Array("A","B","C")
}

Generates the following Java classes:

public class Foo extends java.lang.Object implements scala.ScalaObject{
    public Foo();
    public int $tag()       throws java.rmi.RemoteException;
}
public final class Foo$ extends java.lang.Object implements scala.ScalaObject{
    public static final Foo$ MODULE$;
    public static {};
    public Foo$();
    public java.lang.String[] MY_STRINGS();
    public int $tag()       throws java.rmi.RemoteException;
}

The fact that static members aren't defined on the class when the object has the same name as the class is Scala Bug #1735 and it's fixed in Scala 2.8 snapshots.

So it looks like TwiP isn't going to work at all unless you either upgrade Scala, or find a way to get TwiP to work with non-Static parameter generation methods.

Ken Bloom