Introduce ClassDescriptor#getStaticScope()

Will be used to exterminate hacks related to static Java methods in our
codebase: synthetic class object for enum, synthetic package for static members
of Java classes, etc.
This commit is contained in:
Alexander Udalov
2014-09-03 12:03:13 +04:00
parent 7ad88f7799
commit 75df4a9ad8
10 changed files with 98 additions and 3 deletions
@@ -57,8 +57,9 @@ public class MutableClassDescriptor extends ClassDescriptorBase implements Class
private final WritableScope scopeForMemberResolution;
// This scope contains type parameters but does not contain inner classes
private final WritableScope scopeForSupertypeResolution;
private WritableScope scopeForInitializers = null; //contains members + primary constructor value parameters + map for backing fields
private WritableScope scopeForInitializers; //contains members + primary constructor value parameters + map for backing fields
private JetScope scopeForMemberLookup;
private final JetScope staticScope = new StaticScopeForKotlinClass(this);
public MutableClassDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@@ -295,6 +296,12 @@ public class MutableClassDescriptor extends ClassDescriptorBase implements Class
return (WritableScope) scopeForMemberLookup;
}
@NotNull
@Override
public JetScope getStaticScope() {
return staticScope;
}
public void lockScopes() {
getScopeForMemberLookupAsWritableScope().changeLockLevel(WritableScope.LockLevel.READING);
if (classObjectDescriptor instanceof MutableClassDescriptor) {
@@ -88,6 +88,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
private final MemoizedFunctionToNotNull<JetClassObject, ClassDescriptor> extraClassObjectDescriptors;
private final LazyClassMemberScope unsubstitutedMemberScope;
private final JetScope staticScope = new StaticScopeForKotlinClass(this);
private final NotNullLazyValue<JetScope> scopeForClassHeaderResolution;
private final NotNullLazyValue<JetScope> scopeForMemberDeclarationResolution;
@@ -300,6 +301,12 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
scope, getScopeForMemberDeclarationResolution());
}
@NotNull
@Override
public JetScope getStaticScope() {
return staticScope;
}
@NotNull
@Override
public Collection<ConstructorDescriptor> getConstructors() {
@@ -47,6 +47,7 @@ import org.jetbrains.jet.lang.types.checker.JetTypeChecker
import org.jetbrains.jet.lang.resolve.java.descriptor.JavaClassStaticsPackageFragmentDescriptor
import org.jetbrains.jet.lang.types.AbstractClassTypeConstructor
import org.jetbrains.jet.lang.descriptors.impl.EnumClassObjectDescriptor
import org.jetbrains.jet.lang.resolve.scopes.StaticScopeForKotlinClass
class LazyJavaClassDescriptor(
private val outerC: LazyJavaResolverContextWithTypes,
@@ -90,6 +91,8 @@ class LazyJavaClassDescriptor(
private val _innerClassesScope = InnerClassesScopeWrapper(getScopeForMemberLookup())
override fun getUnsubstitutedInnerClassesScope(): JetScope = _innerClassesScope
override fun getStaticScope(): JetScope = StaticScopeForKotlinClass(this) // TODO
override fun getUnsubstitutedPrimaryConstructor(): ConstructorDescriptor? = null
private val _classObjectDescriptor = c.storageManager.createNullableLazyValue {
@@ -28,13 +28,15 @@ import java.util.Collection;
import java.util.List;
public interface ClassDescriptor extends ClassifierDescriptor, MemberDescriptor, ClassOrPackageFragmentDescriptor {
@NotNull
JetScope getMemberScope(@NotNull List<? extends TypeProjection> typeArguments);
@NotNull
JetScope getUnsubstitutedInnerClassesScope();
@NotNull
JetScope getStaticScope();
@NotNull
@ReadOnly
Collection<ConstructorDescriptor> getConstructors();
@@ -22,6 +22,7 @@ import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.StaticScopeForKotlinClass;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeConstructor;
import org.jetbrains.jet.lang.types.TypeConstructorImpl;
@@ -34,6 +35,7 @@ import java.util.Set;
public class ClassDescriptorImpl extends ClassDescriptorBase {
private final Modality modality;
private final TypeConstructor typeConstructor;
private final JetScope staticScope = new StaticScopeForKotlinClass(this);
private JetScope scopeForMemberLookup;
private Set<ConstructorDescriptor> constructors;
@@ -91,6 +93,12 @@ public class ClassDescriptorImpl extends ClassDescriptorBase {
return scopeForMemberLookup;
}
@NotNull
@Override
public JetScope getStaticScope() {
return staticScope;
}
@Nullable
@Override
public ClassDescriptor getClassObjectDescriptor() {
@@ -35,6 +35,7 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScopeImpl
import org.jetbrains.jet.utils.Printer
import java.util.ArrayList
import kotlin.properties.Delegates
import org.jetbrains.jet.lang.resolve.scopes.StaticScopeForKotlinClass
public class EnumClassObjectDescriptor(
storageManager: StorageManager,
@@ -52,9 +53,12 @@ public class EnumClassObjectDescriptor(
listOf(), listOf(KotlinBuiltIns.getInstance().getAnyType()))
private val scope = EnumClassObjectScope()
private val staticScope = StaticScopeForKotlinClass(this)
override fun getScopeForMemberLookup(): JetScope = scope
override fun getStaticScope(): JetScope = staticScope
override fun getClassObjectDescriptor(): ClassDescriptor? = null
override fun getConstructors(): List<ConstructorDescriptor> = listOf(primaryConstructor)
@@ -28,6 +28,7 @@ import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.name.SpecialNames;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.JetScopeImpl;
import org.jetbrains.jet.lang.resolve.scopes.StaticScopeForKotlinClass;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeConstructor;
import org.jetbrains.jet.lang.types.TypeConstructorImpl;
@@ -46,6 +47,7 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
private final TypeConstructor typeConstructor;
private final ConstructorDescriptor primaryConstructor;
private final JetScope scope;
private final JetScope staticScope = new StaticScopeForKotlinClass(this);
private final EnumEntrySyntheticClassDescriptor classObjectDescriptor;
private final NotNullLazyValue<Collection<Name>> enumMemberNames;
@@ -102,6 +104,12 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
return scope;
}
@NotNull
@Override
public JetScope getStaticScope() {
return staticScope;
}
@NotNull
@Override
public Collection<ConstructorDescriptor> getConstructors() {
@@ -97,6 +97,12 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
return new SubstitutingScope(memberScope, getSubstitutor());
}
@NotNull
@Override
public JetScope getStaticScope() {
return original.getStaticScope();
}
@NotNull
@Override
public JetType getDefaultType() {
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2014 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.jet.lang.resolve.scopes
import org.jetbrains.jet.lang.resolve.name.Name
import org.jetbrains.jet.lang.descriptors.*
import org.jetbrains.jet.utils.Printer
public class StaticScopeForKotlinClass(
private val containingClass: ClassDescriptor
) : JetScope {
override fun getClassifier(name: Name) = null // TODO
override fun getAllDescriptors() = listOf<DeclarationDescriptor>() // TODO
override fun getOwnDeclaredDescriptors() = listOf<DeclarationDescriptor>() // TODO
override fun getPackage(name: Name) = null
override fun getProperties(name: Name) = listOf<VariableDescriptor>()
override fun getLocalVariable(name: Name) = null
override fun getFunctions(name: Name) = listOf<FunctionDescriptor>()
override fun getContainingDeclaration() = containingClass
override fun getDeclarationsByLabel(labelName: Name) = listOf<DeclarationDescriptor>()
override fun getImplicitReceiversHierarchy() = listOf<ReceiverParameterDescriptor>()
override fun printScopeStructure(p: Printer) {
p.println("Static scope for $containingClass")
}
}
@@ -36,6 +36,7 @@ import org.jetbrains.jet.lang.resolve.DescriptorFactory;
import org.jetbrains.jet.lang.resolve.OverridingUtil;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.StaticScopeForKotlinClass;
import org.jetbrains.jet.lang.types.AbstractClassTypeConstructor;
import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.JetType;
@@ -61,8 +62,8 @@ public class DeserializedClassDescriptor extends AbstractClassDescriptor impleme
private final NotNullLazyValue<Annotations> annotations;
private final NullableLazyValue<ClassDescriptor> classObjectDescriptor;
private final NestedClassDescriptors nestedClasses;
private final JetScope staticScope = new StaticScopeForKotlinClass(this);
private final NotNullLazyValue<DeclarationDescriptor> containingDeclaration;
private final DeserializedClassTypeConstructor typeConstructor;
@@ -194,6 +195,12 @@ public class DeserializedClassDescriptor extends AbstractClassDescriptor impleme
return memberScope;
}
@NotNull
@Override
public JetScope getStaticScope() {
return staticScope;
}
@Nullable
private ConstructorDescriptor computePrimaryConstructor() {
if (!classProto.hasPrimaryConstructor()) return null;