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 KtClassOrObject> 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 @NotNull 044 public KtClassOrObject getCorrespondingClassOrObject() { 045 return element; 046 } 047 048 @Override 049 @Nullable 050 public KtModifierList getModifierList() { 051 return element.getModifierList(); 052 } 053 054 @Override 055 @NotNull 056 public List<KtDeclaration> getDeclarations() { 057 return element.getDeclarations(); 058 } 059 060 @NotNull 061 @Override 062 public List<KtObjectDeclaration> getCompanionObjects() { 063 KtClassBody body = element.getBody(); 064 if (body == null) { 065 return Collections.emptyList(); 066 } 067 return body.getAllCompanionObjects(); 068 } 069 070 @NotNull 071 @Override 072 public PsiElement getScopeAnchor() { 073 return element; 074 } 075 076 @NotNull 077 @Override 078 public FqName getContainingPackageFqName() { 079 PsiFile file = element.getContainingFile(); 080 if (file instanceof KtFile) { 081 KtFile jetFile = (KtFile) file; 082 return jetFile.getPackageFqName(); 083 } 084 throw new IllegalArgumentException("Not in a JetFile: " + element); 085 } 086 087 @NotNull 088 @Override 089 public List<KtAnnotationEntry> getDanglingAnnotations() { 090 KtClassBody body = element.getBody(); 091 return body == null ? Collections.<KtAnnotationEntry>emptyList() : body.getDanglingAnnotations(); 092 } 093 094 @NotNull 095 @Override 096 public List<? extends KtParameter> getPrimaryConstructorParameters() { 097 return element.getPrimaryConstructorParameters(); 098 } 099 100 @Override 101 public String toString() { 102 return "info for " + element.getText(); 103 } 104 }