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    import java.util.concurrent.atomic.AtomicLong;
031    
032    public abstract class KtDeclarationStub<T extends StubElement<?>> extends KtModifierListOwnerStub<T> implements KtDeclaration {
033        private final AtomicLong modificationStamp = new AtomicLong();
034    
035        public KtDeclarationStub(@NotNull T stub, @NotNull IStubElementType nodeType) {
036            super(stub, nodeType);
037        }
038    
039        public KtDeclarationStub(@NotNull ASTNode node) {
040            super(node);
041        }
042    
043        @Override
044        public void subtreeChanged() {
045            super.subtreeChanged();
046            modificationStamp.getAndIncrement();
047        }
048    
049        public long getModificationStamp() {
050            return modificationStamp.get();
051        }
052    
053        @Nullable
054        @Override
055        public KDoc getDocComment() {
056            return FindDocCommentKt.findDocComment(this);
057        }
058    
059        @Override
060        public PsiElement getParent() {
061            T stub = getStub();
062            // we build stubs for local classes/objects too but they have wrong parent
063            if (stub != null && !(stub instanceof KotlinClassOrObjectStub && ((KotlinClassOrObjectStub) stub).isLocal())) {
064                return stub.getParentStub().getPsi();
065            }
066            return super.getParent();
067        }
068    
069        @Override
070        public PsiElement getOriginalElement() {
071            KotlinDeclarationNavigationPolicy navigationPolicy = ServiceManager.getService(KotlinDeclarationNavigationPolicy.class);
072            return navigationPolicy != null ? navigationPolicy.getOriginalElement(this) : this;
073        }
074    
075        @NotNull
076        @Override
077        public PsiElement getNavigationElement() {
078            KotlinDeclarationNavigationPolicy navigationPolicy = ServiceManager.getService(KotlinDeclarationNavigationPolicy.class);
079            return navigationPolicy != null ? navigationPolicy.getNavigationElement(this) : this;
080        }
081    }