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 com.intellij.psi.stubs.IStubElementType; 022 import org.jetbrains.annotations.NotNull; 023 import org.jetbrains.annotations.Nullable; 024 import org.jetbrains.kotlin.lexer.JetModifierKeywordToken; 025 import org.jetbrains.kotlin.lexer.JetToken; 026 import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage; 027 import org.jetbrains.kotlin.psi.stubs.KotlinModifierListStub; 028 import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes; 029 030 import java.util.List; 031 032 public abstract class JetModifierList extends JetElementImplStub<KotlinModifierListStub> implements JetAnnotationsContainer { 033 034 public JetModifierList(@NotNull KotlinModifierListStub stub, @NotNull IStubElementType nodeType) { 035 super(stub, nodeType); 036 } 037 038 public JetModifierList(@NotNull ASTNode node) { 039 super(node); 040 } 041 042 @Override 043 public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) { 044 return visitor.visitModifierList(this, data); 045 } 046 047 @NotNull 048 public List<JetAnnotation> getAnnotations() { 049 return getStubOrPsiChildrenAsList(JetStubElementTypes.ANNOTATION); 050 } 051 052 @NotNull 053 public List<JetAnnotationEntry> getAnnotationEntries() { 054 return PsiUtilPackage.collectAnnotationEntriesFromStubOrPsi(this); 055 } 056 057 public boolean hasModifier(@NotNull JetModifierKeywordToken token) { 058 KotlinModifierListStub stub = getStub(); 059 if (stub != null) { 060 return stub.hasModifier(token); 061 } 062 return getModifierNode(token) != null; 063 } 064 065 @Nullable 066 public PsiElement getModifier(@NotNull JetModifierKeywordToken token) { 067 return findChildByType(token); 068 } 069 070 @Nullable 071 public ASTNode getModifierNode(@NotNull JetToken token) { 072 ASTNode node = getNode().getFirstChildNode(); 073 while (node != null) { 074 if (node.getElementType() == token) return node; 075 node = node.getTreeNext(); 076 } 077 return null; 078 } 079 080 public PsiElement getOwner() { 081 return getParentByStub(); 082 } 083 }