2

我正在为 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.Intjava.lang.Integer我使用scala.Int.unbox()的 . 是连接方式吗?而且我不知道如何转换java.lang.Integerscala.Int:在我输入的代码中伪代码SOME_TRANSFORMATION()。这个 SOME_TRANSFORMATION 的正确实现是什么?

谢谢

4

2 回答 2

6

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

scala.Int.box(3)Integer = 3

于 2013-02-18T20:34:43.650 回答
3

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

于 2013-02-19T05:11:05.460 回答