mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-24 09:32:49 +01:00
Change property ordering based on getter methods to setter methods.
Add test to confirm field order.
This commit is contained in:
parent
f6de4c9479
commit
743d69ec0b
@ -2,7 +2,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.msgpack</groupId>
|
<groupId>org.msgpack</groupId>
|
||||||
<artifactId>scala-msgpack</artifactId>
|
<artifactId>scala-msgpack</artifactId>
|
||||||
<version>0.0.1-devel</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>${project.artifactId}</name>
|
<name>${project.artifactId}</name>
|
||||||
<description>My wonderfull scala app</description>
|
<description>My wonderfull scala app</description>
|
||||||
<inceptionYear>2010</inceptionYear>
|
<inceptionYear>2010</inceptionYear>
|
||||||
|
@ -3,12 +3,12 @@ package org.msgpack
|
|||||||
import _root_.javassist.{CtClass, CtNewConstructor}
|
import _root_.javassist.{CtClass, CtNewConstructor}
|
||||||
import annotation._
|
import annotation._
|
||||||
import template._
|
import template._
|
||||||
|
import builder.JavassistTemplateBuilder.JavassistTemplate
|
||||||
|
import builder.{BuildContextBase, JavassistTemplateBuilder}
|
||||||
import java.lang.Class
|
import java.lang.Class
|
||||||
import collection.immutable.{ListMap, TreeMap}
|
import collection.immutable.{ListMap, TreeMap}
|
||||||
import java.lang.reflect.{Type, Modifier, Method, Field}
|
import java.lang.reflect.{Type, Modifier, Method, Field}
|
||||||
import java.lang.annotation.{Annotation => JavaAnnotation}
|
import java.lang.annotation.{Annotation => JavaAnnotation}
|
||||||
import builder.{JavassistTemplateBuilder, BuildContextBase, BuildContext}
|
|
||||||
import builder.JavassistTemplateBuilder.JavassistTemplate
|
|
||||||
import scala.collection.JavaConverters._
|
import scala.collection.JavaConverters._
|
||||||
;
|
;
|
||||||
/*
|
/*
|
||||||
@ -378,7 +378,7 @@ class ScalaFieldEntryReader extends IFieldEntryReader{
|
|||||||
def sameType_?( getter : Method,setter : Method) = {
|
def sameType_?( getter : Method,setter : Method) = {
|
||||||
getter.getReturnType == setter.getParameterTypes()(0)
|
getter.getReturnType == setter.getParameterTypes()(0)
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
for(g <- getters){
|
for(g <- getters){
|
||||||
setters.get(g._1).map( s => {
|
setters.get(g._1).map( s => {
|
||||||
if(sameType_?(g._2,s)){
|
if(sameType_?(g._2,s)){
|
||||||
@ -393,6 +393,22 @@ class ScalaFieldEntryReader extends IFieldEntryReader{
|
|||||||
props +=( name -> (g._2,s,f))
|
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
|
props
|
||||||
}
|
}
|
||||||
|
@ -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"
|
||||||
|
|
||||||
}
|
}
|
@ -19,6 +19,8 @@ import org.specs.runner.{ JUnitSuiteRunner, JUnit }
|
|||||||
class ScalaFieldEntryReaderSpec extends Specification with JUnit {
|
class ScalaFieldEntryReaderSpec extends Specification with JUnit {
|
||||||
|
|
||||||
"ScalaFieldEntryReader" should {
|
"ScalaFieldEntryReader" should {
|
||||||
|
|
||||||
|
|
||||||
"check setter " in {
|
"check setter " in {
|
||||||
val reader = new ScalaFieldEntryReader()
|
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 {
|
"indexing " in {
|
||||||
val reader = new ScalaFieldEntryReader()
|
val reader = new ScalaFieldEntryReader()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user