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