001 /* 002 * Copyright 2010-2016 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.asJava; 018 019 import com.intellij.lang.Language; 020 import com.intellij.psi.PsiClass; 021 import com.intellij.psi.PsiElement; 022 import com.intellij.psi.PsiFile; 023 import com.intellij.psi.impl.light.AbstractLightClass; 024 import org.jetbrains.annotations.NotNull; 025 import org.jetbrains.annotations.Nullable; 026 import org.jetbrains.kotlin.idea.KotlinLanguage; 027 import org.jetbrains.kotlin.name.FqName; 028 import org.jetbrains.kotlin.psi.KtClassOrObject; 029 import org.jetbrains.kotlin.psi.KtFile; 030 031 /** 032 * This class serves as a workaround for usages of {@link JavaElementFinder#findClasses} which eventually only need names of files 033 * containing the class. When queried for a package class (e.g. test/TestPackage), {@code findClasses} along with a 034 * {@link KtLightClassForFacade} would also return multiple instances of this class for each file present in the package. The client 035 * code can make use of every file in the package then, since {@code getContainingFile} of these instances will represent the whole package. 036 * <p/> 037 * See {@link LineBreakpoint#findClassCandidatesInSourceContent} for the primary usage this was introduced 038 */ 039 public class FakeLightClassForFileOfPackage extends AbstractLightClass implements KtLightClass, KtJavaMirrorMarker { 040 private final KtLightClassForFacade delegate; 041 private final KtFile file; 042 043 public FakeLightClassForFileOfPackage(@NotNull KtLightClassForFacade delegate, @NotNull KtFile file) { 044 super(delegate.getManager()); 045 this.delegate = delegate; 046 this.file = file; 047 } 048 049 @NotNull 050 @Override 051 public PsiClass getClsDelegate() { 052 return delegate; 053 } 054 055 @Nullable 056 @Override 057 public KtClassOrObject getKotlinOrigin() { 058 return null; 059 } 060 061 @Override 062 public PsiFile getContainingFile() { 063 return file; 064 } 065 066 @Override 067 public boolean isValid() { 068 // This is intentionally false to prevent using this as a real class 069 return false; 070 } 071 072 073 @NotNull 074 @Override 075 public FqName getFqName() { 076 return delegate.getFqName(); 077 } 078 079 @NotNull 080 @Override 081 public PsiClass getDelegate() { 082 return delegate; 083 } 084 085 @NotNull 086 @Override 087 public PsiElement copy() { 088 return new FakeLightClassForFileOfPackage(delegate, file); 089 } 090 091 @Override 092 public String getText() { 093 return null; 094 } 095 096 @NotNull 097 @Override 098 public Language getLanguage() { 099 return KotlinLanguage.INSTANCE; 100 } 101 102 @Override 103 public boolean equals(Object obj) { 104 if (!(obj instanceof FakeLightClassForFileOfPackage)) return false; 105 106 FakeLightClassForFileOfPackage other = (FakeLightClassForFileOfPackage) obj; 107 return file == other.file && delegate.equals(other.delegate); 108 } 109 110 @Override 111 public int hashCode() { 112 return file.hashCode() * 31 + delegate.hashCode(); 113 } 114 }