Skip to content

Commit 03476bf

Browse files
committed
SONARJAVA-1398 adapt rule to detection of call to constructors
1 parent 37a18d0 commit 03476bf

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

java-checks/src/main/java/org/sonar/java/checks/InvalidDateValuesCheck.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.sonar.java.checks.methods.MethodMatcher;
2929
import org.sonar.java.tag.Tag;
3030
import org.sonar.plugins.java.api.semantic.Symbol;
31+
import org.sonar.plugins.java.api.tree.Arguments;
3132
import org.sonar.plugins.java.api.tree.BinaryExpressionTree;
3233
import org.sonar.plugins.java.api.tree.ExpressionTree;
3334
import org.sonar.plugins.java.api.tree.IdentifierTree;
@@ -163,25 +164,33 @@ private static MethodMatcher dateMethodInvocationMatcherSetter(String type, Stri
163164
@Override
164165
protected void onMethodInvocationFound(MethodInvocationTree mit) {
165166
String name = getMethodName(mit);
167+
Arguments arguments = mit.arguments();
166168
if ("set".equals(name)) {
167169
// Calendar method
168-
ExpressionTree arg0 = mit.arguments().get(0);
169-
ExpressionTree arg1 = mit.arguments().get(1);
170+
ExpressionTree arg0 = arguments.get(0);
171+
ExpressionTree arg1 = arguments.get(1);
170172
String referenceName = getReferencedCalendarName(arg0);
171173
if (referenceName != null) {
172174
checkArgument(arg1, referenceName, "\"{0}\" is not a valid value for setting \"{1}\".");
173175
}
176+
} else if ("<init>".equals(mit.symbol().name())) {
177+
// call to this() or super()
178+
checkConstructorArguments(mit.arguments());
174179
} else {
175-
ExpressionTree arg = mit.arguments().get(0);
176-
checkArgument(arg, name, "\"{0}\" is not a valid value for \"{1}\" method.");
180+
checkArgument(arguments.get(0), name, "\"{0}\" is not a valid value for \"{1}\" method.");
177181
}
178182
}
179183

180184
@Override
181185
protected void onConstructorFound(NewClassTree newClassTree) {
186+
Arguments arguments = newClassTree.arguments();
187+
checkConstructorArguments(arguments);
188+
}
189+
190+
private void checkConstructorArguments(Arguments arguments) {
182191
// Gregorian Calendar : ignore first argument: year.
183-
for (int i = 1; i < newClassTree.arguments().size(); i++) {
184-
checkArgument(newClassTree.arguments().get(i), GREGORIAN_PARAMETERS[i], "\"{0}\" is not a valid value for setting \"{1}\".");
192+
for (int i = 1; i < arguments.size(); i++) {
193+
checkArgument(arguments.get(i), GREGORIAN_PARAMETERS[i], "\"{0}\" is not a valid value for setting \"{1}\".");
185194
}
186195
}
187196

java-checks/src/test/files/checks/InvalidDateValuesCheck.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,15 @@ int foo() {
7171
calendar.get(Calendar.DST_OFFSET) == 0;
7272
}
7373
}
74+
75+
class RollingCalendar extends GregorianCalendar {
76+
77+
RollingCalendar() {
78+
super(); // Compliant
79+
}
80+
81+
RollingCalendar(String s) {
82+
super(2015, 12, 31); // Noncompliant {{"12" is not a valid value for setting "month".}}
83+
}
84+
85+
}

0 commit comments

Comments
 (0)