Remove some usages of KotlinBuiltIns.getInstance()

Introduce DeclarationDescriptor.builtIns extension to get builtins where descriptors are available
Introduce various utilities in KotlinBuiltIns to check for primitive types and get fq names of builtins
This commit is contained in:
Pavel V. Talanov
2015-04-23 15:40:16 +03:00
parent 21d9c272c9
commit ac2cb9af74
71 changed files with 334 additions and 288 deletions
@@ -49,7 +49,6 @@ public class DeprecatedAnnotationVisitor extends AfterAnalysisHighlightingVisito
TokenSet.create(JetTokens.EQ, JetTokens.PLUSEQ, JetTokens.MINUSEQ, JetTokens.MULTEQ,
JetTokens.DIVEQ, JetTokens.PERCEQ, JetTokens.PLUSPLUS, JetTokens.MINUSMINUS);
private static final FqName JAVA_DEPRECATED = new FqName(Deprecated.class.getName());
private static final FqName KOTLIN_DEPRECATED = DescriptorUtils.getFqNameSafe(KotlinBuiltIns.getInstance().getDeprecatedAnnotation());
protected DeprecatedAnnotationVisitor(AnnotationHolder holder, BindingContext bindingContext) {
super(holder, bindingContext);
@@ -221,7 +220,7 @@ public class DeprecatedAnnotationVisitor extends AfterAnalysisHighlightingVisito
@Nullable
private static AnnotationDescriptor getDeprecated(DeclarationDescriptor descriptor) {
AnnotationDescriptor kotlinDeprecated = descriptor.getAnnotations().findAnnotation(KOTLIN_DEPRECATED);
AnnotationDescriptor kotlinDeprecated = descriptor.getAnnotations().findAnnotation(KotlinBuiltIns.FQ_NAMES.deprecated);
return kotlinDeprecated != null ? kotlinDeprecated : descriptor.getAnnotations().findAnnotation(JAVA_DEPRECATED);
}
@@ -44,6 +44,7 @@ import org.jetbrains.kotlin.idea.util.FuzzyType
import org.jetbrains.kotlin.idea.util.makeNotNullable
import org.jetbrains.kotlin.idea.util.nullability
import org.jetbrains.kotlin.idea.util.TypeNullability
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
class ArtificialElementInsertHandler(
val textBeforeCaret: String, val textAfterCaret: String, val shortenRefs: Boolean) : InsertHandler<LookupElement>{
@@ -233,10 +234,11 @@ fun functionType(function: FunctionDescriptor): JetType? {
null
else
extensionReceiverType ?: memberReceiverType
return KotlinBuiltIns.getInstance().getFunctionType(function.getAnnotations(),
receiverType,
function.getValueParameters().map { it.getType() },
function.getReturnType() ?: return null)
return function.builtIns.getFunctionType(
function.getAnnotations(), receiverType,
function.getValueParameters().map { it.getType() },
function.getReturnType() ?: return null
)
}
fun LookupElementFactory.createLookupElement(
@@ -268,9 +268,7 @@ public abstract class OverrideImplementMethodsHandler : LanguageCodeInsightActio
newDescriptor.addOverriddenDescriptor(descriptor)
val returnType = descriptor.getReturnType()
val builtIns = KotlinBuiltIns.getInstance()
val returnsNotUnit = returnType != null && builtIns.getUnitType() != returnType
val returnsNotUnit = returnType != null && !KotlinBuiltIns.isUnit(returnType)
val isAbstract = descriptor.getModality() == Modality.ABSTRACT
val delegation = generateUnsupportedOrSuperCall(classOrObject, descriptor)
@@ -34,6 +34,8 @@ import org.jetbrains.kotlin.types.JetType;
import java.util.ArrayList;
import java.util.List;
import static org.jetbrains.kotlin.builtins.KotlinBuiltIns.*;
public class CodeInsightUtils {
@Nullable
@@ -186,30 +188,28 @@ public class CodeInsightUtils {
@Nullable
public static String defaultInitializer(JetType type) {
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
if (type.isMarkedNullable()) {
return "null";
}
else if (type.equals(builtIns.getIntType()) || type.equals(builtIns.getLongType()) ||
type.equals(builtIns.getShortType()) || type.equals(builtIns.getByteType())) {
else if (isInt(type) || isLong(type) || isShort(type) || isByte(type)) {
return "0";
}
else if (type.equals(builtIns.getFloatType())) {
else if (isFloat(type)) {
return "0.0f";
}
else if (type.equals(builtIns.getDoubleType())) {
else if (isDouble(type)) {
return "0.0";
}
else if (type.equals(builtIns.getCharType())) {
else if (isChar(type)) {
return "'\\u0000'";
}
else if (type.equals(builtIns.getBooleanType())) {
else if (isBoolean(type)) {
return "false";
}
else if (type.equals(builtIns.getUnitType())) {
else if (isUnit(type)) {
return "Unit";
}
else if (type.equals(builtIns.getStringType())) {
else if (isString(type)) {
return "\"\"";
}
@@ -44,6 +44,8 @@ import org.jetbrains.kotlin.types.JetType;
import java.util.Collection;
import java.util.List;
import static org.jetbrains.kotlin.builtins.KotlinBuiltIns.isAny;
public class GotoSuperActionHandler implements CodeInsightActionHandler {
@Override
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
@@ -96,7 +98,7 @@ public class GotoSuperActionHandler implements CodeInsightActionHandler {
List<PsiElement> superDeclarations = ContainerUtil.mapNotNull(superDescriptors, new Function<DeclarationDescriptor, PsiElement>() {
@Override
public PsiElement fun(DeclarationDescriptor descriptor) {
if (KotlinBuiltIns.getInstance().getAny() == descriptor) {
if (descriptor instanceof ClassDescriptor && isAny((ClassDescriptor) descriptor)) {
return null;
}
return DescriptorToSourceUtils.descriptorToDeclaration(descriptor);
@@ -23,15 +23,15 @@ import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage;
import org.jetbrains.kotlin.psi.JetCallExpression;
import org.jetbrains.kotlin.psi.JetExpression;
import org.jetbrains.kotlin.psi.JetQualifiedExpression;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
import org.jetbrains.kotlin.types.JetType;
import static org.jetbrains.kotlin.builtins.KotlinBuiltIns.isUnit;
public abstract class KotlinExpressionSurrounder implements Surrounder {
@Override
@@ -45,7 +45,7 @@ public abstract class KotlinExpressionSurrounder implements Surrounder {
return false;
}
JetType type = ResolvePackage.analyze(expression, BodyResolveMode.PARTIAL).getType(expression);
if (type == null || type.equals(KotlinBuiltIns.getInstance().getUnitType())) {
if (type == null || isUnit(type)) {
return false;
}
return isApplicable(expression);
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage;
import org.jetbrains.kotlin.psi.JetExpression;
import org.jetbrains.kotlin.psi.JetParenthesizedExpression;
import org.jetbrains.kotlin.psi.JetPrefixExpression;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
import org.jetbrains.kotlin.types.JetType;
@@ -43,7 +42,7 @@ public class KotlinNotSurrounder extends KotlinExpressionSurrounder {
@Override
public boolean isApplicable(@NotNull JetExpression expression) {
JetType type = ResolvePackage.analyze(expression, BodyResolveMode.PARTIAL).getType(expression);
return KotlinBuiltIns.getInstance().getBooleanType().equals(type);
return type != null && KotlinBuiltIns.isBoolean(type);
}
@Nullable
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.psi.JetFunctionLiteralExpression
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
public open class ReplaceContainsIntention : AttributeCallReplacementIntention("replace.contains.with.in") {
@@ -32,10 +33,11 @@ public open class ReplaceContainsIntention : AttributeCallReplacementIntention("
}
override fun replaceCall(call: CallDescription, editor: Editor) {
val ret = call.resolved.getResultingDescriptor().getReturnType()
val resultingDescriptor = call.resolved.getResultingDescriptor()
val ret = resultingDescriptor.getReturnType()
?: return intentionFailed(editor, "undefined.returntype")
if (!JetTypeChecker.DEFAULT.isSubtypeOf(ret, KotlinBuiltIns.getInstance().getBooleanType())) {
if (!resultingDescriptor.builtIns.isBooleanOrSubtype(ret)) {
return intentionFailed(editor, "contains.returns.boolean")
}
@@ -92,12 +92,11 @@ fun JetExpression.guessTypes(
module: ModuleDescriptor,
coerceUnusedToUnit: Boolean = true
): Array<JetType> {
val builtIns = KotlinBuiltIns.getInstance()
if (coerceUnusedToUnit
&& this !is JetDeclaration
&& isUsedAsStatement(context)
&& getNonStrictParentOfType<JetAnnotationEntry>() == null) return array(builtIns.getUnitType())
&& getNonStrictParentOfType<JetAnnotationEntry>() == null) return array(module.builtIns.getUnitType())
// if we know the actual type of the expression
val theType1 = context.getType(this)
@@ -166,14 +165,14 @@ fun JetExpression.guessTypes(
val property = context[BindingContext.DECLARATION_TO_DESCRIPTOR, parent.getParent() as JetProperty] as PropertyDescriptor
val delegateClassName = if (property.isVar()) "ReadWriteProperty" else "ReadOnlyProperty"
val delegateClass = module.resolveTopLevelClass(FqName("kotlin.properties.$delegateClassName"))
?: return array(builtIns.getAnyType())
?: return array(module.builtIns.getAnyType())
val receiverType = (property.getExtensionReceiverParameter() ?: property.getDispatchReceiverParameter())?.getType()
?: builtIns.getNullableNothingType()
?: module.builtIns.getNullableNothingType()
val typeArguments = listOf(TypeProjectionImpl(receiverType), TypeProjectionImpl(property.getType()))
array(TypeUtils.substituteProjectionsForParameters(delegateClass, typeArguments))
}
parent is JetStringTemplateEntryWithExpression && parent.getExpression() == this -> {
array(KotlinBuiltIns.getInstance().getStringType())
array(module.builtIns.getStringType())
}
else -> array() // can't infer anything
}
@@ -37,12 +37,11 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.analyzer.AnalysisResult;
import org.jetbrains.kotlin.analyzer.AnalyzerPackage;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage;
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils;
import org.jetbrains.kotlin.idea.core.refactoring.JetNameSuggester;
import org.jetbrains.kotlin.idea.intentions.ConvertToBlockBodyIntention;
import org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention;
import org.jetbrains.kotlin.idea.core.refactoring.JetNameSuggester;
import org.jetbrains.kotlin.idea.refactoring.JetNameValidatorImpl;
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle;
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringUtil;
@@ -151,7 +150,7 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
return;
}
if (expressionType != null &&
JetTypeChecker.DEFAULT.equalTypes(KotlinBuiltIns.getInstance().getUnitType(), expressionType)) {
JetTypeChecker.DEFAULT.equalTypes(analysisResult.getModuleDescriptor().getBuiltIns().getUnitType(), expressionType)) {
showErrorHint(project, editor, JetRefactoringBundle.message("cannot.refactor.expression.has.unit.type"));
return;
}
@@ -38,6 +38,7 @@ import org.jetbrains.kotlin.psi.JetParameter
import org.jetbrains.kotlin.psi.JetVariableDeclaration
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
import org.jetbrains.kotlin.types.JetType
import java.util.ArrayList
@@ -97,7 +98,7 @@ public class AutomaticVariableRenamer(
private fun JetType.isCollectionLikeOf(classPsiElement: PsiNamedElement): Boolean {
val klass = this.getConstructor().getDeclarationDescriptor() as? ClassDescriptor ?: return false
if (KotlinBuiltIns.isArray(this) || DescriptorUtils.isSubclass(klass, KotlinBuiltIns.getInstance().getCollection())) {
if (KotlinBuiltIns.isArray(this) || DescriptorUtils.isSubclass(klass, klass.builtIns.getCollection())) {
val typeArgument = this.getArguments().singleOrNull()?.getType() ?: return false
val typePsiElement = ((typeArgument.getConstructor().getDeclarationDescriptor() as? ClassDescriptor)?.getSource() as? PsiSourceElement)?.psi
return classPsiElement == typePsiElement || typeArgument.isCollectionLikeOf(classPsiElement)