|
| 1 | +// Copyright © 2016 Panayotis Katsaloulis <www.panayotis.com> |
| 2 | +// This software is released under the Apache License 2.0. |
| 3 | +// The license text is at http://www.apache.org/licenses/LICENSE-2.0 |
| 4 | +package net.orfjackal.retrolambda.interfaces; |
| 5 | + |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.HashSet; |
| 14 | +import java.util.List; |
| 15 | +import java.util.function.Predicate; |
| 16 | +import java.util.jar.JarEntry; |
| 17 | +import java.util.jar.JarFile; |
| 18 | + |
| 19 | +public class LibraryInterfaces { |
| 20 | + |
| 21 | + private final Collection<String> requiredInterfaces = new HashSet<>(); |
| 22 | + private final Collection<String> inputDirInterfaces = new HashSet<>(); |
| 23 | + private final Collection<String> resolvedInterfaces = new HashSet<>(); |
| 24 | + |
| 25 | + public void addRequiredInterfaces(String[] interfaceNames) { |
| 26 | + requiredInterfaces.addAll(Arrays.asList(interfaceNames)); |
| 27 | + } |
| 28 | + |
| 29 | + public void addFoundInterface(String interfaceName) { |
| 30 | + inputDirInterfaces.add(interfaceName); |
| 31 | + } |
| 32 | + |
| 33 | + public Collection<byte[]> getMissingInterfaces(List<Path> classpath) { |
| 34 | + Collection<String> missingInterfaces = new HashSet<>(requiredInterfaces); |
| 35 | + missingInterfaces.removeAll(inputDirInterfaces); |
| 36 | + Collection<String> justFound = new HashSet<>(); |
| 37 | + Collection<byte[]> result = new HashSet<>(); |
| 38 | + |
| 39 | + for (Path path : classpath) { |
| 40 | + if (Files.isDirectory(path)) |
| 41 | + for (String missingName : missingInterfaces) { |
| 42 | + Path possibleTarget = path.resolve(missingName + ".class"); |
| 43 | + if (Files.isRegularFile(possibleTarget)) |
| 44 | + try { |
| 45 | + result.add(Files.readAllBytes(possibleTarget)); |
| 46 | + justFound.add(missingName); |
| 47 | + } catch (IOException ex) { |
| 48 | + } |
| 49 | + } |
| 50 | + else if (Files.isRegularFile(path) && path.getFileName().toString().toLowerCase().endsWith(".jar")) { |
| 51 | + JarFile jar = null; |
| 52 | + try { |
| 53 | + jar = new JarFile(path.toFile()); |
| 54 | + } catch (IOException ex) { |
| 55 | + } |
| 56 | + if (jar != null) |
| 57 | + for (String missingName : missingInterfaces) { |
| 58 | + JarEntry entry = jar.getJarEntry(missingName + ".class"); |
| 59 | + if (entry != null) |
| 60 | + try (InputStream inputStream = jar.getInputStream(entry)) { |
| 61 | + byte[] bytes = getBytes(inputStream); |
| 62 | + if (bytes != null) |
| 63 | + result.add(bytes); |
| 64 | + justFound.add(missingName); |
| 65 | + } catch (IOException ex) { |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + missingInterfaces.removeAll(justFound); |
| 70 | + resolvedInterfaces.addAll(justFound); |
| 71 | + justFound.clear(); |
| 72 | + } |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + public Predicate<String> getAcceptedPredicate(boolean defaultMethodsEnabled) { |
| 77 | + return className -> { |
| 78 | + if (className.endsWith("$")) |
| 79 | + className = className.substring(0, className.length() - 1); |
| 80 | + /** |
| 81 | + * Default implementation classes will be emitted only when this |
| 82 | + * class appears in the current classes location, not inside the |
| 83 | + * library classpath. It is the library responsibility to provide |
| 84 | + * default implementation classes for itself. Thus the |
| 85 | + * implementation classes will be emitted only once, when the |
| 86 | + * library is (possibly) downgraded, and not every time the library |
| 87 | + * is consumed by any other project. |
| 88 | + */ |
| 89 | + return !resolvedInterfaces.contains(className); |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + private byte[] getBytes(InputStream in) { |
| 94 | + ByteArrayOutputStream out = new ByteArrayOutputStream(100); |
| 95 | + int value; |
| 96 | + try { |
| 97 | + while ((value = in.read()) >= 0) |
| 98 | + out.write(value); |
| 99 | + } catch (IOException ex) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + return out.toByteArray(); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments