Skip to content

Commit

Permalink
Fix for constructor member references
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Feb 3, 2017
1 parent 78b54bb commit 229ca03
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,14 @@ class OracleJdkParserVisitor(val path: Path, val source: String): TreePathScanne
}

override fun visitMemberReference(node: MemberReferenceTree, fmt: Formatting.Reified): Tree {
return Tr.MemberReference(
(node as JCTree.JCMemberReference).expr.convert { sourceBefore("::") },
Tr.Ident.build(node.name.toString(), null, format(sourceBefore(node.name.toString()))),
node.type(),
fmt
)
val expr = (node as JCTree.JCMemberReference).expr.convert<Expression> { sourceBefore("::") }
val referenceName = when(node.mode!!) {
MemberReferenceTree.ReferenceMode.NEW -> "new"
MemberReferenceTree.ReferenceMode.INVOKE -> node.name.toString()
}
val reference = Tr.Ident.build(referenceName, null, format(sourceBefore(referenceName)))

return Tr.MemberReference(expr, reference, node.type(), fmt)
}

override fun visitMemberSelect(node: MemberSelectTree, fmt: Formatting.Reified): Tree {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,19 @@ abstract class MemberReferenceTest(p: Parser): Parser by p {

assertEquals(0L, memberRefLatch.count)
}

@Test
fun constructorMethodReference() {
val a = parse("""
import java.util.*;
import java.util.stream.*;
public class A {
Stream<Integer> n = Stream.of(1, 2);
Set<Integer> n2 = n.collect(HashSet<Integer>::new, HashSet::add);
}
""")

val collect = a.classes[0].fields()[1].vars[0].initializer!!
assertEquals("n.collect(HashSet<Integer>::new, HashSet::add)", collect.printTrimmed())
}
}

0 comments on commit 229ca03

Please sign in to comment.