【问题标题】:Instantiate scala.Int in Java在 Java 中实例化 scala.Int
【发布时间】:2013-02-03 09:04:56
【问题描述】:

我正在为 Play 2 框架的 SecureSocial 插件编写持久层。我在https://github.com/play-modules/modules.playframework.org/blob/master/app/models/ss/MPOOAuth2Info.java找到了一个例子:

package models.ss;

import models.AbstractModel;
import securesocial.core.java.OAuth2Info;
import javax.persistence.Entity;

@Entity
public class MPOOAuth2Info extends AbstractModel
{
    public String accessToken;

    public String tokenType;

    public Integer expiresIn;

    public String refreshToken;

    public MPOOAuth2Info()
    {
        // no-op
    }

    public MPOOAuth2Info(OAuth2Info oAuth2Info)
    {
        this.accessToken = oAuth2Info.accessToken;
        this.tokenType = oAuth2Info.tokenType;
        this.expiresIn = oAuth2Info.expiresIn;
        this.refreshToken = oAuth2Info.refreshToken;
    }

    public OAuth2Info toOAuth2Info()
    {
        OAuth2Info oAuth2Info = new OAuth2Info();

        oAuth2Info.accessToken = this.accessToken;
        oAuth2Info.tokenType = this.tokenType;
        oAuth2Info.expiresIn = this.expiresIn;
        oAuth2Info.refreshToken = this.refreshToken;

        return oAuth2Info;
    }
}

但 API 已更改,因此我无法使用 securesocial.core.java.OAuth2Info。 SecureSocial 是由 Scala 编写的,这个类是一个 Java 前端。所以我决定直接在哪里使用 Scala:

case class OAuth2Info(accessToken: String, tokenType: Option[String] = None,
                  expiresIn: Option[Int] = None, refreshToken: Option[String] = None)

我的结果:

package models.security.securesocial;

import models.AbstractModel;
import scala.Option;
import securesocial.core.*;

import javax.persistence.Entity;

/**
 * Persistence wrapper for SecureSocial's {@link } class.
 *
 * @author Steve Chaloner (steve@objectify.be)
 */
@Entity
public class MPOOAuth2Info extends AbstractModel
{
    public String accessToken;

    public String tokenType;

    public Integer expiresIn;

    public String refreshToken;

    public MPOOAuth2Info(){
        // no-op
    }

    public MPOOAuth2Info(OAuth2Info oAuth2Info){
        this.accessToken = oAuth2Info.accessToken();
        this.tokenType = oAuth2Info.tokenType().get();
        this.expiresIn = scala.Int.unbox(oAuth2Info.expiresIn().get());
        this.refreshToken = oAuth2Info.refreshToken().get();
    }

    public OAuth2Info toOAuth2Info(){
        return new OAuth2Info(accessToken, Option.apply(tokenType), Option.apply(SOME_TRANSFORMATION(expiresIn)), Option.apply(refreshToken));
    }
}

但我在将scala.Int 转换为java.lang.Integer 类型时遇到问题。 要将scala.Int 转换为java.lang.Integer,我使用了scala.Int.unbox()。是连接方式吗?而且我不知道如何将java.lang.Integer 转换为scala.Int:在我输入伪代码SOME_TRANSFORMATION() 的代码中。这个 SOME_TRANSFORMATION 的正确实现是什么?

谢谢

【问题讨论】:

  • 看源码,好像把一个scala.Int转换成java.lang.Integer就是scala.Int.box()

标签: java scala playframework-2.0 scala-java-interop securesocial


【解决方案1】:

scala.Int.unbox(new java.lang.Integer(3))Int = 3

scala.Int.box(3)Integer = 3

【讨论】:

    【解决方案2】:

    Scala 自动透明地装箱和拆箱。此外,scala.Int 是一种虚构。当存储在局部变量中、作为形式参数传递或存储在类中时,它是本机 int(à la Java / JVM)。但是当它必须通过参数化/泛型类型的值进行交易时,它必须被装箱和拆箱。 (有一个例外,当所讨论的类型参数是专门的,但这是另一回事。)

    【讨论】:

    • 我不敢,但现在你这么说。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 2012-07-16
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多