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