question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[PiranhaJava] Does not remove Java enum constants with fields matching flag value

See original GitHub issue

So let’s say I have an enum with some code like this:

public enum TestExperimentName {
    STALE_FLAG("stale.flag"),
    OTHER_FLAG("other.flag");
    private final String key;
    TestExperimentName(final String key) {
        this.key = key;
    }
}

Running Piranha with -XepOpt:Piranha:FlagName=stale.flag won’t remove the STALE_FLAG constant from the enum (and therefore will also not remove downstream usages).

I have been working on a fix for this, as my organization uses feature flags in this way. Still waiting on legal approval before I can share any code with y’all, however. And AFAIK, it will require its own pass and my current implementation has its own BugChecker implementation.

Would love to know if the maintainers of the Piranha project believe this is out of scope or not for Piranha. While I’d rather contribute to this back upstream to the project, I can see it being arguably made to be in its own project to be run prior to running Piranha, as it is a use case quite possibly specific to my organization’s needs.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
tnterdancommented, Jul 26, 2021

Hmm… I suppose if we can do a look up for every enum’s String field values or constructor when Piranha encounters that enum being used, it would be possible to do in one pass. Let me see what kind of tools I can use from Error Prone to traverse the AST and I’ll get back to y’all with a PR with a new option in the properties.json for this. And it looks like legal just approved me for code sharing 😃

Really appreciate the replies @lazaroclapp and @mkr-plse !

0reactions
mkr-plsecommented, Jul 26, 2021

+1. Let us add it as part of the same EP pass (if necessary, under an option). Some locations to consider updating:

  1. Identify whether the argument matches flagname
  2. Delete the flag definition here
Read more comments on GitHub >

github_iconTop Results From Across the Web

Attaching Values to Java Enum - Baeldung
The Java enum type provides a language-supported way to create and use constant values. By defining a finite set of values, the enum...
Read more >
Enum Types - Java™ Tutorials
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must...
Read more >
java.lang.IllegalArgumentException: No enum constant found
You can introduce static method inside your Enum to check a given string is a correct Enum value. Something like this,
Read more >
Java static code analysis: "enum" fields should not be publicly ...
Java static code analysis. Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your JAVA code.
Read more >
Enum, Flags and bitwise operators - Alan Zucconi
Bitwise operators. A bit mask is, essentially, an integer value in which several binary property (yes/no) are independently stored in its bit.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found