protected int parseExpire(CacheContext ctx) throws AttributeDefineException {
Method targetMethod = ctx.getTargetMethod();
CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class);
ExpireExpr cacheExpire = targetMethod.getAnnotation(ExpireExpr.class);
// check for duplicate setting
if (cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE && cacheExpire != null) {
throw new AttributeDefineException("expire are defined both in @CacheEnable and @ExpireExpr");
}
// expire time defined in @CacheEnable or @ExpireExpr
return cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE ? cacheEnable.expire() : parseExpireExpr(cacheExpire, ctx.getArgument());
}
Run Code Online (Sandbox Code Playgroud)
这是测试的方法,
Method targetMethod = ctx.getTargetMethod();
CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class);
Run Code Online (Sandbox Code Playgroud)
我必须模拟三个CacheContext,Method和CacheEnable.有什么想法让测试用例更简单吗?
我现在正在使用emacs前奏,并发现一切都很好.但
编辑el文件时会显示很多flycheck警告
和语法着色被覆盖
警告如"第一行应该是表格包---摘要"
如何关闭这些警告?
起源
{
"first_name" : "foo",
"last_name" : "bar",
"parent" : {
"first_name" : "baz",
"last_name" : "bazz",
}
}
Run Code Online (Sandbox Code Playgroud)
预期
{
"firstName" : "foo",
"lastName" : "bar",
"parent" : {
"firstName" : "baz",
"lastName" : "bazz",
}
}
Run Code Online (Sandbox Code Playgroud)
如何转换json对象的所有键?
我定义了以下宏来将case字段转换为map
import scala.language.experimental.macros
import scala.reflect.macros.Context
def asMap_impl[T: c.WeakTypeTag](c: Context)(t: c.Expr[T]) = {
import c.universe._
val mapApply = Select(reify(Map).tree, newTermName("apply"))
val pairs = weakTypeOf[T].declarations.collect {
case m: MethodSymbol if m.isCaseAccessor =>
val name = c.literal(m.name.decoded)
val value = c.Expr(Select(t.tree, m.name))
reify(name.splice -> value.splice).tree
}
c.Expr[Map[String, Any]](Apply(mapApply, pairs.toList))
}
Run Code Online (Sandbox Code Playgroud)
并且方法实现
def asMap[T](t: T) = macro asMap_impl[T]
Run Code Online (Sandbox Code Playgroud)
然后我定义一个案例类来测试它
case class User(name : String)
Run Code Online (Sandbox Code Playgroud)
它工作正常(使用scala repl):
scala> asMap(User("foo")) res0:
scala.collection.immutable.Map[String,String] = Map(name -> foo)
Run Code Online (Sandbox Code Playgroud)
但是当我用另一个泛型方法包装此方法时
def printlnMap[T](t: T) = println(asMap(t))
Run Code Online (Sandbox Code Playgroud)
此方法始终打印空地图:
scala> printlnMap(User("foo"))
Map() …
Run Code Online (Sandbox Code Playgroud) 我注意到默认的tomcat 7线程池大小似乎是200.
但正常的CPU似乎有16个核心.
因此只能执行16个线程并行执行
为什么tomcat使用这么多线程.
我从Play
控制器那里得到一张表格
class CustomerTable(tag: Tag) extends Table[Customer]("customer", tag) {
def id = column[Long]("id")
...
def * = ...
}
case class Customer(
id: Long,
remarkName: String
...
)
case class CustomerUpdateForm(
id: Long,
remarkName: Option[String],
realName: Option[String],
birth: Option[Date],
address: Option[String],
phone: Option[String],
qq: Option[String],
email: Option[String])
Run Code Online (Sandbox Code Playgroud)
我想用nonEmpty
表格的字段更新数据库,这是我的方式
def updateField(form: CustomerUpdateForm) = {
def updateField0[T](
field: CustomerTable => Column[T],
value: T) = {
customerTable
.filer(_.id, form.id)
.map(c => field(c) -> c.gmtModified)
.update(value -> new Date)
}
var …
Run Code Online (Sandbox Code Playgroud) 我想实现proxy
一些特征A
(例如委托方法调用到一些 rpc 调用),像这样
def clientProxy[A](using Type[A], Quotes): Expr[A] = {
import quotes.reflect._
val defTrees: List[Tree] = TypeRepr.of[A].typeSymbol.memberFields.collect {
case mf if mf.isDefDef =>
???
}
val exprs = Expr.ofList(defTrees.map(_.asExpr))
'{
new A {
$exprs
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是编译器抱怨
A is not a class type
Run Code Online (Sandbox Code Playgroud) 我想查看akka的源代码,它似乎使用0.7.x版本的sbt.
我将项目配置转换为0.10.x版本.当我在libraray-dependencies中添加sbteclipse并运行eclipse create-src来生成eclipse项目时,它告诉eclipse不是命令.这是我的Build.scala.当我使用库依赖项列出库时,sbteclipse_2.8.1.jar实际上是存在的
我之前犯了一个愚蠢的错误.插件定义应在project/plugins/build.sbt文件中声明,而不是作为project/build.sbt中的库依赖项
import sbt._
import Keys._
object Settings {
import Dependencies._
import Resolvers._
val buildScalaVersion = "2.9.0-1"
val buildSettings = Defaults.defaultSettings ++ Seq(
scalaVersion := buildScalaVersion,
libraryDependencies := Depns,
resolvers := Repos)
}
object Resolvers {
/**
* repos definition
*/
lazy val Repos = Seq(
AkkaRepo,
CodehausRepo,
GuiceyFruitRepo,
JBossRepo,
JavaNetRepo,
SonatypeSnapshotRepo,
GlassfishRepo,
ScalaToolsRelRepo,
ScalaToolsSnapshotRepo,
TypeSafeRepo)
lazy val AkkaRepo = "Akka Repository" at "http://akka.io/repository"
lazy val CodehausRepo = "Codehaus Repo" at "http://repository.codehaus.org"
lazy val GuiceyFruitRepo = "GuiceyFruit Repo" …
Run Code Online (Sandbox Code Playgroud) 我正在使用scala.Serializable,但是当我调用String.asInstanceOf [Serializable]时,抛出了类强制转换异常.这是我的代码,非常简单.
arguments.map(_.asInstanceOf[Serializable])
Run Code Online (Sandbox Code Playgroud)
是的,参数是字符串数组当然
我用scala-ide运行应用程序eclipse与eclipse 3.7和scala 2.9.0-1
查看此文档scala doc
问题现在是"scala.Serializable的典型用例是什么"
testOnly play.api.weibo.StatusesShowBatchSpec
[error] Could not create an instance of play.api.weibo.StatusesShowBatchSpec
[error] caused by java.lang.Exception: Could not instantiate class play.api.weibo.StatusesShowBatchSpec: null
[error] org.specs2.reflect.Classes$class.tryToCreateObjectEither(Classes.scala:93)
[error] org.specs2.reflect.Classes$.tryToCreateObjectEither(Classes.scala:211)
[error] org.specs2.specification.SpecificationStructure$$anonfun$createSpecificationEither$2.apply(BaseSpecification.scala:119)
[error] org.specs2.specification.SpecificationStructure$$anonfun$createSpecificationEither$2.apply(BaseSpecification.scala:119)
...
Run Code Online (Sandbox Code Playgroud)
规范
package play.api.weibo
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner
class StatusesShowBatchSpec extends ApiSpec {
"'statuses show batch' api" should {
"read statuses" in {
val api = StatusesShowBatch(
accessToken = testAdvancedToken,
ids = "3677163356078857")
val res = awaitApi(api)
res.statuses must have size (1)
}
Run Code Online (Sandbox Code Playgroud)
}}
请在此处查看完整代码https://github.com/jilen/play-weibo/tree/spec2_error