001 /* 002 * Copyright 2010-2014 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.lang.resolve.kotlin; 018 019 import com.intellij.ide.highlighter.JavaClassFileType; 020 import com.intellij.openapi.Disposable; 021 import com.intellij.openapi.components.ServiceManager; 022 import com.intellij.openapi.util.Ref; 023 import com.intellij.openapi.vfs.VirtualFile; 024 import com.intellij.util.containers.SLRUCache; 025 import org.jetbrains.annotations.NotNull; 026 import org.jetbrains.annotations.Nullable; 027 028 public final class KotlinBinaryClassCache implements Disposable { 029 030 // This cache must be small: we only query the same file a few times in a row (from different places) 031 // It's local to each thread: we don't want a single instance to synchronize access on, because VirtualFileKotlinClass.create involves 032 // reading files from disk and may take some time 033 private final ThreadLocal<SLRUCache<VirtualFile, Ref<VirtualFileKotlinClass>>> cache = 034 new ThreadLocal<SLRUCache<VirtualFile, Ref<VirtualFileKotlinClass>>>() { 035 @Override 036 protected SLRUCache<VirtualFile, Ref<VirtualFileKotlinClass>> initialValue() { 037 return new SLRUCache<VirtualFile, Ref<VirtualFileKotlinClass>>(2, 2) { 038 @NotNull 039 @Override 040 @SuppressWarnings("deprecation") 041 public Ref<VirtualFileKotlinClass> createValue(VirtualFile virtualFile) { 042 return Ref.create(VirtualFileKotlinClass.OBJECT$.create(virtualFile)); 043 } 044 }; 045 } 046 }; 047 048 @Nullable 049 public static KotlinJvmBinaryClass getKotlinBinaryClass(@NotNull VirtualFile file) { 050 if (file.getFileType() != JavaClassFileType.INSTANCE) return null; 051 052 KotlinBinaryClassCache service = ServiceManager.getService(KotlinBinaryClassCache.class); 053 return service.cache.get().get(file).get(); 054 } 055 056 @Override 057 public void dispose() { 058 // This is only relevant for tests. We create a new instance of Application for each test, and so a new instance of this service is 059 // also created for each test. However all tests share the same event dispatch thread, which would collect all instances of this 060 // thread-local if they're not removed properly. Each instance would transitively retain VFS resulting in OutOfMemoryError 061 cache.remove(); 062 } 063 }