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.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        @Nullable
050        @Override
051        public KtClassOrObject getOrigin() {
052            return null;
053        }
054    
055        @Override
056        public PsiFile getContainingFile() {
057            return file;
058        }
059    
060        @Override
061        public boolean isValid() {
062            // This is intentionally false to prevent using this as a real class
063            return false;
064        }
065    
066    
067        @NotNull
068        @Override
069        public FqName getFqName() {
070            return delegate.getFqName();
071        }
072    
073        @NotNull
074        @Override
075        public PsiClass getDelegate() {
076            return delegate;
077        }
078    
079        @NotNull
080        @Override
081        public PsiElement copy() {
082            return new FakeLightClassForFileOfPackage(delegate, file);
083        }
084    
085        @Override
086        public String getText() {
087            return null;
088        }
089    
090        @NotNull
091        @Override
092        public Language getLanguage() {
093            return KotlinLanguage.INSTANCE;
094        }
095    
096        @Override
097        public boolean equals(Object obj) {
098            if (!(obj instanceof FakeLightClassForFileOfPackage)) return false;
099    
100            FakeLightClassForFileOfPackage other = (FakeLightClassForFileOfPackage) obj;
101            return file == other.file && delegate.equals(other.delegate);
102        }
103    
104        @Override
105        public int hashCode() {
106            return file.hashCode() * 31 + delegate.hashCode();
107        }
108    }