Uast: Migrate to UAnnotation
This commit is contained in:
committed by
Yan Zhulanow
parent
fee54d9b86
commit
6ff29f473b
@@ -16,7 +16,11 @@
|
||||
|
||||
package org.jetbrains.uast.java
|
||||
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.JavaPsiFacade
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiExpression
|
||||
import com.intellij.psi.PsiType
|
||||
import org.jetbrains.uast.UAnnotation
|
||||
import org.jetbrains.uast.UExpression
|
||||
import org.jetbrains.uast.java.internal.JavaUElementWithComments
|
||||
import org.jetbrains.uast.psi.PsiElementBacked
|
||||
@@ -49,7 +53,10 @@ abstract class JavaAbstractUExpression : JavaAbstractUElement(), UExpression {
|
||||
val psi = (this as? PsiElementBacked)?.psi ?: return null
|
||||
return JavaPsiFacade.getInstance(psi.project).constantEvaluationHelper.computeConstantExpression(psi)
|
||||
}
|
||||
|
||||
|
||||
override val annotations: List<UAnnotation>
|
||||
get() = emptyList()
|
||||
|
||||
override fun getExpressionType(): PsiType? {
|
||||
val expression = (this as? PsiElementBacked)?.psi as? PsiExpression ?: return null
|
||||
return expression.type
|
||||
|
||||
@@ -109,7 +109,7 @@ class JavaUastLanguagePlugin(override val project: Project) : UastLanguagePlugin
|
||||
is PsiMethod -> el<UMethod> { JavaUMethod.create(element, this@JavaUastLanguagePlugin, parent) }
|
||||
is PsiClassInitializer -> el<UClassInitializer> { JavaUClassInitializer(element, parent) }
|
||||
is PsiVariable -> el<UVariable> { JavaUVariable.create(element, parent) }
|
||||
is UAnnotation -> el<UAnnotation> { SimpleUAnnotation(element, parent) } //???
|
||||
is PsiAnnotation -> el<UAnnotation> { JavaUAnnotation(element, parent) } //???
|
||||
else -> null
|
||||
}}
|
||||
}
|
||||
|
||||
+3
@@ -46,6 +46,9 @@ class DefaultUSwitchClauseExpression(override val containingElement: UElement?)
|
||||
override val caseValues: List<UExpression>?
|
||||
get() = null
|
||||
|
||||
override val annotations: List<UAnnotation>
|
||||
get() = emptyList()
|
||||
|
||||
override fun asLogString() = "DefaultUSwitchClauseExpression"
|
||||
override fun asRenderString() = "else -> "
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.jetbrains.uast.java
|
||||
|
||||
import com.intellij.psi.PsiAnnotation
|
||||
import com.intellij.psi.PsiClass
|
||||
import org.jetbrains.uast.*
|
||||
|
||||
class JavaUAnnotation(
|
||||
override val psi: PsiAnnotation,
|
||||
override val containingElement: UElement?
|
||||
) : UAnnotation {
|
||||
override val qualifiedName: String?
|
||||
get() = psi.qualifiedName
|
||||
|
||||
override val attributeValues: List<UNamedExpression> by lz {
|
||||
val context = getUastContext()
|
||||
val attributes = psi.parameterList.attributes
|
||||
|
||||
attributes.map { attribute ->
|
||||
UNamedExpression(attribute.name ?: "", this).apply {
|
||||
val value = attribute.value?.let { context.convertElement(it, this, null) } as? UExpression
|
||||
expression = value ?: UastEmptyExpression
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun resolve(): PsiClass? = psi.nameReferenceElement?.resolve() as? PsiClass
|
||||
|
||||
override fun findAttributeValue(name: String?): UNamedExpression? {
|
||||
val context = getUastContext()
|
||||
val attributeValue = psi.findAttributeValue(name) ?: return null
|
||||
val value = context.convertElement(attributeValue, this, null)
|
||||
return UNamedExpression(name ?: "", value)
|
||||
}
|
||||
|
||||
override fun findDeclaredAttributeValue(name: String?): UNamedExpression? {
|
||||
val context = getUastContext()
|
||||
val attributeValue = psi.findDeclaredAttributeValue(name) ?: return null
|
||||
val value = context.convertElement(attributeValue, this, null)
|
||||
return UNamedExpression(name ?: "", value)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun wrap(annotation: PsiAnnotation): UAnnotation = JavaUAnnotation(annotation, null)
|
||||
|
||||
@JvmStatic
|
||||
fun wrap(annotations: List<PsiAnnotation>): List<UAnnotation> = annotations.map { JavaUAnnotation(it, null) }
|
||||
|
||||
@JvmStatic
|
||||
fun wrap(annotations: Array<PsiAnnotation>): List<UAnnotation> = annotations.map { JavaUAnnotation(it, null) }
|
||||
}
|
||||
}
|
||||
@@ -34,12 +34,24 @@ abstract class AbstractJavaUClass : UClass, JavaUElementWithComments {
|
||||
override val uastAnchor: UElement?
|
||||
get() = UIdentifier(psi.nameIdentifier, this)
|
||||
|
||||
override val uastAnnotations by lz { psi.annotations.map { SimpleUAnnotation(it, this) } }
|
||||
|
||||
override val uastFields: List<UVariable> by lz { psi.fields.map { getLanguagePlugin().convert<UVariable>(it, this) } }
|
||||
override val uastInitializers: List<UClassInitializer> by lz { psi.initializers.map { getLanguagePlugin().convert<UClassInitializer>(it, this) } }
|
||||
override val uastMethods: List<UMethod> by lz { psi.methods.map { getLanguagePlugin().convert<UMethod>(it, this) } }
|
||||
override val uastNestedClasses: List<UClass> by lz { psi.innerClasses.map { getLanguagePlugin().convert<UClass>(it, this) } }
|
||||
override val annotations: List<UAnnotation>
|
||||
get() = psi.annotations.map { JavaUAnnotation(it, this) }
|
||||
|
||||
override val uastFields: List<UVariable> by lz {
|
||||
psi.fields.map { getLanguagePlugin().convert<UVariable>(it, this) }
|
||||
}
|
||||
|
||||
override val uastInitializers: List<UClassInitializer> by lz {
|
||||
psi.initializers.map { getLanguagePlugin().convert<UClassInitializer>(it, this) }
|
||||
}
|
||||
|
||||
override val uastMethods: List<UMethod> by lz {
|
||||
psi.methods.map { getLanguagePlugin().convert<UMethod>(it, this) }
|
||||
}
|
||||
|
||||
override val uastNestedClasses: List<UClass> by lz {
|
||||
psi.innerClasses.map { getLanguagePlugin().convert<UClass>(it, this) }
|
||||
}
|
||||
|
||||
override fun equals(other: Any?) = this === other
|
||||
override fun hashCode() = psi.hashCode()
|
||||
|
||||
@@ -33,7 +33,7 @@ class JavaUClassInitializer(
|
||||
getLanguagePlugin().convertElement(psi.body, this, null) as? UExpression ?: UastEmptyExpression
|
||||
}
|
||||
|
||||
override val uastAnnotations by lz { psi.annotations.map { SimpleUAnnotation(it, this) } }
|
||||
override val annotations by lz { psi.annotations.map { JavaUAnnotation(it, this) } }
|
||||
|
||||
override fun equals(other: Any?) = this === other
|
||||
override fun hashCode() = psi.hashCode()
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.jetbrains.uast.java
|
||||
import com.intellij.psi.PsiComment
|
||||
import com.intellij.psi.PsiJavaFile
|
||||
import com.intellij.psi.PsiRecursiveElementWalkingVisitor
|
||||
import org.jetbrains.uast.UAnnotation
|
||||
import org.jetbrains.uast.UComment
|
||||
import org.jetbrains.uast.UFile
|
||||
import org.jetbrains.uast.UastLanguagePlugin
|
||||
@@ -16,6 +17,9 @@ class JavaUFile(override val psi: PsiJavaFile, override val languagePlugin: Uast
|
||||
psi.importList?.allImportStatements?.map { JavaUImportStatement(it, this) } ?: listOf()
|
||||
}
|
||||
|
||||
override val annotations: List<UAnnotation>
|
||||
get() = emptyList()
|
||||
|
||||
override val classes by lz { psi.classes.map { JavaUClass.create(it, this) } }
|
||||
|
||||
override val allCommentsInFile by lz {
|
||||
|
||||
@@ -33,12 +33,15 @@ open class JavaUMethod(
|
||||
getLanguagePlugin().convertElement(body, this) as? UExpression
|
||||
}
|
||||
|
||||
override val uastAnnotations by lz { psi.annotations.map { SimpleUAnnotation(it, this) } }
|
||||
override val annotations by lz { psi.annotations.map { JavaUAnnotation(it, this) } }
|
||||
|
||||
override val uastParameters by lz {
|
||||
psi.parameterList.parameters.map { JavaUParameter(it, this) }
|
||||
}
|
||||
|
||||
override val isOverride: Boolean
|
||||
get() = psi.modifierList.findAnnotation("java.lang.Override") != null
|
||||
|
||||
override val uastAnchor: UElement
|
||||
get() = UIdentifier((psi.originalElement as? PsiNameIdentifierOwner)?.nameIdentifier ?: psi.nameIdentifier, this)
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ abstract class AbstractJavaUVariable : PsiVariable, UVariable, JavaUElementWithC
|
||||
getLanguagePlugin().convertElement(initializer, this) as? UExpression
|
||||
}
|
||||
|
||||
override val uastAnnotations by lz { psi.annotations.map { SimpleUAnnotation(it, this) } }
|
||||
override val annotations by lz { psi.annotations.map { JavaUAnnotation(it, this) } }
|
||||
override val typeReference by lz { getLanguagePlugin().convertOpt<UTypeReferenceExpression>(psi.typeElement, this) }
|
||||
|
||||
override val uastAnchor: UElement
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.jetbrains.uast.java
|
||||
|
||||
import com.intellij.psi.PsiLabeledStatement
|
||||
import org.jetbrains.uast.UElement
|
||||
import org.jetbrains.uast.UIdentifier
|
||||
import org.jetbrains.uast.ULabeledExpression
|
||||
import org.jetbrains.uast.psi.PsiElementBacked
|
||||
|
||||
@@ -24,7 +25,13 @@ class JavaULabeledExpression(
|
||||
override val psi: PsiLabeledStatement,
|
||||
override val containingElement: UElement?
|
||||
) : JavaAbstractUExpression(), ULabeledExpression, PsiElementBacked {
|
||||
override val label by lz { psi.labelIdentifier.text }
|
||||
override val label: String
|
||||
get() = psi.labelIdentifier.text
|
||||
|
||||
override val labelIdentifier: UIdentifier?
|
||||
get() = UIdentifier(psi.labelIdentifier, this)
|
||||
|
||||
override val expression by lz { JavaConverter.convertOrEmpty(psi.statement, this) }
|
||||
|
||||
override fun evaluate() = expression.evaluate()
|
||||
}
|
||||
@@ -17,10 +17,19 @@ package org.jetbrains.uast.java
|
||||
|
||||
import com.intellij.psi.PsiSuperExpression
|
||||
import org.jetbrains.uast.UElement
|
||||
import org.jetbrains.uast.UIdentifier
|
||||
import org.jetbrains.uast.USuperExpression
|
||||
import org.jetbrains.uast.psi.PsiElementBacked
|
||||
|
||||
class JavaUSuperExpression(
|
||||
override val psi: PsiSuperExpression,
|
||||
override val containingElement: UElement?
|
||||
) : JavaAbstractUExpression(), USuperExpression, PsiElementBacked
|
||||
) : JavaAbstractUExpression(), USuperExpression, PsiElementBacked {
|
||||
override val label: String?
|
||||
get() = psi.qualifier?.qualifiedName
|
||||
|
||||
override val labelIdentifier: UIdentifier?
|
||||
get() = psi.qualifier?.let { UIdentifier(it, this) }
|
||||
|
||||
override fun resolve() = psi.qualifier?.resolve()
|
||||
}
|
||||
@@ -17,10 +17,19 @@ package org.jetbrains.uast.java
|
||||
|
||||
import com.intellij.psi.PsiThisExpression
|
||||
import org.jetbrains.uast.UElement
|
||||
import org.jetbrains.uast.UIdentifier
|
||||
import org.jetbrains.uast.UThisExpression
|
||||
import org.jetbrains.uast.psi.PsiElementBacked
|
||||
|
||||
class JavaUThisExpression(
|
||||
override val psi: PsiThisExpression,
|
||||
override val containingElement: UElement?
|
||||
) : JavaAbstractUExpression(), UThisExpression, PsiElementBacked
|
||||
) : JavaAbstractUExpression(), UThisExpression, PsiElementBacked {
|
||||
override val label: String?
|
||||
get() = psi.qualifier?.qualifiedName
|
||||
|
||||
override val labelIdentifier: UIdentifier?
|
||||
get() = psi.qualifier?.let { UIdentifier(it, this) }
|
||||
|
||||
override fun resolve() = psi.qualifier?.resolve()
|
||||
}
|
||||
+4
@@ -1,5 +1,6 @@
|
||||
package org.jetbrains.uast.java
|
||||
|
||||
import org.jetbrains.uast.UAnnotation
|
||||
import org.jetbrains.uast.UElement
|
||||
import org.jetbrains.uast.UVariable
|
||||
import org.jetbrains.uast.UVariableDeclarationsExpression
|
||||
@@ -13,4 +14,7 @@ class JavaUVariableDeclarationsExpression(
|
||||
constructor(parent: UElement?, variables: List<UVariable>) : this(parent) {
|
||||
this.variables = variables
|
||||
}
|
||||
|
||||
override val annotations: List<UAnnotation>
|
||||
get() = emptyList()
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.jetbrains.uast.java
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.uast.UAnnotation
|
||||
import org.jetbrains.uast.UElement
|
||||
import org.jetbrains.uast.UExpression
|
||||
import org.jetbrains.uast.psi.PsiElementBacked
|
||||
@@ -25,4 +26,7 @@ class UnknownJavaExpression(
|
||||
override val containingElement: UElement?
|
||||
) : UExpression, PsiElementBacked {
|
||||
override fun asLogString() = "[!] UnknownJavaExpression ($psi)"
|
||||
|
||||
override val annotations: List<UAnnotation>
|
||||
get() = emptyList()
|
||||
}
|
||||
@@ -21,7 +21,6 @@ import com.intellij.psi.PsiAnnotation
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiModifierListOwner
|
||||
import com.intellij.psi.impl.source.tree.CompositeElement
|
||||
import com.intellij.psi.impl.source.tree.java.PsiDoWhileStatementImpl
|
||||
import com.intellij.psi.tree.IElementType
|
||||
import org.jetbrains.uast.UDeclaration
|
||||
import org.jetbrains.uast.UElement
|
||||
|
||||
Reference in New Issue
Block a user