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