|
| 1 | +package com.github.aselab.activerecord |
| 2 | + |
| 3 | +import java.lang.annotation.Annotation |
| 4 | +import java.lang.reflect.Field |
| 5 | +import annotations._ |
| 6 | + |
| 7 | +/** |
| 8 | + * フィールド情報モデル |
| 9 | + * @param name フィールド名 |
| 10 | + * @param fieldType フィールドタイプ |
| 11 | + * @param isOption Option型かどうか |
| 12 | + * @param isSeq シーケンスかどうか |
| 13 | + * @param annotations アノテーションリスト |
| 14 | + */ |
| 15 | +case class FieldInfo( |
| 16 | + name: String, fieldType: Class[_], |
| 17 | + isOption: Boolean, isSeq: Boolean, |
| 18 | + annotations: Seq[Annotation] = Nil |
| 19 | +) { |
| 20 | + private lazy val annotationMap = annotations.map { |
| 21 | + a => (a.annotationType.getSimpleName, a) |
| 22 | + }.toMap |
| 23 | + |
| 24 | + /** Ignoreフィールドかどうか */ |
| 25 | + lazy val ignored = annotationMap.isDefinedAt("Ignore") |
| 26 | + /** Uniqueフィールドかどうか */ |
| 27 | + lazy val unique = annotationMap.isDefinedAt("Unique") |
| 28 | +} |
| 29 | + |
| 30 | +/** フィールド情報オブジェクト */ |
| 31 | +object FieldInfo { |
| 32 | + def apply(name: String, value: Any, field: Option[Field]): FieldInfo = value match { |
| 33 | + case Some(v) => apply(name, v, None).copy(isOption = true) |
| 34 | + case None => throw ConventionException.optionValueMustBeSome |
| 35 | + |
| 36 | + case l: Traversable[_] => l.toSeq match { |
| 37 | + case Seq(v, _*) => apply(name, v, None).copy(isSeq = true) |
| 38 | + case Nil => throw ConventionException.traversableValueMustNotBeNil |
| 39 | + } |
| 40 | + |
| 41 | + case v: Any => FieldInfo(name, v.getClass, false, false) |
| 42 | + case v => |
| 43 | + val fieldType = field.map(_.getType).getOrElse( |
| 44 | + throw ConventionException.cannotDetectType(v)) |
| 45 | + FieldInfo(name, fieldType, false, false) |
| 46 | + } |
| 47 | + |
| 48 | + def apply(name: String, value: Any): FieldInfo = apply(name, value, None) |
| 49 | + |
| 50 | + def apply(field: Field, value: Any): FieldInfo = |
| 51 | + apply(field.getName, value, Some(field)).copy(annotations = field.getAnnotations.toSeq) |
| 52 | +} |
| 53 | + |
| 54 | +/** リフレクション用ユーティリティ */ |
| 55 | +trait ReflectionUtil { |
| 56 | + /** |
| 57 | + * クラスからコンパニオンオブジェクトを返す. |
| 58 | + * @param className クラス名 |
| 59 | + */ |
| 60 | + def classToCompanion(className: String): Any = { |
| 61 | + val cc = Class.forName(className + "$") |
| 62 | + cc.getField("MODULE$").get(cc) |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * クラスからコンパニオンオブジェクトを返す. |
| 67 | + * @param c クラス |
| 68 | + */ |
| 69 | + def classToCompanion(c: Class[_]): Any = classToCompanion(c.getName) |
| 70 | + |
| 71 | + /** |
| 72 | + * クラスからコンパニオンオブジェクトを返す. |
| 73 | + * @param c 任意のオブジェクト |
| 74 | + */ |
| 75 | + def companionToClass(c: Any) = Class.forName(c.getClass.getName.dropRight(1)) |
| 76 | + |
| 77 | + /** |
| 78 | + * 任意のオブジェクトのフィールド値をリフレクションで取得/変更できるようにする |
| 79 | + * 暗黙変換メソッド. |
| 80 | + */ |
| 81 | + implicit def toReflectable(o: Any) = new { |
| 82 | + val c = o.getClass |
| 83 | + |
| 84 | + /** |
| 85 | + * 値を取得する. |
| 86 | + * @param name フィールド名 |
| 87 | + */ |
| 88 | + def getValue[T](name: String) = c.getMethod(name).invoke(o).asInstanceOf[T] |
| 89 | + |
| 90 | + /** |
| 91 | + * 値を設定する. |
| 92 | + * @param name フィールド名 |
| 93 | + * @param value 設定する値 |
| 94 | + */ |
| 95 | + def setValue(name: String, value: Any) = { |
| 96 | + val f = c.getDeclaredField(name) |
| 97 | + f.setAccessible(true) |
| 98 | + f.set(o, value) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** リフレクション用ユーティリティ */ |
| 104 | +object ReflectionUtil extends ReflectionUtil |
0 commit comments