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.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.jet.lang.psi.JetProperty;
027 import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyStub;
028 import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetPropertyStubImpl;
029 import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
030 import org.jetbrains.jet.lang.resolve.name.FqName;
031
032 import java.io.IOException;
033
034 public class JetPropertyElementType extends JetStubElementType<PsiJetPropertyStub, JetProperty> {
035 public JetPropertyElementType(@NotNull @NonNls String debugName) {
036 super(debugName, JetProperty.class, PsiJetPropertyStub.class);
037 }
038
039 @Override
040 public PsiJetPropertyStub createStub(@NotNull JetProperty 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 PsiJetPropertyStubImpl(
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 PsiJetPropertyStub 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.hasReceiverTypeRef());
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 PsiJetPropertyStub 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 PsiJetPropertyStubImpl(parentStub, name, isVar, isTopLevel, hasDelegate,
085 hasDelegateExpression, hasInitializer, hasReceiverTypeRef, hasReturnTypeRef, fqName);
086 }
087
088 @Override
089 public void indexStub(@NotNull PsiJetPropertyStub stub, @NotNull IndexSink sink) {
090 StubIndexServiceFactory.getInstance().indexProperty(stub, sink);
091 }
092 }