Yohann Roussel | bb57162 | 2017-11-09 10:47:36 +0100 | [diff] [blame] | 1 | // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | package com.android.tools.apiusagesample; |
| 6 | |
| 7 | import com.android.tools.r8.ArchiveClassFileProvider; |
| 8 | import com.android.tools.r8.ClassFileResourceProvider; |
Ian Zerny | ef028f5 | 2018-01-08 14:23:17 +0100 | [diff] [blame] | 9 | import com.android.tools.r8.DirectoryClassFileProvider; |
| 10 | import com.android.tools.r8.ProgramResource; |
Yohann Roussel | bb57162 | 2017-11-09 10:47:36 +0100 | [diff] [blame] | 11 | import java.io.FileNotFoundException; |
| 12 | import java.io.IOException; |
| 13 | import java.nio.file.Files; |
| 14 | import java.nio.file.Path; |
| 15 | import java.util.concurrent.ConcurrentHashMap; |
| 16 | |
| 17 | public class CachingArchiveClassFileProvider extends ArchiveClassFileProvider { |
| 18 | |
Ian Zerny | ef028f5 | 2018-01-08 14:23:17 +0100 | [diff] [blame] | 19 | private ConcurrentHashMap<String, ProgramResource> resources = new ConcurrentHashMap<>(); |
Yohann Roussel | bb57162 | 2017-11-09 10:47:36 +0100 | [diff] [blame] | 20 | |
| 21 | private CachingArchiveClassFileProvider(Path archive) throws IOException { |
| 22 | super(archive); |
| 23 | } |
| 24 | |
| 25 | @Override |
Ian Zerny | ef028f5 | 2018-01-08 14:23:17 +0100 | [diff] [blame] | 26 | public ProgramResource getProgramResource(String descriptor) { |
| 27 | return resources.computeIfAbsent(descriptor, super::getProgramResource); |
Yohann Roussel | bb57162 | 2017-11-09 10:47:36 +0100 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | public static ClassFileResourceProvider getProvider(Path entry) |
| 31 | throws IOException { |
| 32 | if (Files.isRegularFile(entry)) { |
| 33 | return new CachingArchiveClassFileProvider(entry); |
| 34 | } else if (Files.isDirectory(entry)) { |
| 35 | return DirectoryClassFileProvider.fromDirectory(entry); |
| 36 | } else { |
| 37 | throw new FileNotFoundException(entry.toString()); |
| 38 | } |
| 39 | } |
| 40 | } |