[RESTORED] Light Classes: Constant expression evaluator for light annotation arguments

This commit is contained in:
Alexey Sedunov
2016-03-21 19:14:05 +03:00
parent f2e2220560
commit 962b312fb7
6 changed files with 124 additions and 7 deletions
@@ -44,6 +44,7 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil
import org.jetbrains.kotlin.resolve.lazy.NoDescriptorForDeclarationException
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
@@ -225,6 +226,8 @@ class IDELightClassGenerationSupport(private val project: Project) : LightClassG
}
}
override fun analyze(element: KtElement) = element.analyze(BodyResolveMode.PARTIAL)
override fun getFacadeNames(packageFqName: FqName, scope: GlobalSearchScope): Collection<String> {
val facadeFilesInPackage = KotlinFileFacadeClassByPackageIndex.getInstance().get(packageFqName.asString(), project, scope)
return facadeFilesInPackage.map { it.javaFileFacadeFqName.shortName().asString() }
+4
View File
@@ -641,6 +641,10 @@
<testCreator language="kotlin" implementationClass="org.jetbrains.kotlin.idea.testIntegration.KotlinTestCreator"/>
<testFinder implementation="org.jetbrains.kotlin.idea.testIntegration.KotlinTestFinder"/>
<constantExpressionEvaluator
language="kotlin"
implementationClass="org.jetbrains.kotlin.idea.KotlinLightConstantExpressionEvaluator"/>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.IfNullToElvisIntention</className>
<category>Kotlin</category>
@@ -0,0 +1,47 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea
import com.intellij.psi.PsiConstantEvaluationHelper
import com.intellij.psi.PsiElement
import com.intellij.psi.impl.ConstantExpressionEvaluator
import org.jetbrains.kotlin.asJava.KtLightAnnotation
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.resolve.DelegatingBindingTrace
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator as FrontendConstantExpressionEvaluator
class KotlinLightConstantExpressionEvaluator : ConstantExpressionEvaluator {
override fun computeConstantExpression(expression: PsiElement, throwExceptionOnOverflow: Boolean): Any? {
return computeExpression(expression, throwExceptionOnOverflow, null)
}
override fun computeExpression(
expression: PsiElement,
throwExceptionOnOverflow: Boolean,
auxEvaluator: PsiConstantEvaluationHelper.AuxEvaluator?
): Any? {
if (expression !is KtLightAnnotation.LightExpressionValue) return null
val expressionToCompute = expression.originalExpression ?: return null
val resolutionFacade = expressionToCompute.getResolutionFacade()
val evaluator = FrontendConstantExpressionEvaluator(resolutionFacade.moduleDescriptor.builtIns)
val evaluatorTrace = DelegatingBindingTrace(resolutionFacade.analyze(expressionToCompute), "Evaluating annotation argument")
val constant = evaluator.evaluateExpression(expressionToCompute, evaluatorTrace) ?: return null
if (constant.isError) return null
return constant.getValue(TypeUtils.NO_EXPECTED_TYPE)
}
}