• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / scala / 444

29 Mar 2025 10:33PM CUT coverage: 0.0%. Remained the same
444

push

github

web-flow
Update ci.yaml

drop jdk 22 and 23-ea, add jdk 24

0 of 476 branches covered (0.0%)

0 of 996 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/mybatis-scala-core/src/main/scala/org/mybatis/scala/config/ObjectFactory.scala
1
/*
2
 *    Copyright 2011-2015 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.scala.config
17

18
class DefaultObjectFactory extends ObjectFactory {
×
19

20
  def create[T](t : Class[T]) : T = create(t, null, null)
×
21

22
  def create[T](t : Class[T], constructorArgTypes : java.util.List[Class[_]], constructorArgs : java.util.List[AnyRef]) : T = {
23
    val classToCreate = resolveInterface(t)
×
24
    instantiateClass[T](classToCreate, constructorArgTypes, constructorArgs)
×
25
  }
26

27
  override def setProperties(properties : java.util.Properties) : Unit = {}
28

29
  private def instantiateClass[T](t : Class[_], constructorArgTypes : java.util.List[Class[_]], constructorArgs : java.util.List[AnyRef]) : T = {
30

31
    val argTypes = {
32
      if (constructorArgTypes != null)
×
33
        constructorArgTypes.toArray[Class[_]](new Array[Class[_]](constructorArgTypes.size))
×
34
      else
35
        null
×
36
    }
37

38
    val constructor = getConstructor(t, argTypes)
×
39

40
    val argValues = {
41
      if (constructorArgs != null)
×
42
        constructorArgs.toArray[AnyRef](new Array[AnyRef](constructorArgs.size))
×
43
      else
44
        null
×
45
    }
46

47
    try {
48
      if (argTypes == null || argValues == null) {
×
49
        constructor.newInstance().asInstanceOf[T]
×
50
      }
51
      else {
52
        constructor.newInstance(argValues : _*).asInstanceOf[T]
×
53
      }
54
    }
55
    catch {
56
      case e : Exception =>
57
        val types = {
58
          if (argTypes == null) ""
×
59
          else argTypes.map(_.getSimpleName).reduceLeft(_ + ", " + _)
×
60
        }
61
        val values = {
62
          if (argValues == null) ""
×
63
          else argValues.map(String.valueOf(_)).reduceLeft(_ + ", " + _)
×
64
        }
65
        throw new org.apache.ibatis.reflection.ReflectionException(
×
66
          "Error instantiating %s with invalid types (%s) or values (%s). Cause: %s".format(t.getSimpleName, types, values, e.getMessage), e);
×
67
    }
68
  }
69

70
  private def resolveInterface[T](t : Class[T]) : Class[_] = {
71
    // Java Collections
72
    if (t == classOf[java.util.List[_]])
×
73
      classOf[java.util.LinkedList[_]]
×
74
    else if (t == classOf[java.util.Collection[_]])
×
75
      classOf[java.util.LinkedList[_]]
×
76
    else if (t == classOf[java.util.Map[_,_]])
×
77
      classOf[java.util.HashMap[_,_]]
×
78
    else if (t == classOf[java.util.SortedSet[_]])
×
79
      classOf[java.util.TreeSet[_]]
×
80
    else if (t == classOf[java.util.Set[_]])
×
81
      classOf[java.util.HashSet[_]]
×
82
    // Scala Collections
83
    else if (t == classOf[scala.collection.Seq[_]])
×
84
      classOf[scala.collection.mutable.ArrayBuffer[_]]
×
85
    else if (t == classOf[scala.collection.Map[_,_]])
×
86
      classOf[scala.collection.mutable.HashMap[_,_]]
×
87
    else if (t == classOf[scala.collection.Set[_]])
×
88
      classOf[scala.collection.mutable.HashSet[_]]
×
89
    else {
90
      t
×
91
    }
92
  }
93

94
  def isCollection[T](t : Class[T]) : Boolean =
95
    classOf[scala.collection.Seq[_]].isAssignableFrom(t) ||
×
96
    classOf[scala.collection.Set[_]].isAssignableFrom(t)
×
97

98
  sealed class CacheKey(t : Class[_], args : Array[Class[_]]) {
×
99

100
    val _hc : Int = {
×
101
      if (args == null) {
×
102
        t.hashCode
×
103
      }
104
      else {
105
        var code = t.hashCode
×
106
        for (at <- args) {
×
107
          code = code * 41 + at.hashCode
×
108
        }
109
        code
×
110
      }
111
    }
112

113
    override def hashCode = _hc
×
114

115
    override def equals(that : Any) =
116
      that != null &&
×
117
        that.getClass == classOf[CacheKey] &&
×
118
          that.asInstanceOf[CacheKey]._hc == this._hc
×
119

120
  }
121

122
  def getConstructor(t : Class[_], args : Array[Class[_]]) : java.lang.reflect.Constructor[_] = {
123
      try {
124
        if (args == null) {
×
125
          val constructor = t.getDeclaredConstructor()
×
126
          if (!constructor.isAccessible()) {
×
127
            constructor.setAccessible(true)
×
128
          }
129
          constructor
×
130
        }
131
        else {
132
          val constructor = t.getDeclaredConstructor(args : _*)
×
133
          if (!constructor.isAccessible()) {
×
134
            constructor.setAccessible(true)
×
135
          }
136
          constructor
×
137
        }
138
      }
139
      catch {
140
        case e : Exception =>
141
          val types = {
142
            if (args == null) ""
×
143
            else args.map(_.getSimpleName).reduceLeft(_ + ", " + _)
×
144
          }
145
          throw new org.apache.ibatis.reflection.ReflectionException(
×
146
            "Error instantiating %s with invalid types (%s). Cause: %s".format(t.getSimpleName, args, e.getMessage), e);
×
147
      }
148
  }
149

150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc