Drop JetClassObject element and its usages

as class objects are now represented by JetObjectDeclaration element
This commit is contained in:
Pavel V. Talanov
2015-01-21 14:44:18 +03:00
parent be7f5d01bb
commit d9d3f68a39
47 changed files with 87 additions and 411 deletions
@@ -99,9 +99,6 @@ public abstract class ClassBodyCodegen extends MemberCodegen<JetClassOrObject> {
genClassOrObject((JetClassOrObject) declaration);
}
else if (declaration instanceof JetClassObject) {
genClassOrObject(((JetClassObject) declaration).getObjectDeclaration());
}
}
private void generatePrimaryConstructorProperties(PropertyCodegen propertyCodegen, JetClassOrObject origin) {
@@ -150,7 +150,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
isStatic = !jetClass.isInner();
}
else {
isStatic = myClass.getParent() instanceof JetClassObject;
isStatic = myClass instanceof JetObjectDeclaration && ((JetObjectDeclaration) myClass).isClassObject() ;
isFinal = true;
}
@@ -968,9 +968,9 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
fieldTypeDescriptor = descriptor;
}
else if (classObjectDescriptor != null) {
JetClassObject classObject = ((JetClass) myClass).getClassObject();
JetObjectDeclaration classObject = ((JetClass) myClass).getClassObject();
assert classObject != null : "Class object not found: " + myClass.getText();
original = classObject.getObjectDeclaration();
original = classObject;
fieldTypeDescriptor = classObjectDescriptor;
}
else {
@@ -207,46 +207,23 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
}
@Override
public void visitClassObject(@NotNull JetClassObject classObject) {
ClassDescriptor classDescriptor = bindingContext.get(CLASS, classObject.getObjectDeclaration());
public void visitObjectDeclaration(@NotNull JetObjectDeclaration declaration) {
if (!filter.shouldProcessClass(declaration)) return;
assert classDescriptor != null : String.format("No class found in binding context for: \n---\n%s\n---\n",
JetPsiUtil.getElementTextWithContext(classObject));
ClassDescriptor classDescriptor = bindingContext.get(CLASS, declaration);
// working around a problem with shallow analysis
if (classDescriptor == null) return;
//TODO_R: remove visitClassObject
String name = getName(classDescriptor);
recordClosure(classDescriptor, name);
classStack.push(classDescriptor);
nameStack.push(name);
super.visitClassObject(classObject);
super.visitObjectDeclaration(declaration);
nameStack.pop();
classStack.pop();
}
@Override
public void visitObjectDeclaration(@NotNull JetObjectDeclaration declaration) {
if (declaration.getParent() instanceof JetClassObject) {
super.visitObjectDeclaration(declaration);
}
else {
if (!filter.shouldProcessClass(declaration)) return;
ClassDescriptor classDescriptor = bindingContext.get(CLASS, declaration);
// working around a problem with shallow analysis
if (classDescriptor == null) return;
String name = getName(classDescriptor);
recordClosure(classDescriptor, name);
classStack.push(classDescriptor);
nameStack.push(name);
super.visitObjectDeclaration(declaration);
nameStack.pop();
classStack.pop();
}
}
@Override
public void visitClass(@NotNull JetClass klass) {
if (!filter.shouldProcessClass(klass)) return;
@@ -55,10 +55,6 @@ public final class PsiCodegenPredictor {
// TODO: Method won't give correct class name for traits implementations
JetDeclaration parentDeclaration = JetStubbedPsiUtil.getContainingDeclaration(declaration);
if (parentDeclaration instanceof JetClassObject) {
assert declaration instanceof JetObjectDeclaration : "Only object declarations can be children of JetClassObject: " + declaration;
return getPredefinedJvmInternalName(parentDeclaration);
}
String parentInternalName;
if (parentDeclaration != null) {
@@ -78,12 +74,6 @@ public final class PsiCodegenPredictor {
parentInternalName = AsmUtil.internalNameByFqNameWithoutInnerClasses(containingFile.getPackageFqName());
}
if (declaration instanceof JetClassObject) {
// Get parent and assign Class object prefix
//TODO_R: getName() nullable
return parentInternalName + "$" + ((JetClassObject) declaration).getObjectDeclaration().getName();
}
if (!PsiTreeUtil.instanceOf(declaration, JetClass.class, JetObjectDeclaration.class, JetNamedFunction.class, JetProperty.class) ||
declaration instanceof JetEnumEntry) {
// Other subclasses are not valid for class name prediction.
@@ -35,7 +35,6 @@ public interface JetNodeTypes {
IElementType OBJECT_DECLARATION = JetStubElementTypes.OBJECT_DECLARATION;
JetNodeType OBJECT_DECLARATION_NAME = new JetNodeType("OBJECT_DECLARATION_NAME", JetObjectDeclarationName.class);
IElementType CLASS_OBJECT = JetStubElementTypes.CLASS_OBJECT;
IElementType ENUM_ENTRY = JetStubElementTypes.ENUM_ENTRY;
IElementType ANONYMOUS_INITIALIZER = JetStubElementTypes.ANONYMOUS_INITIALIZER;
@@ -187,8 +187,8 @@ public interface Errors {
// Class objects
DiagnosticFactory0<JetClassObject> MANY_CLASS_OBJECTS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetClassObject> CLASS_OBJECT_NOT_ALLOWED = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetObjectDeclaration> MANY_CLASS_OBJECTS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetObjectDeclaration> CLASS_OBJECT_NOT_ALLOWED = DiagnosticFactory0.create(ERROR);
// Objects
@@ -51,10 +51,11 @@ public object PositioningStrategies {
}
return markRange(objectKeyword, delegationSpecifierList)
}
is JetClassObject -> {
val classKeyword = element.getClassKeyword()
val objectKeyword = element.getObjectDeclaration().getObjectKeyword()
return markRange(classKeyword, objectKeyword)
is JetObjectDeclaration -> {
return markRange(
element.getClassKeyword() ?: element.getObjectKeyword(),
element.getNameIdentifier() ?: element.getObjectKeyword()
)
}
else -> {
return super.mark(element)
@@ -99,17 +100,7 @@ public object PositioningStrategies {
}
return markElement(nameIdentifier)
}
if (element is JetObjectDeclaration) {
val objectKeyword = element.getObjectKeyword()
val parent = element.getParent()
if (parent is JetClassObject) {
val classKeyword = parent.getClassKeyword()
val start = classKeyword ?: objectKeyword
return markRange(start, objectKeyword)
}
return markElement(objectKeyword)
}
return super.mark(element)
return DEFAULT.mark(element)
}
}
@@ -240,7 +231,6 @@ public object PositioningStrategies {
is JetObjectDeclaration -> element.getObjectKeyword()
is JetPropertyAccessor -> element.getNamePlaceholder()
is JetClassInitializer -> element
is JetClassObject -> element.getObjectDeclaration().getObjectKeyword()
else -> throw IllegalArgumentException(
"Can't find text range for element '${element.javaClass.getCanonicalName()}' with the text '${element.getText()}'")
}
@@ -188,11 +188,6 @@ private object DebugTextBuildingVisitor : JetVisitor<String, Unit>() {
return "initializer in " + (containingDeclaration?.getDebugText() ?: "...")
}
override fun visitClassObject(classObject: JetClassObject, data: Unit?): String? {
val containingDeclaration = JetStubbedPsiUtil.getContainingDeclaration(classObject)
return "class object in " + (containingDeclaration?.getDebugText() ?: "...")
}
override fun visitClassBody(classBody: JetClassBody, data: Unit?): String? {
val containingDeclaration = JetStubbedPsiUtil.getContainingDeclaration(classBody)
return "class body for " + (containingDeclaration?.getDebugText() ?: "...")
@@ -242,6 +237,9 @@ private object DebugTextBuildingVisitor : JetVisitor<String, Unit>() {
return buildText {
append("STUB: ")
appendInn(declaration.getModifierList(), suffix = " ")
if (declaration.isClassObject()) {
append("class ")
}
append("object ")
appendInn(declaration.getNameAsName())
appendInn(declaration.getDelegationSpecifierList(), prefix = " : ")
@@ -142,7 +142,7 @@ public class JetClass extends JetTypeParameterListOwnerStub<KotlinClassStub> imp
}
@Nullable
public JetClassObject getClassObject() {
public JetObjectDeclaration getClassObject() {
JetClassBody body = getBody();
if (body == null) return null;
return body.getClassObject();
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.psi;
import com.google.common.collect.Lists;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.TokenSet;
@@ -31,6 +32,7 @@ import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
import java.util.Arrays;
import java.util.List;
import static kotlin.KotlinPackage.firstOrNull;
import static org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes.*;
public class JetClassBody extends JetElementImplStub<KotlinPlaceHolderStub<JetClassBody>> implements JetDeclarationContainer {
@@ -65,13 +67,19 @@ public class JetClassBody extends JetElementImplStub<KotlinPlaceHolderStub<JetCl
}
@Nullable
public JetClassObject getClassObject() {
return getStubOrPsiChild(CLASS_OBJECT);
public JetObjectDeclaration getClassObject() {
return firstOrNull(getAllClassObjects());
}
@NotNull
public List<JetClassObject> getAllClassObjects() {
return getStubOrPsiChildrenAsList(JetStubElementTypes.CLASS_OBJECT);
public List<JetObjectDeclaration> getAllClassObjects() {
List<JetObjectDeclaration> result = Lists.newArrayList();
for (JetObjectDeclaration declaration : getStubOrPsiChildrenAsList(JetStubElementTypes.OBJECT_DECLARATION)) {
if (declaration.isClassObject()) {
result.add(declaration);
}
}
return result;
}
@Nullable
@@ -1,49 +0,0 @@
/*
* Copyright 2010-2015 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.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.lexer.JetTokens;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
public class JetClassObject extends JetDeclarationStub<KotlinPlaceHolderStub<JetClassObject>> implements JetStatementExpression {
public JetClassObject(@NotNull ASTNode node) {
super(node);
}
public JetClassObject(@NotNull KotlinPlaceHolderStub<JetClassObject> stub) {
super(stub, JetStubElementTypes.CLASS_OBJECT);
}
@Override
public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
return visitor.visitClassObject(this, data);
}
@NotNull
public JetObjectDeclaration getObjectDeclaration() {
return getRequiredStubOrPsiChild(JetStubElementTypes.OBJECT_DECLARATION);
}
@NotNull
public PsiElement getClassKeyword() {
return findChildByType(JetTokens.CLASS_KEYWORD);
}
}
@@ -51,9 +51,6 @@ public final class JetNamedDeclarationUtil {
@Nullable
public static FqName getParentFqName(@NotNull JetNamedDeclaration namedDeclaration) {
PsiElement parent = namedDeclaration.getParent();
if (parent instanceof JetClassObject) {
parent = parent.getParent();
}
if (parent instanceof JetClassBody) {
// One nesting to JetClassBody doesn't affect to qualified name
parent = parent.getParent();
@@ -645,10 +645,6 @@ public class JetPsiFactory(private val project: Project) {
return createFunction("fun foo() {\n" + bodyText + "\n}").getBodyExpression() as JetBlockExpression
}
public fun createEmptyClassObject(): JetClassObject {
return createClass("class foo { class object { } }").getClassObject()!!
}
public fun createComment(text: String): PsiComment {
val file = createFile(text)
val comments = file.getChildren().filterIsInstance<PsiComment>()
@@ -431,11 +431,6 @@ public class JetPsiUtil {
if (parent instanceof PsiFile) {
return current;
}
if (parent instanceof JetClassObject) {
// current class IS the class object declaration
parent = parent.getParent();
assert parent instanceof JetClassBody : "Parent of class object is not a class body: " + parent;
}
if (!(parent instanceof JetClassBody)) {
// It is a local class, no legitimate outer
return current;
@@ -33,10 +33,6 @@ public class JetVisitor<R, D> extends PsiElementVisitor {
return visitNamedDeclaration(klass, data);
}
public R visitClassObject(@NotNull JetClassObject classObject, D data) {
return visitDeclaration(classObject, data);
}
public R visitNamedFunction(@NotNull JetNamedFunction function, D data) {
return visitNamedDeclaration(function, data);
}
@@ -33,10 +33,6 @@ public class JetVisitorVoid extends JetVisitor<Void, Void> {
super.visitClass(klass, null);
}
public void visitClassObject(@NotNull JetClassObject classObject) {
super.visitClassObject(classObject, null);
}
public void visitNamedFunction(@NotNull JetNamedFunction function) {
super.visitNamedFunction(function, null);
}
@@ -436,12 +432,6 @@ public class JetVisitorVoid extends JetVisitor<Void, Void> {
return null;
}
@Override
public final Void visitClassObject(@NotNull JetClassObject classObject, Void data) {
visitClassObject(classObject);
return null;
}
@Override
public final Void visitNamedFunction(@NotNull JetNamedFunction function, Void data) {
visitNamedFunction(function);
@@ -34,10 +34,6 @@ public class JetVisitorVoidWithParameter<P> extends JetVisitor<Void, P> {
super.visitClass(klass, data);
}
public void visitClassObjectVoid(@NotNull JetClassObject classObject, P data) {
super.visitClassObject(classObject, data);
}
public void visitNamedFunctionVoid(@NotNull JetNamedFunction function, P data) {
super.visitNamedFunction(function, data);
}
@@ -433,12 +429,6 @@ public class JetVisitorVoidWithParameter<P> extends JetVisitor<Void, P> {
return null;
}
@Override
public final Void visitClassObject(@NotNull JetClassObject classObject, P data) {
visitClassObjectVoid(classObject, data);
return null;
}
@Override
public final Void visitNamedFunction(@NotNull JetNamedFunction function, P data) {
visitNamedFunctionVoid(function, data);
@@ -24,7 +24,6 @@ import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.psi.JetClassObject;
import org.jetbrains.kotlin.psi.JetObjectDeclaration;
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.kotlin.psi.stubs.KotlinObjectStub;
@@ -46,7 +45,7 @@ public class JetObjectElementType extends JetStubElementType<KotlinObjectStub, J
FqName fqName = ResolveSessionUtils.safeFqNameForLazyResolve(psi);
List<String> superNames = PsiUtilPackage.getSuperNames(psi);
return new KotlinObjectStubImpl(parentStub, StringRef.fromString(name), fqName, Utils.INSTANCE$.wrapStrings(superNames),
psi.isTopLevel(), isClassObject(psi), psi.isLocal(), psi.isObjectLiteral());
psi.isTopLevel(), psi.isClassObject(), psi.isLocal(), psi.isObjectLiteral());
}
@Override
@@ -93,8 +92,4 @@ public class JetObjectElementType extends JetStubElementType<KotlinObjectStub, J
public void indexStub(@NotNull KotlinObjectStub stub, @NotNull IndexSink sink) {
StubIndexServiceFactory.getInstance().indexObject(stub, sink);
}
private static boolean isClassObject(@NotNull JetObjectDeclaration objectDeclaration) {
return objectDeclaration.getParent() instanceof JetClassObject;
}
}
@@ -31,8 +31,6 @@ public interface JetStubElementTypes {
JetClassElementType ENUM_ENTRY = new JetClassElementType("ENUM_ENTRY");
JetObjectElementType OBJECT_DECLARATION = new JetObjectElementType("OBJECT_DECLARATION");
JetPlaceHolderStubElementType<JetClassObject> CLASS_OBJECT =
new JetPlaceHolderStubElementType<JetClassObject>("CLASS_OBJECT", JetClassObject.class);
JetPlaceHolderStubElementType<JetClassInitializer> ANONYMOUS_INITIALIZER =
new JetPlaceHolderStubElementType<JetClassInitializer>("ANONYMOUS_INITIALIZER", JetClassInitializer.class);
@@ -115,7 +113,7 @@ public interface JetStubElementTypes {
new JetPlaceHolderStubElementType<JetConstructorCalleeExpression>("CONSTRUCTOR_CALLEE", JetConstructorCalleeExpression.class);
TokenSet DECLARATION_TYPES =
TokenSet.create(CLASS, OBJECT_DECLARATION, CLASS_OBJECT, FUNCTION, PROPERTY, ANONYMOUS_INITIALIZER, ENUM_ENTRY);
TokenSet.create(CLASS, OBJECT_DECLARATION, FUNCTION, PROPERTY, ANONYMOUS_INITIALIZER, ENUM_ENTRY);
TokenSet DELEGATION_SPECIFIER_TYPES = TokenSet.create(DELEGATOR_BY, DELEGATOR_SUPER_CALL, DELEGATOR_SUPER_CLASS, THIS_CALL);
@@ -199,9 +199,9 @@ public class LazyTopDownAnalyzer {
for (JetDeclaration jetDeclaration : classOrObject.getDeclarations()) {
jetDeclaration.accept(this);
if (jetDeclaration instanceof JetClassObject) {
if (jetDeclaration instanceof JetObjectDeclaration && ((JetObjectDeclaration) jetDeclaration).isClassObject()) {
if (classObjectAlreadyFound) {
trace.report(MANY_CLASS_OBJECTS.on((JetClassObject) jetDeclaration));
trace.report(MANY_CLASS_OBJECTS.on((JetObjectDeclaration) jetDeclaration));
}
classObjectAlreadyFound = true;
}
@@ -226,11 +226,6 @@ public class LazyTopDownAnalyzer {
}
}
@Override
public void visitClassObject(@NotNull JetClassObject classObject) {
visitClassOrObject(classObject.getObjectDeclaration());
}
@Override
public void visitEnumEntry(@NotNull JetEnumEntry enumEntry) {
visitClassOrObject(enumEntry);
@@ -171,7 +171,7 @@ public class ModifiersChecker {
checkCompatibility(modifierList, Arrays.asList(ABSTRACT_KEYWORD, OPEN_KEYWORD, FINAL_KEYWORD),
Arrays.asList(ABSTRACT_KEYWORD, OPEN_KEYWORD));
if (modifierListOwner.getParent() instanceof JetClassObject || modifierListOwner instanceof JetObjectDeclaration) {
if (modifierListOwner instanceof JetObjectDeclaration) {
checkIllegalModalityModifiers(modifierListOwner);
}
else if (modifierListOwner instanceof JetClassOrObject) {
@@ -68,16 +68,6 @@ public class DeclarationScopeProviderImpl implements DeclarationScopeProvider {
return classDescriptor.getScopeForMemberDeclarationResolution();
}
if (parentDeclaration instanceof JetClassObject) {
assert jetDeclaration instanceof JetObjectDeclaration : "Should be situation for getting scope for object in class [object {...}]";
JetClassObject classObject = (JetClassObject) parentDeclaration;
LazyClassDescriptor classObjectDescriptor =
(LazyClassDescriptor) lazyDeclarationResolver.getClassObjectDescriptor(classObject).getContainingDeclaration();
return classObjectDescriptor.getScopeForMemberDeclarationResolution();
}
throw new IllegalStateException("Don't call this method for local declarations: " + jetDeclaration + "\n" +
JetPsiUtil.getElementTextWithContext(jetDeclaration));
}
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.BindingTrace;
import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyClassDescriptor;
import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyPackageDescriptor;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
import org.jetbrains.kotlin.storage.LockBasedLazyResolveStorageManager;
@@ -65,13 +64,6 @@ public class LazyDeclarationResolver {
@NotNull
public ClassDescriptor getClassDescriptor(@NotNull JetClassOrObject classOrObject) {
if (classOrObject instanceof JetObjectDeclaration) {
JetObjectDeclaration objectDeclaration = (JetObjectDeclaration) classOrObject;
JetClassObject classObjectElement = objectDeclaration.getClassObjectElement();
if (classObjectElement != null) {
return getClassObjectDescriptor(classObjectElement);
}
}
JetScope resolutionScope = resolutionScopeToResolveDeclaration(classOrObject);
// Why not use the result here. Because it may be that there is a redeclaration:
@@ -93,31 +85,6 @@ public class LazyDeclarationResolver {
return (ClassDescriptor) descriptor;
}
@NotNull
/*package*/ LazyClassDescriptor getClassObjectDescriptor(@NotNull JetClassObject classObject) {
JetClass aClass = JetStubbedPsiUtil.getContainingDeclaration(classObject, JetClass.class);
LazyClassDescriptor parentClassDescriptor;
if (aClass != null) {
parentClassDescriptor = (LazyClassDescriptor) getClassDescriptor(aClass);
}
else {
// Class object in object is an error but we want to find descriptors even for this case
JetObjectDeclaration objectDeclaration = PsiTreeUtil.getParentOfType(classObject, JetObjectDeclaration.class);
assert objectDeclaration != null : String.format("Class object %s can be in class or object in file %s", classObject, classObject.getContainingFile().getText());
parentClassDescriptor = (LazyClassDescriptor) getClassDescriptor(objectDeclaration);
}
// Activate resolution and writing to trace
parentClassDescriptor.getClassObjectDescriptor();
parentClassDescriptor.getDescriptorsForExtraClassObjects();
DeclarationDescriptor classObjectDescriptor = getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, classObject.getObjectDeclaration());
assert classObjectDescriptor != null : "No descriptor found for " + JetPsiUtil.getElementTextWithContext(classObject);
return (LazyClassDescriptor) classObjectDescriptor;
}
@NotNull
private BindingContext getBindingContext() {
return trace.getBindingContext();
@@ -133,19 +100,9 @@ public class LazyDeclarationResolver {
@Override
public DeclarationDescriptor visitObjectDeclaration(@NotNull JetObjectDeclaration declaration, Void data) {
PsiElement parent = declaration.getParent();
if (parent instanceof JetClassObject) {
JetClassObject jetClassObject = (JetClassObject) parent;
return resolveToDescriptor(jetClassObject);
}
return getClassDescriptor(declaration);
}
@Override
public DeclarationDescriptor visitClassObject(@NotNull JetClassObject classObject, Void data) {
return getClassObjectDescriptor(classObject);
}
@Override
public DeclarationDescriptor visitTypeParameter(@NotNull JetTypeParameter parameter, Void data) {
JetTypeParameterListOwner ownerElement = PsiTreeUtil.getParentOfType(parameter, JetTypeParameterListOwner.class);
@@ -46,7 +46,7 @@ public class JetClassInfo extends JetClassOrObjectInfo<JetClass> {
}
@Override
public JetClassObject getClassObject() {
public JetObjectDeclaration getClassObject() {
return element.getClassObject();
}
@@ -21,8 +21,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.ReadOnly;
import org.jetbrains.kotlin.descriptors.ClassKind;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.psi.*;
import java.util.List;
@@ -34,11 +34,11 @@ public interface JetClassLikeInfo extends JetDeclarationContainer {
JetModifierList getModifierList();
@Nullable
JetClassObject getClassObject();
JetObjectDeclaration getClassObject();
@NotNull
@ReadOnly
List<JetClassObject> getClassObjects();
List<JetObjectDeclaration> getClassObjects();
// This element is used to identify resolution scope for the class
@NotNull
@@ -58,7 +58,7 @@ public abstract class JetClassOrObjectInfo<E extends JetClassOrObject> implement
@NotNull
@Override
public List<JetClassObject> getClassObjects() {
public List<JetObjectDeclaration> getClassObjects() {
JetClassBody body = element.getBody();
if (body == null) {
return Collections.emptyList();
@@ -19,7 +19,6 @@ package org.jetbrains.kotlin.resolve.lazy.data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.ClassKind;
import org.jetbrains.kotlin.psi.JetClassObject;
import org.jetbrains.kotlin.psi.JetObjectDeclaration;
import org.jetbrains.kotlin.psi.JetParameter;
import org.jetbrains.kotlin.psi.JetTypeParameterList;
@@ -39,7 +38,7 @@ public class JetObjectInfo extends JetClassOrObjectInfo<JetObjectDeclaration> {
}
@Override
public JetClassObject getClassObject() {
public JetObjectDeclaration getClassObject() {
return null;
}
@@ -28,7 +28,7 @@ public class JetScriptInfo(
override fun getContainingPackageFqName() = fqName.parent()
override fun getModifierList() = null
override fun getClassObject() = null
override fun getClassObjects() = listOf<JetClassObject>()
override fun getClassObjects() = listOf<JetObjectDeclaration>()
override fun getScopeAnchor() = script
override fun getCorrespondingClassOrObject() = null
override fun getTypeParameterList() = null
@@ -44,5 +44,5 @@ public fun shouldBeScriptClassMember(declaration: JetDeclaration): Boolean {
// we only add those vals, vars and funs that have explicitly specified return types
// (or implicit Unit for function with block body)
return declaration is JetCallableDeclaration && declaration.getTypeReference() != null
|| declaration is JetNamedFunction && declaration.hasBlockBody()
|| declaration is JetNamedFunction && declaration.hasBlockBody()
}
@@ -29,12 +29,7 @@ public class PsiBasedClassMemberDeclarationProvider(
override fun doCreateIndex(index: AbstractPsiBasedDeclarationProvider.Index) {
for (declaration in classInfo.getDeclarations()) {
if (declaration !is JetClassObject) {
index.putToIndex(declaration)
}
else {
index.putToIndex(declaration.getObjectDeclaration())
}
index.putToIndex(declaration)
}
for (parameter in classInfo.getPrimaryConstructorParameters()) {
@@ -83,7 +83,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
private final Annotations annotations;
private final Annotations danglingAnnotations;
private final NullableLazyValue<LazyClassDescriptor> classObjectDescriptor;
private final MemoizedFunctionToNotNull<JetClassObject, ClassDescriptor> extraClassObjectDescriptors;
private final MemoizedFunctionToNotNull<JetObjectDeclaration, ClassDescriptor> extraClassObjectDescriptors;
private final LazyClassMemberScope unsubstitutedMemberScope;
private final JetScope staticScope = new StaticScopeForKotlinClass(this);
@@ -186,9 +186,9 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
return computeClassObjectDescriptor(getClassObjectIfAllowed());
}
});
this.extraClassObjectDescriptors = storageManager.createMemoizedFunction(new Function1<JetClassObject, ClassDescriptor>() {
this.extraClassObjectDescriptors = storageManager.createMemoizedFunction(new Function1<JetObjectDeclaration, ClassDescriptor>() {
@Override
public ClassDescriptor invoke(JetClassObject classObject) {
public ClassDescriptor invoke(JetObjectDeclaration classObject) {
return computeClassObjectDescriptor(classObject);
}
});
@@ -357,21 +357,21 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
@NotNull
@ReadOnly
public List<ClassDescriptor> getDescriptorsForExtraClassObjects() {
final JetClassObject allowedClassObject = getClassObjectIfAllowed();
final JetObjectDeclaration allowedClassObject = getClassObjectIfAllowed();
return KotlinPackage.map(
KotlinPackage.filter(
declarationProvider.getOwnerInfo().getClassObjects(),
new Function1<JetClassObject, Boolean>() {
new Function1<JetObjectDeclaration, Boolean>() {
@Override
public Boolean invoke(JetClassObject classObject) {
public Boolean invoke(JetObjectDeclaration classObject) {
return classObject != allowedClassObject;
}
}
),
new Function1<JetClassObject, ClassDescriptor>() {
new Function1<JetObjectDeclaration, ClassDescriptor>() {
@Override
public ClassDescriptor invoke(JetClassObject classObject) {
public ClassDescriptor invoke(JetObjectDeclaration classObject) {
return extraClassObjectDescriptors.invoke(classObject);
}
}
@@ -379,7 +379,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
}
@Nullable
private LazyClassDescriptor computeClassObjectDescriptor(@Nullable JetClassObject classObject) {
private LazyClassDescriptor computeClassObjectDescriptor(@Nullable JetObjectDeclaration classObject) {
JetClassLikeInfo classObjectInfo = getClassObjectInfo(classObject);
if (classObjectInfo instanceof JetClassOrObjectInfo) {
Name name = ((JetClassOrObjectInfo) classObjectInfo).getName();
@@ -404,21 +404,21 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
}
@Nullable
private JetClassLikeInfo getClassObjectInfo(@Nullable JetClassObject classObject) {
private JetClassLikeInfo getClassObjectInfo(@Nullable JetObjectDeclaration classObject) {
if (classObject != null) {
if (!isClassObjectAllowed()) {
c.getTrace().report(CLASS_OBJECT_NOT_ALLOWED.on(classObject));
}
return JetClassInfoUtil.createClassLikeInfo(classObject.getObjectDeclaration());
return JetClassInfoUtil.createClassLikeInfo(classObject);
}
return null;
}
@Nullable
private JetClassObject getClassObjectIfAllowed() {
JetClassObject classObject = declarationProvider.getOwnerInfo().getClassObject();
private JetObjectDeclaration getClassObjectIfAllowed() {
JetObjectDeclaration classObject = declarationProvider.getOwnerInfo().getClassObject();
return (classObject != null && isClassObjectAllowed()) ? classObject : null;
}
@@ -71,10 +71,6 @@ public abstract class AbstractDescriptorRendererTest : KotlinTestWithEnvironment
file.acceptChildren(this)
}
override fun visitClassObject(classObject: JetClassObject) {
classObject.acceptChildren(this)
}
override fun visitParameter(parameter: JetParameter) {
val declaringElement = parameter.getParent().getParent()
when (declaringElement) {
@@ -155,7 +155,7 @@ public class JetIconProvider extends IconProvider implements DumbAware {
}
return icon;
}
if (psiElement instanceof JetObjectDeclaration || psiElement instanceof JetClassObject) {
if (psiElement instanceof JetObjectDeclaration) {
return JetIcons.OBJECT;
}
if (psiElement instanceof JetParameter) {
@@ -29,7 +29,6 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.stubs.StringStubIndexExtension;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashSet;
import kotlin.KotlinPackage;
@@ -80,30 +79,9 @@ public class JetSourceNavigationHelper {
@Nullable
public static JetClassOrObject getSourceClassOrObject(@NotNull JetClassOrObject decompiledClassOrObject) {
if (decompiledClassOrObject instanceof JetObjectDeclaration && decompiledClassOrObject.getParent() instanceof JetClassObject) {
return getSourceClassObject((JetClassObject) decompiledClassOrObject.getParent());
}
return getSourceForNamedClassOrObject(decompiledClassOrObject);
}
private static JetClassOrObject getSourceClassObject(JetClassObject decompiledClassObject) {
JetClass decompiledClass = PsiTreeUtil.getParentOfType(decompiledClassObject, JetClass.class);
assert decompiledClass != null;
JetClass sourceClass = (JetClass) getSourceForNamedClassOrObject(decompiledClass);
if (sourceClass == null) {
return null;
}
if (sourceClass.hasModifier(JetTokens.ENUM_KEYWORD)) {
return sourceClass;
}
JetClassObject classObject = sourceClass.getClassObject();
assert classObject != null;
return classObject.getObjectDeclaration();
}
@NotNull
private static GlobalSearchScope createLibrarySourcesScope(@NotNull JetNamedDeclaration decompiledDeclaration) {
JetFile containingFile = decompiledDeclaration.getContainingJetFile();
@@ -458,11 +436,6 @@ public class JetSourceNavigationHelper {
return getSourceClassOrObject(declaration);
}
@Override
public JetDeclaration visitClassObject(@NotNull JetClassObject classObject, Void data) {
return getSourceClassObject(classObject);
}
@Override
public JetDeclaration visitClass(@NotNull JetClass klass, Void data) {
return getSourceClassOrObject(klass);
@@ -31,7 +31,6 @@ import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.psi.JetDelegationSpecifierList
import org.jetbrains.kotlin.psi.JetDelegatorToSuperClass
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetClassObject
import org.jetbrains.kotlin.serialization.deserialization.ProtoContainer
import org.jetbrains.kotlin.psi.stubs.impl.KotlinModifierListStubImpl
import org.jetbrains.kotlin.lexer.JetModifierKeywordToken
@@ -80,20 +79,10 @@ private class ClassClsStubBuilder(
}
private fun createClassOrObjectStubAndModifierListStub(): StubElement<out PsiElement> {
val isClassObject = classKind == ProtoBuf.Class.Kind.CLASS_OBJECT
if (isClassObject) {
val classObjectStub = KotlinPlaceHolderStubImpl<JetClassObject>(parentStub, JetStubElementTypes.CLASS_OBJECT)
val modifierList = createModifierListForClass(classObjectStub)
val objectDeclarationStub = doCreateClassOrObjectStub(classObjectStub)
createAnnotationStubs(c.components.annotationLoader.loadClassAnnotations(classProto, c.nameResolver), modifierList)
return objectDeclarationStub
}
else {
val classOrObjectStub = doCreateClassOrObjectStub(parentStub)
val modifierList = createModifierListForClass(classOrObjectStub)
createAnnotationStubs(c.components.annotationLoader.loadClassAnnotations(classProto, c.nameResolver), modifierList)
return classOrObjectStub
}
val classOrObjectStub = doCreateClassOrObjectStub()
val modifierList = createModifierListForClass(classOrObjectStub)
createAnnotationStubs(c.components.annotationLoader.loadClassAnnotations(classProto, c.nameResolver), modifierList)
return classOrObjectStub
}
private fun createModifierListForClass(parent: StubElement<out PsiElement>): KotlinModifierListStubImpl {
@@ -110,7 +99,7 @@ private class ClassClsStubBuilder(
return createModifierListStubForDeclaration(parent, classProto.getFlags(), relevantFlags, additionalModifiers)
}
private fun doCreateClassOrObjectStub(parent: StubElement<out PsiElement>): StubElement<out PsiElement> {
private fun doCreateClassOrObjectStub(): StubElement<out PsiElement> {
val isClassObject = classKind == ProtoBuf.Class.Kind.CLASS_OBJECT
val fqName = outerContext.memberFqNameProvider.getMemberFqName(classId.getRelativeClassName().shortName())
val shortName = fqName.shortName()?.ref()
@@ -121,7 +110,7 @@ private class ClassClsStubBuilder(
return when (classKind) {
ProtoBuf.Class.Kind.OBJECT, ProtoBuf.Class.Kind.CLASS_OBJECT -> {
KotlinObjectStubImpl(
parent, shortName, fqName, superTypeRefs,
parentStub, shortName, fqName, superTypeRefs,
isTopLevel = classId.isTopLevelClass(),
isClassObject = isClassObject,
isLocal = false,
@@ -131,7 +120,7 @@ private class ClassClsStubBuilder(
else -> {
KotlinClassStubImpl(
JetClassElementType.getStubType(classKind == ProtoBuf.Class.Kind.ENUM_ENTRY),
parent,
parentStub,
fqName.ref(),
shortName,
superTypeRefs,
@@ -85,9 +85,6 @@ private object DeclarationKindDetector : JetVisitor<AnnotationHostKind?, Unit?>(
override fun visitClass(d: JetClass, _: Unit?) = detect(d, if (d.isTrait()) "trait" else "class")
override fun visitClassObject(d: JetClassObject, _: Unit?) = detect(d, "class object",
name = "of " + d.getStrictParentOfType<JetClass>()?.getName())
override fun visitNamedFunction(d: JetNamedFunction, _: Unit?) = detect(d, "fun")
override fun visitProperty(d: JetProperty, _: Unit?) = detect(d, d.getValOrVarNode().getText()!!)
@@ -102,7 +99,7 @@ private object DeclarationKindDetector : JetVisitor<AnnotationHostKind?, Unit?>(
override fun visitParameter(d: JetParameter, _: Unit?) = detect(d, "parameter", newLineNeeded = false)
override fun visitObjectDeclaration(d: JetObjectDeclaration, _: Unit?): AnnotationHostKind? {
if (d.getParent() is JetClassObject) return null
if (d.isClassObject()) return detect(d, "class object", name = "${d.getName()} of ${d.getStrictParentOfType<JetClass>()?.getName()}")
if (d.getParent() is JetObjectLiteralExpression) return null
return detect(d, "object")
}
@@ -166,10 +166,6 @@ public class JetStructureViewElement implements StructureViewTreeElement, Querya
else if (element instanceof JetClassOrObject) {
return ((JetClassOrObject) element).getDeclarations();
}
else if (element instanceof JetClassObject) {
JetObjectDeclaration objectDeclaration = ((JetClassObject) element).getObjectDeclaration();
return objectDeclaration.getDeclarations();
}
return Collections.emptyList();
}
@@ -60,8 +60,10 @@ public class JetDeclarationMover extends AbstractJetUpDownMover {
}
@Override
public void visitClassObject(@NotNull JetClassObject classObject) {
memberSuspects.add(classObject.getClassKeyword());
public void visitObjectDeclaration(@NotNull JetObjectDeclaration declaration) {
if (declaration.isClassObject()) {
memberSuspects.add(declaration.getClassKeyword());
}
}
@Override
@@ -193,9 +193,6 @@ private fun addDebugExpressionBeforeContextElement(codeFragment: JetCodeFragment
contextElement is JetClassOrObject -> {
insertNewInitializer(contextElement.getBody())
}
contextElement is JetClassObject -> {
insertNewInitializer(contextElement.getObjectDeclaration().getBody())
}
contextElement is JetFunctionLiteral -> {
val block = contextElement.getBodyExpression()!!
block.getStatements().firstOrNull() ?: block.getLastChild()
@@ -1,66 +0,0 @@
/*
* Copyright 2010-2015 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.projectView;
import com.intellij.ide.projectView.PresentationData;
import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.projectView.impl.nodes.AbstractPsiBasedNode;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import org.jetbrains.kotlin.psi.JetClassObject;
import org.jetbrains.kotlin.psi.JetPsiUtil;
import java.util.Collection;
import static org.jetbrains.kotlin.idea.projectView.JetProjectViewUtil.canRepresentPsiElement;
import static org.jetbrains.kotlin.idea.projectView.JetProjectViewUtil.getClassOrObjectChildren;
public class JetClassObjectTreeNode extends AbstractPsiBasedNode<JetClassObject> {
protected JetClassObjectTreeNode(Project project, JetClassObject classObject, ViewSettings viewSettings) {
super(project, classObject, viewSettings);
}
@Override
protected PsiElement extractPsiFromValue() {
return getValue();
}
@Override
protected Collection<AbstractTreeNode> getChildrenImpl() {
return getClassOrObjectChildren(getValue().getObjectDeclaration(), getProject(), getSettings());
}
@Override
protected void updateImpl(PresentationData data) {
data.setPresentableText("<class object>");
}
@Override
public boolean canRepresent(Object element) {
if (!isValid()) {
return false;
}
return super.canRepresent(element) || canRepresentPsiElement(getValue(), element, getSettings());
}
@Override
protected boolean isDeprecated() {
return JetPsiUtil.isDeprecated(getValue());
}
}
@@ -21,7 +21,6 @@ import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.kotlin.psi.JetClassObject;
import org.jetbrains.kotlin.psi.JetClassOrObject;
import org.jetbrains.kotlin.psi.JetDeclaration;
@@ -44,9 +43,6 @@ public final class JetProjectViewUtil {
if (declaration instanceof JetClassOrObject) {
result.add(new JetClassOrObjectTreeNode(project, (JetClassOrObject) declaration, settings));
}
else if (declaration instanceof JetClassObject) {
result.add(new JetClassObjectTreeNode(project, (JetClassObject) declaration, settings));
}
else {
result.add(new JetDeclarationTreeNode(project, declaration, settings));
}
@@ -117,8 +117,7 @@ public class JetRunConfigurationProducer extends RuntimeConfigurationProducer im
currentElement = PsiTreeUtil.getParentOfType((PsiElement) currentElement, JetClassOrObject.class, JetFile.class)) {
JetDeclarationContainer entryPointContainer = currentElement;
if (entryPointContainer instanceof JetClass) {
JetClassObject classObject = ((JetClass) currentElement).getClassObject();
entryPointContainer = classObject != null ? classObject.getObjectDeclaration() : null;
entryPointContainer = ((JetClass) currentElement).getClassObject();
}
if (entryPointContainer != null && mainFunctionDetector.hasMain(entryPointContainer.getDeclarations())) return entryPointContainer;
}
@@ -72,7 +72,6 @@ import org.jetbrains.kotlin.psi.JetParameter
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.psi.JetClassOrObject
import org.jetbrains.kotlin.psi.JetCallableDeclaration
import org.jetbrains.kotlin.psi.JetClassObject
import org.jetbrains.kotlin.psi.JetTypeParameter
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.renderer.DescriptorRenderer
@@ -695,9 +694,6 @@ public class JetPsiUnifier(
e1 is JetMultiDeclaration && e2 is JetMultiDeclaration ->
if (matchMultiDeclarations(e1, e2)) null else UNMATCHED
e1 is JetClassObject && e2 is JetClassObject ->
e1.getObjectDeclaration().matchDeclarations(e2.getObjectDeclaration())
e1 is JetClassInitializer && e2 is JetClassInitializer ->
null
@@ -1,4 +1,4 @@
// "Suppress 'REDUNDANT_NULLABLE' for class object of C" "true"
// "Suppress 'REDUNDANT_NULLABLE' for class object Default of C" "true"
class C {
[suppress("REDUNDANT_NULLABLE")]
@@ -1,4 +1,4 @@
// "Suppress 'REDUNDANT_NULLABLE' for class object of C" "true"
// "Suppress 'REDUNDANT_NULLABLE' for class object Default of C" "true"
class C {
class object {
@@ -37,7 +37,6 @@ import org.jetbrains.kotlin.psi.stubs.KotlinPropertyStub
import kotlin.test.assertEquals
import org.jetbrains.kotlin.psi.JetClassBody
import org.jetbrains.kotlin.psi.JetClassInitializer
import org.jetbrains.kotlin.psi.JetClassObject
import org.jetbrains.kotlin.psi.debugText.getDebugText
public class DebugTextByStubTest : LightCodeInsightFixtureTestCase() {
@@ -201,10 +200,10 @@ public class DebugTextByStubTest : LightCodeInsightFixtureTestCase() {
}
fun testClassObject() {
val tree = createStubTree("class A { class object {} }")
val tree = createStubTree("class A { class object Def {} }")
val classObject = tree.findChildStubByType(JetStubElementTypes.CLASS)!!.findChildStubByType(JetStubElementTypes.CLASS_BODY)!!
.findChildStubByType(JetStubElementTypes.CLASS_OBJECT)
assertEquals("class object in STUB: class A", JetClassObject(classObject as KotlinPlaceHolderStub).getDebugText())
.findChildStubByType(JetStubElementTypes.OBJECT_DECLARATION)
assertEquals("STUB: class object Def", JetObjectDeclaration(classObject as KotlinObjectStub).getDebugText())
}
fun testPropertyAccessors() {
@@ -84,8 +84,11 @@ public class DeclarationBodyVisitor extends TranslatorVisitor<Void> {
}
@Override
public Void visitClassObject(@NotNull JetClassObject classObject, TranslationContext context) {
JetObjectDeclaration declaration = classObject.getObjectDeclaration();
public Void visitObjectDeclaration(@NotNull JetObjectDeclaration declaration, TranslationContext context) {
if (!declaration.isClassObject()) {
// parsed it in initializer visitor => no additional actions are needed
return null;
}
JsExpression value = ClassTranslator.generateClassCreation(declaration, context);
ClassDescriptor descriptor = getClassDescriptor(context.bindingContext(), declaration);
@@ -94,12 +97,6 @@ public class DeclarationBodyVisitor extends TranslatorVisitor<Void> {
return null;
}
@Override
public Void visitObjectDeclaration(@NotNull JetObjectDeclaration declaration, TranslationContext context) {
// parsed it in initializer visitor => no additional actions are needed
return null;
}
@Override
public Void visitNamedFunction(@NotNull JetNamedFunction expression, TranslationContext context) {
FunctionDescriptor descriptor = getFunctionDescriptor(context.bindingContext(), expression);
@@ -65,7 +65,9 @@ public final class InitializerVisitor extends TranslatorVisitor<Void> {
@Override
public Void visitObjectDeclaration(@NotNull JetObjectDeclaration declaration, @NotNull TranslationContext context) {
InitializerUtils.generateObjectInitializer(declaration, result, context);
if (!declaration.isClassObject()) {
InitializerUtils.generateObjectInitializer(declaration, result, context);
}
return null;
}
}