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.cli.jvm.compiler;
018    
019    import com.intellij.openapi.vfs.VirtualFile;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.kotlin.load.kotlin.KotlinBinaryClassCache;
023    import org.jetbrains.kotlin.load.kotlin.VirtualFileFinder;
024    import org.jetbrains.kotlin.load.kotlin.VirtualFileKotlinClassFinder;
025    import org.jetbrains.kotlin.name.FqName;
026    
027    public class CliVirtualFileFinder extends VirtualFileKotlinClassFinder implements VirtualFileFinder {
028    
029        @NotNull
030        private final ClassPath classPath;
031    
032        public CliVirtualFileFinder(@NotNull ClassPath path) {
033            classPath = path;
034        }
035    
036        @Nullable
037        @Override
038        public VirtualFile findVirtualFileWithHeader(@NotNull FqName className) {
039            for (VirtualFile root : classPath) {
040                VirtualFile fileInRoot = findFileInRoot(className.asString(), root, '.');
041                //NOTE: currently we use VirtualFileFinder to find Kotlin binaries only
042                if (fileInRoot != null && KotlinBinaryClassCache.getKotlinBinaryClass(fileInRoot) != null) {
043                    return fileInRoot;
044                }
045            }
046            return null;
047        }
048    
049        @Override
050        public VirtualFile findVirtualFile(@NotNull String internalName) {
051            for (VirtualFile root : classPath) {
052                VirtualFile fileInRoot = findFileInRoot(internalName, root, '/');
053                if (fileInRoot != null) {
054                    return fileInRoot;
055                }
056            }
057            return null;
058        }
059    
060        //NOTE: copied with some changes from CoreJavaFileManager
061        @Nullable
062        private static VirtualFile findFileInRoot(@NotNull String qName, @NotNull VirtualFile root, char separator) {
063            String pathRest = qName;
064            VirtualFile cur = root;
065    
066            while (true) {
067                int dot = pathRest.indexOf(separator);
068                if (dot < 0) break;
069    
070                String pathComponent = pathRest.substring(0, dot);
071                VirtualFile child = cur.findChild(pathComponent);
072    
073                if (child == null) break;
074                pathRest = pathRest.substring(dot + 1);
075                cur = child;
076            }
077    
078            String className = pathRest.replace('.', '$');
079            VirtualFile vFile = cur.findChild(className + ".class");
080            if (vFile != null) {
081                if (!vFile.isValid()) {
082                    //TODO: log
083                    return null;
084                }
085                return vFile;
086            }
087            return null;
088        }
089    
090    }