In October 1899, James Joyce, aged 17, attended all three days of the trial in Dublin of Samuel Childs for the brutal murder of his brother. This allowed him later to stitch references to the case throughout his novel Ulysses, including a moment when his protagonist Leopold Bloom and others are on their way to Paddy Dignam’s funeral in Glasnevin cemetery and pass Bengal Terrace, where the murder occurred: “Gloomy gardens then went by: one by one: gloomy houses.” When one man says: “That is where Childs was murdered … The last house,” Simon Dedalus replies: “So it is … A gruesome case. Seymour Bushe got him off. Murdered his brother. Or so they said.”

This, as Adrian Hardiman writes in his fascinating, painstaking book on Joyce and the law, “is the first mention in Ulysses of the Childs murder case. In one way or another the case or its protagonists are referred to more than 20 times in the text, sometimes very plainly, at other times obscurely. The case thus emerges as just one of the numerous threads, often submerged but constantly recurring, that form the fabric of the novel.”

Hardiman takes us through a number of law cases that are referred to in this way in Ulysses with such clarity and vivid use of detail that it is easy to imagine how they preoccupied the characters as they wandered in Dublin on 16 June 1904.

Joyce took pains to make his novel topographically and historically accurate, but was careful not to force his characters to do the same. Thus protagonists often forget names or get a detail wrong. The Childs murder, for example, did not happen in “the last house” on Bengal Terrace, but, as Hardiman points out, the second to last. And, while Seymour Bushe was involved in the case, another lawyer, Tim Healy, was more influential in achieving the acquittal.

As Hardiman explains, Joyce may have left Healy’s name out because he actively disliked him. But there may have been another reason. Hardiman offers us a pen picture of Bushe, making clear that he was a man intriguing enough to appear in other parts of Ulysses. Joyce may simply have found Bushe’s antics and colourful reputation more useful in his narrative than Healy’s. It may be that the needs of the book always came first.

In connecting Joyce’s imagination to the Childs murder, Hardiman also points out that the day before the murder, a relative of Joyce’s, who lived in the house next door to Childs, had died. His funeral, which Joyce probably attended, happened on the same day as the Childs inquest, when the jury went to the house to view the body. Also, one of the jurors in the trial, Alexander Keyes, formerly a publican, “is referred to frequently in Ulysses under his own name”. Indeed, keys as a symbol occur throughout the book, thus integrating Mr Keyes and the Childs murder further into the intricate texture of the novel.

The spectre of two political trials and subsequent executions also haunts the novel. The first was the trial of Robert Emmet, whose execution in 1803 offers Joyce a subject for immense mirth in the Cyclops chapter. The other is the trial of the Invincibles in 1882 for the fatal stabbing of the chief secretary for Ireland and the Irish Office’s permanent undersecretary in Phoenix Park.

Hardiman misses no chance to connect what happened legally with the fictional characters

One of the men, known as Skin-the-Goat, who drove the cab that got the murderers away is mentioned in Ulysses a number of times as having “that cabman’s shelter, they say, down there at Butt Bridge”, which Bloom and Stephen Dedalus visit in the Eumaeus chapter. One of the actual murderers is also invoked in Cyclops. His erection (“standing up in their faces like a poker”) when he has been hanged is described to the assembled company in Kiernan’s public house. (“That can be explained by science, says Bloom.”)

With forensic care, Hardiman takes us through the trials of Emmet and the Invincibles. His advantage is that he knows the book as well as he knows the law, and so misses no chance to connect what happened legally with what enters the minds and conversations of the fictional characters. The narrative he offers is thus a sort of undercurrent in Joyce’s book, one of the essential ways in which the novel contains the world and dramatises what people were thinking about in Dublin in 1904. Joyce in Court helps us to notice things in the novel that are hinted at, casually referred to, but are subtly and often powerfully present and remain oddly significant.

The other story of Ulysses and the law is what happened in the UK and US when the book was published. Once more, Hardiman, who was an Irish supreme court judge until his sudden death last year, writes with clarity and with a lawyer’s eye as he describes what the authorities did to prevent the book being published.

