Immigrants being held for deportation don’t have an automatic right under the law to post bond and be set free, the U.S. Supreme Court ruled Tuesday in a decision that could give the Trump administration more freedom to pursue stiff detention policies for illegal immigrants who show up at the border claiming asylum.

In the 5-3 ruling, the justices also took a dim view of the kinds of class-action lawsuits on behalf of immigrants that have become a key tool for anti-Trump immigrant rights activists, leaving the activists worried about the fates of other cases winding their way through the lower courts challenging President Trump on issues such as illegal immigrant Dreamers and Iraqi deportees.

The ruling was also a rebuke by the high court to the U.S. Court of Appeals for the 9th Circuit, which had its decision overturned.

Justice Samuel A. Alito Jr. chided the 9th Circuit for ignoring the text of immigration laws and creating a legal standard for bail hearings “out of thin air” by requiring that immigrants be given periodic bail hearings.

“Spotting a constitutional issue does not give a court the authority to rewrite a statute as it pleases,” Justice Alito wrote for the majority.

The complex case has been percolating through the courts for years, based on a challenge first brought by Alejandro Rodriguez, a Mexican national who has been in the U.S. as a lawful permanent resident since 1987.

The government tried to deport him in 2004 after several criminal convictions. Mr. Rodriguez fought deportation, but the government detained him during that time. He sued, arguing that under the detention law he was entitled to a bond hearing that could result in his release.

The lower courts established a class action for Mr. Rodriguez and other immigrants being similarly detained, and ruled that they were entitled to bond hearings after six months and periodically after that. The lower courts said the law could be unconstitutional otherwise, based on a 2001 Supreme Court ruling that mandated release of immigrants whose home countries refused to take them back.

Justice Alito, joined by Chief Justice John G. Roberts Jr. and Justices Anthony M. Kennedy and Neil Gorsuch — and to an extent, Clarence Thomas — said that amounted to a wholesale “rewrite” of the law.

They sent the case back to the 9th Circuit for another review consistent with their ruling that the law doesn’t require bond hearings for all immigrants in detention.

Immigrant rights groups said they will continue to fight as the case goes back to the lower courts and will argue that the law is unconstitutional if it doesn’t allow for bond hearings.

They got backing Tuesday from the dissenters, led by Justice Stephen G. Breyer. He wrote that the country’s core liberty rights have always included the chance to challenge detention by seeking bail.

“The bail questions before us are technical but at heart they are simple. We need only recall the words of the Declaration of Independence, in particular its insistence that all men and women have ‘certain unalienable Rights,’ and that among them is the right to ‘Liberty,’” he wrote.

Justice Breyer said the majority decision appeared to eat into a key 2001 Supreme Court precedent, the Zadvydas ruling, which established a six-month maximum on detentions in cases in which the government is trying to deport immigrants but their home country won’t take them back.

Detention applies to illegal immigrants caught at the border and inside the U.S., and to legal immigrants whose criminal activity has made them eligible for deportation.

The biggest effect of Tuesday’s ruling could be on the surge of migrants who have shown up on the border in the past few years demanding asylum. Their numbers have swelled as smugglers have taught illegal immigrants to use “magic words” claiming fear of being sent back home to get on the asylum track.

The Trump administration has argued that it should be allowed to hold them while their cases proceed — hoping that it will tamp down on bogus claims.

Analysts said the Tuesday ruling appears to give the administration broad detention powers in those cases.