Skip to content

Commit c20e9e5

Browse files
authored
Merge pull request #115 from lightbend/flattenInnerClasses
Correctly flatten members of a class
2 parents e9181f1 + c7540a1 commit c20e9e5

File tree

11 files changed

+126
-21
lines changed

11 files changed

+126
-21
lines changed

plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ trait AST { this: TransformCake ⇒
3838
ret
3939
}
4040

41+
def classMembers = members.collect { case classInfo: ClassInfo => classInfo }
42+
def methodMembers = members.collect { case methodInfo: MethodInfo => methodInfo }
43+
4144
override def toString =
4245
s"ClassInfo($name, ${pattern("XXXXX", "AAAAA")}, module=$module, pckg=$pckg, ${filepattern("FFFFFF")}, interface=$interface, static=$static)" +
4346
comment.mkString("\n ", "\n ", "\n ") + members.mkString("\n ")

plugin/src/main/scala/com/typesafe/genjavadoc/Output.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ trait Output { this: TransformCake ⇒
7575
case (Some(o), Some(c)) merge(o, c, forwarders, staticScope)
7676
case (Some(o), None) if forwarders merge(o, fabricateCompanion(o), forwarders, staticScope)
7777
case (Some(o), None) Vector(mangleModule(o, addMODULE = forwarders, pruneClasses = false))
78-
case (None, Some(c)) Vector(c)
78+
case (None, Some(c)) Vector(c.copy(members = flatten(c.classMembers) ++ c.methodMembers.sortBy(_.name)))
7979
case (None, None) ???
8080
}
8181
}
@@ -104,7 +104,12 @@ trait Output { this: TransformCake ⇒
104104
Some(MethodInfo(x x, "public static final", s"${obj.name}$$ MODULE$$ = null;",
105105
Seq("/**", " * Static reference to the singleton instance of this Scala object.", " */")))
106106
else None
107-
val members = (moduleInstance ++: obj.members) filter (!pruneClasses || _.isInstanceOf[MethodInfo])
107+
108+
val members = moduleInstance ++: (
109+
if (pruneClasses) obj.methodMembers
110+
else flatten(obj.classMembers) ++ obj.methodMembers
111+
)
112+
108113
val (com: Seq[String], moduleMembers: Vector[Templ]) = ((obj.comment, Vector.empty[Templ]) /: members)((p, mem) mem match {
109114
case x: MethodInfo if x.name == obj.name (p._1 ++ x.comment, p._2 :+ x.copy(name = x.name + '$', comment = Seq()))
110115
case x (p._1, p._2 :+ x)
@@ -134,18 +139,19 @@ trait Output { this: TransformCake ⇒
134139
case m: MethodInfo if !(m.name == obj.name) && !cls.members.exists(_.name == m.name)
135140
m.copy(pattern = n "static " + m.pattern(n))
136141
}
137-
val exclude = (direct.iterator ++ methods.iterator).map(_.name).toSet
142+
val exclude = (direct.iterator ++ methods.iterator).map(_.name).toSet ++ this.javaKeywords
138143
direct ++ inheritedMethods(cls.sym, exclude)
139144
}
140145
val nestedClasses = flatten(classes, forwarders = false, staticScope = false)
141146
val nestedStaticClasses = flatten(staticClasses, forwarders = false, staticScope = staticScope)
142147
if (forwarders) {
143148
val base = cls.copy(members = nestedClasses ++ nestedStaticClasses ++ staticMethods ++ methods)
144-
flatten(Vector(base)) ++ Vector(mangleModule(obj, addMODULE = forwarders, pruneClasses = true))
149+
val mod = mangleModule(obj, addMODULE = forwarders, pruneClasses = true)
150+
Vector(base, mod)
145151
} else {
146152
val base = cls.copy(members = nestedClasses ++ staticMethods ++ methods)
147153
val mod = mangleModule(obj, addMODULE = forwarders, pruneClasses = true)
148-
flatten(Vector(base)) ++ Vector(mod.copy(members = nestedStaticClasses ++ mod.members))
154+
Vector(base, mod.copy(members = nestedStaticClasses ++ mod.members))
149155
}
150156
}
151157

plugin/src/test/resources/expected_output/basic/akka/rk/buh/is/it/A.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,18 @@ public class NonStatic {
6060
* class A.B
6161
*/
6262
public class B implements akka.rk.buh.is.it.X {
63-
/**
64-
* def b(args: java.lang.String*): Unit
65-
* @param args (undocumented)
66-
*/
67-
public void b (java.lang.String... args) { throw new RuntimeException(); }
6863
// not preceding
6964
public B () { throw new RuntimeException(); }
7065
/**
7166
* secondary constructor
7267
* @param stest (undocumented)
7368
*/
7469
public B (java.lang.String stest) { throw new RuntimeException(); }
70+
/**
71+
* def b(args: java.lang.String*): Unit
72+
* @param args (undocumented)
73+
*/
74+
public void b (java.lang.String... args) { throw new RuntimeException(); }
7575
/**
7676
* def b(args: java.lang.String*): Unit
7777
* @param args (undocumented)
@@ -80,9 +80,9 @@ public class B implements akka.rk.buh.is.it.X {
8080
public java.lang.String d (java.lang.String a, akka.rk.buh.is.it.X b) { throw new RuntimeException(); }
8181
}
8282
public class C implements akka.rk.buh.is.it.X {
83-
public int i () { throw new RuntimeException(); }
8483
// not preceding
8584
public C () { throw new RuntimeException(); }
85+
public int i () { throw new RuntimeException(); }
8686
}
8787
/**
8888
* class A.C

plugin/src/test/resources/expected_output/basic/akka/rk/buh/is/it/AnAbstractTypeRef.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
* AbstractTypeRef
44
*/
55
public interface AnAbstractTypeRef {
6-
public akka.rk.buh.is.it.AnAbstractTypeRef someMethod () ;
76
/**
87
* And a parameter type ref.
98
* @param t (undocumented)
109
* @param string (undocumented)
1110
* @return (undocumented)
1211
*/
1312
public akka.rk.buh.is.it.PTrait otherMethod (akka.rk.buh.is.it.PTrait t, java.lang.String string) ;
13+
public akka.rk.buh.is.it.AnAbstractTypeRef someMethod () ;
1414
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package akka.rk.buh.is.it;
2+
public class ClassWithInner {
3+
public class Foo {
4+
static public int companionObjectValue () { throw new RuntimeException(); }
5+
public Foo () { throw new RuntimeException(); }
6+
public int methodOfFoo (int i) { throw new RuntimeException(); }
7+
}
8+
public class Foo$ {
9+
/**
10+
* Static reference to the singleton instance of this Scala object.
11+
*/
12+
public static final Foo$ MODULE$ = null;
13+
public Foo$ () { throw new RuntimeException(); }
14+
public int companionObjectValue () { throw new RuntimeException(); }
15+
}
16+
public ClassWithInner () { throw new RuntimeException(); }
17+
public akka.rk.buh.is.it.ClassWithInner.Foo$ Foo () { throw new RuntimeException(); }
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package akka.rk.buh.is.it;
2+
public class CompressionProtocol$ {
3+
/**
4+
* Static reference to the singleton instance of this Scala object.
5+
*/
6+
public static final CompressionProtocol$ MODULE$ = null;
7+
public CompressionProtocol$ () { throw new RuntimeException(); }
8+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package akka.rk.buh.is.it;
2+
public class CompressionProtocol {
3+
static public class Events$ {
4+
/**
5+
* Static reference to the singleton instance of this Scala object.
6+
*/
7+
public static final Events$ MODULE$ = null;
8+
public final class HeavyHitterDetected implements akka.rk.buh.is.it.CompressionProtocol.Events.Event, scala.Product, scala.Serializable {
9+
static public akka.rk.buh.is.it.CompressionProtocol.Events.HeavyHitterDetected apply (Object key, int id, long count) { throw new RuntimeException(); }
10+
static public scala.Option<scala.Tuple3<java.lang.Object, java.lang.Object, java.lang.Object>> unapply (akka.rk.buh.is.it.CompressionProtocol.Events.HeavyHitterDetected x$0) { throw new RuntimeException(); }
11+
static private java.lang.Object readResolve () { throw new RuntimeException(); }
12+
public Object key () { throw new RuntimeException(); }
13+
public int id () { throw new RuntimeException(); }
14+
public long count () { throw new RuntimeException(); }
15+
// not preceding
16+
public HeavyHitterDetected (Object key, int id, long count) { throw new RuntimeException(); }
17+
public akka.rk.buh.is.it.CompressionProtocol.Events.HeavyHitterDetected copy (Object key, int id, long count) { throw new RuntimeException(); }
18+
// not preceding
19+
public Object copy$default$1 () { throw new RuntimeException(); }
20+
public int copy$default$2 () { throw new RuntimeException(); }
21+
public long copy$default$3 () { throw new RuntimeException(); }
22+
// not preceding
23+
public java.lang.String productPrefix () { throw new RuntimeException(); }
24+
public int productArity () { throw new RuntimeException(); }
25+
public Object productElement (int x$1) { throw new RuntimeException(); }
26+
public scala.collection.Iterator<java.lang.Object> productIterator () { throw new RuntimeException(); }
27+
public boolean canEqual (Object x$1) { throw new RuntimeException(); }
28+
public int hashCode () { throw new RuntimeException(); }
29+
public java.lang.String toString () { throw new RuntimeException(); }
30+
public boolean equals (Object x$1) { throw new RuntimeException(); }
31+
}
32+
public class HeavyHitterDetected$ extends scala.runtime.AbstractFunction3<java.lang.Object, java.lang.Object, java.lang.Object, akka.rk.buh.is.it.CompressionProtocol.Events.HeavyHitterDetected> implements scala.Serializable {
33+
/**
34+
* Static reference to the singleton instance of this Scala object.
35+
*/
36+
public static final HeavyHitterDetected$ MODULE$ = null;
37+
public HeavyHitterDetected$ () { throw new RuntimeException(); }
38+
public final java.lang.String toString () { throw new RuntimeException(); }
39+
public akka.rk.buh.is.it.CompressionProtocol.Events.HeavyHitterDetected apply (Object key, int id, long count) { throw new RuntimeException(); }
40+
public scala.Option<scala.Tuple3<java.lang.Object, java.lang.Object, java.lang.Object>> unapply (akka.rk.buh.is.it.CompressionProtocol.Events.HeavyHitterDetected x$0) { throw new RuntimeException(); }
41+
private java.lang.Object readResolve () { throw new RuntimeException(); }
42+
}
43+
public interface Event {
44+
}
45+
public Events$ () { throw new RuntimeException(); }
46+
}
47+
}

plugin/src/test/resources/expected_output/basic/akka/rk/buh/is/it/DontTouchThis.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
*/
77
public class DontTouchThis {
88
public DontTouchThis () { throw new RuntimeException(); }
9+
/**
10+
* @deprecated This is already deprecated. Since now.
11+
*/
12+
public void alreadyDeprecatedInComment () { throw new RuntimeException(); }
913
/**
1014
* Some methods are forever.
1115
*
1216
* @deprecated This is replaced by someDiamondsAre. Since now.
1317
*/
1418
public void orNotSoMuch () { throw new RuntimeException(); }
15-
/**
16-
* @deprecated This is already deprecated. Since now.
17-
*/
18-
public void alreadyDeprecatedInComment () { throw new RuntimeException(); }
1919
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.example;
22
class Clazz {
33
public Clazz () { throw new RuntimeException(); }
4+
private void t () { throw new RuntimeException(); }
5+
public void u () { throw new RuntimeException(); }
46
private void x () { throw new RuntimeException(); }
57
void y () { throw new RuntimeException(); }
68
void z () { throw new RuntimeException(); }
7-
private void t () { throw new RuntimeException(); }
8-
public void u () { throw new RuntimeException(); }
99
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.example;
22
public class PublicClazz {
33
public PublicClazz () { throw new RuntimeException(); }
4-
private void x () { throw new RuntimeException(); }
5-
void y () { throw new RuntimeException(); }
6-
void z () { throw new RuntimeException(); }
74
private void t () { throw new RuntimeException(); }
85
public void u () { throw new RuntimeException(); }
96
void v2 () { throw new RuntimeException(); }
107
void v3 () { throw new RuntimeException(); }
118
private void v4 () { throw new RuntimeException(); }
129
public void v5 () { throw new RuntimeException(); }
10+
private void x () { throw new RuntimeException(); }
11+
void y () { throw new RuntimeException(); }
12+
void z () { throw new RuntimeException(); }
1313
}

0 commit comments

Comments
 (0)