Skip to content

Commit

Permalink
Add support for com.ibm.icu.text.SimpleDateFormat to MisusedWeekYear …
Browse files Browse the repository at this point in the history
…checker.

RELNOTES: MisusedWeekYear checker now checks for com.ibm.icu.text.SimpleDateFormat.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=111885124
  • Loading branch information
eaftan authored and cushon committed Jan 11, 2016
1 parent 45eaced commit c7d58d7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 12 deletions.
6 changes: 6 additions & 0 deletions core/pom.xml
Expand Up @@ -190,6 +190,12 @@
<version>0.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>56.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -60,19 +60,40 @@
category = JDK, severity = ERROR, maturity = MATURE)
public class MisusedWeekYear extends BugChecker
implements MethodInvocationTreeMatcher, NewClassTreeMatcher {

private static final String JAVA_SIMPLE_DATE_FORMAT = "java.text.SimpleDateFormat";
private static final String ICU_SIMPLE_DATE_FORMAT = "com.ibm.icu.text.SimpleDateFormat";

private static final Matcher<NewClassTree> simpleDateFormatConstructorMatcher =
Matchers.<NewClassTree>anyOf(
constructor().forClass("java.text.SimpleDateFormat").withParameters("java.lang.String"),
constructor().forClass("java.text.SimpleDateFormat")
constructor().forClass(JAVA_SIMPLE_DATE_FORMAT)
.withParameters("java.lang.String"),
constructor().forClass(JAVA_SIMPLE_DATE_FORMAT)
.withParameters("java.lang.String", "java.text.DateFormatSymbols"),
constructor().forClass("java.text.SimpleDateFormat")
.withParameters("java.lang.String", "java.util.Locale"));
constructor().forClass(JAVA_SIMPLE_DATE_FORMAT)
.withParameters("java.lang.String", "java.util.Locale"),
constructor().forClass(ICU_SIMPLE_DATE_FORMAT)
.withParameters("java.lang.String"),
constructor().forClass(ICU_SIMPLE_DATE_FORMAT)
.withParameters("java.lang.String", "com.ibm.icu.text.DateFormatSymbols"),
constructor().forClass(ICU_SIMPLE_DATE_FORMAT)
.withParameters(
"java.lang.String",
"com.ibm.icu.text.DateFormatSymbols",
"com.ibm.icu.util.ULocale"),
constructor().forClass(ICU_SIMPLE_DATE_FORMAT)
.withParameters("java.lang.String", "java.util.Locale"),
constructor().forClass(ICU_SIMPLE_DATE_FORMAT)
.withParameters("java.lang.String", "java.lang.String", "com.ibm.icu.util.ULocale"),
constructor().forClass(ICU_SIMPLE_DATE_FORMAT)
.withParameters("java.lang.String", "com.ibm.icu.util.ULocale"));

private static final Matcher<ExpressionTree> applyPatternMatcher =
Matchers.<ExpressionTree>anyOf(
instanceMethod().onExactClass("java.text.SimpleDateFormat").named("applyPattern"),
instanceMethod().onExactClass("java.text.SimpleDateFormat").named("applyLocalizedPattern"));
instanceMethod().onExactClass(JAVA_SIMPLE_DATE_FORMAT).named("applyPattern"),
instanceMethod().onExactClass(JAVA_SIMPLE_DATE_FORMAT).named("applyLocalizedPattern"),
instanceMethod().onExactClass(ICU_SIMPLE_DATE_FORMAT).named("applyPattern"),
instanceMethod().onExactClass(ICU_SIMPLE_DATE_FORMAT).named("applyLocalizedPattern"));

/**
* Match uses of SimpleDateFormat.applyPattern and SimpleDateFormat.applyLocalizedPattern in
Expand Down Expand Up @@ -106,10 +127,9 @@ public Description matchNewClass(NewClassTree tree, VisitorState state) {

/**
* Given the {@link ExpressionTree} representing the pattern argument to the various
* methods in {@link java.text.SimpleDateFormat} that accept a pattern, construct the description
* for this matcher to return. May be {@link Description#NO_MATCH} if the pattern does not
* have a constant value, does not use the week year format specifier, or is in proper week
* date format.
* methods in SimpleDateFormat that accept a pattern, construct the description for this matcher
* to return. May be {@link Description#NO_MATCH} if the pattern does not have a constant value,
* does not use the week year format specifier, or is in proper week date format.
*/
private Description constructDescription(Tree tree, ExpressionTree patternArg) {
String pattern = (String) ASTHelpers.constValue((JCTree) patternArg);
Expand Down
Expand Up @@ -37,12 +37,17 @@ public void setUp() {
}

@Test
public void testPositiveCase() throws Exception {
public void testPositiveCases() throws Exception {
compilationHelper.addSourceFile("MisusedWeekYearPositiveCases.java").doTest();
}

@Test
public void testPositiveCases2() throws Exception {
compilationHelper.addSourceFile("MisusedWeekYearPositiveCases2.java").doTest();
}

@Test
public void testNegativeCase() throws Exception {
public void testNegativeCases() throws Exception {
compilationHelper.addSourceFile("MisusedWeekYearNegativeCases.java").doTest();
}

Expand Down
@@ -0,0 +1,57 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.bugpatterns;

import com.ibm.icu.text.DateFormatSymbols;
import com.ibm.icu.text.SimpleDateFormat;
import com.ibm.icu.util.ULocale;

import java.util.Locale;

/**
* Tests for {@link com.ibm.icu.text.SimpleDateFormat}.
*/
public class MisusedWeekYearPositiveCases2 {

void testConstructors() {
// BUG: Diagnostic contains: new SimpleDateFormat("yyyy-MM-dd")
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");

// BUG: Diagnostic contains:
sdf = new SimpleDateFormat("YYYY-MM-dd", DateFormatSymbols.getInstance());

// BUG: Diagnostic contains:
sdf = new SimpleDateFormat("YYYY-MM-dd", DateFormatSymbols.getInstance(), ULocale.CANADA);

// BUG: Diagnostic contains:
sdf = new SimpleDateFormat("YYYY-MM-dd", Locale.getDefault());

// BUG: Diagnostic contains:
sdf = new SimpleDateFormat("YYYY-MM-dd", "", ULocale.CANADA);

// BUG: Diagnostic contains:
sdf = new SimpleDateFormat("YYYY-MM-dd", ULocale.CANADA);
}

void testApplyPatternAndApplyLocalizedPattern() {
SimpleDateFormat sdf = new SimpleDateFormat();
// BUG: Diagnostic contains: sdf.applyPattern("yyyy-MM-dd")
sdf.applyPattern("YYYY-MM-dd");
// BUG: Diagnostic contains:
sdf.applyLocalizedPattern("YYYY-MM-dd");
}
}

0 comments on commit c7d58d7

Please sign in to comment.