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.JetNodeTypes;
024 import org.jetbrains.kotlin.lexer.JetTokens;
025 import org.jetbrains.kotlin.psi.stubs.KotlinAnnotationEntryStub;
026 import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
027
028 import java.util.Collections;
029 import java.util.List;
030
031 public class JetAnnotationEntry extends JetElementImplStub<KotlinAnnotationEntryStub> implements JetCallElement {
032 public JetAnnotationEntry(@NotNull ASTNode node) {
033 super(node);
034 }
035
036 public JetAnnotationEntry(@NotNull KotlinAnnotationEntryStub stub) {
037 super(stub, JetStubElementTypes.ANNOTATION_ENTRY);
038 }
039
040 @Override
041 public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
042 return visitor.visitAnnotationEntry(this, data);
043 }
044
045
046 @Nullable @IfNotParsed
047 public JetTypeReference getTypeReference() {
048 JetConstructorCalleeExpression calleeExpression = getCalleeExpression();
049 if (calleeExpression == null) {
050 return null;
051 }
052 return calleeExpression.getTypeReference();
053 }
054
055 @Override
056 public JetConstructorCalleeExpression getCalleeExpression() {
057 return getStubOrPsiChild(JetStubElementTypes.CONSTRUCTOR_CALLEE);
058 }
059
060 @Override
061 public JetValueArgumentList getValueArgumentList() {
062 KotlinAnnotationEntryStub stub = getStub();
063 if (stub != null && !stub.hasValueArguments()) {
064 return null;
065 }
066 return (JetValueArgumentList) findChildByType(JetNodeTypes.VALUE_ARGUMENT_LIST);
067 }
068
069 @NotNull
070 @Override
071 public List<? extends ValueArgument> getValueArguments() {
072 JetValueArgumentList list = getValueArgumentList();
073 return list != null ? list.getArguments() : Collections.<JetValueArgument>emptyList();
074 }
075
076 @NotNull
077 @Override
078 public List<JetFunctionLiteralArgument> getFunctionLiteralArguments() {
079 return Collections.emptyList();
080 }
081
082 @NotNull
083 @Override
084 public List<JetTypeProjection> getTypeArguments() {
085 JetTypeArgumentList typeArgumentList = getTypeArgumentList();
086 if (typeArgumentList == null) {
087 return Collections.emptyList();
088 }
089 return typeArgumentList.getArguments();
090 }
091
092 @Override
093 public JetTypeArgumentList getTypeArgumentList() {
094 JetTypeReference typeReference = getTypeReference();
095 if (typeReference == null) {
096 return null;
097 }
098 JetTypeElement typeElement = typeReference.getTypeElement();
099 if (typeElement instanceof JetUserType) {
100 JetUserType userType = (JetUserType) typeElement;
101 return userType.getTypeArgumentList();
102 }
103 return null;
104 }
105
106 @Nullable
107 public PsiElement getAtSymbol() {
108 return findChildByType(JetTokens.AT);
109 }
110
111 public boolean hasAtSymbolOrInList() {
112 if (getStub() != null) return true;
113
114 return getAtSymbol() != null || getParent() instanceof JetAnnotation;
115 }
116
117 @Nullable
118 public JetAnnotationUseSiteTarget getUseSiteTarget() {
119 JetAnnotationUseSiteTarget target = getStubOrPsiChild(JetStubElementTypes.ANNOTATION_TARGET);
120
121 if (target == null) {
122 PsiElement parent = getStubOrPsiParent();
123 if (parent instanceof JetAnnotation) {
124 return ((JetAnnotation) parent).getUseSiteTarget();
125 }
126 }
127
128 return target;
129 }
130 }