Skip to content

Commit e7fe9df

Browse files
committed
consolidate entity serialization into single class
- refactored JavaEntity to be DRY with respect to repeating the serialization of fields shared by many types of entities - added tests
1 parent 3c4d29e commit e7fe9df

File tree

2 files changed

+194
-166
lines changed

2 files changed

+194
-166
lines changed

doc-tool/src/dotty/tools/dottydoc/model/JavaConverters.scala

Lines changed: 83 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ package model
44
import comment._
55
import references._
66

7-
import _root_.java.util.HashMap
87
import _root_.java.util.LinkedList
98

109
object JavaConverters {
1110
import scala.collection.JavaConverters._
12-
import _root_.java.util.{ Optional => JOptional, Map => JMap }
11+
import _root_.java.util.{ Map => JMap }
1312

1413
implicit class OptStr(val opt: Option[String]) extends AnyVal {
1514
def asJava = opt.getOrElse(null)
@@ -44,160 +43,6 @@ object JavaConverters {
4443
).asJava
4544
}
4645

47-
implicit class JavaPackage(val ent: Package) extends AnyVal {
48-
def asJava(extras: Map[String, _] = Map.empty): JMap[String, _] = (Map(
49-
"kind" -> ent.kind,
50-
"annotations" -> ent.annotations.asJava,
51-
"name" -> ent.name,
52-
"path" -> ent.path.asJava,
53-
"members" -> ent.members.map(_.asJava()).asJava,
54-
"children" -> ent.children.map(_.asJava()).asJava,
55-
"comment" -> ent.comment.map(_.asJava).asJava,
56-
"superTypes" -> ent.superTypes,
57-
"hasVisibleMembers" -> ent.hasVisibleMembers,
58-
"hasShortenedDocstring" -> ent.hasShortenedDocstring,
59-
"signature" -> ent.signature
60-
) ++ extras).asJava
61-
}
62-
63-
implicit class JavaCaseClass(val ent: CaseClass) extends AnyVal {
64-
def asJava(extras: Map[String, _] = Map.empty): JMap[String, _] = (Map(
65-
"kind" -> ent.kind,
66-
"annotations" -> ent.annotations.asJava,
67-
"name" -> ent.name,
68-
"members" -> ent.members.map(_.asJava()).asJava,
69-
"modifiers" -> ent.modifiers.asJava,
70-
"path" -> ent.path.asJava,
71-
"typeParams" -> ent.typeParams.asJava,
72-
"superTypes" -> ent.superTypes.map(_.asJava).asJava,
73-
"comment" -> ent.comment.map(_.asJava).asJava,
74-
"constructors" -> ent.constructors.map(_.map(_.asJava).asJava).asJava,
75-
"isPrivate" -> ent.isPrivate,
76-
"isProtected" -> ent.isProtected,
77-
"hasVisibleMembers" -> ent.hasVisibleMembers,
78-
"hasShortenedDocstring" -> ent.hasShortenedDocstring,
79-
"hasCompanion" -> ent.hasCompanion,
80-
"companionPath" -> ent.companionPath.asJava,
81-
"signature" -> ent.signature
82-
) ++ extras).asJava
83-
}
84-
85-
implicit class JavaClass(val ent: Class) extends AnyVal {
86-
def asJava(extras: Map[String, _] = Map.empty): JMap[String, _] = (Map(
87-
"kind" -> ent.kind,
88-
"annotations" -> ent.annotations.asJava,
89-
"name" -> ent.name,
90-
"members" -> ent.members.map(_.asJava()).asJava,
91-
"modifiers" -> ent.modifiers.asJava,
92-
"path" -> ent.path.asJava,
93-
"typeParams" -> ent.typeParams.asJava,
94-
"superTypes" -> ent.superTypes.map(_.asJava).asJava,
95-
"comment" -> ent.comment.map(_.asJava).asJava,
96-
"constructors" -> ent.constructors.map(_.map(_.asJava).asJava).asJava,
97-
"isPrivate" -> ent.isPrivate,
98-
"isProtected" -> ent.isProtected,
99-
"hasVisibleMembers" -> ent.hasVisibleMembers,
100-
"hasShortenedDocstring" -> ent.hasShortenedDocstring,
101-
"hasCompanion" -> ent.hasCompanion,
102-
"companionPath" -> ent.companionPath.asJava,
103-
"signature" -> ent.signature
104-
) ++ extras).asJava
105-
}
106-
107-
implicit class JavaTrait(val ent: Trait) extends AnyVal {
108-
def asJava(extras: Map[String, _] = Map.empty): JMap[String, _] = (Map(
109-
"kind" -> ent.kind,
110-
"annotations" -> ent.annotations.asJava,
111-
"name" -> ent.name,
112-
"members" -> ent.members.map(_.asJava()).asJava,
113-
"modifiers" -> ent.modifiers.asJava,
114-
"path" -> ent.path.asJava,
115-
"typeParams" -> ent.typeParams.asJava,
116-
"superTypes" -> ent.superTypes.map(_.asJava).asJava,
117-
"comment" -> ent.comment.map(_.asJava).asJava,
118-
"isPrivate" -> ent.isPrivate,
119-
"isProtected" -> ent.isProtected,
120-
"hasVisibleMembers" -> ent.hasVisibleMembers,
121-
"hasShortenedDocstring" -> ent.hasShortenedDocstring,
122-
"hasCompanion" -> ent.hasCompanion,
123-
"companionPath" -> ent.companionPath.asJava,
124-
"signature" -> ent.signature
125-
) ++ extras).asJava
126-
}
127-
128-
implicit class JavaObject(val ent: Object) extends AnyVal {
129-
def asJava(extras: Map[String, _] = Map.empty): JMap[String, _] = (Map(
130-
"kind" -> ent.kind,
131-
"annotations" -> ent.annotations.asJava,
132-
"name" -> ent.name,
133-
"members" -> ent.members.map(_.asJava()).asJava,
134-
"modifiers" -> ent.modifiers.asJava,
135-
"path" -> ent.path.asJava,
136-
"superTypes" -> ent.superTypes.map(_.asJava).asJava,
137-
"comment" -> ent.comment.map(_.asJava).asJava,
138-
"isPrivate" -> ent.isPrivate,
139-
"isProtected" -> ent.isProtected,
140-
"hasVisibleMembers" -> ent.hasVisibleMembers,
141-
"hasShortenedDocstring" -> ent.hasShortenedDocstring,
142-
"hasCompanion" -> ent.hasCompanion,
143-
"companionPath" -> ent.companionPath.asJava,
144-
"signature" -> ent.signature
145-
) ++ extras).asJava
146-
}
147-
148-
implicit class JavaDef(val ent: Def) extends AnyVal {
149-
def asJava: JMap[String, _] = Map(
150-
"kind" -> ent.kind,
151-
"annotations" -> ent.annotations.asJava,
152-
"name" -> ent.name,
153-
"modifiers" -> ent.modifiers.asJava,
154-
"path" -> ent.path.asJava,
155-
"returnValue" -> ent.returnValue.asJava,
156-
"typeParams" -> ent.typeParams.asJava,
157-
"paramLists" -> ent.paramLists.map(_.asJava).asJava,
158-
"comment" -> ent.comment.map(_.asJava).asJava,
159-
"implicitlyAddedFrom" -> ent.implicitlyAddedFrom.map(_.asJava).asJava,
160-
"hasShortenedDocstring" -> ent.hasShortenedDocstring,
161-
"isPrivate" -> ent.isPrivate,
162-
"isProtected" -> ent.isProtected,
163-
"signature" -> ent.signature
164-
).asJava
165-
}
166-
167-
implicit class JavaVal(val ent: Val) extends AnyVal {
168-
def asJava: JMap[String, _] = Map(
169-
"kind" -> ent.kind,
170-
"annotations" -> ent.annotations.asJava,
171-
"name" -> ent.name,
172-
"modifiers" -> ent.modifiers.asJava,
173-
"path" -> ent.path.asJava,
174-
"returnValue" -> ent.returnValue.asJava,
175-
"comment" -> ent.comment.map(_.asJava).asJava,
176-
"implicitlyAddedFrom" -> ent.implicitlyAddedFrom.map(_.asJava).asJava,
177-
"hasShortenedDocstring" -> ent.hasShortenedDocstring,
178-
"isPrivate" -> ent.isPrivate,
179-
"isProtected" -> ent.isProtected,
180-
"signature" -> ent.signature
181-
).asJava
182-
}
183-
184-
implicit class JavaTypeAlias(val ent: TypeAlias) extends AnyVal {
185-
def asJava: JMap[String, _] = Map(
186-
"kind" -> ent.kind,
187-
"annotations" -> ent.annotations.asJava,
188-
"modifiers" -> ent.modifiers.asJava,
189-
"name" -> ent.name,
190-
"path" -> ent.path.asJava,
191-
"alias" -> ent.alias.map(_.asJava).asJava,
192-
"typeParams" -> ent.typeParams.asJava,
193-
"comment" -> ent.comment.map(_.asJava).asJava,
194-
"hasShortenedDocstring" -> ent.hasShortenedDocstring,
195-
"isPrivate" -> ent.isPrivate,
196-
"isProtected" -> ent.isProtected,
197-
"signature" -> ent.signature
198-
).asJava
199-
}
200-
20146
implicit class JavaParamList(val pl: ParamList) extends AnyVal {
20247
def asJava: JMap[String, _] = Map(
20348
"list" -> pl.list.map(_.asJava).asJava,
@@ -298,16 +143,88 @@ object JavaConverters {
298143
def asJava(extras: Map[String, _] = Map.empty): JMap[String, _] = parseEntity(ent, extras)
299144
}
300145

301-
private def parseEntity(ent: Entity, extras: Map[String, _]): JMap[String, _] = ent match {
302-
case ent: Package => ent.asJava(extras)
303-
case ent: CaseClass => ent.asJava(extras)
304-
case ent: Class => ent.asJava(extras)
305-
case ent: Trait => ent.asJava(extras)
306-
case ent: Object => ent.asJava(extras)
307-
case ent: Def => ent.asJava
308-
case ent: Val => ent.asJava
309-
case ent: TypeAlias => ent.asJava
310-
case _ => Map.empty.asJava
146+
private def parseEntity(ent: Entity, extras: Map[String, _]): JMap[String, _] = {
147+
val entity = Map(
148+
"kind" -> ent.kind,
149+
"annotations" -> ent.annotations.asJava,
150+
"name" -> ent.name,
151+
"path" -> ent.path.asJava,
152+
"children" -> ent.children.map(_.asJava()).asJava,
153+
"comment" -> ent.comment.map(_.asJava).asJava,
154+
"hasShortenedDocstring" -> ent.hasShortenedDocstring,
155+
"signature" -> ent.signature
156+
)
157+
val members = ent match {
158+
case ent: Members => Map(
159+
"members" -> ent.members.map(_.asJava()).asJava,
160+
"hasVisibleMembers" -> ent.hasVisibleMembers
161+
)
162+
case _ => Map.empty
163+
}
164+
val superTypes = ent match {
165+
case ent: SuperTypes => Map(
166+
"superTypes" -> ent.superTypes.map(_.asJava).asJava
167+
)
168+
case _ => Map.empty
169+
}
170+
val modifiers = ent match {
171+
case ent: Modifiers => Map(
172+
"modifiers" -> ent.modifiers.asJava,
173+
"isPrivate" -> ent.isPrivate,
174+
"isProtected" -> ent.isProtected
175+
)
176+
case _ => Map.empty
177+
}
178+
val typeParams = ent match {
179+
case ent: TypeParams => Map(
180+
"typeParams" -> ent.typeParams.asJava
181+
)
182+
case _ => Map.empty
183+
}
184+
val constructors = ent match {
185+
case ent: Constructors => Map(
186+
"constructors" -> ent.constructors.map(_.map(_.asJava).asJava).asJava
187+
)
188+
case _ => Map.empty
189+
}
190+
val companion = ent match {
191+
case ent: Companion => Map(
192+
"hasCompanion" -> ent.hasCompanion,
193+
"companionPath" -> ent.companionPath.asJava
194+
)
195+
case _ => Map.empty
196+
}
197+
val returnValue = ent match {
198+
case ent: ReturnValue => Map(
199+
"returnValue" -> ent.returnValue.asJava
200+
)
201+
case _ => Map.empty
202+
}
203+
val implicitlyAddedEntity = ent match {
204+
case ent: ImplicitlyAddedEntity => Map(
205+
"implicitlyAddedFrom" -> ent.implicitlyAddedFrom.map(_.asJava).asJava
206+
)
207+
case _ => Map.empty
208+
}
209+
val typeAlias : Map[String, _] = ent match {
210+
case ent: TypeAlias => Map(
211+
"alias" -> ent.alias.map(_.asJava).asJava
212+
)
213+
case _ => Map.empty
214+
}
215+
val trt = ent match {
216+
case ent: Trait => Map(
217+
"traitParams" -> ent.traitParams.map(_.asJava).asJava
218+
)
219+
case _ => Map.empty
220+
}
221+
val df = ent match {
222+
case ent: Def => Map(
223+
"paramLists" -> ent.paramLists.map(_.asJava).asJava
224+
)
225+
case _ => Map.empty
226+
}
227+
(entity ++ members ++ superTypes ++ modifiers ++ typeParams ++ constructors ++ companion ++ returnValue ++ implicitlyAddedEntity ++ typeAlias ++ trt ++ df).asJava
311228
}
312229

313230
implicit class JavaMap(val map: collection.Map[String, Package]) extends AnyVal {

doc-tool/test/JavaConverterTest.scala

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package dotty.tools
2+
package dottydoc
3+
4+
import org.junit.Test
5+
import org.junit.Assert._
6+
import model.{
7+
Entity,
8+
Members,
9+
SuperTypes,
10+
Modifiers,
11+
TypeParams,
12+
Constructors => MConstructors,
13+
Companion,
14+
ReturnValue,
15+
ImplicitlyAddedEntity,
16+
TypeAlias,
17+
Trait,
18+
Def,
19+
NonEntity
20+
}
21+
import model.references.{ConstantReference, TypeReference, NoLink}
22+
import model.comment.Comment
23+
import dotty.tools.dotc.core.Symbols.NoSymbol
24+
import _root_.java.util.{Optional => JOptional, Map => JMap}
25+
26+
class JavaConverterTest {
27+
import model.JavaConverters._
28+
29+
@Test def entityConversions = {
30+
trait TestEntity extends Entity {
31+
def symbol = NoSymbol
32+
33+
def name = "test"
34+
35+
def kind = "ent"
36+
37+
def path = "path" :: "to" :: "test" :: Nil
38+
39+
def comment = Some(new Comment("", "", List(), List(), None, Map(), Map(), Map(), None, None, List(), None, List(), List(), None, None, Map(), Map(), Map(), List()))
40+
41+
def annotations = List("test")
42+
43+
def parent = NonEntity
44+
45+
}
46+
trait TestMembers extends TestEntity with Members {
47+
def members = new TestEntity{} :: Nil
48+
}
49+
trait TestSuperTypes extends TestEntity with SuperTypes {
50+
def superTypes = new NoLink("title", "query") :: Nil
51+
}
52+
trait TestModifiers extends TestEntity with Modifiers {
53+
def modifiers = "private" :: Nil
54+
}
55+
trait TestTypeParams extends TestEntity with TypeParams {
56+
def typeParams = "String" :: "String" :: Nil
57+
}
58+
trait TestConstructors extends TestEntity with MConstructors {
59+
def constructors = List(List())
60+
}
61+
trait TestCompanion extends TestEntity with Companion {
62+
def companionPath = "path" :: "to" :: "companion" :: Nil
63+
def companionPath_=(xs: List[String]) = Unit
64+
}
65+
trait TestReturnValue extends TestEntity with ReturnValue {
66+
def returnValue =
67+
new TypeReference("String", new NoLink("title", "target"), List())
68+
}
69+
trait TestImplicitlyAddedEntity
70+
extends TestEntity
71+
with ImplicitlyAddedEntity {
72+
def implicitlyAddedFrom =
73+
Some(
74+
new TypeReference("String", new NoLink("title", "target"), List()))
75+
}
76+
trait TestTypeAlias extends TestTypeParams with TestModifiers with TypeAlias {
77+
override val kind = "type"
78+
def alias = None
79+
}
80+
trait TestDef extends TestModifiers with TestTypeParams with TestImplicitlyAddedEntity {
81+
def paramLists = List()
82+
}
83+
trait TestTrait extends TestModifiers with TestTypeParams with TestSuperTypes with TestMembers with TestCompanion {
84+
def traitParams = List()
85+
}
86+
val ent = new TestEntity {}
87+
val members = new TestMembers {}
88+
val superTypes = new TestSuperTypes {}
89+
val modifiers = new TestModifiers {}
90+
val typeParams = new TestTypeParams {}
91+
val constructors = new TestConstructors {}
92+
val companion = new TestCompanion {}
93+
val returnValue = new TestReturnValue {}
94+
val implicitlyAddedEntity = new TestImplicitlyAddedEntity {}
95+
val typeAlias = new TestTypeAlias {}
96+
val df = new TestDef {}
97+
val trt = new TestTrait {}
98+
val ent_serialized = ent.asJava()
99+
val members_serialized = members.asJava()
100+
val superTypes_serialized = superTypes.asJava()
101+
val modifiers_serialized = modifiers.asJava()
102+
val typeParams_serialized = typeParams.asJava()
103+
val constructors_serialized = constructors.asJava()
104+
val companion_serialized = companion.asJava()
105+
val returnValue_serialized = returnValue.asJava()
106+
val implicitlyAddedEntity_serialized = implicitlyAddedEntity.asJava()
107+
val typeAlias_serialized = typeAlias.asJava()
108+
val def_serialized = df.asJava()
109+
val trait_serialized = trt.asJava()
110+
}
111+
}

0 commit comments

Comments
 (0)