001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.lang.psi.stubs.elements;
018
019 import com.intellij.lang.ASTNode;
020 import com.intellij.psi.stubs.IndexSink;
021 import com.intellij.psi.stubs.StubElement;
022 import com.intellij.psi.stubs.StubInputStream;
023 import com.intellij.psi.stubs.StubOutputStream;
024 import com.intellij.util.io.StringRef;
025 import org.jetbrains.annotations.NonNls;
026 import org.jetbrains.annotations.NotNull;
027 import org.jetbrains.jet.lang.psi.JetClassObject;
028 import org.jetbrains.jet.lang.psi.JetObjectDeclaration;
029 import org.jetbrains.jet.lang.psi.JetPsiUtil;
030 import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
031 import org.jetbrains.jet.lang.psi.stubs.PsiJetObjectStub;
032 import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetObjectStubImpl;
033 import org.jetbrains.jet.lang.psi.stubs.impl.Utils;
034 import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
035 import org.jetbrains.jet.lang.resolve.name.FqName;
036
037 import java.io.IOException;
038 import java.util.List;
039
040 public class JetObjectElementType extends JetStubElementType<PsiJetObjectStub, JetObjectDeclaration> {
041 public JetObjectElementType(@NotNull @NonNls String debugName) {
042 super(debugName, JetObjectDeclaration.class, PsiJetObjectStub.class);
043 }
044
045 @Override
046 public PsiJetObjectStub createStub(@NotNull JetObjectDeclaration psi, StubElement parentStub) {
047 String name = psi.getName();
048 FqName fqName = ResolveSessionUtils.safeFqNameForLazyResolve(psi);
049 List<String> superNames = PsiUtilPackage.getSuperNames(psi);
050 return new PsiJetObjectStubImpl(parentStub, StringRef.fromString(name), fqName, Utils.INSTANCE$.wrapStrings(superNames),
051 psi.isTopLevel(), isClassObject(psi), psi.isLocal(), psi.isObjectLiteral());
052 }
053
054 @Override
055 public void serialize(@NotNull PsiJetObjectStub stub, @NotNull StubOutputStream dataStream) throws IOException {
056 dataStream.writeName(stub.getName());
057
058 FqName fqName = stub.getFqName();
059 dataStream.writeName(fqName != null ? fqName.toString() : null);
060
061 dataStream.writeBoolean(stub.isTopLevel());
062 dataStream.writeBoolean(stub.isClassObject());
063 dataStream.writeBoolean(stub.isLocal());
064 dataStream.writeBoolean(stub.isObjectLiteral());
065
066 List<String> superNames = stub.getSuperNames();
067 dataStream.writeVarInt(superNames.size());
068 for (String name : superNames) {
069 dataStream.writeName(name);
070 }
071 }
072
073 @NotNull
074 @Override
075 public PsiJetObjectStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
076 StringRef name = dataStream.readName();
077 StringRef fqNameStr = dataStream.readName();
078 FqName fqName = fqNameStr != null ? new FqName(fqNameStr.toString()) : null;
079
080 boolean isTopLevel = dataStream.readBoolean();
081 boolean isClassObject = dataStream.readBoolean();
082 boolean isLocal = dataStream.readBoolean();
083 boolean isObjectLiteral = dataStream.readBoolean();
084
085 int superCount = dataStream.readVarInt();
086 StringRef[] superNames = StringRef.createArray(superCount);
087 for (int i = 0; i < superCount; i++) {
088 superNames[i] = dataStream.readName();
089 }
090
091 return new PsiJetObjectStubImpl(parentStub, name, fqName, superNames, isTopLevel, isClassObject, isLocal, isObjectLiteral);
092 }
093
094 @Override
095 public void indexStub(@NotNull PsiJetObjectStub stub, @NotNull IndexSink sink) {
096 StubIndexServiceFactory.getInstance().indexObject(stub, sink);
097 }
098
099 private static boolean isClassObject(@NotNull JetObjectDeclaration objectDeclaration) {
100 return objectDeclaration.getParent() instanceof JetClassObject;
101 }
102 }