001 /*
002 * Copyright 2010-2015 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.kotlin.psi.stubs.elements;
018
019 import com.intellij.psi.stubs.IndexSink;
020 import com.intellij.psi.stubs.StubElement;
021 import com.intellij.psi.stubs.StubInputStream;
022 import com.intellij.psi.stubs.StubOutputStream;
023 import com.intellij.util.io.StringRef;
024 import org.jetbrains.annotations.NonNls;
025 import org.jetbrains.annotations.NotNull;
026 import org.jetbrains.kotlin.name.FqName;
027 import org.jetbrains.kotlin.psi.JetObjectDeclaration;
028 import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage;
029 import org.jetbrains.kotlin.psi.stubs.KotlinObjectStub;
030 import org.jetbrains.kotlin.psi.stubs.impl.KotlinObjectStubImpl;
031 import org.jetbrains.kotlin.psi.stubs.impl.Utils;
032 import org.jetbrains.kotlin.resolve.lazy.ResolveSessionUtils;
033
034 import java.io.IOException;
035 import java.util.List;
036
037 public class JetObjectElementType extends JetStubElementType<KotlinObjectStub, JetObjectDeclaration> {
038 public JetObjectElementType(@NotNull @NonNls String debugName) {
039 super(debugName, JetObjectDeclaration.class, KotlinObjectStub.class);
040 }
041
042 @Override
043 public KotlinObjectStub createStub(@NotNull JetObjectDeclaration psi, StubElement parentStub) {
044 String name = psi.getName();
045 FqName fqName = ResolveSessionUtils.safeFqNameForLazyResolve(psi);
046 List<String> superNames = PsiUtilPackage.getSuperNames(psi);
047 return new KotlinObjectStubImpl(parentStub, StringRef.fromString(name), fqName, Utils.INSTANCE$.wrapStrings(superNames),
048 psi.isTopLevel(), psi.isCompanion(), psi.isLocal(), psi.isObjectLiteral());
049 }
050
051 @Override
052 public void serialize(@NotNull KotlinObjectStub stub, @NotNull StubOutputStream dataStream) throws IOException {
053 dataStream.writeName(stub.getName());
054
055 FqName fqName = stub.getFqName();
056 dataStream.writeName(fqName != null ? fqName.toString() : null);
057
058 dataStream.writeBoolean(stub.isTopLevel());
059 dataStream.writeBoolean(stub.isCompanion());
060 dataStream.writeBoolean(stub.isLocal());
061 dataStream.writeBoolean(stub.isObjectLiteral());
062
063 List<String> superNames = stub.getSuperNames();
064 dataStream.writeVarInt(superNames.size());
065 for (String name : superNames) {
066 dataStream.writeName(name);
067 }
068 }
069
070 @NotNull
071 @Override
072 public KotlinObjectStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
073 StringRef name = dataStream.readName();
074 StringRef fqNameStr = dataStream.readName();
075 FqName fqName = fqNameStr != null ? new FqName(fqNameStr.toString()) : null;
076
077 boolean isTopLevel = dataStream.readBoolean();
078 boolean isCompanion = dataStream.readBoolean();
079 boolean isLocal = dataStream.readBoolean();
080 boolean isObjectLiteral = dataStream.readBoolean();
081
082 int superCount = dataStream.readVarInt();
083 StringRef[] superNames = StringRef.createArray(superCount);
084 for (int i = 0; i < superCount; i++) {
085 superNames[i] = dataStream.readName();
086 }
087
088 return new KotlinObjectStubImpl(parentStub, name, fqName, superNames, isTopLevel, isCompanion, isLocal, isObjectLiteral);
089 }
090
091 @Override
092 public void indexStub(@NotNull KotlinObjectStub stub, @NotNull IndexSink sink) {
093 StubIndexService.getInstance().indexObject(stub, sink);
094 }
095 }