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.google.common.collect.Lists;
020 import com.intellij.lang.ASTNode;
021 import com.intellij.psi.PsiElement;
022 import com.intellij.psi.tree.TokenSet;
023 import kotlin.CollectionsKt;
024 import kotlin.jvm.functions.Function1;
025 import org.jetbrains.annotations.NotNull;
026 import org.jetbrains.annotations.Nullable;
027 import org.jetbrains.kotlin.KtNodeTypes;
028 import org.jetbrains.kotlin.lexer.KtTokens;
029 import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
030 import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
031
032 import java.util.Arrays;
033 import java.util.List;
034
035 import static org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes.*;
036
037 public class KtClassBody extends KtElementImplStub<KotlinPlaceHolderStub<KtClassBody>> implements KtDeclarationContainer {
038
039 public KtClassBody(@NotNull ASTNode node) {
040 super(node);
041 }
042
043 public KtClassBody(@NotNull KotlinPlaceHolderStub<KtClassBody> stub) {
044 super(stub, CLASS_BODY);
045 }
046
047 @Override
048 @NotNull
049 public List<KtDeclaration> getDeclarations() {
050 return Arrays.asList(getStubOrPsiChildren(DECLARATION_TYPES, KtDeclaration.ARRAY_FACTORY));
051 }
052
053 @Override
054 public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
055 return visitor.visitClassBody(this, data);
056 }
057
058 @NotNull
059 public List<KtClassInitializer> getAnonymousInitializers() {
060 return findChildrenByType(KtNodeTypes.ANONYMOUS_INITIALIZER);
061 }
062
063 @NotNull
064 /* package-protected */ List<KtSecondaryConstructor> getSecondaryConstructors() {
065 return getStubOrPsiChildrenAsList(KtStubElementTypes.SECONDARY_CONSTRUCTOR);
066 }
067
068 @NotNull
069 public List<KtProperty> getProperties() {
070 return getStubOrPsiChildrenAsList(KtStubElementTypes.PROPERTY);
071 }
072
073 @NotNull
074 public List<KtObjectDeclaration> getAllCompanionObjects() {
075 List<KtObjectDeclaration> result = Lists.newArrayList();
076 for (KtObjectDeclaration declaration : getStubOrPsiChildrenAsList(KtStubElementTypes.OBJECT_DECLARATION)) {
077 if (declaration.isCompanion()) {
078 result.add(declaration);
079 }
080 }
081 return result;
082 }
083
084 @Nullable
085 public PsiElement getRBrace() {
086 ASTNode[] children = getNode().getChildren(TokenSet.create(KtTokens.RBRACE));
087 return children.length == 1 ? children[0].getPsi() : null;
088 }
089
090 @Nullable
091 public PsiElement getLBrace() {
092 ASTNode[] children = getNode().getChildren(TokenSet.create(KtTokens.LBRACE));
093 return children.length == 1 ? children[0].getPsi() : null;
094 }
095
096 /**
097 * @return annotations that do not belong to any declaration due to incomplete code or syntax errors
098 */
099 @NotNull
100 public List<KtAnnotationEntry> getDanglingAnnotations() {
101 return CollectionsKt.flatMap(
102 getStubOrPsiChildrenAsList(MODIFIER_LIST),
103 new Function1<KtModifierList, Iterable<KtAnnotationEntry>>() {
104 @Override
105 public Iterable<KtAnnotationEntry> invoke(KtModifierList modifierList) {
106 return modifierList.getAnnotationEntries();
107 }
108 });
109 }
110 }