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.load.java.structure.impl;
018
019 import com.intellij.psi.*;
020 import com.intellij.psi.impl.PsiSubstitutorImpl;
021 import kotlin.KotlinPackage;
022 import kotlin.jvm.functions.Function1;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.annotations.Nullable;
025 import org.jetbrains.kotlin.asJava.JetJavaMirrorMarker;
026 import org.jetbrains.kotlin.descriptors.Visibility;
027 import org.jetbrains.kotlin.load.java.structure.*;
028 import org.jetbrains.kotlin.name.FqName;
029 import org.jetbrains.kotlin.name.Name;
030
031 import java.util.Collection;
032 import java.util.HashMap;
033 import java.util.List;
034 import java.util.Map;
035
036 import static org.jetbrains.kotlin.load.java.structure.impl.JavaElementCollectionFromPsiArrayUtil.*;
037
038 public class JavaClassImpl extends JavaClassifierImpl<PsiClass> implements JavaClass, JavaAnnotationOwnerImpl, JavaModifierListOwnerImpl {
039 public JavaClassImpl(@NotNull PsiClass psiClass) {
040 super(psiClass);
041 assert !(psiClass instanceof PsiTypeParameter)
042 : "PsiTypeParameter should be wrapped in JavaTypeParameter, not JavaClass: use JavaClassifier.create()";
043 }
044
045 @Override
046 @NotNull
047 public Collection<JavaClass> getInnerClasses() {
048 return classes(getPsi().getInnerClasses());
049 }
050
051 @Override
052 @Nullable
053 public FqName getFqName() {
054 String qualifiedName = getPsi().getQualifiedName();
055 return qualifiedName == null ? null : new FqName(qualifiedName);
056 }
057
058 @NotNull
059 @Override
060 public Name getName() {
061 return Name.identifier(getPsi().getName());
062 }
063
064 @Override
065 public boolean isInterface() {
066 return getPsi().isInterface();
067 }
068
069 @Override
070 public boolean isAnnotationType() {
071 return getPsi().isAnnotationType();
072 }
073
074 @Override
075 public boolean isEnum() {
076 return getPsi().isEnum();
077 }
078
079 @Override
080 @Nullable
081 public JavaClass getOuterClass() {
082 PsiClass outer = getPsi().getContainingClass();
083 return outer == null ? null : new JavaClassImpl(outer);
084 }
085
086 @NotNull
087 @Override
088 public List<JavaTypeParameter> getTypeParameters() {
089 return typeParameters(getPsi().getTypeParameters());
090 }
091
092 @Override
093 @NotNull
094 public Collection<JavaClassifierType> getSupertypes() {
095 return classifierTypes(getPsi().getSuperTypes());
096 }
097
098 @Override
099 @NotNull
100 public Collection<JavaMethod> getMethods() {
101 return methods(KotlinPackage.filter(getPsi().getMethods(), new Function1<PsiMethod, Boolean>() {
102 @Override
103 public Boolean invoke(PsiMethod method) {
104 return !method.isConstructor();
105 }
106 }));
107 }
108
109 @Override
110 @NotNull
111 public Collection<JavaField> getFields() {
112 // ex. Android plugin generates LightFields for resources started from '.' (.DS_Store file etc)
113 return fields(KotlinPackage.filter(getPsi().getFields(), new Function1<PsiField, Boolean>() {
114 @Override
115 public Boolean invoke(PsiField field) {
116 String name = field.getName();
117 return name != null && Name.isValidIdentifier(name);
118 }
119 }));
120 }
121
122 @Override
123 @NotNull
124 public Collection<JavaConstructor> getConstructors() {
125 return constructors(getPsi().getConstructors());
126 }
127
128 @Override
129 public boolean isAbstract() {
130 return JavaElementUtil.isAbstract(this);
131 }
132
133 @Override
134 public boolean isStatic() {
135 return JavaElementUtil.isStatic(this);
136 }
137
138 @Override
139 public boolean isFinal() {
140 return JavaElementUtil.isFinal(this);
141 }
142
143 @NotNull
144 @Override
145 public Visibility getVisibility() {
146 return JavaElementUtil.getVisibility(this);
147 }
148
149 @NotNull
150 @Override
151 public Collection<JavaAnnotation> getAnnotations() {
152 return JavaElementUtil.getAnnotations(this);
153 }
154
155 @Nullable
156 @Override
157 public JavaAnnotation findAnnotation(@NotNull FqName fqName) {
158 return JavaElementUtil.findAnnotation(this, fqName);
159 }
160
161 @Override
162 @NotNull
163 public JavaClassifierType getDefaultType() {
164 return new JavaClassifierTypeImpl(JavaPsiFacade.getElementFactory(getPsi().getProject()).createType(getPsi()));
165 }
166
167 @Override
168 @NotNull
169 public OriginKind getOriginKind() {
170 PsiClass psiClass = getPsi();
171 if (psiClass instanceof JetJavaMirrorMarker) {
172 return OriginKind.KOTLIN_LIGHT_CLASS;
173 }
174 else if (psiClass instanceof PsiCompiledElement) {
175 return OriginKind.COMPILED;
176 }
177 else {
178 return OriginKind.SOURCE;
179 }
180 }
181
182 @NotNull
183 @Override
184 public JavaType createImmediateType(@NotNull JavaTypeSubstitutor substitutor) {
185 return new JavaClassifierTypeImpl(
186 JavaPsiFacade.getElementFactory(getPsi().getProject()).createType(getPsi(), createPsiSubstitutor(substitutor)));
187 }
188
189 @NotNull
190 private static PsiSubstitutor createPsiSubstitutor(@NotNull JavaTypeSubstitutor substitutor) {
191 Map<PsiTypeParameter, PsiType> substMap = new HashMap<PsiTypeParameter, PsiType>();
192 for (Map.Entry<JavaTypeParameter, JavaType> entry : substitutor.getSubstitutionMap().entrySet()) {
193 PsiTypeParameter key = ((JavaTypeParameterImpl) entry.getKey()).getPsi();
194 if (entry.getValue() == null) {
195 substMap.put(key, null);
196 }
197 else {
198 substMap.put(key, ((JavaTypeImpl) entry.getValue()).getPsi());
199 }
200 }
201
202 return PsiSubstitutorImpl.createSubstitutor(substMap);
203 }
204 }