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        public List<JetProperty> getProperties() {
066            return getStubOrPsiChildrenAsList(JetStubElementTypes.PROPERTY);
067        }
068    
069        @NotNull
070        public List<JetObjectDeclaration> getAllDefaultObjects() {
071            List<JetObjectDeclaration> result = Lists.newArrayList();
072            for (JetObjectDeclaration declaration : getStubOrPsiChildrenAsList(JetStubElementTypes.OBJECT_DECLARATION)) {
073                if (declaration.isDefault()) {
074                    result.add(declaration);
075                }
076            }
077            return result;
078        }
079    
080        @Nullable
081        public PsiElement getRBrace() {
082            ASTNode[] children = getNode().getChildren(TokenSet.create(JetTokens.RBRACE));
083            return children.length == 1 ? children[0].getPsi() : null;
084        }
085    
086        @Nullable
087        public PsiElement getLBrace() {
088            ASTNode[] children = getNode().getChildren(TokenSet.create(JetTokens.LBRACE));
089            return children.length == 1 ? children[0].getPsi() : null;
090        }
091    
092        /**
093         * @return annotations that do not belong to any declaration due to incomplete code or syntax errors
094         */
095        @NotNull
096        public List<JetAnnotationEntry> getDanglingAnnotations() {
097            return KotlinPackage.flatMap(
098                    getStubOrPsiChildrenAsList(MODIFIER_LIST),
099                    new Function1<JetModifierList, Iterable<JetAnnotationEntry>>() {
100                        @Override
101                        public Iterable<JetAnnotationEntry> invoke(JetModifierList modifierList) {
102                            return modifierList.getAnnotationEntries();
103                        }
104                    });
105        }
106    }