Change enum entry resolution strategy in JDR
Enum entry is now resolved into a class with a class object, which inherits from the former class, as in the other parts of the compiler. Create a special class EnumEntrySyntheticClassDescriptor which will be reused in deserialization later
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package test;
|
||||
|
||||
public enum EnumMembers {
|
||||
FIRST(true),
|
||||
SECOND(false);
|
||||
|
||||
public final boolean isFirst;
|
||||
|
||||
private EnumMembers(boolean isFirst) {
|
||||
this.isFirst = isFirst;
|
||||
}
|
||||
|
||||
public boolean first() {
|
||||
return isFirst;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package test
|
||||
|
||||
public final enum class EnumMembers : jet.Enum<test.EnumMembers> {
|
||||
private constructor EnumMembers(/*0*/ p0: jet.Boolean)
|
||||
public final val isFirst: jet.Boolean
|
||||
public open fun first(): jet.Boolean
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
|
||||
public class object <class-object-for-EnumMembers> {
|
||||
private constructor <class-object-for-EnumMembers>()
|
||||
public final /*synthesized*/ fun valueOf(/*0*/ value: jet.String): test.EnumMembers
|
||||
public final /*synthesized*/ fun values(): jet.Array<test.EnumMembers>
|
||||
|
||||
public enum entry FIRST : test.EnumMembers {
|
||||
private constructor FIRST()
|
||||
public final override /*1*/ /*fake_override*/ val isFirst: jet.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun first(): jet.Boolean
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
|
||||
public class object <class-object-for-FIRST> : test.EnumMembers.FIRST {
|
||||
private constructor <class-object-for-FIRST>()
|
||||
public final override /*1*/ /*fake_override*/ val isFirst: jet.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun first(): jet.Boolean
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
}
|
||||
}
|
||||
|
||||
public enum entry SECOND : test.EnumMembers {
|
||||
private constructor SECOND()
|
||||
public final override /*1*/ /*fake_override*/ val isFirst: jet.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun first(): jet.Boolean
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
|
||||
public class object <class-object-for-SECOND> : test.EnumMembers.SECOND {
|
||||
private constructor <class-object-for-SECOND>()
|
||||
public final override /*1*/ /*fake_override*/ val isFirst: jet.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun first(): jet.Boolean
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package test;
|
||||
|
||||
//TODO: move to LoadJavaTestGenerated when possible
|
||||
public enum JavaEnum {
|
||||
ENTRY,
|
||||
ANOTHER;
|
||||
|
||||
@@ -7,9 +7,31 @@ public final enum class JavaEnum : jet.Enum<test.JavaEnum> {
|
||||
|
||||
public class object <class-object-for-JavaEnum> {
|
||||
private constructor <class-object-for-JavaEnum>()
|
||||
public final val ANOTHER: test.JavaEnum
|
||||
public final val ENTRY: test.JavaEnum
|
||||
public final /*synthesized*/ fun valueOf(/*0*/ value: jet.String): test.JavaEnum
|
||||
public final /*synthesized*/ fun values(): jet.Array<test.JavaEnum>
|
||||
|
||||
public enum entry ANOTHER : test.JavaEnum {
|
||||
private constructor ANOTHER()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
|
||||
public class object <class-object-for-ANOTHER> : test.JavaEnum.ANOTHER {
|
||||
private constructor <class-object-for-ANOTHER>()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
}
|
||||
}
|
||||
|
||||
public enum entry ENTRY : test.JavaEnum {
|
||||
private constructor ENTRY()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
|
||||
public class object <class-object-for-ENTRY> : test.JavaEnum.ENTRY {
|
||||
private constructor <class-object-for-ENTRY>()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,20 @@ public trait CustomAnnotation : java.lang.Object {
|
||||
|
||||
public class object <class-object-for-MyEnum> {
|
||||
private constructor <class-object-for-MyEnum>()
|
||||
public final val ONE: test.CustomAnnotation.MyEnum
|
||||
public final /*synthesized*/ fun valueOf(/*0*/ value: jet.String): test.CustomAnnotation.MyEnum
|
||||
public final /*synthesized*/ fun values(): jet.Array<test.CustomAnnotation.MyEnum>
|
||||
|
||||
public enum entry ONE : test.CustomAnnotation.MyEnum {
|
||||
private constructor ONE()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
|
||||
public class object <class-object-for-ONE> : test.CustomAnnotation.MyEnum.ONE {
|
||||
private constructor <class-object-for-ONE>()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,11 +7,44 @@ public final enum class Enum : jet.Enum<test.Enum> {
|
||||
|
||||
public class object <class-object-for-Enum> {
|
||||
private constructor <class-object-for-Enum>()
|
||||
public final val A: test.Enum
|
||||
public final val B: test.Enum
|
||||
public final val C: test.Enum
|
||||
public final /*synthesized*/ fun valueOf(/*0*/ value: jet.String): test.Enum
|
||||
public final /*synthesized*/ fun values(): jet.Array<test.Enum>
|
||||
|
||||
public enum entry A : test.Enum {
|
||||
private constructor A()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
|
||||
public class object <class-object-for-A> : test.Enum.A {
|
||||
private constructor <class-object-for-A>()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
}
|
||||
}
|
||||
|
||||
public enum entry B : test.Enum {
|
||||
private constructor B()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
|
||||
public class object <class-object-for-B> : test.Enum.B {
|
||||
private constructor <class-object-for-B>()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
}
|
||||
}
|
||||
|
||||
public enum entry C : test.Enum {
|
||||
private constructor C()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
|
||||
public class object <class-object-for-C> : test.Enum.C {
|
||||
private constructor <class-object-for-C>()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public open inner class Inner : java.lang.Object {
|
||||
|
||||
@@ -7,9 +7,20 @@ public final enum class StaticMembersInEnum : jet.Enum<test.StaticMembersInEnum>
|
||||
|
||||
public class object <class-object-for-StaticMembersInEnum> {
|
||||
private constructor <class-object-for-StaticMembersInEnum>()
|
||||
public final val ENTRY: test.StaticMembersInEnum
|
||||
public final /*synthesized*/ fun valueOf(/*0*/ value: jet.String): test.StaticMembersInEnum
|
||||
public final /*synthesized*/ fun values(): jet.Array<test.StaticMembersInEnum>
|
||||
|
||||
public enum entry ENTRY : test.StaticMembersInEnum {
|
||||
private constructor ENTRY()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
|
||||
public class object <class-object-for-ENTRY> : test.StaticMembersInEnum.ENTRY {
|
||||
private constructor <class-object-for-ENTRY>()
|
||||
public final override /*1*/ /*fake_override*/ fun name(): jet.String
|
||||
public final override /*1*/ /*fake_override*/ fun ordinal(): jet.Int
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1034,6 +1034,11 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadJava/compiledJava"), Pattern.compile("^(.+)\\.java$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("EnumMembers.java")
|
||||
public void testEnumMembers() throws Exception {
|
||||
doTestCompiledJava("compiler/testData/loadJava/compiledJava/EnumMembers.java");
|
||||
}
|
||||
|
||||
@TestMetadata("JavaEnum.java")
|
||||
public void testJavaEnum() throws Exception {
|
||||
doTestCompiledJava("compiler/testData/loadJava/compiledJava/JavaEnum.java");
|
||||
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 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.java.descriptor;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassOrNamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.Visibility;
|
||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.impl.PropertyDescriptorForObjectImpl;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class JavaPropertyDescriptorForObject extends PropertyDescriptorForObjectImpl implements JavaCallableMemberDescriptor {
|
||||
public JavaPropertyDescriptorForObject(
|
||||
@NotNull ClassOrNamespaceDescriptor containingDeclaration,
|
||||
@NotNull List<AnnotationDescriptor> annotations,
|
||||
@NotNull Visibility visibility,
|
||||
@NotNull Name name,
|
||||
@NotNull ClassDescriptor objectClass
|
||||
) {
|
||||
super(containingDeclaration, annotations, visibility, name, objectClass);
|
||||
}
|
||||
}
|
||||
+9
-30
@@ -18,22 +18,19 @@ package org.jetbrains.jet.lang.resolve.java.resolver;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.impl.ClassDescriptorImpl;
|
||||
import org.jetbrains.jet.lang.descriptors.impl.PropertyDescriptorImpl;
|
||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||
import org.jetbrains.jet.lang.resolve.java.descriptor.JavaPropertyDescriptor;
|
||||
import org.jetbrains.jet.lang.resolve.java.descriptor.JavaPropertyDescriptorForObject;
|
||||
import org.jetbrains.jet.lang.resolve.java.scope.NamedMembers;
|
||||
import org.jetbrains.jet.lang.resolve.java.structure.JavaField;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lang.types.TypeUtils;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.*;
|
||||
|
||||
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isEnumClassObject;
|
||||
import static org.jetbrains.jet.lang.resolve.java.resolver.DescriptorResolverUtils.resolveOverrides;
|
||||
|
||||
public final class JavaPropertyResolver {
|
||||
@@ -70,6 +67,10 @@ public final class JavaPropertyResolver {
|
||||
|
||||
@NotNull
|
||||
public Set<VariableDescriptor> resolveFieldGroup(@NotNull NamedMembers members, @NotNull ClassOrNamespaceDescriptor owner) {
|
||||
if (isEnumClassObject(owner)) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
Name propertyName = members.getName();
|
||||
|
||||
List<JavaField> fields = members.getFields();
|
||||
@@ -100,9 +101,12 @@ public final class JavaPropertyResolver {
|
||||
|
||||
@NotNull
|
||||
private PropertyDescriptor resolveProperty(@NotNull ClassOrNamespaceDescriptor owner, @NotNull Name name, @NotNull JavaField field) {
|
||||
assert !field.isEnumEntry() : "Enum entries are resolved into classes, not into properties: " + name;
|
||||
|
||||
boolean isVar = !field.isFinal();
|
||||
|
||||
PropertyDescriptorImpl propertyDescriptor = createPropertyDescriptor(owner, name, field, isVar);
|
||||
PropertyDescriptorImpl propertyDescriptor =
|
||||
new JavaPropertyDescriptor(owner, annotationResolver.resolveAnnotations(field), field.getVisibility(), isVar, name);
|
||||
propertyDescriptor.initialize(null, null);
|
||||
|
||||
TypeVariableResolver typeVariableResolver =
|
||||
@@ -129,31 +133,6 @@ public final class JavaPropertyResolver {
|
||||
return propertyDescriptor;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private PropertyDescriptorImpl createPropertyDescriptor(
|
||||
@NotNull ClassOrNamespaceDescriptor owner,
|
||||
@NotNull Name propertyName,
|
||||
@NotNull JavaField field,
|
||||
boolean isVar
|
||||
) {
|
||||
List<AnnotationDescriptor> annotations = annotationResolver.resolveAnnotations(field);
|
||||
Visibility visibility = field.getVisibility();
|
||||
|
||||
if (field.isEnumEntry()) {
|
||||
assert !isVar : "Enum entries should be immutable.";
|
||||
assert DescriptorUtils.isEnumClassObject(owner) : "Enum entries should be put into class object of enum only: " + owner;
|
||||
//TODO: this is a hack to indicate that this enum entry is an object
|
||||
// class descriptor for enum entries is not used by backends so for now this should be safe to use
|
||||
ClassDescriptorImpl dummyClassDescriptorForEnumEntryObject =
|
||||
new ClassDescriptorImpl(owner, propertyName, Modality.FINAL, Collections.<JetType>emptyList());
|
||||
dummyClassDescriptorForEnumEntryObject.initialize(JetScope.EMPTY, Collections.<ConstructorDescriptor>emptySet(), null);
|
||||
return new JavaPropertyDescriptorForObject(owner, annotations, visibility, propertyName,
|
||||
dummyClassDescriptorForEnumEntryObject);
|
||||
}
|
||||
|
||||
return new JavaPropertyDescriptor(owner, annotations, visibility, isVar, propertyName);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JetType getPropertyType(@NotNull JavaField field, @NotNull TypeVariableResolver typeVariableResolver) {
|
||||
JetType propertyType = typeTransformer.transformToType(field.getType(), typeVariableResolver);
|
||||
|
||||
+2
-18
@@ -39,8 +39,6 @@ public abstract class JavaBaseScope extends JetScopeImpl {
|
||||
private final Map<Name, Set<VariableDescriptor>> propertyDescriptors = new HashMap<Name, Set<VariableDescriptor>>();
|
||||
@Nullable
|
||||
private Collection<DeclarationDescriptor> allDescriptors = null;
|
||||
@Nullable
|
||||
private Set<ClassDescriptor> objectDescriptors = null;
|
||||
@NotNull
|
||||
protected final ClassOrNamespaceDescriptor descriptor;
|
||||
|
||||
@@ -125,17 +123,14 @@ public abstract class JavaBaseScope extends JetScopeImpl {
|
||||
protected Collection<DeclarationDescriptor> computeAllDescriptors() {
|
||||
Collection<DeclarationDescriptor> result = new HashSet<DeclarationDescriptor>();
|
||||
result.addAll(computeFieldAndFunctionDescriptors());
|
||||
result.addAll(filterObjects(getInnerClasses(), false));
|
||||
result.addAll(getInnerClasses());
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Set<ClassDescriptor> getObjectDescriptors() {
|
||||
if (objectDescriptors == null) {
|
||||
objectDescriptors = new HashSet<ClassDescriptor>(filterObjects(getInnerClasses(), true));
|
||||
}
|
||||
return objectDescriptors;
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -162,17 +157,6 @@ public abstract class JavaBaseScope extends JetScopeImpl {
|
||||
return innerClasses;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Collection<ClassDescriptor> filterObjects(@NotNull Collection<ClassDescriptor> classes, boolean objects) {
|
||||
List<ClassDescriptor> result = new ArrayList<ClassDescriptor>();
|
||||
for (ClassDescriptor descriptor : classes) {
|
||||
if (descriptor.getKind().isSingleton() == objects) {
|
||||
result.add(descriptor);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
@Override
|
||||
public void printScopeStructure(@NotNull Printer p) {
|
||||
|
||||
+8
-21
@@ -52,8 +52,13 @@ public abstract class JavaClassMembersScope extends JavaBaseScope {
|
||||
return memberResolver.resolveFunctionGroupForClass(members, descriptor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Map<Name, ClassDescriptor> getInnerClassesMap() {
|
||||
@Override
|
||||
public ClassDescriptor getObjectDescriptor(@NotNull Name name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassifierDescriptor getClassifier(@NotNull Name name) {
|
||||
if (innerClassesMap == null) {
|
||||
Collection<ClassDescriptor> innerClasses = getInnerClasses();
|
||||
innerClassesMap = new HashMap<Name, ClassDescriptor>();
|
||||
@@ -61,24 +66,6 @@ public abstract class JavaClassMembersScope extends JavaBaseScope {
|
||||
innerClassesMap.put(innerClass.getName(), innerClass);
|
||||
}
|
||||
}
|
||||
return innerClassesMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassDescriptor getObjectDescriptor(@NotNull Name name) {
|
||||
ClassDescriptor innerClass = getInnerClassesMap().get(name);
|
||||
if (innerClass != null && innerClass.getKind().isSingleton()) {
|
||||
return innerClass;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassifierDescriptor getClassifier(@NotNull Name name) {
|
||||
ClassDescriptor innerClass = getInnerClassesMap().get(name);
|
||||
if (innerClass == null || innerClass.getKind().isSingleton()) {
|
||||
return null;
|
||||
}
|
||||
return innerClass;
|
||||
return innerClassesMap.get(name);
|
||||
}
|
||||
}
|
||||
|
||||
+40
-2
@@ -16,14 +16,24 @@
|
||||
|
||||
package org.jetbrains.jet.lang.resolve.java.scope;
|
||||
|
||||
import jet.Function0;
|
||||
import jet.Function1;
|
||||
import kotlin.KotlinPackage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassOrNamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.impl.EnumEntrySyntheticClassDescriptor;
|
||||
import org.jetbrains.jet.lang.resolve.java.resolver.JavaMemberResolver;
|
||||
import org.jetbrains.jet.lang.resolve.java.structure.JavaClass;
|
||||
import org.jetbrains.jet.lang.resolve.java.structure.JavaField;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.storage.LockBasedStorageManager;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValue;
|
||||
import org.jetbrains.jet.storage.StorageManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class JavaEnumClassObjectScope extends JavaClassMembersScope {
|
||||
public JavaEnumClassObjectScope(
|
||||
@@ -37,6 +47,34 @@ public class JavaEnumClassObjectScope extends JavaClassMembersScope {
|
||||
@NotNull
|
||||
@Override
|
||||
protected Collection<ClassDescriptor> computeInnerClasses() {
|
||||
return Collections.emptyList();
|
||||
List<ClassDescriptor> result = new ArrayList<ClassDescriptor>();
|
||||
|
||||
final Collection<NamedMembers> enumNonStaticMembers = membersProvider.allMembers();
|
||||
|
||||
NotNullLazyValue<Collection<Name>> enumMemberNames =
|
||||
LockBasedStorageManager.NO_LOCKS.createLazyValue(new Function0<Collection<Name>>() {
|
||||
@Override
|
||||
public Collection<Name> invoke() {
|
||||
return KotlinPackage.map(enumNonStaticMembers, new Function1<NamedMembers, Name>() {
|
||||
@Override
|
||||
public Name invoke(@NotNull NamedMembers members) {
|
||||
return members.getName();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
for (NamedMembers members : enumNonStaticMembers) {
|
||||
for (JavaField field : members.getFields()) {
|
||||
if (field.isEnumEntry()) {
|
||||
EnumEntrySyntheticClassDescriptor enumEntry = EnumEntrySyntheticClassDescriptor
|
||||
.create(LockBasedStorageManager.NO_LOCKS, (ClassDescriptor) descriptor, members.getName(), enumMemberNames);
|
||||
result.add(enumEntry);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
+271
@@ -0,0 +1,271 @@
|
||||
/*
|
||||
* Copyright 2010-2013 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.descriptors.impl;
|
||||
|
||||
import jet.Function0;
|
||||
import jet.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
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.name.SpecialNames;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScopeImpl;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lang.types.TypeConstructor;
|
||||
import org.jetbrains.jet.lang.types.TypeConstructorImpl;
|
||||
import org.jetbrains.jet.storage.MemoizedFunctionToNotNull;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValue;
|
||||
import org.jetbrains.jet.storage.StorageManager;
|
||||
import org.jetbrains.jet.utils.Printer;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
|
||||
private final ClassKind kind;
|
||||
private final TypeConstructor typeConstructor;
|
||||
private final ConstructorDescriptor primaryConstructor;
|
||||
private final JetScope scope;
|
||||
private final EnumEntrySyntheticClassDescriptor classObjectDescriptor;
|
||||
private final NotNullLazyValue<Collection<Name>> enumMemberNames;
|
||||
|
||||
/**
|
||||
* Creates and initializes descriptors for enum entry with the given name and its class object
|
||||
* @param enumMemberNames needed for fake overrides resolution
|
||||
*/
|
||||
@NotNull
|
||||
public static EnumEntrySyntheticClassDescriptor create(
|
||||
@NotNull StorageManager storageManager,
|
||||
@NotNull ClassDescriptor enumClassObject,
|
||||
@NotNull Name name,
|
||||
@NotNull NotNullLazyValue<Collection<Name>> enumMemberNames
|
||||
) {
|
||||
JetType enumType = ((ClassDescriptor) enumClassObject.getContainingDeclaration()).getDefaultType();
|
||||
|
||||
return new EnumEntrySyntheticClassDescriptor(storageManager, enumClassObject, enumType, name, ClassKind.ENUM_ENTRY,
|
||||
enumMemberNames);
|
||||
}
|
||||
|
||||
private EnumEntrySyntheticClassDescriptor(
|
||||
@NotNull StorageManager storageManager,
|
||||
@NotNull ClassDescriptor containingClass,
|
||||
@NotNull JetType supertype,
|
||||
@NotNull Name name,
|
||||
@NotNull ClassKind kind,
|
||||
@NotNull NotNullLazyValue<Collection<Name>> enumMemberNames
|
||||
) {
|
||||
super(storageManager, containingClass, name);
|
||||
this.kind = kind;
|
||||
|
||||
this.typeConstructor =
|
||||
new TypeConstructorImpl(this, getAnnotations(), true, "enum entry", Collections.<TypeParameterDescriptor>emptyList(),
|
||||
Collections.singleton(supertype));
|
||||
|
||||
this.scope = new EnumEntryScope(storageManager);
|
||||
this.enumMemberNames = enumMemberNames;
|
||||
|
||||
ConstructorDescriptorImpl primaryConstructor = DescriptorFactory.createPrimaryConstructorForObject(this);
|
||||
primaryConstructor.setReturnType(getDefaultType());
|
||||
this.primaryConstructor = primaryConstructor;
|
||||
|
||||
this.classObjectDescriptor =
|
||||
kind == ClassKind.CLASS_OBJECT
|
||||
? null
|
||||
: new EnumEntrySyntheticClassDescriptor(storageManager, this, getDefaultType(), SpecialNames.getClassObjectName(name),
|
||||
ClassKind.CLASS_OBJECT, enumMemberNames);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetScope getScopeForMemberLookup() {
|
||||
return scope;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<ConstructorDescriptor> getConstructors() {
|
||||
return Collections.singleton(primaryConstructor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public TypeConstructor getTypeConstructor() {
|
||||
return typeConstructor;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ClassDescriptor getClassObjectDescriptor() {
|
||||
return classObjectDescriptor;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ClassKind getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Modality getModality() {
|
||||
return Modality.FINAL;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Visibility getVisibility() {
|
||||
return Visibilities.PUBLIC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInner() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
|
||||
return primaryConstructor;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<AnnotationDescriptor> getAnnotations() {
|
||||
// TODO
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
private class EnumEntryScope extends JetScopeImpl {
|
||||
private final MemoizedFunctionToNotNull<Name, Collection<FunctionDescriptor>> functions;
|
||||
private final MemoizedFunctionToNotNull<Name, Collection<PropertyDescriptor>> properties;
|
||||
private final NotNullLazyValue<Collection<DeclarationDescriptor>> allDescriptors;
|
||||
|
||||
public EnumEntryScope(@NotNull StorageManager storageManager) {
|
||||
this.functions = storageManager.createMemoizedFunction(new Function1<Name, Collection<FunctionDescriptor>>() {
|
||||
@Override
|
||||
public Collection<FunctionDescriptor> invoke(Name name) {
|
||||
return computeFunctions(name);
|
||||
}
|
||||
});
|
||||
this.properties = storageManager.createMemoizedFunction(new Function1<Name, Collection<PropertyDescriptor>>() {
|
||||
@Override
|
||||
public Collection<PropertyDescriptor> invoke(Name name) {
|
||||
return computeProperties(name);
|
||||
}
|
||||
});
|
||||
this.allDescriptors = storageManager.createLazyValue(new Function0<Collection<DeclarationDescriptor>>() {
|
||||
@Override
|
||||
public Collection<DeclarationDescriptor> invoke() {
|
||||
return computeAllDeclarations();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Collection<VariableDescriptor> getProperties(@NotNull Name name) {
|
||||
return (Collection) properties.invoke(name);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("unchecked")
|
||||
private Collection<PropertyDescriptor> computeProperties(@NotNull Name name) {
|
||||
return resolveFakeOverrides(name, (Collection) getSupertypeScope().getProperties(name));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<FunctionDescriptor> getFunctions(@NotNull Name name) {
|
||||
return functions.invoke(name);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Collection<FunctionDescriptor> computeFunctions(@NotNull Name name) {
|
||||
return resolveFakeOverrides(name, getSupertypeScope().getFunctions(name));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JetScope getSupertypeScope() {
|
||||
Collection<JetType> supertype = getTypeConstructor().getSupertypes();
|
||||
assert supertype.size() == 1 : "Enum entry and its class object both should have exactly one supertype: " + supertype;
|
||||
return supertype.iterator().next().getMemberScope();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private <D extends CallableMemberDescriptor> Collection<D> resolveFakeOverrides(
|
||||
@NotNull Name name,
|
||||
@NotNull Collection<D> fromSupertypes
|
||||
) {
|
||||
final Set<D> result = new HashSet<D>();
|
||||
|
||||
OverridingUtil.generateOverridesInFunctionGroup(
|
||||
name, fromSupertypes, Collections.<D>emptySet(), EnumEntrySyntheticClassDescriptor.this,
|
||||
new OverridingUtil.DescriptorSink() {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void addToScope(@NotNull CallableMemberDescriptor fakeOverride) {
|
||||
OverridingUtil.resolveUnknownVisibilityForMember(fakeOverride, new OverridingUtil.NotInferredVisibilitySink() {
|
||||
@Override
|
||||
public void cannotInferVisibility(@NotNull CallableMemberDescriptor descriptor) {
|
||||
// Do nothing
|
||||
}
|
||||
});
|
||||
result.add((D) fakeOverride);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void conflict(@NotNull CallableMemberDescriptor fromSuper, @NotNull CallableMemberDescriptor fromCurrent) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public DeclarationDescriptor getContainingDeclaration() {
|
||||
return EnumEntrySyntheticClassDescriptor.this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<DeclarationDescriptor> getAllDescriptors() {
|
||||
return allDescriptors.invoke();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Collection<DeclarationDescriptor> computeAllDeclarations() {
|
||||
Collection<DeclarationDescriptor> result = new HashSet<DeclarationDescriptor>();
|
||||
for (Name name : enumMemberNames.invoke()) {
|
||||
result.addAll(getFunctions(name));
|
||||
result.addAll(getProperties(name));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printScopeStructure(@NotNull Printer p) {
|
||||
p.println("enum entry scope for " + EnumEntrySyntheticClassDescriptor.this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user