[英]Use Scala constants in Java
I am currently evaluating Scala for future projects and came across something strange. 我目前正在评估Scala的未来项目,并遇到了一些奇怪的事情。 I created the following constant for us in a JSP: 我在JSP中为我们创建了以下常量:
val FORMATED_TIME = "formatedTime";
And it did not work. 它没有用。 After some experimenting I decided to decompile to get to the bottom of it: 经过一些实验,我决定反编译以找到它的底部:
private final java.lang.String FORMATED_TIME;
public java.lang.String FORMATED_TIME();
Code:
0: aload_0
1: getfield #25; //Field FORMATED_TIME:Ljava/lang/String;
4: areturn
Now that is interesting! 现在这很有趣! Personally I have been wondering for quite a while why an inspector needs the prefix get and a mutator the prefix set in Java as they live in different name-spaces. 就个人而言,我一直想知道为什么一个检查员需要前缀get和一个mutator在Java中设置的前缀,因为它们位于不同的名称空间中。
However it might still be awkward to explain that to the rest of the team. 然而,向团队的其他成员解释这可能仍然很尴尬。 So is it possible to have a public constant without the inspector? 那么没有检查员就可以有一个公共常数吗?
This is because of the Uniform Access Principle , ie: Methods and Fields Are Indistinguishable 这是因为统一访问原则 ,即:方法和字段无法区分
See this answer 看到这个答案
In Scala 2.8.0 this means if you have a companion object, you lose your static forwarders) 在Scala 2.8.0中,这意味着如果您有一个伴随对象,则会丢失静态转发器)
If you have this in Scala: 如果你在Scala中有这个:
//Scala
object CommonControler {
val FORMATED_TIME = "formatedTime";
}
You may use it like this from Java 你可以在Java中使用它
//Java
// Variables become methods
CommonControler$.MODULE$.FORMATED_TIME();
// A static forwarder is avaliable
CommonControler.FORMATED_TIME();
Also see the book Scala in Depth 另请参阅Scala in Depth一书
Also note the @scala.reflect.BeanProperty for classes. 另请注意类的@ scala.reflect.BeanProperty 。
I had a further look into the decompiled code and noted something else. 我进一步研究了反编译代码,并注意到了其他一些东西。 The variables are not actually static. 变量实际上不是静态的。 So my next idea was to use an object instead: 所以我的下一个想法是使用一个对象:
object KommenControler
{
val FORMATED_TIME = "formatedTime";
} // KommenControler
But now things turn really ugly: 但现在事情变得非常丑陋:
public final class ….KommenControler$ extends java.lang.Object implements scala.ScalaObject{
public static final ….KommenControler$ MODULE$;
private final java.lang.String FORMATED_TIME;
public static {};
Code:
0: new #9; //class …/KommenControler$
3: invokespecial #12; //Method "<init>":()V
6: return
public java.lang.String FORMATED_TIME();
Code:
0: aload_0
1: getfield #26; //Field FORMATED_TIME:Ljava/lang/String;
4: areturn
So I get an additional class ending on $ which has a singleton instance called MOUDLE$. 所以我得到一个以$结尾的额外类,它有一个名为MOUDLE $的单例实例。 And there is still the inspector. 还有检查员。 So the access to the variable inside a jsp becomes: 因此,对jsp内变量的访问变为:
final String formatedTime = (String) request.getAttribute (….KommenControler$.MODULE$.FORMATED_TIME ());
This works as expected and I personally can live with it but how am I going to explain that to the team? 这可以按预期工作,我个人可以忍受它,但我将如何向团队解释?
Of course if there is a simpler way I like to hear of it. 当然,如果有一种更简单的方式我喜欢听到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.