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.resolve.lazy.data;
018    
019    import com.intellij.psi.PsiElement;
020    import com.intellij.psi.PsiFile;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.kotlin.name.FqName;
024    import org.jetbrains.kotlin.name.Name;
025    import org.jetbrains.kotlin.psi.*;
026    
027    import java.util.Collections;
028    import java.util.List;
029    
030    public abstract class JetClassOrObjectInfo<E extends JetClassOrObject> implements JetClassLikeInfo {
031        protected final E element;
032    
033        protected JetClassOrObjectInfo(@NotNull E element) {
034            this.element = element;
035        }
036    
037        @Nullable
038        public Name getName() {
039            return element.getNameAsName();
040        }
041    
042        @Override
043        public JetClassOrObject getCorrespondingClassOrObject() {
044            return element;
045        }
046    
047        @Override
048        @Nullable
049        public JetModifierList getModifierList() {
050            return element.getModifierList();
051        }
052    
053        @Override
054        @NotNull
055        public List<JetDeclaration> getDeclarations() {
056            return element.getDeclarations();
057        }
058    
059        @NotNull
060        @Override
061        public List<JetObjectDeclaration> getDefaultObjects() {
062            JetClassBody body = element.getBody();
063            if (body == null) {
064                return Collections.emptyList();
065            }
066            return body.getAllDefaultObjects();
067        }
068    
069        @NotNull
070        @Override
071        public PsiElement getScopeAnchor() {
072            return element;
073        }
074    
075        @NotNull
076        @Override
077        public FqName getContainingPackageFqName() {
078            PsiFile file = element.getContainingFile();
079            if (file instanceof JetFile) {
080                JetFile jetFile = (JetFile) file;
081                return jetFile.getPackageFqName();
082            }
083            throw new IllegalArgumentException("Not in a JetFile: " + element);
084        }
085    
086        @NotNull
087        @Override
088        public List<JetAnnotationEntry> getDanglingAnnotations() {
089            JetClassBody body = element.getBody();
090            return body == null ? Collections.<JetAnnotationEntry>emptyList() : body.getDanglingAnnotations();
091        }
092    
093        @Override
094        public String toString() {
095            return "info for " + element.getText();
096        }
097    }