Change property ordering based on getter methods to setter methods.

Add test to confirm field order.
This commit is contained in:
takeshita 2011-04-14 16:34:52 +09:00
parent f6de4c9479
commit 743d69ec0b
4 changed files with 67 additions and 4 deletions

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.msgpack</groupId>
<artifactId>scala-msgpack</artifactId>
<version>0.0.1-devel</version>
<version>0.0.1-SNAPSHOT</version>
<name>${project.artifactId}</name>
<description>My wonderfull scala app</description>
<inceptionYear>2010</inceptionYear>

View File

@ -3,12 +3,12 @@ package org.msgpack
import _root_.javassist.{CtClass, CtNewConstructor}
import annotation._
import template._
import builder.JavassistTemplateBuilder.JavassistTemplate
import builder.{BuildContextBase, JavassistTemplateBuilder}
import java.lang.Class
import collection.immutable.{ListMap, TreeMap}
import java.lang.reflect.{Type, Modifier, Method, Field}
import java.lang.annotation.{Annotation => JavaAnnotation}
import builder.{JavassistTemplateBuilder, BuildContextBase, BuildContext}
import builder.JavassistTemplateBuilder.JavassistTemplate
import scala.collection.JavaConverters._
;
/*
@ -378,7 +378,7 @@ class ScalaFieldEntryReader extends IFieldEntryReader{
def sameType_?( getter : Method,setter : Method) = {
getter.getReturnType == setter.getParameterTypes()(0)
}
/*
for(g <- getters){
setters.get(g._1).map( s => {
if(sameType_?(g._2,s)){
@ -393,6 +393,22 @@ class ScalaFieldEntryReader extends IFieldEntryReader{
props +=( name -> (g._2,s,f))
}
})
}*/
// In some situation, reflection returns wrong ordered getter methods compare with declaration order.
// So to avoid such situation, list up props with setter methods
for(s <- setters){
getters.get(s._1).map( g => {
if(sameType_?(g,s._2)){
val name = s._1
val f = try{
targetClass.getDeclaredField(name)
}catch{
case e : NoSuchFieldException => null
}
props +=(name -> (g,s._2,f))
}
})
}
props
}

View File

@ -74,4 +74,28 @@ class BasicalTypes{
}
object FieldOrder{
val None = 0
val Offering = 11
val BeOffered = 12
val Friend = 13
val Block = 21
}
@MessagePackMessage
class FieldOrder{
var one : Int = 0
var two : String = "aaa"
var three : String = "bbb"
var four : String = ""
var five : Boolean = false
def six : Int = 1
def six_=(v : Int) = one = v
override def toString = "hogehoge"
}

View File

@ -19,6 +19,8 @@ import org.specs.runner.{ JUnitSuiteRunner, JUnit }
class ScalaFieldEntryReaderSpec extends Specification with JUnit {
"ScalaFieldEntryReader" should {
"check setter " in {
val reader = new ScalaFieldEntryReader()
@ -67,6 +69,27 @@ class ScalaFieldEntryReaderSpec extends Specification with JUnit {
}
"field order" in {
var reader = new ScalaFieldEntryReader
val c = classOf[FieldOrder]
println("Methods of FieldOrder class")
c.getMethods.foreach(println(_))
println("-- end --")
val props = reader.findPropertyMethods(c)
var index : Int = 0
val names = List("one","two","three","four","five","six")
for( p <- props.values){
p._1.getName must_== names(index)
index += 1
}
}
"indexing " in {
val reader = new ScalaFieldEntryReader()