Search code examples
javascalaoopjgrapht

JGraphT throws NoSuchMethodException when initializing case classes through ClassBasedVertexFactory


JGraphT's ClassBasedVertexFactory.createVertex() is throwing a NoSuchMethodException for my case classes' constructors. (The runtime reports that as TJunction.<init>() not being found.)

Here is my parent class:

package org.uom.fyp.engine

   class Node {

     private var nType: RoadStructure.EnumVal = RoadStructure.Default

     /**
      * Returns the node's type. (e.g.: <b>RoadStructure.Default</b>)
      */
     def nodeType: RoadStructure.EnumVal = nType

     /**
      * Sets the node's type.
      * @param nType The node's type.
      */
     def nodeType_(nType: RoadStructure.EnumVal) = {
       this.nType = nType
     }

   }

Followed by one of my case classes:

   package org.uom.fyp.engine

   case class TJunction(p: Edge, c1: Edge, c2: Edge) extends Node {
     val priority = p
     val converging1 = c1
     val converging2 = c2
   }

Here is where I'm creating my vertices:

 def createLaneSlice(network: RoadNetwork, start: Node = null, edgeType: RoadStructure.EnumVal): Edge = {
  var vertexFactory: ClassBasedVertexFactory[Node] = null
  if (edgeType == RoadStructure.TJunction) {
    vertexFactory = new ClassBasedVertexFactory(classOf[TJunction])
  } else if (edgeType == RoadStructure.Roadabout) {
    vertexFactory = new ClassBasedVertexFactory(classOf[Roundabout])
  } else if (edgeType == RoadStructure.Crossroads) {
    vertexFactory = new ClassBasedVertexFactory(classOf[Crossroads])
  } else {
    vertexFactory = new ClassBasedVertexFactory(classOf[Node])
  }

  var v1: Node = null
  if (start == null) {
    v1 = vertexFactory.createVertex()
  } else {
    v1 = start
  }
  val v2: Node = vertexFactory.createVertex()
  network.addVertex(v1)
  network.addVertex(v2)

Solution

  • Removing the custom constructor from my case class and replacing it by setters for the subsequent properties does the trick.

    Thanks to @Marco13 for his enlightening comment.