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