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