001    /*
002     * Copyright 2010-2013 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.jet.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.PsiManager;
024    import com.intellij.psi.impl.light.AbstractLightClass;
025    import org.jetbrains.annotations.NotNull;
026    import org.jetbrains.annotations.Nullable;
027    import org.jetbrains.jet.lang.psi.JetClassOrObject;
028    import org.jetbrains.jet.lang.psi.JetFile;
029    import org.jetbrains.jet.lang.resolve.java.jetAsJava.JetJavaMirrorMarker;
030    import org.jetbrains.jet.lang.resolve.name.FqName;
031    import org.jetbrains.jet.plugin.JetLanguage;
032    
033    /**
034     * This class serves as a workaround for usages of {@link JavaElementFinder#findClasses} which eventually only need names of files
035     * containing the class. When queried for a package class (e.g. test/TestPackage), {@code findClasses} along with a
036     * {@link KotlinLightClassForPackage} would also return multiple instances of this class for each file present in the package. The client
037     * code can make use of every file in the package then, since {@code getContainingFile} of these instances will represent the whole package.
038     * <p/>
039     * See {@link LineBreakpoint#findClassCandidatesInSourceContent} for the primary usage this was introduced
040     */
041    public class FakeLightClassForFileOfPackage extends AbstractLightClass implements KotlinLightClass, JetJavaMirrorMarker {
042        private final KotlinLightClassForPackage delegate;
043        private final JetFile file;
044    
045        public FakeLightClassForFileOfPackage(
046                @NotNull PsiManager manager, @NotNull KotlinLightClassForPackage delegate, @NotNull JetFile file
047        ) {
048            super(manager);
049            this.delegate = delegate;
050            this.file = file;
051        }
052    
053        @Nullable
054        @Override
055        public JetClassOrObject getOrigin() {
056            return null;
057        }
058    
059        @Override
060        public PsiFile getContainingFile() {
061            return file;
062        }
063    
064        @Override
065        public boolean isValid() {
066            // This is intentionally false to prevent using this as a real class
067            return false;
068        }
069    
070    
071        @NotNull
072        @Override
073        public FqName getFqName() {
074            return delegate.getFqName();
075        }
076    
077        @NotNull
078        @Override
079        public PsiClass getDelegate() {
080            return delegate;
081        }
082    
083        @NotNull
084        @Override
085        public PsiElement copy() {
086            return new FakeLightClassForFileOfPackage(getManager(), delegate, file);
087        }
088    
089        @Override
090        public String getText() {
091            return null;
092        }
093    
094        @NotNull
095        @Override
096        public Language getLanguage() {
097            return JetLanguage.INSTANCE;
098        }
099    }