Skip to content

Commit 865a0df

Browse files
authored
Factorize code in AbstractNameCheck (#98)
1 parent 9aa2322 commit 865a0df

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

python-checks/src/main/java/org/sonar/python/checks/AbstractFunctionNameCheck.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@
2323
import com.sonar.sslr.api.AstNode;
2424
import com.sonar.sslr.api.AstNodeType;
2525
import java.util.Set;
26-
import java.util.regex.Pattern;
2726
import org.sonar.check.RuleProperty;
28-
import org.sonar.python.PythonCheck;
2927
import org.sonar.python.api.PythonGrammar;
3028

31-
public abstract class AbstractFunctionNameCheck extends PythonCheck {
29+
public abstract class AbstractFunctionNameCheck extends AbstractNameCheck {
3230

3331
private static final String DEFAULT = "^[a-z_][a-z0-9_]{2,}$";
3432
private static final String MESSAGE = "Rename %s \"%s\" to match the regular expression %s.";
@@ -37,13 +35,10 @@ public abstract class AbstractFunctionNameCheck extends PythonCheck {
3735
key = "format",
3836
defaultValue = "" + DEFAULT)
3937
public String format = DEFAULT;
40-
private Pattern pattern = null;
4138

42-
private Pattern pattern() {
43-
if (pattern == null) {
44-
pattern = Pattern.compile(format);
45-
}
46-
return pattern;
39+
@Override
40+
protected String format() {
41+
return format;
4742
}
4843

4944
@Override
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011-2017 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.python.checks;
21+
22+
import java.util.regex.Pattern;
23+
import org.sonar.python.PythonCheck;
24+
25+
public abstract class AbstractNameCheck extends PythonCheck {
26+
27+
private Pattern pattern = null;
28+
29+
protected Pattern pattern() {
30+
if (pattern == null) {
31+
pattern = Pattern.compile(format());
32+
}
33+
return pattern;
34+
}
35+
36+
protected abstract String format();
37+
38+
}

python-checks/src/main/java/org/sonar/python/checks/ClassNameCheck.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323
import com.sonar.sslr.api.AstNode;
2424
import com.sonar.sslr.api.AstNodeType;
2525
import java.util.Set;
26-
import java.util.regex.Pattern;
2726
import org.sonar.check.Rule;
2827
import org.sonar.check.RuleProperty;
29-
import org.sonar.python.PythonCheck;
3028
import org.sonar.python.api.PythonGrammar;
3129

3230
@Rule(key = ClassNameCheck.CHECK_KEY)
33-
public class ClassNameCheck extends PythonCheck {
31+
public class ClassNameCheck extends AbstractNameCheck {
3432

3533
public static final String CHECK_KEY = "S101";
3634
private static final String DEFAULT = "^[A-Z_][a-zA-Z0-9]+$";
@@ -40,13 +38,10 @@ public class ClassNameCheck extends PythonCheck {
4038
key = "format",
4139
defaultValue = "" + DEFAULT)
4240
public String format = DEFAULT;
43-
private Pattern pattern = null;
4441

45-
private Pattern pattern() {
46-
if (pattern == null) {
47-
pattern = Pattern.compile(format);
48-
}
49-
return pattern;
42+
@Override
43+
protected String format() {
44+
return format;
5045
}
5146

5247
@Override

0 commit comments

Comments
 (0)