001 /*
002 * Copyright 2010-2014 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;
018
019 import com.google.common.collect.Lists;
020 import com.intellij.lang.ASTNode;
021 import com.intellij.psi.PsiElement;
022 import com.intellij.psi.stubs.IStubElementType;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.annotations.Nullable;
025 import org.jetbrains.jet.lang.psi.stubs.PsiJetModifierListStub;
026 import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
027 import org.jetbrains.jet.lexer.JetModifierKeywordToken;
028 import org.jetbrains.jet.lexer.JetToken;
029
030 import java.util.ArrayList;
031 import java.util.Collections;
032 import java.util.List;
033
034 public abstract class JetModifierList extends JetElementImplStub<PsiJetModifierListStub> {
035
036 public JetModifierList(@NotNull PsiJetModifierListStub stub, @NotNull IStubElementType nodeType) {
037 super(stub, nodeType);
038 }
039
040 public JetModifierList(@NotNull ASTNode node) {
041 super(node);
042 }
043
044 @Override
045 public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
046 return visitor.visitModifierList(this, data);
047 }
048
049 @NotNull
050 public List<JetAnnotation> getAnnotations() {
051 return getStubOrPsiChildrenAsList(JetStubElementTypes.ANNOTATION);
052 }
053
054 @NotNull
055 public List<JetAnnotationEntry> getAnnotationEntries() {
056 List<JetAnnotationEntry> entries = getStubOrPsiChildrenAsList(JetStubElementTypes.ANNOTATION_ENTRY);
057 List<JetAnnotationEntry> answer = entries.isEmpty() ? null : Lists.newArrayList(entries);
058 for (JetAnnotation annotation : getAnnotations()) {
059 if (answer == null) answer = new ArrayList<JetAnnotationEntry>();
060 answer.addAll(annotation.getEntries());
061 }
062 return answer != null ? answer : Collections.<JetAnnotationEntry>emptyList();
063 }
064
065 public boolean hasModifier(@NotNull JetModifierKeywordToken token) {
066 PsiJetModifierListStub stub = getStub();
067 if (stub != null) {
068 return stub.hasModifier(token);
069 }
070 return getModifierNode(token) != null;
071 }
072
073 @Nullable
074 public PsiElement getModifier(@NotNull JetModifierKeywordToken token) {
075 return findChildByType(token);
076 }
077
078 @Nullable
079 public ASTNode getModifierNode(@NotNull JetToken token) {
080 ASTNode node = getNode().getFirstChildNode();
081 while (node != null) {
082 if (node.getElementType() == token) return node;
083 node = node.getTreeNext();
084 }
085 return null;
086 }
087 }