/** * Filter the given file (given by a path). * * @param path the path within the jar file * @param input the contents of the file * @return a stream which provides the content of the file, which may be null (to delete/not * package the file), or the original input stream if the file should be packaged as is, or * possibly a different input stream with the file rewritten */ @Nullable public InputStream filter(@NonNull String path, @NonNull InputStream input) { if (mAnnotationClassFiles.contains(path)) { return null; } if (!mAnnotationOuterClassFiles.contains(path)) { return input; } // Transform try { ClassReader reader = new ClassReader(input); byte[] rewritten = rewriteOuterClass(reader); return new ByteArrayInputStream(rewritten); } catch (IOException ioe) { Extractor.error("Could not process " + path + ": " + ioe.getLocalizedMessage()); return input; } }
/** * Rewrites the outer classes containing the typedefs such that they no longer refer to * the (now removed) typedef annotation inner classes */ private void rewriteOuterClasses(@NonNull File classDir) { for (String relative : mAnnotationOuterClassFiles) { File file = new File(classDir, relative.replace('/', File.separatorChar)); if (!file.isFile()) { Extractor.error("Warning: Could not find outer class " + file + " for typedef"); continue; } byte[] bytes; try { bytes = Files.toByteArray(file); } catch (IOException e) { Extractor.error("Could not read " + file + ": " + e.getLocalizedMessage()); continue; } ClassReader reader = new ClassReader(bytes); byte[] rewritten = rewriteOuterClass(reader); try { Files.write(rewritten, file); } catch (IOException e) { Extractor.error("Could not write " + file + ": " + e.getLocalizedMessage()); //noinspection UnnecessaryContinue continue; } } }