Permalink
Browse files

Annotated trees processing is modified

1. Problem with multiple parameter lists in annotations is resolved
2. Tests for Annotated trees are added
  • Loading branch information...
1 parent 2357e5d commit 68ba3efba997593ee24d1d72512ee4120b8fca6b @VladimirNik VladimirNik committed Jan 6, 2014
@@ -297,19 +297,6 @@ trait Printers extends api.Printers { self: SymbolTable =>
print("this")
}
- protected def printAnnotated(tree: Annotated)(printArg: => Unit) {
- val Annotated(Apply(Select(New(tpt), nme.CONSTRUCTOR), args), atree) = tree
-
- def printAnnot() {
- print("@", tpt)
- if (args.nonEmpty)
- printRow(args, "(", ",", ")")
- }
- printArg
- printAnnot()
- }
-
-
def printTree(tree: Tree) {
tree match {
case EmptyTree =>
@@ -473,7 +460,13 @@ trait Printers extends api.Printers { self: SymbolTable =>
}
case an @ Annotated(Apply(Select(New(tpt), nme.CONSTRUCTOR), args), tree) =>
- printAnnotated(an)(print(tree, if (tree.isType) " " else ": "))
+ def printAnnot() {
+ print("@", tpt)
+ if (args.nonEmpty)
+ printRow(args, "(", ",", ")")
+ }
+ print(tree, if (tree.isType) " " else ": ")
+ printAnnot()
case SingletonTypeTree(ref) =>
print(ref, ".type")
@@ -677,17 +670,20 @@ trait Printers extends api.Printers { self: SymbolTable =>
override def printAnnotations(tree: MemberDef) {
val annots = tree.mods.annotations
- annots foreach {
+ annots foreach {annot => printAnnot(annot); print(" ")}
+ }
+
+ protected def printAnnot(tree: Tree) {
+ tree match {
case treeInfo.Applied(core, _, argss) =>
print("@")
core match {
case Select(New(tree), _) => print(tree)
case _ =>
}
printArgss(argss)
- print(" ")
case _ => super.printTree(tree)
- }
+ }
}
override def printTree(tree: Tree) {
@@ -972,11 +968,10 @@ trait Printers extends api.Printers { self: SymbolTable =>
print(printValue)
}
- case an @ Annotated(Apply(Select(New(tpt), nme.CONSTRUCTOR), args), tree) =>
- printAnnotated(an) {
- val printParentheses = needsParentheses(tree)()
- parenthesize(printParentheses) { print(tree) }; print(if (tree.isType) " " else ": ")
- }
+ case an @ Annotated(ap, tree) =>
+ val printParentheses = needsParentheses(tree)()
+ parenthesize(printParentheses) { print(tree) }; print(if (tree.isType) " " else ": ")
+ printAnnot(ap)
case SelectFromTypeTree(qualifier, selector) =>
print("(", qualifier, ")#", blankForOperatorName(selector), printedName(selector))
@@ -692,6 +692,35 @@ trait ValAndDefPrintTests {
@Test def testDefWithAnn4 = assertPrintedCode("@Foo(a)(b)(x, y) def foo = null")
@Test def testDefWithAnn5 = assertPrintedCode("@Foo[A, B](a)(b) @Bar def foo(x: Int) = null")
+
+ @Test def testDefWithAnn6 = assertPrintedCode("@test1(new test2()) def foo = 42")
+
+ @Test def testDefWithAnn7 = assertPrintedCode("@`t*` def foo = 42")
+
+ @Test def testDefWithAnn8 = assertPrintedCode("@throws(classOf[Exception]) def foo = throw new Exception()")
+
+ @Test def testAnnotated1 = assertPrintedCode("def foo = 42: @test1")
+
+ @Test def testAnnotated2 = assertPrintedCode("""def foo = 42: @test1(42, z = "5")""")
+
+ @Test def testAnnotated3 = assertPrintedCode("def foo = (42: @test1): @test2(new test1())")
+
+ @Test def testAnnotated4 = assertPrintedCode("""def foo = 42: @test1(4, "testing")(4.2)""")
+
+ @Test def testAnnotated5 = assertPrintedCode("""def foo = (42: @test1(4, "testing")(4.2)): @test2(1, "bar")(3.14)""")
+
+ @Test def testAnnotated6 = assertPrintedCode("def foo = ((42: @test1): @test2(new test1())): @test3(1)(2, 3)(4)")
+
+ @Test def testAnnotated7 = assertPrintedCode(sm"""
+ |(x: @unchecked) match {
+ | case ((_): Int) => true
+ | case _ => false
+ |}""")
+
+ @Test def testAnnotated8 = assertPrintedCode(sm"""
+ |((x: @unchecked): @test1(1, "testing")(3.14)) match {
+ | case _ => true
+ |}""")
}
trait PackagePrintTests {

0 comments on commit 68ba3ef

Please sign in to comment.