EA-39003 - ISE: ResolveSession.resolveToDescriptor
Lokking up classes and objects by PSI element, rather than by name
This commit is contained in:
+27
-19
@@ -65,21 +65,27 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
|
|||||||
|
|
||||||
if (allDescriptorsComputed) return null;
|
if (allDescriptorsComputed) return null;
|
||||||
|
|
||||||
JetClassOrObject classOrObjectDeclaration = declarationProvider.getClassOrObjectDeclaration(name);
|
Collection<JetClassOrObject> classOrObjectDeclarations = declarationProvider.getClassOrObjectDeclarations(name);
|
||||||
if (classOrObjectDeclaration == null) return null;
|
if (classOrObjectDeclarations.isEmpty()) return null;
|
||||||
|
|
||||||
// TODO: when enum entries with constructors are dropped, replace with declaresObjectOrEnumConstant()
|
ClassDescriptor result = null;
|
||||||
if (object != declaresObjectOrEnumConstant(classOrObjectDeclaration)) return null;
|
|
||||||
|
|
||||||
ClassDescriptor classDescriptor = new LazyClassDescriptor(resolveSession, thisDescriptor, name,
|
for (JetClassOrObject classOrObjectDeclaration : classOrObjectDeclarations) {
|
||||||
JetClassInfoUtil.createClassLikeInfo(classOrObjectDeclaration));
|
|
||||||
|
|
||||||
cache.put(name, classDescriptor);
|
// TODO: when enum entries with constructors are dropped, replace with declaresObjectOrEnumConstant()
|
||||||
if (!object) {
|
if (object != declaresObjectOrEnumConstant(classOrObjectDeclaration)) continue;
|
||||||
allDescriptors.add(classDescriptor);
|
|
||||||
|
ClassDescriptor classDescriptor = new LazyClassDescriptor(resolveSession, thisDescriptor, name,
|
||||||
|
JetClassInfoUtil.createClassLikeInfo(classOrObjectDeclaration));
|
||||||
|
|
||||||
|
cache.put(name, classDescriptor);
|
||||||
|
if (!object) {
|
||||||
|
allDescriptors.add(classDescriptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
result = classDescriptor;
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
return classDescriptor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean declaresObjectOrEnumConstant(JetClassOrObject declaration) {
|
private static boolean declaresObjectOrEnumConstant(JetClassOrObject declaration) {
|
||||||
@@ -149,15 +155,17 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Objects are also properties
|
// Objects are also properties
|
||||||
JetClassOrObject classOrObjectDeclaration = declarationProvider.getClassOrObjectDeclaration(name);
|
Collection<JetClassOrObject> classOrObjectDeclarations = declarationProvider.getClassOrObjectDeclarations(name);
|
||||||
if (declaresObjectOrEnumConstant(classOrObjectDeclaration)) {
|
for (JetClassOrObject classOrObjectDeclaration : classOrObjectDeclarations) {
|
||||||
ClassDescriptor classifier = getObjectDescriptor(name);
|
if (declaresObjectOrEnumConstant(classOrObjectDeclaration)) {
|
||||||
if (classifier == null) {
|
ClassDescriptor classifier = getObjectDescriptor(name);
|
||||||
throw new IllegalStateException("Object declaration " + name + " found in the DeclarationProvider " + declarationProvider + " but not in the scope " + this);
|
if (classifier == null) {
|
||||||
|
throw new IllegalStateException("Object declaration " + name + " found in the DeclarationProvider " + declarationProvider + " but not in the scope " + this);
|
||||||
|
}
|
||||||
|
VariableDescriptor propertyDescriptor = resolveSession.getInjector().getDescriptorResolver()
|
||||||
|
.resolveObjectDeclaration(thisDescriptor, classOrObjectDeclaration, classifier, resolveSession.getTrace());
|
||||||
|
result.add(propertyDescriptor);
|
||||||
}
|
}
|
||||||
VariableDescriptor propertyDescriptor = resolveSession.getInjector().getDescriptorResolver()
|
|
||||||
.resolveObjectDeclaration(thisDescriptor, classOrObjectDeclaration, classifier, resolveSession.getTrace());
|
|
||||||
result.add(propertyDescriptor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getNonDeclaredProperties(name, result);
|
getNonDeclaredProperties(name, result);
|
||||||
|
|||||||
+5
-4
@@ -16,16 +16,16 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.lang.resolve.lazy;
|
package org.jetbrains.jet.lang.resolve.lazy;
|
||||||
|
|
||||||
|
import com.google.common.collect.ArrayListMultimap;
|
||||||
import com.google.common.collect.HashMultimap;
|
import com.google.common.collect.HashMultimap;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
import com.google.common.collect.Multimap;
|
import com.google.common.collect.Multimap;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.lang.psi.*;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author abreslav
|
* @author abreslav
|
||||||
@@ -34,7 +34,7 @@ public abstract class AbstractPsiBasedDeclarationProvider implements Declaration
|
|||||||
private final List<JetDeclaration> allDeclarations = Lists.newArrayList();
|
private final List<JetDeclaration> allDeclarations = Lists.newArrayList();
|
||||||
private final Multimap<Name, JetNamedFunction> functions = HashMultimap.create();
|
private final Multimap<Name, JetNamedFunction> functions = HashMultimap.create();
|
||||||
private final Multimap<Name, JetProperty> properties = HashMultimap.create();
|
private final Multimap<Name, JetProperty> properties = HashMultimap.create();
|
||||||
private final Map<Name, JetClassOrObject> classesAndObjects = Maps.newHashMap();
|
private final Multimap<Name, JetClassOrObject> classesAndObjects = ArrayListMultimap.create(); // order matters here
|
||||||
|
|
||||||
private boolean indexCreated = false;
|
private boolean indexCreated = false;
|
||||||
|
|
||||||
@@ -92,8 +92,9 @@ public abstract class AbstractPsiBasedDeclarationProvider implements Declaration
|
|||||||
return Lists.newArrayList(properties.get(name));
|
return Lists.newArrayList(properties.get(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public JetClassOrObject getClassOrObjectDeclaration(@NotNull Name name) {
|
public Collection<JetClassOrObject> getClassOrObjectDeclarations(@NotNull Name name) {
|
||||||
createIndex();
|
createIndex();
|
||||||
return classesAndObjects.get(name);
|
return classesAndObjects.get(name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
package org.jetbrains.jet.lang.resolve.lazy;
|
package org.jetbrains.jet.lang.resolve.lazy;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
||||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
||||||
import org.jetbrains.jet.lang.psi.JetNamedFunction;
|
import org.jetbrains.jet.lang.psi.JetNamedFunction;
|
||||||
@@ -39,6 +38,6 @@ public interface DeclarationProvider {
|
|||||||
@NotNull
|
@NotNull
|
||||||
Collection<JetProperty> getPropertyDeclarations(@NotNull Name name);
|
Collection<JetProperty> getPropertyDeclarations(@NotNull Name name);
|
||||||
|
|
||||||
@Nullable
|
@NotNull
|
||||||
JetClassOrObject getClassOrObjectDeclaration(@NotNull Name name);
|
Collection<JetClassOrObject> getClassOrObjectDeclarations(@NotNull Name name);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -64,8 +64,9 @@ public class EmptyPackageMemberDeclarationProvider implements PackageMemberDecla
|
|||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public JetClassOrObject getClassOrObjectDeclaration(@NotNull Name name) {
|
public Collection<JetClassOrObject> getClassOrObjectDeclarations(@NotNull Name name) {
|
||||||
return null;
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,10 +28,7 @@ import org.jetbrains.jet.di.InjectorForLazyResolve;
|
|||||||
import org.jetbrains.jet.lang.ModuleConfiguration;
|
import org.jetbrains.jet.lang.ModuleConfiguration;
|
||||||
import org.jetbrains.jet.lang.descriptors.*;
|
import org.jetbrains.jet.lang.descriptors.*;
|
||||||
import org.jetbrains.jet.lang.psi.*;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
import org.jetbrains.jet.lang.resolve.*;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
|
||||||
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
|
|
||||||
import org.jetbrains.jet.lang.resolve.ObservableBindingTrace;
|
|
||||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||||
import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
|
import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
|
||||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||||
@@ -158,14 +155,23 @@ public class ResolveSession {
|
|||||||
JetScope resolutionScope = getInjector().getScopeProvider().getResolutionScopeForDeclaration(classOrObject);
|
JetScope resolutionScope = getInjector().getScopeProvider().getResolutionScopeForDeclaration(classOrObject);
|
||||||
Name name = classOrObject.getNameAsName();
|
Name name = classOrObject.getNameAsName();
|
||||||
assert name != null : "Name is null for " + classOrObject + " " + classOrObject.getText();
|
assert name != null : "Name is null for " + classOrObject + " " + classOrObject.getText();
|
||||||
ClassifierDescriptor classifier = resolutionScope.getClassifier(name);
|
|
||||||
if (classifier == null) {
|
// Why not use the result here. Because it may be that there is a redeclaration:
|
||||||
classifier = resolutionScope.getObjectDescriptor(name);
|
// class A {} class A { fun foo(): A<completion here>}
|
||||||
|
// and if we find teh class by name only, we may b-not get the right one.
|
||||||
|
// This call is only needed to make sure the classes are written to trace
|
||||||
|
resolutionScope.getClassifier(name);
|
||||||
|
DeclarationDescriptor declaration = getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, classOrObject);
|
||||||
|
|
||||||
|
if (declaration == null) {
|
||||||
|
// Why not use the result here. See the comment
|
||||||
|
resolutionScope.getObjectDescriptor(name);
|
||||||
|
declaration = getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, classOrObject);
|
||||||
}
|
}
|
||||||
if (classifier == null) {
|
if (declaration == null) {
|
||||||
throw new IllegalArgumentException("Could not find a classifier for " + classOrObject + " " + classOrObject.getText());
|
throw new IllegalArgumentException("Could not find a classifier for " + classOrObject + " " + classOrObject.getText());
|
||||||
}
|
}
|
||||||
return (ClassDescriptor) classifier;
|
return (ClassDescriptor) declaration;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*package*/ LazyClassDescriptor getClassObjectDescriptor(JetClassObject classObject) {
|
/*package*/ LazyClassDescriptor getClassObjectDescriptor(JetClassObject classObject) {
|
||||||
|
|||||||
@@ -308,7 +308,20 @@ public class ResolveSessionUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Collection<ClassDescriptor> getClassDescriptorsByFqName(@NotNull ResolveSession resolveSession, @NotNull FqName fqName) {
|
@NotNull
|
||||||
|
public static Collection<ClassDescriptor> getClassDescriptorsByFqName(
|
||||||
|
@NotNull ResolveSession resolveSession,
|
||||||
|
@NotNull FqName fqName
|
||||||
|
) {
|
||||||
|
return getClassOrObjectDescriptorsByFqName(resolveSession, fqName, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static Collection<ClassDescriptor> getClassOrObjectDescriptorsByFqName(
|
||||||
|
@NotNull ResolveSession resolveSession,
|
||||||
|
@NotNull FqName fqName,
|
||||||
|
boolean includeObjectDeclarations
|
||||||
|
) {
|
||||||
if (fqName.isRoot()) {
|
if (fqName.isRoot()) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
@@ -320,7 +333,8 @@ public class ResolveSessionUtils {
|
|||||||
NamespaceDescriptor packageDescriptor = resolveSession.getPackageDescriptorByFqName(packageFqName);
|
NamespaceDescriptor packageDescriptor = resolveSession.getPackageDescriptorByFqName(packageFqName);
|
||||||
if (packageDescriptor != null) {
|
if (packageDescriptor != null) {
|
||||||
FqName classInPackagePath = new FqName(QualifiedNamesUtil.tail(packageFqName, fqName));
|
FqName classInPackagePath = new FqName(QualifiedNamesUtil.tail(packageFqName, fqName));
|
||||||
Collection<ClassDescriptor> descriptors = getClassDescriptorsByFqName(packageDescriptor, classInPackagePath);
|
Collection<ClassDescriptor> descriptors = getClassOrObjectDescriptorsByFqName(packageDescriptor, classInPackagePath,
|
||||||
|
includeObjectDeclarations);
|
||||||
classDescriptors.addAll(descriptors);
|
classDescriptors.addAll(descriptors);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,7 +349,11 @@ public class ResolveSessionUtils {
|
|||||||
return classDescriptors;
|
return classDescriptors;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Collection<ClassDescriptor> getClassDescriptorsByFqName(NamespaceDescriptor packageDescriptor, FqName path) {
|
private static Collection<ClassDescriptor> getClassOrObjectDescriptorsByFqName(
|
||||||
|
NamespaceDescriptor packageDescriptor,
|
||||||
|
FqName path,
|
||||||
|
boolean includeObjectDeclarations
|
||||||
|
) {
|
||||||
if (path.isRoot()) {
|
if (path.isRoot()) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
@@ -358,14 +376,20 @@ public class ResolveSessionUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Name shortName = path.shortName();
|
Name shortName = path.shortName();
|
||||||
Collection<ClassDescriptor> resultClassDescriptors = Lists.newArrayList();
|
Collection<ClassDescriptor> resultClassifierDescriptors = Lists.newArrayList();
|
||||||
for (JetScope scope : scopes) {
|
for (JetScope scope : scopes) {
|
||||||
ClassifierDescriptor classifier = scope.getClassifier(shortName);
|
ClassifierDescriptor classifier = scope.getClassifier(shortName);
|
||||||
if (classifier instanceof ClassDescriptor) {
|
if (classifier instanceof ClassDescriptor) {
|
||||||
resultClassDescriptors.add((ClassDescriptor) classifier);
|
resultClassifierDescriptors.add((ClassDescriptor) classifier);
|
||||||
|
}
|
||||||
|
if (includeObjectDeclarations) {
|
||||||
|
ClassDescriptor objectDescriptor = scope.getObjectDescriptor(shortName);
|
||||||
|
if (objectDescriptor != null) {
|
||||||
|
resultClassifierDescriptors.add(objectDescriptor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return resultClassDescriptors;
|
return resultClassifierDescriptors;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -158,7 +158,9 @@ public class JetShortNamesCache extends PsiShortNamesCache {
|
|||||||
|
|
||||||
Collection<JetObjectDeclaration> topObjects = JetTopLevelShortObjectNameIndex.getInstance().get(name, project, scope);
|
Collection<JetObjectDeclaration> topObjects = JetTopLevelShortObjectNameIndex.getInstance().get(name, project, scope);
|
||||||
for (JetObjectDeclaration objectDeclaration : topObjects) {
|
for (JetObjectDeclaration objectDeclaration : topObjects) {
|
||||||
result.add(resolveSession.getClassDescriptor(objectDeclaration));
|
FqName fqName = JetPsiUtil.getFQName(objectDeclaration);
|
||||||
|
assert fqName != null : "Local object declaration in JetTopLevelShortObjectNameIndex:" + objectDeclaration.getText();
|
||||||
|
result.addAll(ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(resolveSession, fqName, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (PsiClass psiClass : JetFromJavaDescriptorHelper
|
for (PsiClass psiClass : JetFromJavaDescriptorHelper
|
||||||
@@ -166,7 +168,7 @@ public class JetShortNamesCache extends PsiShortNamesCache {
|
|||||||
String qualifiedName = psiClass.getQualifiedName();
|
String qualifiedName = psiClass.getQualifiedName();
|
||||||
if (qualifiedName != null) {
|
if (qualifiedName != null) {
|
||||||
FqName fqName = new FqName(qualifiedName);
|
FqName fqName = new FqName(qualifiedName);
|
||||||
result.addAll(ResolveSessionUtils.getClassDescriptorsByFqName(resolveSession, fqName));
|
result.addAll(ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(resolveSession, fqName, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
-2
@@ -22,6 +22,8 @@ import org.jetbrains.jet.lang.psi.JetDeclaration;
|
|||||||
import org.jetbrains.jet.lang.resolve.lazy.DeclarationProvider;
|
import org.jetbrains.jet.lang.resolve.lazy.DeclarationProvider;
|
||||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,9 +36,10 @@ public abstract class AbstractStubDeclarationProvider implements DeclarationProv
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public JetClassOrObject getClassOrObjectDeclaration(@NotNull Name name) {
|
public Collection<JetClassOrObject> getClassOrObjectDeclarations(@NotNull Name name) {
|
||||||
// TODO:
|
// TODO:
|
||||||
return null;
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
class A {
|
||||||
|
fun f() : A<caret>
|
||||||
|
}
|
||||||
|
|
||||||
|
class A {
|
||||||
|
fun f() : A
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXIST: A
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
class A {
|
||||||
|
fun f() : A
|
||||||
|
}
|
||||||
|
|
||||||
|
class A {
|
||||||
|
fun f() : A<caret>
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXIST: A
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
object A {
|
||||||
|
fun f() : S<caret>
|
||||||
|
}
|
||||||
|
|
||||||
|
object A {
|
||||||
|
fun f() : S
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXIST: String
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
object A {
|
||||||
|
fun f() : S
|
||||||
|
}
|
||||||
|
|
||||||
|
object A {
|
||||||
|
fun f() : S<caret>
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXIST: String
|
||||||
@@ -269,6 +269,22 @@ public class JetBasicCompletionTest extends JetCompletionTestBase {
|
|||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testClassRedeclaration1() {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testClassRedeclaration2() {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testObjectRedeclaration1() {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testObjectRedeclaration2() {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
protected String getTestDataPath() {
|
protected String getTestDataPath() {
|
||||||
|
|||||||
Reference in New Issue
Block a user