Java-Uast: Support for package annotations

This commit is contained in:
Nicolay Mitropolsky
2017-10-04 14:07:25 +03:00
parent e209b7956a
commit ebf5134388
8 changed files with 27 additions and 1 deletions

View File

@@ -59,6 +59,10 @@ interface UFile : UElement, UAnnotated {
override fun asLogString() = log("package = $packageName")
override fun asRenderString() = buildString {
if (annotations.isNotEmpty()) {
annotations.joinTo(buffer = this, separator = "\n", postfix = "\n", transform = UAnnotation::asRenderString)
}
val packageName = this@UFile.packageName
if (packageName.isNotEmpty()) appendln("package $packageName").appendln()

View File

@@ -160,6 +160,7 @@ internal object JavaConverter {
is PsiAnnotationParameterList -> unwrapElements(element.parent)
is PsiModifierList -> unwrapElements(element.parent)
is PsiExpressionList -> unwrapElements(element.parent)
is PsiPackageStatement -> unwrapElements(element.parent)
else -> element
}

View File

@@ -33,7 +33,7 @@ class JavaUFile(override val psi: PsiJavaFile, override val languagePlugin: Uast
}
override val annotations: List<UAnnotation>
get() = emptyList()
get() = psi.packageStatement?.annotationList?.annotations?.map { JavaUAnnotation(it, this) } ?: emptyList()
override val classes by lz { psi.classes.map { JavaUClass.create(it, this) } }

View File

@@ -0,0 +1,2 @@
@org.apache.struts2.convention.annotation.ParentPackage("foo")
package testpackage;

View File

@@ -0,0 +1,4 @@
UFile (package = testpackage)
UAnnotation (fqName = org.apache.struts2.convention.annotation.ParentPackage)
UNamedExpression (name = null)
ULiteralExpression (value = "foo")

View File

@@ -0,0 +1,2 @@
@org.apache.struts2.convention.annotation.ParentPackage(null = "foo")
package testpackage

View File

@@ -19,6 +19,7 @@ import com.intellij.psi.PsiCallExpression
import com.intellij.psi.PsiLiteralExpression
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.testFramework.UsefulTestCase
import junit.framework.TestCase
import org.jetbrains.uast.*
import org.jetbrains.uast.test.env.findElementByText
import org.junit.Test
@@ -41,6 +42,16 @@ class JavaUastApiTest : AbstractJavaUastTest() {
}
}
@Test fun testPackageInfo() {
doTest("Simple/package-info.java") { name, file ->
val index2 = file.psi.text.indexOf("foo")
val literal = PsiTreeUtil.getParentOfType(file.psi.findElementAt(index2), PsiLiteralExpression::class.java)!!
val uLiteral = literal.toUElement()!!
UsefulTestCase.assertInstanceOf(uLiteral, ULiteralExpression::class.java)
TestCase.assertNotNull(uLiteral.getParentOfType<UAnnotation>())
}
}
@Test fun testCallExpression() {
doTest("Simple/CallExpression.java") { name, file ->
val index = file.psi.text.indexOf("format")

View File

@@ -39,4 +39,6 @@ class SimpleJavaRenderLogTest : AbstractJavaRenderLogTest() {
@Test fun testAnonymousClassWithParameters() = doTest("Simple/AnonymousClassWithParameters.java")
@Test fun testVariableAnnotation() = doTest("Simple/VariableAnnotation.java")
@Test fun testPackageInfo() = doTest("Simple/package-info.java")
}