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.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.jet.lang.resolve.kotlin.KotlinBinaryClassCache;
023    import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinder;
024    import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileKotlinClassFinder;
025    import org.jetbrains.jet.lang.resolve.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 = findKotlinFile(className, root);
041                if (fileInRoot != null) {
042                    return fileInRoot;
043                }
044            }
045            return null;
046        }
047    
048        private static VirtualFile findKotlinFile(@NotNull FqName className, @NotNull VirtualFile root) {
049            VirtualFile vFile = findFileInRoot(className.asString(), root, '.');
050            //NOTE: currently we use VirtualFileFinder to find Kotlin binaries only
051            if (vFile != null && KotlinBinaryClassCache.getKotlinBinaryClass(vFile).getClassHeader() != null) {
052                return vFile;
053            }
054            return null;
055        }
056    
057        @Override
058        public VirtualFile findVirtualFile(@NotNull String internalName) {
059            for (VirtualFile root : classPath) {
060                VirtualFile fileInRoot = findFileInRoot(internalName, root, '/');
061                if (fileInRoot != null) {
062                    return fileInRoot;
063                }
064            }
065            return null;
066        }
067    
068        //NOTE: copied with some changes from CoreJavaFileManager
069        @Nullable
070        private static VirtualFile findFileInRoot(@NotNull String qName, @NotNull VirtualFile root, char separator) {
071            String pathRest = qName;
072            VirtualFile cur = root;
073    
074            while (true) {
075                int dot = pathRest.indexOf(separator);
076                if (dot < 0) break;
077    
078                String pathComponent = pathRest.substring(0, dot);
079                VirtualFile child = cur.findChild(pathComponent);
080    
081                if (child == null) break;
082                pathRest = pathRest.substring(dot + 1);
083                cur = child;
084            }
085    
086            String className = pathRest.replace('.', '$');
087            VirtualFile vFile = cur.findChild(className + ".class");
088            if (vFile != null) {
089                if (!vFile.isValid()) {
090                    //TODO: log
091                    return null;
092                }
093                return vFile;
094            }
095            return null;
096        }
097    
098    }