Skip to content

Commit

Permalink
Merge 2069e80 into b7c8c72
Browse files Browse the repository at this point in the history
  • Loading branch information
nrmancuso committed Aug 9, 2021
2 parents b7c8c72 + 2069e80 commit 298033f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;

/**
* Checks whether {@code private} methods can be declared as {@code static}.
Expand Down Expand Up @@ -527,10 +528,13 @@ else if (findFrameByName(frame, objCalledOn.getText()).isPresent()) {
* @return true, if LITERAL_THIS is used or the usage is too complex to check.
*/
private static boolean isIdentShouldBeChecked(DetailAST parentAst) {
final int parentAstType = parentAst.getType();
return parentAstType != TokenTypes.LITERAL_NEW
&& parentAstType != TokenTypes.TYPE
&& parentAstType != TokenTypes.METHOD_DEF;
final int[] parentTypesNotToBeChecked = {
TokenTypes.LITERAL_NEW,
TokenTypes.TYPE,
TokenTypes.METHOD_DEF,
TokenTypes.TYPE_ARGUMENT,
};
return !TokenUtil.isOfType(parentAst, parentTypesNotToBeChecked);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,16 @@ public void testInterface() throws Exception {
.getCanonicalPath(), expected);
}

@Test
public void testTypeParameter() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(StaticMethodCandidateCheck.class);
final String[] expected = {
"20:5: " + getCheckMessage(MSG_KEY, "f2"),
"78:5: " + getCheckMessage(MSG_KEY, "f6"),
"91:5: " + getCheckMessage(MSG_KEY, "f7"),
};
verify(checkConfig, getPath("InputStaticMethodCandidateCheckTypeParameter.java"), expected);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.github.sevntu.checkstyle.checks.design;

public class InputStaticMethodCandidateCheckTypeParameter {
int f1() {
int x = 0;
for (int i = (0+1); ((i) < (6+6)); i += (1+0)) {
x += (i + 100);
(x) += (i + 100/**comment test*/);
x = (x + i + 100);
(x) = (x + i + 100);
}

for (int i = (0+1); (i) < ((6+6)); i += (1+0)) {
System.identityHashCode("hi");
}

return (0);
}

private int f2(int arg1, double arg2) {
int x, a, b, c, d;
String e, f;

x = 0;
a = 0;
b = 0;
c = (a + b);
d = c - 1;

int i = (int) arg2;
i = ((int) arg2);

x += (i + 100 + arg1);
a = (a + b) * (c + d);
b = ((((a + b) * (c + d))));
c = (((a) <= b)) ? 0 : 1;
d = (a) + (b) * (600) / (int) (12.5f) + (int) (arg2);
e = ("this") + ("that") + ("is" + "other");
f = ("this is a really, really long string that should be truncated.");

return (x + a + b + d);
}

private boolean f3() {
int x = f2((1), (13.5));
boolean b = (true);
return (b);
}

public static int f4(int z, int a) {
int r = (z * a);
r = (a > z) ? a : z;
r = ((a > z) ? a : z);
r = (a > z) ? a : (z + z);
return (r * r - 1);
}

public void f5() {
int x, y;
x = 0;
y = 0;
if (x == y) {
print(x);
}
if ((x > y)) {
print(y);
}

while ((x < 10)) {
print(x++);
}

do {
print((y+=100));
} while (y < (4000));
}

private void f6(TypeA a) {
TypeB b = (TypeB) a;
TypeC c = ((TypeC) a);
int r = 12345;
r <<= (3);
TypeParameterized<String> d = ((TypeParameterized<String>) a);
}

private void print(int arg)
{
System.identityHashCode("arg = " + arg);
}

private int f7() {
String f;

f = ("12345678901234567890123");

return 0;
}

static class TypeParameterized<T> {}
static class TypeA extends TypeParameterized<String> {}
static class TypeB extends TypeA {}
static class TypeC extends TypeA {}
}


0 comments on commit 298033f

Please sign in to comment.