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