Fix big bug.

I had gotten order of MessagePackObject field from order of reflection methods.But it changes depends on order of calling in some environment(Scala's optimization or Java's specification?).
So I change to get it from order of reflection fields.
This commit is contained in:
takeshita 2011-04-27 01:04:49 +09:00
parent ed3ead06fe
commit f199b80453
4 changed files with 51 additions and 12 deletions

View File

@ -396,7 +396,7 @@ class ScalaFieldEntryReader extends IFieldEntryReader{
}*/ }*/
// In some situation, reflection returns wrong ordered getter methods compare with declaration order. // In some situation, reflection returns wrong ordered getter methods compare with declaration order.
// So to avoid such situation, list up props with setter methods // So to avoid such situation, list up props with setter methods
for(s <- setters){ /*for(s <- setters){
getters.get(s._1).map( g => { getters.get(s._1).map( g => {
if(sameType_?(g,s._2)){ if(sameType_?(g,s._2)){
val name = s._1 val name = s._1
@ -409,7 +409,42 @@ class ScalaFieldEntryReader extends IFieldEntryReader{
props +=(name -> (g,s._2,f)) props +=(name -> (g,s._2,f))
} }
}) })
}*/
// order of methods changes depends on call order, NOT declaration.
def getterAndSetter(name : String) : Option[(Method,Method)] = {
if(getters.contains(name) && setters.contains(name)){
val getter = getters(name)
val setter = setters(name)
if(getter.getReturnType == setter.getParameterTypes()(0)){
Some(getter -> setter)
}else{
None
} }
}else None
}
def recursiveFind( clazz : Class[_]) : Unit = {
if(clazz.getSuperclass != classOf[Object]){
recursiveFind(clazz.getSuperclass)
}
for(f <- clazz.getDeclaredFields){
val name =f.getName
getterAndSetter(name) match{
case Some((g,s)) => props +=( name -> (g,s,f))
case None => {
if(name.startsWith("_")){
val sname = name.substring(1)
getterAndSetter(sname) match{
case Some((g,s)) => props +=( sname -> (g,s,f))
case None =>
}
}
}
}
}
}
recursiveFind(targetClass)
props props
} }

View File

@ -18,7 +18,7 @@ import scala.collection.mutable.LinkedList
* *
*/ */
@RunWith(classOf[JUnitSuiteRunner]) @RunWith(classOf[JUnitSuiteRunner])
class CollectionPackSpec extends Specification with JUnit { class CollectionPackTest extends Specification with JUnit {
"ScalaMessagePack" should { "ScalaMessagePack" should {
"pack scala-list" in { "pack scala-list" in {

View File

@ -32,12 +32,15 @@ class SampleClass2 extends SampleClass with SampleTrait {
@Index(3) @Index(3)
var sampleClass2Name : String = "sampleclass2" var sampleClass2Name : String = "sampleclass2"
// Support getter/setter but must have _{name} field!
private var _sampleClass2Num : Int = 0
@Index(0) @Index(0)
def sampleClass2Num : Int = 22 def sampleClass2Num : Int = _sampleClass2Num
def sampleClass2Num_=(v : Int) = {} def sampleClass2Num_=(v : Int) = {_sampleClass2Num = v}
val notProperty : String ="This is not prop.Only getter" val notProperty : String ="This is not prop.Only getter"
private var _wrongValue : Int = 0
// wrong property // wrong property
def wrongValue : Int = 53 def wrongValue : Int = 53
def wrongValue_=(v : String) = {} def wrongValue_=(v : String) = {}

View File

@ -16,7 +16,7 @@ import org.specs.runner.{ JUnitSuiteRunner, JUnit }
* *
*/ */
@RunWith(classOf[JUnitSuiteRunner]) @RunWith(classOf[JUnitSuiteRunner])
class ScalaFieldEntryReaderSpec extends Specification with JUnit { class ScalaFieldEntryReaderTest extends Specification with JUnit {
"ScalaFieldEntryReader" should { "ScalaFieldEntryReader" should {
@ -56,15 +56,16 @@ class ScalaFieldEntryReaderSpec extends Specification with JUnit {
val props = reader.findPropertyMethods(c) val props = reader.findPropertyMethods(c)
println(props.keys) println("props=" + props.keys)
props.size must be_==(6) props.size must be_==(6)
props must haveKey("name") val l = props.toList
props must haveKey("number") l(0)._1 must_== "name"
props must haveKey("traitName") l(1)._1 must_== "number"
props must haveKey("traitNum") l(2)._1 must_== "sampleClass2Name"
props must haveKey("sampleClass2Name") l(3)._1 must_== "sampleClass2Num"
props must haveKey("sampleClass2Num") l(4)._1 must_== "traitName"
l(5)._1 must_== "traitNum"
} }
} }