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