Skip to content

Commit

Permalink
Fix optional empty parentheses after annotation with no parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Feb 6, 2017
1 parent 1fb7870 commit 2314555
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,17 @@ class OracleJdkParserVisitor(val path: Path, val source: String): TreePathScanne
}

Tr.Annotation.Arguments(args, format(argsPrefix))
} else null
} else {
val remaining = source.substring(cursor, node.endPos())

// NOTE: technically, if there is code like this, we have a bug, but seems exceedingly unlikely:
// @MyAnnotation /* Comment () that contains parentheses */ ()

if(remaining.contains("(") && remaining.contains(")")) {
val parenPrefix = sourceBefore("(")
Tr.Annotation.Arguments(listOf(Tr.Empty(format(sourceBefore(")")))), format(parenPrefix))
} else null
}

return Tr.Annotation(name, args, node.type(), fmt)
}
Expand Down Expand Up @@ -1178,7 +1188,7 @@ class OracleJdkParserVisitor(val path: Path, val source: String): TreePathScanne
* and if not found in the remaining source, the empty String. If <code>stop</code> is reached before
* <code>untilDelim</code> return the empty String.
*/
private fun sourceBefore(untilDelim: String, stop: Char? = null): String {
private fun sourceBefore(untilDelim: String, stop: Char? = null, stopIndex: Int? = null): String {
val delimIndex = positionOfNext(untilDelim, stop)
if(delimIndex < 0) {
return "" // unable to find this delimiter
Expand All @@ -1189,7 +1199,7 @@ class OracleJdkParserVisitor(val path: Path, val source: String): TreePathScanne
return prefix
}

private fun positionOfNext(untilDelim: String, stop: Char? = null): Int {
private fun positionOfNext(untilDelim: String, stop: Char? = null, stopIndex: Int? = null): Int {
var delimIndex = cursor
var inMultiLineComment = false
var inSingleLineComment = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ abstract class AnnotationTest(p: Parser): Parser by p {
assertEquals("@SuppressWarnings(\"ALL\")", ann.printTrimmed())
}

@Test
fun preserveOptionalEmptyParentheses() {
val a = parse("""
|@Deprecated ( )
|public class A {}
""")

val ann = a.classes[0].annotations[0]

assertEquals("@Deprecated ( )", ann.printTrimmed())
}

@Test
fun default() {
val a = parse("""
Expand Down

0 comments on commit 2314555

Please sign in to comment.