Permalink
Browse files

Fixed issues with reading mdoel classes from base class and also read…

…ing model class from scala option
  • Loading branch information...
1 parent f808390 commit 3b31dc7dd74aab5e5d2912df04be486eab47ef24 @rpidikiti rpidikiti committed Apr 5, 2012
@@ -72,6 +72,16 @@ object TypeUtil {
return isArray
}
+ def isParameterizedScalaOption(genericType: Type): Boolean = {
+ var isOption = false
+ val isTypeParameterized: Boolean = classOf[ParameterizedType].isAssignableFrom(genericType.getClass)
+ if (isTypeParameterized) {
+ val parameterizedType: ParameterizedType = genericType.asInstanceOf[ParameterizedType]
+ isOption = (parameterizedType.getRawType == classOf[Option[_]])
+ }
+ return isOption
+ }
+
/**
* Gets a parameterized lists types if they are in com.wordnik.* packages
*/
@@ -80,33 +90,36 @@ object TypeUtil {
if (isParameterizedList(genericType) || isParameterizedSet(genericType)) {
val parameterizedType: ParameterizedType = genericType.asInstanceOf[ParameterizedType]
for (_listType <- parameterizedType.getActualTypeArguments) {
- if (!_listType.getClass.isAssignableFrom(classOf[ParameterizedTypeImpl])){
- val listType: Class[_] = _listType.asInstanceOf[Class[_]]
- if (listType.getName.startsWith(WORDNIK_PACKAGES)) list.add(listType.getName)
- }
+ checkAndAddConcreteObjectType(_listType, list)
}
} else if (isParameterizedMap(genericType)) {
val parameterizedType: ParameterizedType = genericType.asInstanceOf[ParameterizedType]
val typeArgs = parameterizedType.getActualTypeArguments
val keyType = typeArgs(0)
val valueType = typeArgs(1)
if (keyType.isInstanceOf[Class[_]]) {
- if (!keyType.getClass.isAssignableFrom(classOf[ParameterizedTypeImpl])){
- val keyTypeClass: Class[_] = keyType.asInstanceOf[Class[_]]
- if (keyTypeClass.getName.startsWith(WORDNIK_PACKAGES)) list.add(keyTypeClass.getName)
- }
+ checkAndAddConcreteObjectType(keyType, list)
}
if (valueType.isInstanceOf[Class[_]]) {
- if (!valueType.getClass.isAssignableFrom(classOf[ParameterizedTypeImpl])){
- val valueTypeClass: Class[_] = valueType.asInstanceOf[Class[_]]
- if (valueTypeClass.getName.startsWith(WORDNIK_PACKAGES)) list.add(valueTypeClass.getName)
- }
+ checkAndAddConcreteObjectType(valueType, list)
}
list.addAll(getWordnikParameterTypes(keyType))
list.addAll(getWordnikParameterTypes(valueType))
+ } else if (isParameterizedScalaOption(genericType)){
+ val parameterizedType: ParameterizedType = genericType.asInstanceOf[ParameterizedType]
+ for (optionType <- parameterizedType.getActualTypeArguments) {
+ checkAndAddConcreteObjectType(optionType, list)
+ }
}
return list
}
+
+ private def checkAndAddConcreteObjectType(classType:Type, list: java.util.List[String]) {
+ if (!classType.getClass.isAssignableFrom(classOf[ParameterizedTypeImpl])){
+ val listType: Class[_] = classType.asInstanceOf[Class[_]]
+ if (listType.getName.startsWith(WORDNIK_PACKAGES)) list.add(listType.getName)
+ }
+ }
/**
* Get all classes references by a given list of classes. This includes types of method params and fields
@@ -139,7 +152,7 @@ object TypeUtil {
}
}
if (clazz != null) {
- for (field <- clazz.getDeclaredFields) {
+ for (field <- clazz.getFields) {
if (Modifier.isPublic(field.getModifiers)) {
var fieldClass: String = field.getType.getName
var fieldGenericType = field.getGenericType
@@ -159,7 +172,7 @@ object TypeUtil {
}
}
}
- for (method <- clazz.getDeclaredMethods) {
+ for (method <- clazz.getMethods) {
if (Modifier.isPublic(method.getModifiers)) {
var methodReturnClass: String = method.getReturnType.getName
var methodGenericType = method.getGenericReturnType
@@ -148,7 +148,6 @@ class SpecReaderTest extends FlatSpec with ShouldMatchers {
var classes:java.util.List[String] = new java.util.ArrayList[String]()
classes.add(classOf[ObjectWithDifferentElementAndPropertyName].getName);
val types = TypeUtil.getReferencedClasses(classes)
- println(types)
assert(types.size() === 2)
}
@@ -167,6 +166,21 @@ class SpecReaderTest extends FlatSpec with ShouldMatchers {
}
}
+ it should "read objects with objects form scala option properties" in {
+ var classes:java.util.List[String] = new java.util.ArrayList[String]()
+ classes.add(classOf[ScalaCaseClassWithScalaSupportedType].getName);
+ val types = TypeUtil.getReferencedClasses(classes)
+ assert(types.size() === 3)
+ }
+
+ it should "read objects from base class for identifying model classes " in {
+ var classes:java.util.List[String] = new java.util.ArrayList[String]()
+ classes.add(classOf[ClassToTestModelClassesFromBaseClass].getName);
+ val types = TypeUtil.getReferencedClasses(classes)
+ println("types are ::::::::::::::::" + types)
+ assert(types.size() === 2)
+ }
+
}
@RunWith(classOf[JUnitRunner])
@@ -439,11 +453,13 @@ case class ScalaCaseClassWithScalaSupportedType(
stringType: String,
dateType: java.util.Date,
mapType: Map[String, String],
- @(ApiProperty @field)(dataType="Long")optionType: Option[Long],
+ optionType: Option[TestClassWithConstructorProperties],
seqType: Seq[String],
setType: Set[String],
seqOfTuples: Seq[(String, Double)],
@(ApiProperty @field)(dataType="String")enumType:ScalaEnums.Value,
collectionOfCollections:Map[String, Seq[ObjectWithRootElementName]]){
}
+class ClassToTestModelClassesFromBaseClass extends ObjectWithChildObjectsInMap {
+}

0 comments on commit 3b31dc7

Please sign in to comment.