Yes. You go into the court of relevant jurisdiction, and file a lawsuit to "quiet the question".
Usually there is a counterparty who has something to say on the subject; sometimes not. If there is an apparent counterparty, they must be served notice of all aspects of the case.
Let's say I manage a pipeline property management company; we are wholly owned by a pipeline company and we buy the land and lease it to said pipeline company. I need a parcel of land to complete a pipeline connection, and as part of my offer I gently remind the landowner, Beth, that we do have the legal right to take the property by eminent domain, with the hopes this will inspire them to a sensible price. Beth says "Bunk! Your LLC doesn't run any pipelines! You don't have eminent domain rights! 500% market price, or go away." Well.
So, for the sake of moving the negotiation along, I go to court and ask the judge to quiet the question of whether we have eminent domain rights. Mind you, I don't go any further than that; I don't want to actually take it by eminent domain if I can talk Beth out of a consensual sale.
The judge will, of course, suspect this applies to an imminent case, and will ask... so I might as well disclose the Beth situation. As such, I will have to serve Beth and keep Beth fully in the loop, so that Beth has full right to make the other side of the argument. Since it could affect others as well, the judge will probably also require publication in whichever newspaper handles legal notices. (Many newspapers are propped up financially by being official legal-notice organs for their county. They have six pages of editorial content, and 18 pages of legal notices and other ads.)
Most likely, the judge will cowardly refuse to rule on whether a pipeline's land holding company has eminent domain, and simply rule that the pipeline company itself does, and could simply buy the land itself then transfer it to my company as a trivial, taxless transaction. So the judge would grant eminent domain conditional on that transaction being done. Beth then offers 133% of market which we haggle to 120% of market + legal fees. Good enough!
- Or, Beth refuses, in which case we file an eminent domain action in the same court and before the same judge; being already aware of the facts of the situation, there is nothing left to litigate, and the gavel drops quickly and predictably.
This is how "quieting a question" works. It is somewhat less confrontational with the counterparty, since you are going to court, sort-of together, to resolve a hypothetical question; once we know whether we are on solid legal footing, we then are able to continue negotiating. Courts love this, because they really want people to negotiate and make consensual settlements.
And if a party insists on filing a suit to on the quieted matter, they can pay their filing fees and lawyer fees to get a lecture: "we already resolved this." (Or alternately, to present new facts which mean the past ruling is no longer on-point; e.g., Beth discovers we don't want the land for a pipeline at all, but for an access road.)
You can also do this with the IRS. This is called a Private Letter Ruling, and it will set you back a $750 filing fee (and legal costs, of course). You'll still get audited, but then you wave the Ruling in front of them and done.
Doing this preemptively in a criminal matter would be adventuresome. Your biggest problem would be the press: it would be impossible to do this discreetly, as the case would be fascinating; and it would put every cop and prosecutor on notice that you have a mind to do the potentially illegal thing.