001    /*
002     * Copyright 2010-2013 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.jet.lang.psi;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.psi.PsiElement;
021    import com.intellij.psi.tree.TokenSet;
022    import com.intellij.psi.util.PsiTreeUtil;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.annotations.Nullable;
025    import org.jetbrains.jet.JetNodeTypes;
026    import org.jetbrains.jet.lang.psi.stubs.PsiJetClassBodyStub;
027    import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
028    import org.jetbrains.jet.lexer.JetTokens;
029    
030    import java.util.List;
031    
032    public class JetClassBody extends JetElementImplStub<PsiJetClassBodyStub> implements JetDeclarationContainer {
033        public JetClassBody(@NotNull ASTNode node) {
034            super(node);
035        }
036    
037        public JetClassBody(@NotNull PsiJetClassBodyStub stub) {
038            super(stub, JetStubElementTypes.CLASS_BODY);
039        }
040    
041        @Override
042        @NotNull
043        public List<JetDeclaration> getDeclarations() {
044            return PsiTreeUtil.getChildrenOfTypeAsList(this, JetDeclaration.class);
045        }
046    
047        @Override
048        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
049            return visitor.visitClassBody(this, data);
050        }
051    
052        @NotNull
053        public List<JetClassInitializer> getAnonymousInitializers() {
054            return findChildrenByType(JetNodeTypes.ANONYMOUS_INITIALIZER);
055        }
056    
057        @NotNull
058        public List<JetProperty> getProperties() {
059            return findChildrenByType(JetNodeTypes.PROPERTY);
060        }
061    
062        @Nullable
063        public JetClassObject getClassObject() {
064            return (JetClassObject) findChildByType(JetNodeTypes.CLASS_OBJECT);
065        }
066    
067        @NotNull
068        public List<JetClassObject> getAllClassObjects() {
069            //noinspection unchecked
070            return (List) findChildrenByType(JetNodeTypes.CLASS_OBJECT);
071        }
072    
073        @Nullable
074        public PsiElement getRBrace() {
075            ASTNode[] children = getNode().getChildren(TokenSet.create(JetTokens.RBRACE));
076            return children.length == 1 ? children[0].getPsi() : null;
077        }
078    
079        @Nullable
080        public PsiElement getLBrace() {
081            ASTNode[] children = getNode().getChildren(TokenSet.create(JetTokens.LBRACE));
082            return children.length == 1 ? children[0].getPsi() : null;
083        }
084    }