I finally found an answer to this hidden away in a tutorial on WPS processes.
There isn't much explanation! But it seems to work.
public class PolygonTools {
public static Geometry polygonize(Geometry geometry) {
List lines = LineStringExtracter.getLines(geometry);
Polygonizer polygonizer = new Polygonizer();
polygonizer.add(lines);
Collection polys = polygonizer.getPolygons();
Polygon[] polyArray = GeometryFactory.toPolygonArray(polys);
return geometry.getFactory().createGeometryCollection(polyArray);
}
public static Geometry splitPolygon(Geometry poly, Geometry line) {
Geometry nodedLinework = poly.getBoundary().union(line);
Geometry polys = polygonize(nodedLinework);
// Only keep polygons which are inside the input
List output = new ArrayList();
for (int i = 0; i < polys.getNumGeometries(); i++) {
Polygon candpoly = (Polygon) polys.getGeometryN(i);
if (poly.contains(candpoly.getInteriorPoint())) {
output.add(candpoly);
}
}
return poly.getFactory().createGeometryCollection(GeometryFactory.toGeometryArray(output));
}
}