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;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.openapi.components.ServiceManager;
021    import com.intellij.psi.PsiElement;
022    import com.intellij.psi.stubs.IStubElementType;
023    import com.intellij.psi.stubs.StubElement;
024    import org.jetbrains.annotations.NotNull;
025    import org.jetbrains.annotations.Nullable;
026    import org.jetbrains.kotlin.kdoc.psi.api.KDoc;
027    import org.jetbrains.kotlin.psi.findDocComment.FindDocCommentKt;
028    import org.jetbrains.kotlin.psi.stubs.KotlinClassOrObjectStub;
029    
030    public abstract class KtDeclarationStub<T extends StubElement<?>> extends KtModifierListOwnerStub<T> implements KtDeclaration {
031        private long modificationStamp = 0;
032    
033        public KtDeclarationStub(@NotNull T stub, @NotNull IStubElementType nodeType) {
034            super(stub, nodeType);
035        }
036    
037        public KtDeclarationStub(@NotNull ASTNode node) {
038            super(node);
039        }
040    
041        @Override
042        public void subtreeChanged() {
043            super.subtreeChanged();
044            modificationStamp++;
045        }
046    
047        public long getModificationStamp() {
048            return modificationStamp;
049        }
050    
051        @Nullable
052        @Override
053        public KDoc getDocComment() {
054            return FindDocCommentKt.findDocComment(this);
055        }
056    
057        @Override
058        public PsiElement getParent() {
059            T stub = getStub();
060            // we build stubs for local classes/objects too but they have wrong parent
061            if (stub != null && !(stub instanceof KotlinClassOrObjectStub && ((KotlinClassOrObjectStub) stub).isLocal())) {
062                return stub.getParentStub().getPsi();
063            }
064            return super.getParent();
065        }
066    
067        @Override
068        public PsiElement getOriginalElement() {
069            KotlinDeclarationNavigationPolicy navigationPolicy = ServiceManager.getService(KotlinDeclarationNavigationPolicy.class);
070            return navigationPolicy != null ? navigationPolicy.getOriginalElement(this) : this;
071        }
072    
073        @NotNull
074        @Override
075        public PsiElement getNavigationElement() {
076            KotlinDeclarationNavigationPolicy navigationPolicy = ServiceManager.getService(KotlinDeclarationNavigationPolicy.class);
077            return navigationPolicy != null ? navigationPolicy.getNavigationElement(this) : this;
078        }
079    }