Skip to content

Commit

Permalink
skip field should be final check if main method is missing #429
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Sep 29, 2024
1 parent 272124b commit c2895bf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ private static <T> boolean canBeFinal(CtField<T> ctField) {

@Override
protected void check(StaticAnalysis staticAnalysis) {
if (!staticAnalysis.getCodeModel().hasMainMethod()) {
return;
}

staticAnalysis.processWith(new AbstractProcessor<CtField<?>>() {
@Override
public void process(CtField<?> ctField) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Test {
void foo() {
this.value = "Value";
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -69,6 +71,8 @@ class Test {
public String toString() {
return this.value;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -90,6 +94,8 @@ class Test {
Test() {
this.value = 2;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -109,6 +115,8 @@ class Test {
Test() {
this.value = 2;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -132,6 +140,8 @@ protected A() {
protected A(String value) {
this.value = value;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -152,6 +162,8 @@ class User {
User() {
this.id = nextId++;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -172,6 +184,8 @@ class User {
User() {
this.id = nextId;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -195,6 +209,8 @@ class User {
nextId = 1;
this.id = nextId;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -216,6 +232,8 @@ class User {
nextId = next;
this.id = next;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -234,6 +252,8 @@ public User(String name, int id) {
this.name = name;
this.id = id;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -258,6 +278,8 @@ public String getName() {
public int getId() {
return id;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand Down Expand Up @@ -289,6 +311,8 @@ public String getName() {
public int getId() {
return id;
}
public static void main(String[] args) {}
}
"""
),
Expand Down Expand Up @@ -329,6 +353,8 @@ public User(String name, int id) {
this.id = id;
this.name = name;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand Down Expand Up @@ -361,6 +387,8 @@ public User(int id) {
public User() {
// no name and id init -> name = null, id = 0
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -386,6 +414,8 @@ public User(String name) {
this.id = 1;
}
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand Down Expand Up @@ -413,6 +443,8 @@ public User(String name) {
this.id = 0;
}
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand Down Expand Up @@ -443,6 +475,8 @@ public User(String name) {
this.id = 5;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -469,6 +503,8 @@ public User(String name) {
this.id = 0;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -494,6 +530,8 @@ public User(String name) {
this.id = 1;
}
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -520,6 +558,8 @@ public User(String name) {
public void update() {
next += 1;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -542,6 +582,8 @@ public User(String name) {
this.name = name;
this.id = 0;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -557,6 +599,8 @@ void testNoConstructorButValue() throws LinterException, IOException {
"""
public class User {
private int id = 1;
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -573,7 +617,9 @@ void testRecordImplicitConstructor() throws LinterException, IOException {
JavaVersion.JAVA_17,
"User",
"""
public record User(int id, String name) {}
public record User(int id, String name) {
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);

Expand All @@ -588,6 +634,8 @@ void testRecordStaticField() throws LinterException, IOException {
"""
public record User(int id, String name) {
private static String ADMIN_NAME = "admin";
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand All @@ -613,6 +661,8 @@ public User() {
private void setUser(String name) {
this.name = name;
}
public static void main(String[] args) {}
}
"""
), PROBLEM_TYPES);
Expand Down

0 comments on commit c2895bf

Please sign in to comment.