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.lazy.ResolveSessionUtils;
034    import org.jetbrains.jet.lang.resolve.name.FqName;
035    
036    import java.io.IOException;
037    import java.util.List;
038    
039    public class JetObjectElementType extends JetStubElementType<PsiJetObjectStub, JetObjectDeclaration> {
040        public JetObjectElementType(@NotNull @NonNls String debugName) {
041            super(debugName);
042        }
043    
044        @Override
045        public JetObjectDeclaration createPsiFromAst(@NotNull ASTNode node) {
046            return new JetObjectDeclaration(node);
047        }
048    
049        @Override
050        public JetObjectDeclaration createPsi(@NotNull PsiJetObjectStub stub) {
051            return new JetObjectDeclaration(stub);
052        }
053    
054        @Override
055        public boolean shouldCreateStub(ASTNode node) {
056            return true;
057        }
058    
059        @Override
060        public PsiJetObjectStub createStub(@NotNull JetObjectDeclaration psi, StubElement parentStub) {
061            String name = psi.getName();
062            FqName fqName = ResolveSessionUtils.safeFqNameForLazyResolve(psi);
063            List<String> superNames = PsiUtilPackage.getSuperNames(psi);
064            return new PsiJetObjectStubImpl(
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(parentStub, name, fqName, superNames, isTopLevel, isClassObject, isLocal);
111        }
112    
113        @Override
114        public void indexStub(@NotNull PsiJetObjectStub stub, @NotNull IndexSink sink) {
115            StubIndexServiceFactory.getInstance().indexObject(stub, sink);
116        }
117    
118        private static boolean isClassObject(@NotNull JetObjectDeclaration objectDeclaration) {
119            return objectDeclaration.getParent() instanceof JetClassObject;
120        }
121    }