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.resolve.name.FqName;
034
035 import java.io.IOException;
036 import java.util.List;
037
038 public class JetObjectElementType extends JetStubElementType<PsiJetObjectStub, JetObjectDeclaration> {
039 public JetObjectElementType(@NotNull @NonNls String debugName) {
040 super(debugName);
041 }
042
043 @Override
044 public JetObjectDeclaration createPsiFromAst(@NotNull ASTNode node) {
045 return new JetObjectDeclaration(node);
046 }
047
048 @Override
049 public JetObjectDeclaration createPsi(@NotNull PsiJetObjectStub stub) {
050 return new JetObjectDeclaration(stub);
051 }
052
053 @Override
054 public boolean shouldCreateStub(ASTNode node) {
055 return true;
056 }
057
058 @Override
059 public PsiJetObjectStub createStub(@NotNull JetObjectDeclaration psi, StubElement parentStub) {
060 String name = psi.getName();
061 FqName fqName = psi.getFqName();
062 List<String> superNames = PsiUtilPackage.getSuperNames(psi);
063 return new PsiJetObjectStubImpl(
064 JetStubElementTypes.OBJECT_DECLARATION,
065 parentStub,
066 name,
067 fqName,
068 superNames,
069 psi.isTopLevel(),
070 isClassObject(psi),
071 JetPsiUtil.isLocal(psi)
072 );
073 }
074
075 @Override
076 public void serialize(@NotNull PsiJetObjectStub stub, @NotNull StubOutputStream dataStream) throws IOException {
077 dataStream.writeName(stub.getName());
078
079 FqName fqName = stub.getFqName();
080 dataStream.writeName(fqName != null ? fqName.toString() : null);
081
082 dataStream.writeBoolean(stub.isTopLevel());
083 dataStream.writeBoolean(stub.isClassObject());
084 dataStream.writeBoolean(stub.isLocal());
085
086 List<String> superNames = stub.getSuperNames();
087 dataStream.writeVarInt(superNames.size());
088 for (String name : superNames) {
089 dataStream.writeName(name);
090 }
091 }
092
093 @NotNull
094 @Override
095 public PsiJetObjectStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
096 StringRef name = dataStream.readName();
097 StringRef fqNameStr = dataStream.readName();
098 FqName fqName = fqNameStr != null ? new FqName(fqNameStr.toString()) : null;
099
100 boolean isTopLevel = dataStream.readBoolean();
101 boolean isClassObject = dataStream.readBoolean();
102 boolean isLocal = dataStream.readBoolean();
103
104 int superCount = dataStream.readVarInt();
105 StringRef[] superNames = StringRef.createArray(superCount);
106 for (int i = 0; i < superCount; i++) {
107 superNames[i] = dataStream.readName();
108 }
109
110 return new PsiJetObjectStubImpl(
111 JetStubElementTypes.OBJECT_DECLARATION, parentStub, name, fqName, superNames, isTopLevel, isClassObject, isLocal
112 );
113 }
114
115 @Override
116 public void indexStub(@NotNull PsiJetObjectStub stub, @NotNull IndexSink sink) {
117 StubIndexServiceFactory.getInstance().indexObject(stub, sink);
118 }
119
120 private static boolean isClassObject(@NotNull JetObjectDeclaration objectDeclaration) {
121 return objectDeclaration.getParent() instanceof JetClassObject;
122 }
123 }