Open Source Library Subclasses

I use a large open source library and must generate personal subclasses of several classes. What are the best strategies? I would like to keep the original library intact and easily reconfigure it when it is updated. It is unlikely that my code is worth contributing to the project (although I am happy to write in such a way that this is allowed).

The problem is general, but I will illustrate it with an example. I am using Apache PDFBox , which has a program for writing to java.awt.Graphics2D . I replaced this with the Apache Batik toolkit, which provides a subclass of Graphics2D ( org.apache.batik.svggen.SVGGraphics2D ), so I can capture the SVG view. I create an instance

 public static org.apache.batik.svggen.SVGGraphics2D createSVG() { org.w3c.dom.DOMImplementation domImpl = org.apache.batik.dom.GenericDOMImplementation.getDOMImplementation(); org.w3c.dom.Document document = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null); return new org.apache.batik.svggen.SVGGraphics2D(document); } 

The place where the PDFBox uses graphics is org.apache.pdfbox.PDFReader , which I edited to allow new graphics:

  protected void showPage(int pageNumber) { try { PageDrawer drawer = new PageDrawer(); PageWrapper wrapper = new PageWrapper( this ); PDPage page = (PDPage)pages.get(pageNumber); wrapper.displayPage( page ); PDRectangle cropBox = page.findCropBox(); Dimension drawDimension = cropBox.createDimension(); svg = PDFPagePanel.createSVG(); // MY EDIT!!!!!!!!! drawer.drawPage( svg, page, drawDimension ); writeSVG(pageNumber); } catch (IOException exception) { exception.printStackTrace(); } } 

I have a job (this is not a problem). My concern is that I had to hack / edit and recompile a number of distributed PDFBox classes, just to generate and use a subclass. I end up with classes like PMRPDFReader, in the same packages as the library. This is very dirty - I can’t immediately remember where I made the changes, etc.

I feel that I should be able to use the library as is and just add / link my subclasses. I am using maven, so there may be a way to exclude source classes.

+6
source share
2 answers

Here's how I would do it:

  • Create a separate project containing changes to the source code. Check it out for source code / version control (SVN, Git, whatever you use). This project will contain your changes.
  • Make sure this is a Maven project. Use the same version number as the open source project, but add the application to the version. If you are using version 1.2, create your own project to have version 1.2-Peter-1 or something similar. This way you will always know what version it is based on, you can also provide several versions / changes of your changes. This will also not contradict the official versions. Use the same group / artifact identifiers as the official ones.
  • Use the source project as a dependency in your own.

For deployment, you have two options:

+1
source

If the library did not provide light subclasses, you do not have much choice. Given that they have a git mirror, I would just fork it (keep the mirror somewhere safe and back up, preferably in the immediate vicinity of your regular SCM). My usual strategy is to add -companyName to the version number (in Maven) so that I can remember that this is a fixed version. You can easily see what changes you made, as they will be in your change history.

You can also explore using the maven shade plugin to change / replace classes, but this is a bit of a hack IMHO.

+1
source

Source: https://habr.com/ru/post/914104/


All Articles