-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a new Java Rule checking serialVersionUID is static final long
CLoses gh-44
- Loading branch information
Showing
6 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/enofex/taikai/java/SerialVersionUID.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.enofex.taikai.java; | ||
|
||
import com.tngtech.archunit.base.DescribedPredicate; | ||
import com.tngtech.archunit.core.domain.JavaField; | ||
import com.tngtech.archunit.core.domain.JavaModifier; | ||
import com.tngtech.archunit.lang.ArchCondition; | ||
import com.tngtech.archunit.lang.ConditionEvents; | ||
import com.tngtech.archunit.lang.SimpleConditionEvent; | ||
|
||
final class SerialVersionUID { | ||
|
||
private SerialVersionUID() { | ||
} | ||
|
||
static ArchCondition<JavaField> beStaticFinalLong() { | ||
return new ArchCondition<>("be static final long") { | ||
@Override | ||
public void check(JavaField javaField, ConditionEvents events) { | ||
boolean isStatic = javaField.getModifiers().contains(JavaModifier.STATIC); | ||
boolean isFinal = javaField.getModifiers().contains(JavaModifier.FINAL); | ||
boolean isLong = javaField.getRawType().isEquivalentTo(long.class); | ||
|
||
if (!isStatic || !isFinal || !isLong) { | ||
String message = String.format("Field %s in class %s is not static final long", | ||
javaField.getName(), javaField.getOwner().getName()); | ||
events.add(SimpleConditionEvent.violated(javaField, message)); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
static DescribedPredicate<JavaField> namedSerialVersionUID() { | ||
return new DescribedPredicate<>("named serialVersionUID") { | ||
|
||
@Override | ||
public boolean test(JavaField javaField) { | ||
return "serialVersionUID".equals(javaField.getName()); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters