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.VirtualFileFinder; 023 import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileKotlinClassFinder; 024 import org.jetbrains.jet.lang.resolve.name.FqName; 025 026 public class CliVirtualFileFinder extends VirtualFileKotlinClassFinder implements VirtualFileFinder { 027 028 @NotNull 029 private final ClassPath classPath; 030 031 public CliVirtualFileFinder(@NotNull ClassPath path) { 032 classPath = path; 033 } 034 035 @Nullable 036 @Override 037 public VirtualFile findVirtualFile(@NotNull FqName className) { 038 for (VirtualFile root : classPath) { 039 VirtualFile fileInRoot = findFileInRoot(className.asString(), root); 040 if (fileInRoot != null) { 041 return fileInRoot; 042 } 043 } 044 return null; 045 } 046 047 //NOTE: copied with some changes from CoreJavaFileManager 048 @Nullable 049 private VirtualFile findFileInRoot(@NotNull String qName, @NotNull VirtualFile root) { 050 String pathRest = qName; 051 VirtualFile cur = root; 052 053 while (true) { 054 int dot = pathRest.indexOf('.'); 055 if (dot < 0) break; 056 057 String pathComponent = pathRest.substring(0, dot); 058 VirtualFile child = cur.findChild(pathComponent); 059 060 if (child == null) break; 061 pathRest = pathRest.substring(dot + 1); 062 cur = child; 063 } 064 065 String className = pathRest.replace('.', '$'); 066 VirtualFile vFile = cur.findChild(className + ".class"); 067 if (vFile != null) { 068 if (!vFile.isValid()) { 069 //TODO: log 070 return null; 071 } 072 //NOTE: currently we use VirtualFileFinder to find Kotlin binaries only 073 if (createKotlinClass(vFile).getClassHeader() != null) { 074 return vFile; 075 } 076 } 077 return null; 078 } 079 }