blob: 775a3089cd34e341d47d0ea91965301bc2b21dfd [file] [log] [blame]
Yohann Rousselbb571622017-11-09 10:47:36 +01001// 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
5package com.android.tools.apiusagesample;
6
7import com.android.tools.r8.ArchiveClassFileProvider;
8import com.android.tools.r8.ClassFileResourceProvider;
Ian Zernyef028f52018-01-08 14:23:17 +01009import com.android.tools.r8.DirectoryClassFileProvider;
10import com.android.tools.r8.ProgramResource;
Yohann Rousselbb571622017-11-09 10:47:36 +010011import java.io.FileNotFoundException;
12import java.io.IOException;
13import java.nio.file.Files;
14import java.nio.file.Path;
15import java.util.concurrent.ConcurrentHashMap;
16
17public class CachingArchiveClassFileProvider extends ArchiveClassFileProvider {
18
Ian Zernyef028f52018-01-08 14:23:17 +010019 private ConcurrentHashMap<String, ProgramResource> resources = new ConcurrentHashMap<>();
Yohann Rousselbb571622017-11-09 10:47:36 +010020
21 private CachingArchiveClassFileProvider(Path archive) throws IOException {
22 super(archive);
23 }
24
25 @Override
Ian Zernyef028f52018-01-08 14:23:17 +010026 public ProgramResource getProgramResource(String descriptor) {
27 return resources.computeIfAbsent(descriptor, super::getProgramResource);
Yohann Rousselbb571622017-11-09 10:47:36 +010028 }
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}