He is scathing and scrupulously mean about the English: “To a reader in the early 21st century, what is very striking about the English official correspondence concerning Ulysses is the total self-confidence of politicians and bureaucrats who were still expounding and enforcing high Victorian values in the 1920s and 30s.” And on the objections to Molly Bloom’s soliloquy, he writes: “Equally striking is the elitism they manifest: what possible value could the ‘memoirs’ of ‘an Irish chambermaid’ have?”

He also takes us through how the book was finally published in the US, providing a useful context for the landmark judgment by Judge John Woolsey in 1933 that is perhaps more perceptive than much writing about Ulysses by literary critics. “Joyce,” Woolsey wrote, “has attempted – it seems to me an astonishing success – to show how the screen of consciousness with its ever-changing, kaleidoscopic impression, carries, as it were, on a plastic palimpsest, not only what is there focused of each new observation of the actual things around him, but also in a penumbral zone, residua of past impressions, some recent and some drawn up by association from the domain of the subconscious.”

Joyce in Zurich in 1915, where he began working in earnest on Ulysses. Photograph: Alamy Stock Photo

Joseph Hassett is a US lawyer who has written widely on the work of WB Yeats. His book on the American trials of Ulysses for obscenity is, like Hardiman’s, distinguished by its knowledge of the novel’s text (and indeed by Hassett’s talent as a literary critic) as much as by a familiarity with the niceties of the law and personalities of the lawyers.

Like Hardiman, Hassett deals with how the final victory was won. But his book offers much more detailed and sharper analysis than Hardiman’s of the earlier, disastrous US trial of Ulysses when chapters of the book were serialised in the Little Review.

He writes about the complex Irish-American lawyer and collector John Quinn, who owned the draft of The Waste Land with Ezra Pound’s emendations, most of Conrad’s manuscripts, many Yeats manuscripts, plus the manuscript of Ulysses, as well as 2,500 drawings and paintings, including 12 Picassos and 20 Matisses.

Quinn, as Hassett points out, had also collected some rich and very conservative Catholic Irish-American clients and associates, and had prejudices of his own against sexually explicit material and independent-minded women. Since he was energetic and enthusiastic about art, he might have seemed the perfect lawyer to represent the Little Review when it was prosecuted. Instead, as Hassett recounts, Quinn could not have been a worse choice, and emerges with little honour from this riveting account of his work on behalf of the magazine’s two editors, Margaret Anderson and Jane Heap.

When Anderson read the first chapters of Ulysses, which Joyce began sending in December 1917, she commented to Heap: “This is the most beautiful thing we’ll ever have … We’ll print it if it’s the last efforts of our lives.” Hassett’s reading of what happened next is groundbreaking, going against the general view – in Richard Ellmann’s biography of Joyce, for example, or in other, more recent versions of the story – that Quinn, under the circumstances, did his best in a case that was unwinnable.

Instead, Hassett establishes that Quinn disliked the novel’s more sexually explicit passages, and was not willing to argue the case for its literary merit, one that might have succeeded in an appeals court. Hassett shows that Quinn was not merely incompetent but cynical and almost malevolent as he argued the case. He offers evidence, too, that Quinn was a misogynist who had nothing good to say of the Little Review’s two female editors (who were fined and ordered not to publish any more chapters), and that he confided to Pound that the magazine was “a sewer that covers” its contents “with the common stench and filth”.

Joyce, in the meantime, was busy finishing his book in Paris. As the trial went on in New York, as Hassett writes, “in a fascinating example of the relationship between law and literature”, the novelist used phrases and images from the law reports about his early chapters to add spice to his work in progress, and to anchor it further in life in all its variety, particularity and strangeness. The law, like everything else that came Joyce’s way, had its uses.

• Colm Tóibín’s House of Names is published by Viking.

• Joyce in Court is published by Head of Zeus. To order a copy for £21.25 (RRP £25) go to bookshop.theguardian.com or call 0330 333 6846. Free UK p&p over £10, online orders only. Phone orders min p&p of £1.99.

• The Ulysses Trials: Beauty and Truth Meet the Law is published by Lilliput. To order a copy for £21.25 (RRP £25) go to bookshop.theguardian.com or call 0330 333 6846. Free UK p&p over £10, online orders only. Phone orders min p&p of £1.99.