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.psi.PsiElement;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.kotlin.KtNodeTypes;
024    import org.jetbrains.kotlin.lexer.KtTokens;
025    import org.jetbrains.kotlin.psi.stubs.KotlinAnnotationEntryStub;
026    import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
027    
028    import java.util.Collections;
029    import java.util.List;
030    
031    public class KtAnnotationEntry extends KtElementImplStub<KotlinAnnotationEntryStub> implements KtCallElement {
032        public KtAnnotationEntry(@NotNull ASTNode node) {
033            super(node);
034        }
035    
036        public KtAnnotationEntry(@NotNull KotlinAnnotationEntryStub stub) {
037            super(stub, KtStubElementTypes.ANNOTATION_ENTRY);
038        }
039    
040        @Override
041        public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
042            return visitor.visitAnnotationEntry(this, data);
043        }
044    
045    
046        @Nullable @IfNotParsed
047        public KtTypeReference getTypeReference() {
048            KtConstructorCalleeExpression calleeExpression = getCalleeExpression();
049            if (calleeExpression == null) {
050                return null;
051            }
052            return calleeExpression.getTypeReference();
053        }
054    
055        @Override
056        public KtConstructorCalleeExpression getCalleeExpression() {
057            return getStubOrPsiChild(KtStubElementTypes.CONSTRUCTOR_CALLEE);
058        }
059    
060        @Override
061        public KtValueArgumentList getValueArgumentList() {
062            KotlinAnnotationEntryStub stub = getStub();
063            if (stub != null && !stub.hasValueArguments()) {
064                return null;
065            }
066            return (KtValueArgumentList) findChildByType(KtNodeTypes.VALUE_ARGUMENT_LIST);
067        }
068    
069        @NotNull
070        @Override
071        public List<? extends ValueArgument> getValueArguments() {
072            KtValueArgumentList list = getValueArgumentList();
073            return list != null ? list.getArguments() : Collections.<KtValueArgument>emptyList();
074        }
075    
076        @NotNull
077        @Override
078        public List<KtLambdaArgument> getLambdaArguments() {
079            return Collections.emptyList();
080        }
081    
082        @NotNull
083        @Override
084        public List<KtTypeProjection> getTypeArguments() {
085            KtTypeArgumentList typeArgumentList = getTypeArgumentList();
086            if (typeArgumentList == null) {
087                return Collections.emptyList();
088            }
089            return typeArgumentList.getArguments();
090        }
091    
092        @Override
093        public KtTypeArgumentList getTypeArgumentList() {
094            KtTypeReference typeReference = getTypeReference();
095            if (typeReference == null) {
096                return null;
097            }
098            KtTypeElement typeElement = typeReference.getTypeElement();
099            if (typeElement instanceof KtUserType) {
100                KtUserType userType = (KtUserType) typeElement;
101                return userType.getTypeArgumentList();
102            }
103            return null;
104        }
105    
106        @Nullable
107        public PsiElement getAtSymbol() {
108            return findChildByType(KtTokens.AT);
109        }
110    
111        @Nullable
112        public KtAnnotationUseSiteTarget getUseSiteTarget() {
113            KtAnnotationUseSiteTarget target = getStubOrPsiChild(KtStubElementTypes.ANNOTATION_TARGET);
114    
115            if (target == null) {
116                PsiElement parent = getParentByStub();
117                if (parent instanceof KtAnnotation) {
118                    return ((KtAnnotation) parent).getUseSiteTarget();
119                }
120            }
121    
122            return target;
123        }
124    }