Skip to content

Minor cleanup using the ClassName util to convert dotted or slashed#2805

Merged
hazendaz merged 7 commits intospotbugs:masterfrom
JuditKnoll:classnameutil
Jan 9, 2024
Merged

Minor cleanup using the ClassName util to convert dotted or slashed#2805
hazendaz merged 7 commits intospotbugs:masterfrom
JuditKnoll:classnameutil

Conversation

@JuditKnoll
Copy link
Copy Markdown
Collaborator

A lot of the times the conversion from dotted to slashed names and wise versa are done on the spot, without much context and adding the annotations. There are already functions for these in the ClassName util class. This PR mainly uses these util functions wherever it's possible, and adds the fromFieldSignatureToDottedClassName() method to ClassName, since the fromFieldSignature() and toDottedClassName() methods are used together quite frequently.
I didn't add a changelog entry, since I don't think it's necessary.

// Lorg/immutables/value/Generated; -> org.immutables.value.Generated
List<String> javaAnnotationNames = Arrays.asList(annotationEntries).stream()
.map((AnnotationEntry ae) -> ClassName.fromFieldSignatureToDottedClassName(ae.getAnnotationType()))
.collect(Collectors.toList());
Copy link
Copy Markdown
Collaborator Author

@JuditKnoll JuditKnoll Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only change where it's not only the usage of the method from the ClassName util class, but also removing the unnecessary block and return, and moving the comment.

@hazendaz
Copy link
Copy Markdown
Member

hazendaz commented Jan 9, 2024

I think due to the size its worth mentioning in a change log just in case something goes wrong users know that it was listed in change log. It otherwise is ok.

@hazendaz hazendaz self-assigned this Jan 9, 2024
@hazendaz hazendaz added this to the SpotBugs 4.8.4 milestone Jan 9, 2024
if (signature.indexOf('.') >= 0) {
assert false;
signature = signature.replace('.', '/');
signature = ClassName.toDottedClassName(signature);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the opposite of the original change, it would be toSlashedClassName.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! Fixed it. I checked it once more, and I couldn't find any other mistakes like this.

@DottedClassName
public static @CheckForNull String fromFieldSignatureToDottedClassName(String signature) {
String slashedClassName = ClassName.fromFieldSignature(signature);
if (slashedClassName != null) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"if not..else" is a double negative, perhaps refactor to "if null then return null else..."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That and remove the 'else' all together. It doesn't allow proper completion when return is within it. Not a huge deal, we need a large cleanup in general of spotbugs but while at it flip it as noted and remove else as unnecessary then.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed it.
I agree, we need a cleanup in spotbugs. I think making it in smaller steps makes it is less overwhelming, easier to reivew and less likely to add issues 😄

@hazendaz hazendaz merged commit ddfbaba into spotbugs:master Jan 9, 2024
@JuditKnoll JuditKnoll deleted the classnameutil branch January 10, 2024 07:23
PatrikScully pushed a commit to PatrikScully/spotbugs that referenced this pull request Jun 14, 2024
…potbugs#2805)

* add ClassName.fromFieldSignatureToDottedClassName

* use ClassName.toSlashedClassName where possible

* remove unnecessary braces and return

* use ClassName.toDottedClassName where possible

* use ClassName.fromFieldSignatureToDottedClassName where possible

* use ClassName util class in PreorderVisitor

* Fixes according to reviews
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants