• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.google.android.jacoco.reporter;
18 
19 import org.jacoco.report.InputStreamSourceFileLocator;
20 
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Optional;
24 import java.util.jar.JarEntry;
25 import java.util.jar.JarFile;
26 
27 /**
28  * Jacoco source file locator that picks files from a jar containing .java files.
29  */
30 public class JarSourceFileLocator extends InputStreamSourceFileLocator {
31 
32     private final JarFile mJarFile;
33 
JarSourceFileLocator(JarFile jarFile, String encoding, int tabWidth)34     protected JarSourceFileLocator(JarFile jarFile, String encoding, int tabWidth) {
35         super(encoding, tabWidth);
36 
37         mJarFile = jarFile;
38     }
39 
40     @Override
getSourceStream(String s)41     protected InputStream getSourceStream(String s) throws IOException {
42         Optional<JarEntry> e = mJarFile.stream().filter(it -> it.getName().endsWith(s)).findFirst();
43         if (e.isPresent()) {
44             return mJarFile.getInputStream(e.get());
45         } else {
46             return null;
47         }
48     }
49 }
50