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.PsiElement;
021    import com.intellij.psi.stubs.IndexSink;
022    import com.intellij.psi.stubs.StubElement;
023    import com.intellij.psi.stubs.StubInputStream;
024    import com.intellij.psi.stubs.StubOutputStream;
025    import com.intellij.util.io.StringRef;
026    import org.jetbrains.annotations.NonNls;
027    import org.jetbrains.annotations.NotNull;
028    import org.jetbrains.jet.lang.psi.JetDeclaration;
029    import org.jetbrains.jet.lang.psi.JetProperty;
030    import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyStub;
031    import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetPropertyStubImpl;
032    import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
033    import org.jetbrains.jet.lang.resolve.name.FqName;
034    
035    import java.io.IOException;
036    
037    public class JetPropertyElementType extends JetStubElementType<PsiJetPropertyStub, JetProperty> {
038        public JetPropertyElementType(@NotNull @NonNls String debugName) {
039            super(debugName, JetProperty.class, PsiJetPropertyStub.class);
040        }
041    
042        @Override
043        public PsiJetPropertyStub createStub(@NotNull JetProperty psi, StubElement parentStub) {
044            assert !psi.isLocal() :
045                    String.format("Should not store local property: %s, parent %s",
046                                  psi.getText(), psi.getParent() != null ? psi.getParent().getText() : "<no parent>");
047    
048            return new PsiJetPropertyStubImpl(
049                    parentStub, StringRef.fromString(psi.getName()),
050                    psi.isVar(), psi.isTopLevel(), psi.hasDelegate(),
051                    psi.hasDelegateExpression(), psi.hasInitializer(),
052                    psi.getReceiverTypeRef() != null, psi.getReturnTypeRef() != null,
053                    ResolveSessionUtils.safeFqNameForLazyResolve(psi)
054            );
055        }
056    
057        @Override
058        public void serialize(@NotNull PsiJetPropertyStub stub, @NotNull StubOutputStream dataStream) throws IOException {
059            dataStream.writeName(stub.getName());
060            dataStream.writeBoolean(stub.isVar());
061            dataStream.writeBoolean(stub.isTopLevel());
062            dataStream.writeBoolean(stub.hasDelegate());
063            dataStream.writeBoolean(stub.hasDelegateExpression());
064            dataStream.writeBoolean(stub.hasInitializer());
065            dataStream.writeBoolean(stub.hasReceiverTypeRef());
066            dataStream.writeBoolean(stub.hasReturnTypeRef());
067    
068            FqName fqName = stub.getFqName();
069            dataStream.writeName(fqName != null ? fqName.asString() : null);
070        }
071    
072        @NotNull
073        @Override
074        public PsiJetPropertyStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
075            StringRef name = dataStream.readName();
076            boolean isVar = dataStream.readBoolean();
077            boolean isTopLevel = dataStream.readBoolean();
078            boolean hasDelegate = dataStream.readBoolean();
079            boolean hasDelegateExpression = dataStream.readBoolean();
080            boolean hasInitializer = dataStream.readBoolean();
081            boolean hasReceiverTypeRef = dataStream.readBoolean();
082            boolean hasReturnTypeRef = dataStream.readBoolean();
083    
084            StringRef fqNameAsString = dataStream.readName();
085            FqName fqName = fqNameAsString != null ? new FqName(fqNameAsString.toString()) : null;
086    
087            return new PsiJetPropertyStubImpl(parentStub, name, isVar, isTopLevel, hasDelegate,
088                                              hasDelegateExpression, hasInitializer, hasReceiverTypeRef, hasReturnTypeRef, fqName);
089        }
090    
091        @Override
092        public void indexStub(@NotNull PsiJetPropertyStub stub, @NotNull IndexSink sink) {
093            StubIndexServiceFactory.getInstance().indexProperty(stub, sink);
094        }
095    }